First off, post your code in a code box.
Bellow I have posted the corrected code. It was very close, just a few changes, I commented everything I changed, anything I deleted I commented out and put and "X" right after the slashes. IE //X: perimeter = 4; I also explained why I did everything. Make sure you don't just use this code. Make sure you understand why.
java
public class Square {
private double side;
//private double perimeter; //not used
//private double area; //not used
public Square(int x){
side = x; //X: side = side; //this does nothing
}
public String dataToString(){ //X: toString(){ //don't use "toString()," it can end up overriding a class method
String s = ""; //X: "---" //start with it blank
String endl = "\n"; //new line variable
s += endl + "Side: " + getSide(); //X:+ side; //you never assigned a value to this variable, that's why it appeared as a 0
s += endl + "Area: " + getArea(); //X:+ area; //you never assigned a value to this variable, that's why it appeared as a 0
s += endl + "Perimeter: " + getPerimeter(); //X:+ perimeter; //you never assigned a value to this variable, that's why it appeared as a 0
return s;
}
public double getSide(){
return side;
}
public double getArea(){ //X:calculateArea(){ //stick with the root "get", consistency makes usage easier. it is also less to type
return side*side;
}
public double getPerimeter(){ //X:calculatePerimeter(){ //stick with the root "get", consistency makes usage easier. it is also less to type
return side*4;
}
}
I wrote a test class. I'm sure you did too, but it likely isn't compatible anymore.
java
//import the scanner
import java.util.Scanner;
public class TestSquare{
//create variables
public static Scanner reader = new Scanner(System.in);
public static int num;
public static void main(String[] args){
//tell the user what to do
System.out.print("Enter the measurement of square side: ");
//get user data
num = reader.nextInt();
//instantiate the class
Square square = new Square(num);
//use the classes output method
System.out.print(square.dataToString());
}
}
If you have anymore questions or don't understand something, just let me know.
Good Luck!
This post has been edited by stauffski: 6 Oct, 2008 - 10:57 AM