Skip to main content

Posts

Area of a Circle without new Class and Math Class-Example0009

Area of a Circle without new Class and Math Class-Example0009 This example is example for using Scanner class to get input from user and calculate area of a circle. Using of Class Math is also shown Also casting from double to float is also shown package com.swprogramdeveloper; Keyword: java, aoc, scanner, user input, casting, math class import java.util.Scanner; public class Main {     public static void main(String[] args) { // write your code here         final float PI=3.14F;         Scanner askinput= new Scanner(System.in);         System.out.print("Please Enter Radius of the Circle:");         float radius= askinput.nextFloat();         //Using Mathematical expression         float area= PI * radius * radius;         System.out.print("Area of Circle(PIXRXR):");         System.out.println(...
Recent posts

Polymorphism and Method Overloading and Overriding- Example0008

Polymorphism and Method Overloading and Overriding- Example0008 This example of Polymorphism(One of the characteristics of the  - Both Compile and Runtime shown here. Method Overloading (Compile time Polymirphism)- Two methods defined in both parent and child Classes with same name but without differnet arguments Method Overriding (Run time Polymorphism) - Two methods defined with same name and same inputs, but output can be different package com.swprogramdeveloper; Keyword: java, polymorphism, method overloading, method overriding, compile polymorphism, runtime polymorphism, arguments //Textual presentation of an object, how it will look like in the memory //Whatever we write in class is actually belong to object. Note: if you want something for class, need to write static class Product {     //Attribute         int pid;         String name;         int price;         //Cons...

Creating Class and Child Class with extends- Example0007

Creating Class and Child Class with extends- Example0007 Using 'extends' keyword to create a child Class- IS-A relation. We 'extend' a child Class with IS-A relation. Child Class will have all the methods and attributes. The child can have additional attributes or methods. But that is for another example Keyword: java, extends, inheritance, IS-A, IS-A relation, child class, parent class, sub class, parent class package com.swprogramdeveloper; //Textual presentation of an object, how it will look like in the memory //Whatever we write in class is actually belong to object. Note: if you want something for class, need to write static class Product {     //Attribute         int pid;         String name;         int price;         //Constructor         Product() {         System.out.println(">>Product Object Constructed");   ...

Creating Class with attributes and method and using them- Example0006

Creating Class with attributes and method and using them- Example0006 This example shows creating own Class We will create a class called 'Product' with Attribute, Contructor and Methods We will see how we can create an Object of that Class and use the methods in that Class.  We can see two ways to access the attributes of the Class Keyword: java, class, attribute, constructor, methods, object, instance Creating own Class and use it ====================== package com.swprogramdeveloper; //Textual presentation of an object, how it will look like in the memory //Whatever we write in class is actually belong to object. Note: if you want something for class, need to write static class Product {     //Attribute         int pid;         String name;         int price;         //Constructor         Product() {         System.out.printl...

Mortgage Calculator with User Input and Mathematical Expression - Example0005

Mortgage Calculator with User Input and Mathematical Expression - Example0005 In this code sample, we will see how mortgage(EMI) calculator can be made Mortgage formula M=P * (r(1+r)**n)/((1+r)**n-1) will be used. Scanner Class will be used to get user input. We need to get the rightly formatted output (with dollar sign as currency). For this purpose we will use Class NumberFormat:NumberFormat.getCurrencyInstance. We use NumberFormat.getCurrencyInstance method. Keyword: java, mortgage calculator, user input, scanner, currency format, dollar sign, numberformat, getcurrencyinstance package com.swprogramdeveloper; import java.text.NumberFormat; import java.util.Scanner; public class Main {     public static void main(String[] args) { // write your code here         //Mortgage Calculator         Scanner userInput = new Scanner(System.in);         System.out.print("Please Input the Principal Amo...

Example to see how to get user input and do with mathematical expressions- Example0004

Example to see how to get user input and do with mathematical expressions- Example0004 In this example we are going to see how to get input from user, Mathematical expressions, For input we create an object from Class Scanner and with System.in Then we use method 'next' to get the input.  We will also see how the normal expression calculations are also done in Java // Input from keyboard, Mathematical expression package com.swprogramdeveloper; import java.util.Scanner; public class Main {     public static void main(String[] args) { // write your code here     // Constants         final float PI=3.14F;         float radius = 4.0F;         System.out.print("Please, enter radius of the circle : ");        //Scanner Class to get user input         Scanner radiusInput = new Scanner(System.in);        //NextFloat will read ...

Java Program to see how to use Arrays- Example0003

Java Program to see how to use Arrays- Example0003 Example0003 =========== Description: In this example we learn to create Arrays in different  We will also see lots of methods like sort, length. We will see how to initialize data in the array. We will see how to display content of single dimension and multidimensional arrays. //Arrays package com.swprogramdeveloper; import java.awt.*; import java.util.Arrays; import java.util.Date; public class Main {     public static void main(String[] args) { // write your code here     //Arrays     int[] numbers1 = new int[5];     numbers1[0] = 5;     numbers1[3] = 9;         System.out.println("Address where valuese of numbers1 array stored : " +  numbers1);         System.out.println("Size of Array numbers1 : " + numbers1.length);         System.out.println("Content of Array numbers1 : " + Arrays.to...