2

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.

Srinivas
  • 1,760
  • 1
  • 14
  • 27

3 Answers3

1

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

Community
  • 1
  • 1
giampaolo
  • 6,648
  • 5
  • 42
  • 73
  • I can use this way but I must prompts user to enter width and height ... not enter the value inside the constructor –  Dec 22 '12 at 08:15
  • @trapo I think OP's problem is the user input. Using setters will not solve his problem, because he doesn't want hardcoded values, he wants user input. – jlordo Dec 22 '12 at 08:30
  • @jlordo: yes first comment explains this better, but without fixing the class it will never work. I added a reference for second part of problem. – giampaolo Dec 22 '12 at 08:33
1

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;
    }
}
vels4j
  • 10,808
  • 4
  • 35
  • 54
0

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;
}

}

sir mo
  • 1