Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

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




Square class...

 
Reply to this topicStart new topic

Square class..., Area&perimeter

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

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 33


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
+Quote Post


William_Wilson
RE: Square Class...
6 Oct, 2008 - 10:50 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,446



Thanked: 55 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 offlineProfile CardPM
+Quote Post

stauffski
RE: Square Class...
6 Oct, 2008 - 10:55 AM
Post #3

D.I.C Head
**

Joined: 3 Nov, 2007
Posts: 161



Thanked: 20 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
+Quote Post

bsq935
RE: Square Class...
6 Oct, 2008 - 12:44 PM
Post #4

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 33


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
+Quote Post

BigAnt
RE: Square Class...
6 Oct, 2008 - 12:51 PM
Post #5

May Your Swords Stay Sharp
Group Icon

Joined: 16 Aug, 2008
Posts: 2,384



Thanked: 95 times
Dream Kudos: 75
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 offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:46PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month