Reputation:
I have created a Java class called Rectangle
that has the two instance variables (width & height) &
two instance methods (area and circumference) both method do not take parameters but
return double values. The area method returns area of rectangle (width * height) while
circumference returns (2*width+2*height). Then create Demo class with main method to test
the class Rectangle by instantiating 4 objects and prompts user to enter width and height for
each instance. Then print out the area and circumference for each instance.
I create two class and the first class is Rectangle :
public class Rectagle {
private double width;
private double height;
public double area() {
return width * height;
}
public double circumference() {
return 2*width+2*height;
}
}
and I create the second class Demo to Test the class :
import java.util.Scanner;
public class Demo {
public static void main(String []args){
Scanner console=new Scanner(System.in);
Rectagle R1=new Rectagle();
Rectagle R2=new Rectagle();
Rectagle R3=new Rectagle();
Rectagle R4=new Rectagle();
}
}
my problem , I don't understand this point " and prompts user to enter width and height for each instance. Then print out the area and circumference for each instance.
Upvotes: 2
Views: 41143
Reputation: 1
public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getCircumference() {
return 2*width+2*height;
}
@Override
public String toString() {
return "Rectangle["+width+","+height+"]Area:"+getArea()+",Circumference:"+getCircumference();
}
public static void main(String[] args) {
Scanner console=new Scanner(System.in);
double width = getValue(console, "Width");
double height = getValue(console, "Height");
Rectangle rectangle = new Rectangle(width, height);
System.out.println(rectangle);
}
public static double getValue(Scanner console, String name) {
System.out.println("Enter "+name + " : ");
String widthStr = console.nextLine();
double parseDouble;
try {
parseDouble = Double.parseDouble(widthStr);
}catch(NumberFormatException ne) {
System.out.println("Unable to parse your input, enter correct value ");
return getValue(console, name);
}
return parseDouble;
}
}
Upvotes: 0
Reputation: 11298
May this help you
public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getCircumference() {
return 2*width+2*height;
}
@Override
public String toString() {
return "Rectangle["+width+","+height+"]Area:"+getArea()+",Circumference:"+getCircumference();
}
public static void main(String[] args) {
Scanner console=new Scanner(System.in);
double width = getValue(console, "Width");
double height = getValue(console, "Height");
Rectangle rectangle = new Rectangle(width, height);
System.out.println(rectangle);
}
public static double getValue(Scanner console, String name) {
System.out.println("Enter "+name + " : ");
String widthStr = console.nextLine();
double parseDouble;
try {
parseDouble = Double.parseDouble(widthStr);
}catch(NumberFormatException ne) {
System.out.println("Unable to parse your input, enter correct value ");
return getValue(console, name);
}
return parseDouble;
}
}
Upvotes: 1
Reputation: 6934
Your constructor has no parameters. There's no way to assign to width and height a value.
I propose you to have this kind of constructor
public Rectangle(double w, double h){
width = w;
height = h;
}
and use it this way:
Rectagle R1=new Rectagle(30.0, 40.0);
or if you need, add a setter and getter for your instance variable like this:
public void setWidth(double w){
width = w
}
public double getWidth(){
return width;
}
now your class is complete. Refer to proper use of Scanner
class to know how to read from console. Read this for example: How to read integer value from the standard input in Java
Upvotes: 1