I am working on an exercise called Inheritence.java. I am supposed to put together a program made up of three Classes, Point, Square and Cube. I needed to use Point as the Super Class and demonstrate Inheritence by calculating Point - Length, Square - Area and Cube - Volume. I believe everything I have done up to this point is correct. The problem I am having is implementing the String toString()method to get my output from all three classes.
Can anyone please help!
Thank you,
ChrisDeCamp
Here is my code:
import javax.swing.JOptionPane;
public class Inheritence2 {
/**
* @param args
*/
public static void main(String[] args) {
//Declare variables
int length, volume, area;
//Get user input
String digit1;
digit1 = JOptionPane.showInputDialog("Enter your first point ");
String digit2;
digit2 = JOptionPane.showInputDialog("Enter your second point ");
//Convert string into integers
int firstNumber = Integer.parseInt(digit1);//First Number entered by user
int secondNumber = Integer.parseInt(digit2);//Second Number entered by user
new Point(firstNumber, secondNumber);
//Calculate length
length = Point.getLength();
//Calculate the area of the square
area = Square.getArea();
//Calculate the volume of the square
volume = Cube.getVolume();
//Display results for point
JOptionPane.showMessageDialog(null,"The length of your line is: " + Point.toString());
//Display results for square
JOptionPane.showMessageDialog(null, " The area of your square is: " + Square.toString());
//Display results for cube
JOptionPane.showMessageDialog(null, " The volume of your cube is " + Cube.toString());
}
}
public class Point {
public static int number1;
public static int number2;
public Point (int number1, int number2){
Point.number1 = number1;
Point.number2 = number2;
}
public static int getLength(){
if (number1 > number2){
return number1 - number2;
}else
return number2 - number1;
}
public String toString(){
return " The length of your line is" + getLength();
}
}
public class Square extends Point {
public Square(int number1, int number2) {
super(number1, number2);
}
public static int sideLength;
public static int area;
public static int getArea(){
return area = sideLength * 2;
}
public String toString(){
return "The area of your Square is: " + area;
}
}
public class Cube extends Square {
public Cube(int number1, int number2) {
super(number1, number2);
}
public static int volume;
public static int getVolume(){
return volume = sideLength * 3;
}
public String toString(){
return " The Volume of your cube is: " + volume;
}
}
Edited by Dogstopper:
This post has been edited by Dogstopper: 21 November 2010 - 07:57 AM

New Topic/Question
Reply




MultiQuote




|