2 Replies - 204 Views - Last Post: 06 March 2012 - 07:36 PM Rate Topic: -----

#1 vienhuynh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 24-April 10

why my toString doesn't print? I debug yet it is the same?

Posted 06 March 2012 - 07:30 PM

public class Sphere 
{
	private double diameter;
	private double area;
	private double vol;
	public Sphere()
	{
		diameter=0.0;
	}
	public Sphere(double dia)
	{
		diameter = dia;
	}
	public double getDiameter() 
	{
		return diameter;
	}
	public void setDiameter(double diameter) 
	{
		this.diameter = diameter;
	}
	public double SurfaceArea (double diameter)
	{
		area = 4 * 3.13 * ((diameter/2)*(diameter/2))  ;
		return area;
	}
	public double Vol(double diameter)
	{
		vol = (4/3) * 3.14*((diameter/2)*(diameter/2))  ;
		return vol;
	}
	public String toString()
	{
		return "Here is the sphere " +area+ " and volume " + vol;
	}
	
	
}



and my driver
import java.util.*;
public class Multisphere 
{

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		System.out.println("Please enter diameter number: ");
		double dia = scan.nextDouble();
		
		Sphere obj = new Sphere(dia);
		obj.SurfaceArea(dia);
		obj.Vol(dia);
		obj.toString();

	}

}


can someone tell me why it doesn't print?

Is This A Good Question/Topic? 0
  • +

Replies To: why my toString doesn't print? I debug yet it is the same?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: why my toString doesn't print? I debug yet it is the same?

Posted 06 March 2012 - 07:33 PM

You get the String but do nothing with it

obj.toString();

System.out.println(obj); should do the job :)
Was This Post Helpful? 1
  • +
  • -

#3 vienhuynh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 24-April 10

Re: why my toString doesn't print? I debug yet it is the same?

Posted 06 March 2012 - 07:36 PM

View Postpbl, on 06 March 2012 - 07:33 PM, said:

You get the String but do nothing with it

obj.toString();

System.out.println(obj); should do the job :)


love you pal :) I thought if I just return the toString and call the function use the obj, it would return the String to me? NVM, I got it, yea.... i need to print it out, system print out is needed.

This post has been edited by vienhuynh: 06 March 2012 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1