toString()

I'm having trouble getting my toString method to work

Page 1 of 1

5 Replies - 1020 Views - Last Post: 23 February 2009 - 09:39 PM Rate Topic: -----

#1 grifter8875  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-February 09

toString()

Post icon  Posted 23 February 2009 - 06:07 PM

I'm setting up a class for telling info about a square. I'm trying to create a toString method in order to output the information in a neat fashion and i keep on getting a compiler error "File: K:\College\CS\lab 5\Square.java [line: 31]
Error: K:\College\CS\lab 5\Square.java:31: calculatePerimeter(double) in Square cannot be applied to ()
File: K:\College\CS\lab 5\Square.java [line: 31]
Error: K:\College\CS\lab 5\Square.java:31: calculatePerimeter(double) in Square cannot be applied to ()"

This is what I have so far
public class Square {
//Varibles
  private double mySide;
  private double myArea;
  private double myPerimeter;
//Constructors
  public Square(double side, double area){
   mySide = side;
   myArea = area;

  }//end of Square
//Methods
  public double getSide(){
	return mySide;
  }//end of getSide
  
  public double calculateArea(double side){
	return myArea = side*side;
  }//end of calArea
  
  public double calculatePerimeter(double side){
	return myPerimeter = side+side+side+side;
  }//end of calPerim
  
  public String toString(){
	return "Side= "+getSide()+" Area ="+calculatePerimeter()+" Perimeter= "+calculatePerimeter();

	
  }//end of toString
  
}//End Square



any help pointing me in the right direction would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: toString()

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: toString()

Posted 23 February 2009 - 06:29 PM

The String Class has a method, format(), that will do what you want. The toString() method should be returning the current state of the object. You do not need or want to call the methods internally in the object. The reason you don't want to call them is what are you going to use as arguments to calculateArea() or calculatePerimeter()? Instead, use the instance's current values of side, area, and perimeter.
public String toString(){
	//return "Side= "+getSide()+" Area ="+calculatePerimeter()+" Perimeter= "+calculatePerimeter();
	return String.format("Side = %f, Area = %f, Perimeter = %f", mySide, myArea, myPerimeter);
}//end of toString



See Class String

This post has been edited by n8wxs: 23 February 2009 - 06:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: toString()

Posted 23 February 2009 - 07:13 PM

Wow this is like the 8th question this week on toString()... I think i'm going to write a tutorial on it!
Was This Post Helpful? 0
  • +
  • -

#4 grifter8875  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-February 09

Re: toString()

Posted 23 February 2009 - 07:47 PM

The reply from n8wxs was right on and helped out, unfortantly my code was wrong. The only constuctor I should have is mySide. this is the revised code
public class Square {
//Varibles
  private double mySide;
  private double myArea;
  private double myPerimeter;
//Constructors
  public Square(double side){
   mySide = side;
   }//end of Square
//Methods
  public double getSide(){
	return mySide;
  }//end of getSide
  
  public double calculateArea(double side){
	return myArea = side*side;
   
  }//end of calArea
  
  public double calculatePerimeter(double side){
	return myPerimeter = side+side+side+side;
  }//end of calPerim
  
  public String toString(){
	return "Side: "+getSide()+
		   "Area: "+calculateArea()+
		   "Perimeter: "+calculatePerimeter();

	
  }//end of toString
  
}//End Square



When I try to compile it says this:
File: K:\College\CS\lab 5\Square.java [line: 31]
Error: K:\College\CS\lab 5\Square.java:31: calculateArea(double) in Square cannot be applied to ()
File: K:\College\CS\lab 5\Square.java [line: 32]
Error: K:\College\CS\lab 5\Square.java:32: calculatePerimeter(double) in Square cannot be applied to ()
Was This Post Helpful? 0
  • +
  • -

#5 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: toString()

Posted 23 February 2009 - 07:59 PM

public double calculateArea(double side){
	return myArea = side*side;
   
  }//end of calArea
 
  public double calculatePerimeter(double side){
	return myPerimeter = side+side+side+side;


Should be changed to:
public double calculateArea(double side){
	return (side*side);
   
  }//end of calArea
 
  public double calculatePerimeter(double side){
	return (side*4);
  }


OR
public double calculateArea(double side){
	myArea = side*side;
	return myArea;
   
  }//end of calArea
 
  public double calculatePerimeter(double side){
	myPerimeter = side*4;
	return myPerimeter;
  }


Was This Post Helpful? 1
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: toString()

Posted 23 February 2009 - 09:39 PM

Try this instead
public class NewClass {
//Varibles

	private double mySide;
	private double myArea;
	private double myPerimeter;
//Constructors

	public NewClass(double side) {
		mySide = side;
		myArea = side * side;
		myPerimeter = side + side + side + side;
	}//end of Square
//Methods

	public double getSide() {
		return mySide;
	}//end of getSide

	public double calculateArea(double side) {
		return side * side;
	}//end of calArea

	public double calculatePerimeter(double side) {
		return side + side + side + side;
	}//end of calPerim

	@Override
	public String toString() {
		return "Side: " + mySide +
				", Area: " + myArea +
				", Perimeter: " + myPerimeter;
	}//end of toString
}//End Square


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1