Monday, January 22, 2018

Solutions for the problems of 22-Jan-2018

Note : Save this class inside Rectangle.java file

import java.util.Scanner;

public class Rectangle
{
// non static data members
int width;
int height;

// define a no argument constructor
public Rectangle()
{
System.out.println("object of rectangle created by no arg cnst");
}

// define a parameterized constructor
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}

// define a non static method to show the data of rectangle
public void showRectangleInfo()
{
System.out.println("Rectangle info =>");
System.out.println("Width : "+width);
System.out.println("Height : "+height);

// get area of rectangle and store it
int area = width * height;
System.out.println("Area : "+area);

System.out.println(); // print a new line
}

// define main method
public static void main(String[] args)
{
/// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Input width of rectangle :");
// get input from keyboard and store it inside local variables
int wi = s.nextInt();

System.out.print("Input width of rectangle :");
// get input from keyboard and store it inside local variables
int hi = s.nextInt();

// create object of rectangle by using parameterized constructor
// inside this constructor pass the values those were taken from the keyboard
Rectangle r = new Rectangle(wi, hi);

// show the information of rectangle using the method
r.showRectangleInfo();

}
}

===================================================

Note : Save this class inside Circle.java file

import java.util.Scanner;

public class Circle
{
// non static data member
double radius;

// define a no argument constructor
public Circle()
{
System.out.println("object of Circle created by no arg cnst");
}

// define a parameterized constructor
public Circle(double radius)
{
this.radius = radius;
}

// define a non static method to show the data of Circle
public void showCircleInfo()
{
System.out.println("## Circle info ##");
System.out.println("Radius : "+radius);
// get circumference of circle and store it
double cir = 2.0 * 3.14 * radius;
System.out.println("Circumference : "+cir);
// get diameter of circle and store it
double dia = 2.0 * radius;
System.out.println("Diameter : "+dia);
// get area of circle and store it
double area = 3.14 * radius * radius;
System.out.println("Area : "+area);
System.out.println();
}

// define main method
public static void main(String[] args)
{
/// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Input radius of Circle :");
// get input from keyboard and store it inside local variables
double ra = s.nextDouble();

// create object of Circle by using parameterized constructor
// inside this constructor pass the value which is taken from the keyboard
Circle r = new Circle(ra);

// show the information of Circle using the method
r.showCircleInfo();
}
}

===================================================

Note : Save this class inside Square.java file

import java.util.Scanner;

public class Square
{
// non static data member
long side;

// define a no argument constructor
public Square()
{
System.out.println("object of Square created by no arg cnst");
}

// define a parameterized constructor
public Square(long side)
{
this.side = side;
}

// define a non static method to show the data of Square
public void showSquareInfo()
{
System.out.println("## Square info ##");
System.out.println("Side : "+side);
// get area of Square and store it
long area = side * side;
System.out.println("Area : "+area);
System.out.println();
}

// define main method
public static void main(String[] args)
{
/// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Input side of Square :");
// get input from keyboard and store it inside local variables
long si = s.nextLong();

// create object of Square by using parameterized constructor
// inside this constructor pass the value which is taken from the keyboard
Square r = new Square(si);

// show the information of Square using the method
r.showSquareInfo();
}
}

No comments:

Post a Comment