coderrr
coderrr

Reputation: 67

Why can't my main class identify my methods?

The question states:

Create a class Rectangle with double attributes length and width. The default constructor should set these attributes to
1. Provide methods that calculate the rectangle's perimeter and area, as well as accessors and mutators for both data fields. The mutator methods for length and width should verify that the number being passed in is larger than 0.0 and less than 20.0 -- if it doesn't fit those criteria, the value of the field should not be changed.

Write a Driver class in the same file to test your Rectangle class . It should prompt the user to enter a length and width of a rectangle, and then print out the area and perimeter of the rectangle. (Use the mutators to set the length and width of the rectangle, not the constructor .)

This is the sample run: Sample run

And this is the code that I have so far:

import java.util.Scanner;

public class Driver{
public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    System.out.print("Enter length of rectangle:");
    double height = input.nextDouble();
    System.out.print("Enter width of rectangle:");
    double width = input.nextDouble();

    System.out.printf("Area: %f, Perimeter: %f", getArea(), getPerimeter());

}

public static class Rectangle {
private double height;
private double width;

public Rectangle(double wid, double high) {
    height = high;
    width = wid;
} 

public void setHeight(double high) {
    width = wid > 0.0 && wid < 20.0 ? wid : 0;
}

public void setWidth(double wid){
    width = wid > 0.0 && wid < 20.0 ? wid : 0;
}

public double getArea() {
    return height*width;
}

public double getPerimeter() {
    return 2*(height + width);
}
}
}

I appreciate any help! Error

Upvotes: 0

Views: 889

Answers (2)

ujulu
ujulu

Reputation: 3309

To call a method of a class you should have an instance of that class. In this case you are trying to access the methods of the Rectangle class in the the Class Driver which is not possible. So you have to create an instance the of the Rectangle class as

Rectangle rect = new Rectangle();

and then you can access the methods of this instance as:

rect.getArea(); 
...

I recommend to read the Java basics, for example at Java Tutorial site.

I commented all the relevant parts so that you can see why your code is not working:

public class Driver {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter length of rectangle:");
    double height = input.nextDouble();
    System.out.print("Enter width of rectangle:");
    double width = input.nextDouble();

    Rectangle rect = new Rectangle(); // Create instance of Rectangle
    rect.setHeight(height); // use mutators to set the values
    rect.setWidth(width);

    DecimalFormat df = new DecimalFormat("##.0#");
    System.out.printf("Area: %s, Perimeter: %s", df.format(rect.getArea()), df.format(rect.getPerimeter()));

}

static class Rectangle {
    private double height;
    private double width;

    // default constructor(=constrcutor without parameter) should set dimensions to 1
    public Rectangle() {
        height = 1.0;
        width = 1.0;
    }

    // Define mutators
    public void setHeight(double height) {
        if (height > 0.0 && height < 20.0) {
            this.height = height;
        }
    }

    public void setWidth(double width) {
        if (width > 0.0 && width < 20.0) {
            this.width = width;
        }
    }


    // Define accessors
    double getHeight() {
        return height;
    }

    double getWidth() {
        return width;
    }

    public double getArea() {
        return height * width;
    }

    public double getPerimeter() {
        return 2 * (height + width);
    }
}

}

Upvotes: 0

Alexey Soshin
Alexey Soshin

Reputation: 17691

That's because those methods don't belong to Driver class. They belong to Rectangle. Instantiate your Rectangle first, then use its methods:

public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    System.out.print("Enter length of rectangle:");
    double height = input.nextDouble();
    System.out.print("Enter width of rectangle:");
    double width = input.nextDouble();

    Rectangle r = new Rectangle(width, height);

    System.out.printf("Area: %f, Perimeter: %f", r.getArea(), r.getPerimeter());

}

Also, your setters are totally mixed up.

    public Rectangle(double wid, double high) {
        // Your constructor didn't use validation
        setWidth(wid);
        setHeight(high);
    }

    public void setHeight(double high) {
       // If you really want to have validation in your setter, that's the syntax, yours doesn't compile
        height = high > 0.0 && high < 20.0 ? high : 0;
    }

    public void setWidth(double wid){
        width = wid > 0.0 && wid < 20.0 ? wid : 0;
    }

Upvotes: 1

Related Questions