Hey guys I have created a class which compiles fine, but I am having trouble trying to use it in a program. I am not clear on how to instantiate the class in a program can you give me an example of how to do this?
Thanks!
Here are my class and program so far.
The Class....................................................
PHP Code:public class Rectangle{
private double length = 1;
private double width = 1;
public double getLength(){
return length;
}//end getLength
public void setLength(double l){
if(l > 0.0 && l < 20.0){
length = l;
}/// end if
}// end setLength
public double getWidth(){
return width;
}//end getWidth
public void setWidth(double w){
if(w > 0.0 && w < 20.0){
width = w;
}// end if
}//end setWidth
public double perimeter(){
double thePerimeter = 0;
thePerimeter = length * 2 + width * 2;
return thePerimeter;
}//end perimeter
public double area(){
double theArea = 0;
theArea = length * width;
return theArea;
}//end perimeter
}//end class Rectangle
The program to use it...........................
These are the errors I getPHP Code:import javax.swing.JOptionPane;
public class RectangleTest{
private Rectangle testIt;
public static void main( String Args[] ){
testIt = new Rectangle();
testIt.setLength(5.0);
testIt.setWidth(6.0);
JOptionPane.showMessageDialog(null, "The rectangle's width perimeter is: "
+ testIt.perimeter + "\nThe rectangle's area is: "
+ testIt.area, "Results", JOptionPane.PLAIN_MESSAGE);
}//end main
}//end class Rectangle
C:\...\RectangleTest.java:12: non-static variable testIt cannot be referenced from a static context
testIt = new Rectangle();
^
C:\...\RectangleTest.java:14: non-static variable testIt cannot be referenced from a static context
testIt.setLength(5.0);
^
C:\...\RectangleTest.java:15: non-static variable testIt cannot be referenced from a static context
testIt.setWidth(6.0);
^
C:\...\RectangleTest.java:18: non-static variable testIt cannot be referenced from a static context
+ testIt.perimeter + "\nThe rectangle's area is: "
^
C:\...\RectangleTest.java:18: cannot find symbol
symbol : variable perimeter
location: class Rectangle
+ testIt.perimeter + "\nThe rectangle's area is: "
^
C:\...\RectangleTest.java:19: non-static variable testIt cannot be referenced from a static context
+ testIt.area, "Results", JOptionPane.PLAIN_MESSAGE);
^
C:\...\RectangleTest.java:19: cannot find symbol
symbol : variable area
location: class Rectangle
+ testIt.area, "Results", JOptionPane.PLAIN_MESSAGE);
I'm not sure what the errors mean or what I am doing wrong...![]()





Reply With Quote