Results 1 to 3 of 3

Thread: toString Issues

  1. #1
    dudeman007 is offline Member
    Join Date
    Sep 2013
    Rep Power

    Default toString Issues

    I am working on some home work and cannot figure out how to call the toString methods from the Point, Square and Cube classes to print out. I know it has to be something stupid, I think I am just toast and my mind is spent. Right now I have ??? in place where the toSting methods should go. Have tried every combination ("class".toString, etc) I can think of. Can anyone tell me where I am messing up? Thanks!!

    Java Code:
    import javax.swing.JOptionPane;
    public class InheritanceTest {
    	public static void main(String args[]){
    		// Declare variables
    		String xString = null;
    		String yString = null;
    		String sideString = null;
    		int x = 0;
    		int y = 0;
    		int sideLength = 0;
    			xString = JOptionPane.showInputDialog("Enter x coordinate:");
    			x = Integer.parseInt(xString);
    			yString = JOptionPane.showInputDialog("Enter y coordinate:");
    			y = Integer.parseInt(yString);
    			sideString = JOptionPane.showInputDialog("Enter side of Square:");
    			sideLength = Integer.parseInt(sideString);
    			} // End try
    			catch(NumberFormatException e){
    				JOptionPane.showMessageDialog(	null,"The value you entered is not a valid number. Please try again",
    						"Error", JOptionPane.ERROR_MESSAGE);
    			} // End catch
    			JOptionPane.showMessageDialog(null, "Point: \n" +????????, "Results", JOptionPane.INFORMATION_MESSAGE);
    	} // End main			
    }// End Class
    class Point{
    	//Declare variables
    	private int x;
    	private int y;
    	// Point constructor
    	Point(int xCoordinate,int yCoordinate){
    	x = xCoordinate;
    	y = yCoordinate;
    	}// End Point Constructor
    	// Accessor to return x coordinate
    	public int getX(){
    		return x;
    	}// End getX method
    	// Accessor to return y coordinate
    	public int getY(){
    		return y;
    	}// End getY method
    	// Format and display coordinates
    	public String toString(){
    		return "Corner = [" + x + "," + y + "]\n";
    	}// End toString
    }// End Point Class
    abstract class Square extends Point{
    	//Declare variables
    	private double sideLength;
    	// Square constructor
    	Square(int x, int y, double s){
    		super(x, y);
    		sideLength = s;
    	} // End Square constructor
    	// Accessor to return side length
    	public double getSide(){
    		return sideLength;
    	} // End getSide
    	// Method to calculate area of square
    	public double area(){
    		return sideLength * sideLength;
    	} // End area method 
    	// Method to calculate perimeter of square
    	public double perimeter(){
    		return 4 * sideLength;
    	} // End perimeter method
    	// Format and display the square
    	public String toString(){
    		return super.toString() + "Side length is: " + sideLength + "\n" +
    					"Area is: " + area() + "\n" + "Perimeter is: " + perimeter();
    	} // End toString
    }// End Square Class
    abstract class Cube extends Square{
    	// Declare variable
    	double depth;
    	// Cube constructor
    	public Cube(int x, int y, double s, double z){
    	super(x, y, s);
    	depth = z;
    	} // End Cube constructor
    	// Method to calculate area of cube
    	public double area(){
    		return 6 * super.area();
    	} // End Area
    	// Method  to calculate volume of cube
    	public double volume(){
    		return super.area() * depth;
    	} // End volume
    	// Format and display the cube
    	public String toString(){
    		return "Depth is: " + depth + "\n" + "Area is: " + area() + "\n" + "Volume is: " + volume();
    	} // End toString
    } // End Cube class
    Last edited by dudeman007; 09-23-2013 at 04:30 AM.

  2. #2
    sibernewf is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default Re: toString Issues

    ok, just quickly, you havent even used your Point class in your main method.
    Your toString() method is in your Point class, so you cant call it until you have a Point reference as you have written it.

  3. #3
    dudeman007 is offline Member
    Join Date
    Sep 2013
    Rep Power

    Default Re: toString Issues

    Duh, should have realized that. Like I said, I'm toast right now. Thanks @sibermewf

Similar Threads

  1. How to display toString() inside another toString()
    By SVTermiCobra in forum New To Java
    Replies: 1
    Last Post: 08-08-2012, 01:49 AM
  2. toString
    By Waarten in forum New To Java
    Replies: 11
    Last Post: 01-25-2012, 03:02 PM
  3. toString method call issues
    By Scopic in forum New To Java
    Replies: 16
    Last Post: 11-13-2011, 09:19 PM
  4. help with toString
    By teardrop3903 in forum New To Java
    Replies: 5
    Last Post: 04-28-2011, 02:39 AM
  5. toString
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-11-2010, 08:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts