-
help!!!! please
i want to test the Square class and instantiate two squares with different length sides and then print the side, area and perimeter (for example, i want one square to be 4 & the other square to be 5 in length). here's what i have but im stuck please help. im using dr. java
this is my square class:
public class Square {
private String mySquare;
//private instance variables
//the side of the square
double getSide;
// the area of the square
double myArea;
//the perimeter of the square
double myPerimeter;
double mySide;
//constructor
public Square() {
mySide = 4;
}
public Square(String ryan) {
mySide = 5;
}
//methods
public double getSide(){
return mySide;}
public String toString(){
String tmpString;
tmpString = "square: " + mySquare +
" area = " + myArea +
" perimeter = " + myPerimeter;
return tmpString;
}
public double calculateArea()
{
return myArea = (mySide*4);
}
public double calculatePerimeter()
{
return myPerimeter = (mySide*4);
}
}
And this is my SquareTest:public class SquareTest {
public static void main(String[] args) {
//define object variables (local)
Square stacy, ryan;
//create/instantiate objects
stacy = new Square("stacy");
ryan = new Square("ryan");
stacy.getSide();
stacy.calculateArea();
stacy.calculatePerimeter();
System.out.println(stacy + " side = ");
ryan.getSide();
ryan.calculateArea();
ryan.calculatePerimeter();
System.out.println(ryan + " side = ");
}
}
-
Is there a question coming any time soon?
-
well its an implicit question but apparently u didnt notice at the beginning
but here it is anyways...
how do i create two squares with different side lengths and print them out to show the area & perimeter?
-
It's not implicit what parts you have problems with. In case you didn't notice.
- Do you trouble understanding how constructors work? I'd say so, since they don't really make any sense. At least not the second one. What's the point of passing an argument to the constructor if you're not going to use it?
- What is getSide? You don't use it anywhere.
- What's the point of myArea? And since when is a square's area "length of side * 4"? And you set myArea in calculateArea(), which means that if you call toString() before calculateArea(), myArea will be 0 (zero).
- What does your comments say your variables are private, when in fact they are package private. That's not the same.
- What is mySquare? Where do you ever assign that a value?
Do you want me to go on? As you can see, it's impossible to implicitly understand exactly what you believe the problem is, since your code creates so many of them.
You asked how to create a square with different side lengths (a rectangle)? Well.. if you need two different side length, it stands to reason you need two instance variables. It also implies you need to assign them both values, either in the constructor (my recommendation) or via set-methods (stupid). Read up on how constructors work, and how you make them take multiple arguments.
-
A great resource that has helped me and will probably help you is an article entitled How To Ask Questions The Smart Way. It will tell you how to formulate your questions so that the folks here will be better able to answer them. Good luck.