I keep getting can\'t find class error at the bottom where I am trying to test t
ID: 3540398 • Letter: I
Question
I keep getting can't find class error at the bottom where I am trying to test the rectangle. HELP!
public class TestMyRectangle {
public static void main(String[] args){
}
private double width = 1;
private double height = 1;
private static String color = "white";
public TestMyRectangle(){
width++;
height++;
}
public TestMyRectangle (double widthParam, double heightParam, String colorParam){
width = widthParam;
height = heightParam;
color = colorParam;
width++;
height++;
}
public double getWidth (){
return width;
}
public void setWidth (double widthParam){
width = (widthParam >= 0) ? widthParam : 0;
}
public double getHeight (){
return height;
}
public void setHeight (double heightParam){
height = (heightParam >= 0) ? heightParam : 0;
}
public String getColor (){
return color;
}
public static void setColor (String colorParam){
color = colorParam;
}
public double findArea (){
return width * height;
}
}
class MyRectangle {
public static void main(String args[]) {
// Create the first rectangle
myRectangle rectangleOne = new Rectangle(5.0, 15.0);
// Set the color to red
rectangleOne.setColor("red");
// Create the second rectangle
myRectangle rectangleTwo = new Rectangle(6,7);
// Set the color to yellow
rectangleTwo.setColor("yellow");
// Display the first rectangle
System.out.println("Rectangle One:");
System.out.println(" " + rectangleOne);
System.out.println(" Area: " + rectangleOne.findArea());
// Display the second rectangle
System.out.println("Rectangle Two:");
System.out.println(" " + rectangleTwo);
System.out.println(" Area: " + rectangleTwo.findArea());
Explanation / Answer
Your only problem is the use of public static void main in the first class, since it has to contain only the declarations and not the execution.
Just remove the public static void main from the first class and you are done :)
public class TestMyRectangle {
private double width = 1;
private double height = 1;
private static String color = "white";
public TestMyRectangle(){
width++;
height++;
}
public TestMyRectangle (double widthParam, double heightParam, String colorParam){
width = widthParam;
height = heightParam;
color = colorParam;
width++;
height++;
}
public double getWidth (){
return width;
}
public void setWidth (double widthParam){
width = (widthParam >= 0) ? widthParam : 0;
}
public double getHeight (){
return height;
}
public void setHeight (double heightParam){
height = (heightParam >= 0) ? heightParam : 0;
}
public String getColor (){
return color;
}
public static void setColor (String colorParam){
color = colorParam;
}
public double findArea (){
return width * height;
}
}
class MyRectangle {
public static void main(String args[]) {
// Create the first rectangle
myRectangle rectangleOne = new Rectangle(5.0, 15.0);
// Set the color to red
rectangleOne.setColor("red");
// Create the second rectangle
myRectangle rectangleTwo = new Rectangle(6,7);
// Set the color to yellow
rectangleTwo.setColor("yellow");
// Display the first rectangle
System.out.println("Rectangle One:");
System.out.println(" " + rectangleOne);
System.out.println(" Area: " + rectangleOne.findArea());
// Display the second rectangle
System.out.println("Rectangle Two:");
System.out.println(" " + rectangleTwo);
System.out.println(" Area: " + rectangleTwo.findArea());
}
}