Jim
Jim

Reputation: 51

Using toString correctly

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!!

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

Upvotes: 0

Views: 997

Answers (4)

P.C.DING
P.C.DING

Reputation: 76

JOptionPane.showMessageDialog(null, 
"Point: \n" + new Point(x, y).toString, 
"Results", 
JOptionPane.INFORMATION_MESSAGE);

Upvotes: 0

Erick G. Hagstrom
Erick G. Hagstrom

Reputation: 4945

You don't invoke the toString() method from a Class, you invoke it from an object (i.e. an instance of that Class). It's obj.toString() where obj is an instance of Point, Square or Cube (or any other class).

I don't see you instantiating any objects of those classes, though. You need to create an instance of the class before you can invoke an instance-based method like toString().

Point myPoint = new Point(x, y);
String pointString = myPoint.toString();

Upvotes: 2

Robbie
Robbie

Reputation: 101

new Point(x,y).toString()

toString() is a method of the Point class. You need to instantiate it first with x and y coordinates then call its toString() method.

Upvotes: 1

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15148

Well you need to create an instance of the class that you want to work with first, for example:

Square s = new Square(10, 10, 2.0);
System.out.println(s.toString());

Upvotes: 2

Related Questions