confusedcat
confusedcat

Reputation: 79

Calculating Rectangle Perimeter and Area

We are supposed to make a program that creates a class with attributes length and width whose defaults are 1.0 and also has methods to get the perimeter and area. I'm done with creating the class but we have to test it out in a program. This is how my code looks like:

public class Rectangle {
private double length, width;

public Rectangle() {
    length = 1.0;
    width = 1.0;
}
public Rectangle (double l, double w){
    length = l;
    width = w;
}
public void setLength(double l){
    length = l;
}
public void setWidth(double w){
    width = w;
}
public double getLength(){
    return length;
}
public double getWidth(){
    return width;
}
public double getArea(){
    return length*width;
}
public double getPerimeter(){
    return (2*length)+(2*width);
}
}

import java.util.Scanner;
public class TestRectangle {
public static void main(String[]args) {
    int choice;
    double area, perimeter;     
    Scanner keyboard = new Scanner (System.in);
    Rectangle rec1 = new Rectangle();

    System.out.print("Choose an action:\n" + "1. Set Length\n" + "2. Set Width\n" + "3. Exit\n" + "Choice: ");
    choice = keyboard.nextInt();

    switch(choice){
        case 1:
            System.out.print("Enter Length: ");
            l = input.nextDouble;
            rec1.setLength(i);
            System.out.print("Length: "+rec1.getLength());
            System.out.print("Width: "+w);
            System.out.print("Area: "+rec1.getArea);
            System.out.print("Perimeter: "+rec1.getPerimeter);
            break;
        case 2:
            System.out.print("Enter Width: ");
            w = input.nextDouble;
            rec1.setWidth(w);
            System.out.print("Length: "+l);
            System.out.print("Width: "+rec1.getWidth);
            System.out.print("Area: "+rec1.getArea);
            System.out.print("Perimeter: "+rec1.getPerimeter);
            break;
        case 3:
            System.exit();
    }
}
}

It keeps on getting the error 'cannot find symbol.'

Upvotes: 0

Views: 3416

Answers (1)

sampathsris
sampathsris

Reputation: 22270

You are missing parentheses to method calls. getPerimeter, getArea should be getPerimeter(), getArea().

Same goes for nextDouble.

When you are compiling, java compiler points out exactly where it thinks the problem is. Check out this image:

enter image description here

Compiler shows where error is using a ^ character. Please use compiler hints to find the syntax errors of your program.

Upvotes: 3

Related Questions