Search code examples
javanetbeansmethod-call

myRectangle.calculateArea(); returns error "package myRectangle does not exist"


Rectangle

public class Rectangle {

  private double width;
  private double length; 


public Rectangle(double L, double W){
  length = L;
  width = W;
}

public void setLength(double Length){
  if (length>=0 && length <=20)
    length = Length;
else{
  length = 0;
}
}
public double getLength(){
  return length;
}
public void setWidth(double Width){
  if (width>=0 && length <=20)
    width = Width;
else{
    width = 0;
}
}
public double getWidth(){
  return width;
}

public void calculatePerimeter(){
  System.out.println("The perimeter of rectangle is: " + 2 * (length + width));
}
public void calculateArea(){
  System.out.println("The area of the rectangle is: " + (length * width));
}
}    
   

TestRectangle

import java.util.Scanner;

public class TestRectangle {
public static void main(String[] args){

Scanner input = new Scanner(System.in);
  Rectangle myRectangle = new Rectangle (0,0);//I did the same thing in a 
//previous assignment, and someone else who did this assignment (code found 
//online) also did this. It worked before but seems useless now?

System.out.println("Enter length: ");
  double L = input.nextDouble();
  System.out.println();

System.out.println("Enter width: ");
  double W = input.nextDouble();
  System.out.println();
}
myRectangle.calculateArea();//here
myRectangle.calculatePerimeter();//and here is where I get the error
//<identifier> expected, package myRectangle does not exist
}

I am trying to create a program "Rectangle" to calculate the area and perimeter of a rectangle, and then create a test program to run program "Rectangle" I have copied code from a previous assignment called "Date", where the basic idea is similar, but when I get to the end of the program where I need to call on "calculateArea();" and "calculatePerimeter();" in the test program, I get an error telling me that package myRectangle doesn't exist.... can someone tell me why this is happening? A similar code worked in the previous assignment, and I found someone else's code for the same "Rectangle" program and it shows the same error. Did I do something wrong or is there something wrong with my NetBeans?

This is the code I based the Rectangle and TestRectangle program off of Date

public class Date {
private int month;
private int day;
private int year;

public Date(int m, int d, int y){
    month = m;
    day = d;
    year = y;
}

public void setMonth(int Months){
    if(Months>=0 && Months <= 12)
        month=Months;
    else{
        month=0;
    }
}
public int getMonth(){
    return month;
}

public void setDay(int Days){
    if(Days>= 0 && Days<=31)
        day = Days;
        else{
                day=0;
                }
}

public int getDay(){
    return day;
}

public void setYear(int Years){
    year=Years;
}
public int getYear(){
    return year;
}
public void displayDate(){
    System.out.printf
            ("%d/%d/%d\n", getMonth(), getDay(), getYear() );
}
}

TestDate

import java.util.Scanner;

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

Date myDate = new Date(0,0,0);

System.out.println("Justine Dodge, assignment 6\n");


System.out.println("Please enter month: ");
  int m = input.nextInt();
  myDate.setMonth(m);
  System.out.println();


System.out.println("Enter day: ");
  int d = input.nextInt();
  myDate.setDay(d);//assign d to Day?
  System.out.println();//output blank line

System.out.println("Enter year: ");
  int y = input.nextInt();
  myDate.setYear(y);
  System.out.println();


myDate.displayDate();

}
}


Solution

  • in the TestRectangle you are closing the main method before calling these functions

    } // this should be at at the end of the main function 
    myRectangle.calculateArea();//here
    myRectangle.calculatePerimeter();//and here is where I get the error
    

    i.e

    myRectangle.calculateArea();//here
    myRectangle.calculatePerimeter();//and here is where I get the error
    }
    

    This will remove the compilation error. Now when you run it , you will get both area and perimeter as 0 because in the constructor , you are passing values of length and width as 0 and not really using the length and width taken as input. To resolve this issue, insstead of passing 0,0 in constructor, try to pass L,W in constructor like this

    
      Rectangle myRectangle = new Rectangle (L,W);
    myRectangle.getLength()); //
    myRectangle.calculateArea();//here
    myRectangle.calculatePerimeter();
    
    
    import java.util.Scanner;
    
    public class TestRectangle {
    public static void main(String[] args){
    
    Scanner input = new Scanner(System.in);
    //  Rectangle myRectangle = new Rectangle (0,0);//I did the same thing in a 
    //previous assignment, and someone else who did this assignment (code found 
    //online) also did this. It worked before but seems useless now?
    
    System.out.println("Enter length: ");
      double L = input.nextDouble();
      System.out.println();
    
    System.out.println("Enter width: ");
      double W = input.nextDouble();
      System.out.println();
    
      
      Rectangle myRectangle = new Rectangle (L,W);
    //  System.out.println("get length " + myRectangle.getLength());
    myRectangle.calculateArea();//here
    myRectangle.calculatePerimeter();//and here is where I get the error
    //<identifier> expected, package myRectangle does not exist
    }
    }
    

    then you will get o/p like this enter image description here