Home > Back-end >  Building a test Class for rectangle, but wont find main
Building a test Class for rectangle, but wont find main

Time:10-02

Trying to create a test class, but I can't seem to connect both of them. Any ideas? I keep gettign the error of

Error: Main method not found in class Rectangle, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

import java.util.Scanner;

class Rectangle {

    private double length;
    private double width;
    
    //constructors
    //set width and length to one as defaults
    public void Rectangle() {
        length = 1;
        width = 1;
    }
    
    public void Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    //Set for length and width
    public void setLength(double length) {
        if(length > 0.0 || length < 20.0) {
            this.length = length;
        }else{
            throw new IllegalArgumentException("Please choose between 0.0 and 20.0");
        }
    }
    
    public void setWidth(double width){
        if(width > 0.0 || width < 20.0) {
            this.width = width;
        }else{
            throw new IllegalArgumentException("Please choose between 0.0 and 20.0");
        }
    }
    
    //Get for length and width
    public double getLength(){
        return length;
    }
    public double getWidth(){
        return width;
    }
    
    //Methods to calculate
    public double getPerimeter() {
        return (length * 2)   (width * 2);
    }
    
    public double getArea() {
        return length * width;
    }
    
    public void showRec() {
        System.out.println("Length: "   getLength()   "\nWidth: "   getWidth()   "\nArea: "   getArea()   "\nPerimeter: "   getPerimeter());
    }
}
    
class TestRectangle{

    public static void main(String[] args) {
        System.out.println("Skylar Jackson - Assignment 7\n");
        Scanner input = new Scanner(System.in);
        double length;
        double width;
        
        System.out.print("1. Set Length\n2. Set Width\n3. Exit\nChoice: ");
        int number = input.nextInt();
        
        try {
            Rectangle rec = new Rectangle();
            
            switch(number) {
            case 1:
                System.out.println("Length:");
                input.nextDouble();
                rec.setLength(length);
            break;
            case 2:
                System.out.println("Width:");
                input.nextDouble();
                rec.setWidth(width);
            break;
            case 3:
                System.exit(0);
            break;
            default: 
                System.out.println("Unknown Option");
            break;
            }
            rec.showRec();
        }catch(IllegalArgumentException e) {
            System.out.println("The Length or Width needs to be between 0.0 and 20.0");
        }
        
        
        }//end main
    } //end test

CodePudding user response:

I am a beginner myself, but I found out some issues with your program.

I assume you created two seperate files named Rectangle.java and TestRectangle.java. If not do so first.

Compile them seperately, you will come across this 3 errors :

Error: Main method not found in class Rectangle, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

TestRectangle.java:20: error: variable length might not have been initialized rec.setLength(length);

TestRectangle.java:25: error: variable width might not have been initialized rec.setWidth(width);

As you can see you haven't initialised the local variables "length", "width". And also you are passing these two uninitialised variables to methods : setLength(double) and setWidth(double) of your other class - Rectangle.java

I think you meant to accept certain "length" and "width" from user as inputs.

If you do this in your switch case of TestRectangle class :

switch(number) {
        case 1:
            System.out.println("Length:");
            **length=input.nextDouble();**
            rec.setLength(length);
        break;
        case 2:
            System.out.println("Width:");
            **width=input.nextDouble();**
            rec.setWidth(width);
        break;

Your program will run but there is a logical error in your program. Let's see how your TestRectangle class runs :

Code execution 1:

Skylar Jackson - Assignment 7

  1. Set Length
  2. Set Width
  3. Exit

Choice: 2

Width:

89.4

Length: 0.0

Width: 89.4

Area: 0.0

Perimeter: 178.8

Code execution 2:

Skylar Jackson - Assignment 7

  1. Set Length
  2. Set Width
  3. Exit

Choice: 1

Length:

24.95

Length: 24.95

Width: 0.0

Area: 0.0

Perimeter: 49.9

You see, your code can either accept only length or width because of your switch case logic.

Also your program is NOT able to catch the exception of "your length and width should be between 0.0 and 20.0", when you accept input (as you can see from above - I've accepted length and width well above 20.0).

HINT: Use a single case for accepting both inputs together. Something like this :

switch(number) {
        case 1:
            System.out.println("Length:");
            length=input.nextDouble();
            rec.setLength(length);
            System.out.println("Width:");
            width=input.nextDouble();
            rec.setWidth(width);
        break;
        case 2:
            System.exit(0);
        break;
        default:
            System.out.println("Unknown Option");
        break;
        }

If you do so, your code will run like this :-

Code Execution 3:

Skylar Jackson - Assignment 7

  1. Set Length and Width
  2. Exit

Choice: 1

Length:

23.5

Width:

35.6

Length: 23.5

Width: 35.6

Area: 836.6

Perimeter: 118.2

But still I can easily enter length and width values over 20.0 and your program does not detect that. So you need to do use a do while loop. Using that will allow to keep on asking the user for input until and unless he enters within the given range of (0-20)

 switch(number) {
        case 1:
        do {
            System.out.println(" Enter Length value (0-20):");
            length=input.nextDouble();
            System.out.println("Enter Width value (0-20):");
            width=input.nextDouble();
        } while ((length > 20.0 && width > 20.0) || (length < 0.0 && width <0.0));
            
        rec.setLength(length);
        rec.setWidth(width);
        break;
        case 2:
            System.exit(0);
        break;
        default:
            System.out.println("Unknown Option");
        break;
        }

Now your program will keep on asking for input if it exceeds the range (0-20) Lets run again :

Code Execution 4:

Skylar Jackson - Assignment 7

  1. Set Length and Width
  2. Exit

Choice: 1

Enter Length value (0-20):

-96

Enter Width value (0-20):

-24

Enter Length value (0-20):

85

Enter Width value (0-20):

31

Enter Length value (0-20):

12

Enter Width value (0-20):

7

Length: 12.0

Width: 7.0

Area: 84.0

Perimeter: 38.0

NOW YOUR PROGRAM IS GIVING THE DESIRED OUTPUT

Since I do not know exactly what is required of your assignment, I cannot give a appropriate solution. As of now, I have exposed some of the flaws in your programming logic and given you some hints how to fix your code. All the best for your assignment.

CodePudding user response:

You do not have a main method in your Rectangle class. Double check you're not calling it incorrectly.

  •  Tags:  
  • java
  • Related