Results 1 to 3 of 3
Thread: toString Issues
- 09-23-2013, 05:14 AM #1
Member
- Join Date
- Sep 2013
- Posts
- 2
- Rep Power
- 0
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; try{ 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 05:30 AM.
- 09-23-2013, 05:30 AM #2
Member
- Join Date
- Mar 2011
- Posts
- 50
- Rep Power
- 0
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.
- 09-23-2013, 05:44 AM #3
Member
- Join Date
- Sep 2013
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
How to display toString() inside another toString()
By SVTermiCobra in forum New To JavaReplies: 1Last Post: 08-08-2012, 02:49 AM -
toString
By Waarten in forum New To JavaReplies: 11Last Post: 01-25-2012, 04:02 PM -
toString method call issues
By Scopic in forum New To JavaReplies: 16Last Post: 11-13-2011, 10:19 PM -
help with toString
By teardrop3903 in forum New To JavaReplies: 5Last Post: 04-28-2011, 03:39 AM -
toString
By luckyleaf95 in forum New To JavaReplies: 9Last Post: 02-11-2010, 09:52 AM
Bookmarks