Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,165 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,632 people online right now. Registration is fast and FREE... Join Now!




Square class...

 
Reply to this topicStart new topic

Square class..., Area&perimeter

bsq935
post 6 Oct, 2008 - 10:11 AM
Post #1


New D.I.C Head

*
Joined: 2 Oct, 2008
Posts: 29


My Contributions


This is the same program I asked about last time, but this time I only get zeros for the areas and perimeters and stuff. I'm pretty sure it's something I left out when constructing the square... Here's what I have.

java

public class Square {


private double side;
private double perimeter;
private double area;




public Square(int x){

side = side;

}

public double getSide(){


return side;
}



public String toString(){
String s = side + "---";
s = s + side;
s = s + area;
s = s + perimeter;
return s;

}


public double calculateArea(){

return side*side;
}


public double calculatePerimeter(){

return side*4;
}
}


Sooooo close to being done ><

This post has been edited by William_Wilson: 6 Oct, 2008 - 10:48 AM
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 6 Oct, 2008 - 10:50 AM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,969



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


code.gif

you never calculate those values.
Add to your constructor:
java

perimeter = calculatePerimeter();
area = calculateArea();

and you are good to go.


EDIT:
just for future knowledge, when you put s = s + side; this is the same as s += side;. Though you will probably want to add spaces in the String too, but it still works the same way.

This post has been edited by William_Wilson: 6 Oct, 2008 - 10:52 AM
User is online!Profile CardPM

Go to the top of the page

stauffski
post 6 Oct, 2008 - 10:55 AM
Post #3


D.I.C Head

**
Joined: 3 Nov, 2007
Posts: 137



Thanked 16 times
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

bsq935
post 6 Oct, 2008 - 12:44 PM
Post #4


New D.I.C Head

*
Joined: 2 Oct, 2008
Posts: 29


My Contributions


Gahhh thanks again you guys! I feel like I really understand that one now. I'm gonna see if I can do the next one completely on my own... Just me and my Java book rolleyes.gif
User is offlineProfile CardPM

Go to the top of the page

BigAnt
post 6 Oct, 2008 - 12:51 PM
Post #5


D.I.C Head

**
Joined: 16 Aug, 2008
Posts: 133



Thanked 13 times
My Contributions


public String dataToString(){ //X: toString(){ //don't use "toString()," it can end up overriding a class method

the toString method overrides the Object class' toString method, every class inherits from the Object class.

So you can use the toString method provided that another super class does not implement this method, which in this case the square class does not have any super classes(beside Object) so overiding this method is fine.

So if you override this method then the statement
System.out.print(square.dataToString());
in the Test class can be simplified to
System.out.print(square);
Where the toString method is automatically called.
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 02:51PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month