Skip to main content

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 float information from the user input
        radius= radiusInput.nextFloat();
       
        float area = PI * radius * radius;
        float perimeter = 2 * PI * radius;
        System.out.println("Area of circle with radius = " + radius +  " is : " + area + " and perimeter = " + perimeter);

       //Various Mathematical Expressions
        int x=20;
        int y = x * x+ 50;
        int z = x * 999999 + 2-3*4;
        System.out.println("Value of x : " + x);
        System.out.println("Value of y : " + y);
        System.out.println("Value of z : " + z);

     
        int k = 13+4-3*5;
        System.out.println("Value of 13+4-3*5 = " + k);

        int l = 3*2+2-3;
        System.out.println("Value of 3*2+2-3 = " + l);


        int m = 3*(2+2)-3;
        System.out.println("Value of 3*(2+2)-3 = " + m);


    }
}



Output0004
==========

Please, enter radius of the circle : 4
Area of circle with radius = 4.0 is : 50.24 and perimeter = 25.12
Value of x : 20
Value of y : 450
Value of z : 19999970
Value of 13+4-3*5 = 2
Value of 3*2+2-3 = 5
Value of 3*(2+2)-3 = 9

Process finished with exit code 0

Comments

Popular posts from this blog

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

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

Basic Java Program with Variable Introduction and Class Introduction - Example0001

Basic Java Program with Variable Introduction and Class Introduction - Example0001  Example1: ========= This is example of a basic java program which shows structure of the program. It is basic java program showing different type of Java variables. Primitive types: Integer, float, double, Boolean are shown Creating an object. Reference types and methods to that object. How to print output in a screen is shown using System Class package com.swprogramdeveloper; import java.util.Date; public class Main {     public static void main(String[] args) {  // write your code here      //variable types double and integer         double age = 30;         int herage = (int) age + 20;   //Printing output using System Class         System.out.println("Hello World "+age + "---" + herage);      //float type. Note the F- it is required as the compiler will take doub...