Practice JAVA like Never Before!
Day 2 🔥
Objective
Learning to implement Classes, Objects, and Constructor concepts.
1. Add two floating-point numbers by taking input using java.util.Scanner class and java.io.BufferedReader class.
Program
Part 1:
//Firstly, we use java.util.Scanner classimport java.util.Scanner;public class A2a{public static void main(String[] args){// Enter data using ScannerScanner sc = new Scanner(System.in);//message to the user:-System.out.println("Enter two floating-point numbers:- ");float a = sc.nextFloat();float b = sc.nextFloat();float sum = a+b;// Printing the sumSystem.out.println("The Sum is: "+sum);}}
Part 2:
//Next, we use java.io.BufferedReader classimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class A2b{public static void main(String[] args)throws IOException{// Enter data using BufferReaderBufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//message to the user:-System.out.println("Enter two floating-point numbers:- ");// Reading data using readLinefloat a = Float.parseFloat(reader.readLine());float b = Float.parseFloat(reader.readLine());float sum = a+b;// Printing the read lineSystem.out.println("The Sum is: "+sum);}}
Output:
---------------------------------------------------
2. Write a program to find surface area and volume of Cylinder Using Constructor(s) - Keyboard Input or Command Line Input.
Program:
class Cylinder{public static double pi = 3.14159265359;
private double radius, height;
Cylinder(double r, double h){
radius = r;
height = h;
}
public double getSA(){
return 2*pi*radius*(radius+height);
}
public double getVolume(){
return pi*radius*radius*height;
}
public static void main(String[] args){
//accepting inputs as Command Line arguments
double rad = Double.parseDouble(args[0]);
double ht = Double.parseDouble(args[1]);
Cylinder cy; //declaring instance/object cy of class Cylinder
cy = new Cylinder(rad, ht); //instantiating cy, passing values as arguments to constructor
double area = cy.getSA(), volume = cy.getVolume();
System.out.println("The Surface Area of the Cylinder = "+area);
System.out.println("The Volume of the Cylinder = "+volume);
}
}//end class definition
Output:
---------------------------------------------------
3. Create a class named First, make instance variable [int x], instance method [void show()] and put main() method inside that class and use the instance variable and method from main().
Program:
class First{//Instance variable definition:-public static int x = 10;//Instance method definition:-public static void show(){System.out.print("x is: ");}//main method:-public static void main(String[] args){show();if((x&1)==1)System.out.println("ODD");elseSystem.out.println("EVEN");}}//end class
Output:
--------------------------------------------------
4. Create a class; make its instance variable and instance method. Use them from main() which is declared in a different class.
Program:
class First{//Instance variable definition:-public static int x = 135;//Instance method definition:-public static void show(){System.out.print("x is: ");}}//end first classclass Test{//main method:-public static void main(String[] args){First obj = new First();obj.show();if((obj.x&1)==1)System.out.println("ODD");elseSystem.out.println("EVEN");}}//end second class
Output:
--------------------------------------------------
<---------- End of Part - II ---------->😋
No comments:
Post a Comment