Skip to main content

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(area);

        //Using Math Class
        //Need Explicit Casting- Casting: changing types from double to float- Math.pow returns double
        float area1= PI * (float)Math.pow(radius,2);
        System.out.print("Area of Circle(Using Math Class for Power(PIXR*2):");
        System.out.println(area1);
    }
}


Output0009
==========

Please Enter Radius of the Circle:7
Area of Circle(PIXRXR):153.86002
Area of Circle(Using Math Class for Power(PIXR*2):153.86

Process finished with exit code 0

Comments

Popular posts from this blog

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...

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...