String toString() Help!

String toString() Help!

Page 1 of 1

5 Replies - 4703 Views - Last Post: 21 November 2010 - 08:07 PM Rate Topic: -----

#1 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

String toString() Help!

Posted 20 November 2010 - 11:30 PM

Hello,

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: :code:

This post has been edited by Dogstopper: 21 November 2010 - 07:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: String toString() Help!

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: String toString() Help!

Posted 21 November 2010 - 03:15 AM

What exacly is the problem with the toString() methods? do you get any error messages? or does it print the wrong output? please provide some more info about it.
Also, please post your code inside code tags, it will be much easier to read and debug.
Was This Post Helpful? 1
  • +
  • -

#3 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Re: String toString() Help!

Posted 21 November 2010 - 04:16 PM

Hello japanir!

Thank you for your response!



The Error messages I keep getting are:



Cannot make a static reference to the non-static method toString() from the type Point

Cannot make a static reference to the non-static method toString() from the type Square

Cannot make a static reference to the non-static method toString() from the type Cube



When I try to modify the methods in each of the classes by adding "static" to each toString() method, I then get an error message telling me to Remove 'static' modifier of 'toString(...)'.

Thank you,



Chris DeCamp
Was This Post Helpful? 0
  • +
  • -

#4 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 110
  • Joined: 04-December 09

Re: String toString() Help!

Posted 21 November 2010 - 04:28 PM

try making your volume and area variables non-static or doing something like Cube.volume when accessing it (not sure if that will work)
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: String toString() Help!

Posted 21 November 2010 - 04:28 PM

A lot lot lot too much static in your code
Beside the main() method nothing should be static

import javax.swing.JOptionPane;
public class Inheritence2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		//Declare variables
		int 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

		Point p = new Point(firstNumber, secondNumber);

		//Calculate the area of the square
		area = Square.getArea();

		//Calculate the volume of the square
		Cube c = new Cube(firstNumber, secondNumber);
		volume = c.getVolume();

		//Display results for point
		JOptionPane.showMessageDialog(null,"The length of your line is: " + p.toString());

		//Display results for square
		JOptionPane.showMessageDialog(null, " The area of  your square is: " + area);		
		//Display results for cube
		JOptionPane.showMessageDialog(null, " The volume of your cube is " + volume);
	}

}






class Point {

	int number1;
    int number2;


	public Point (int number1, int number2){
		this.number1 = number1;
		this.number2 = number2;
	}

	int getLength(){
		if (number1 > number2){

			return number1 - number2;

		}else

			return number2 - number1;
	}

	public String toString(){
		return " The length of your line is" + getLength();
	}

}


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;

	}		

}

class Cube extends Square {
    int volume;
    
	public Cube(int number1, int number2) {
		super(number1, number2);
		volume = sideLength * 3;

	}
	public int getVolume(){
		return volume;

	}

	public String toString(){

		return " The Volume of your cube is: " + volume;
	}


}


Was This Post Helpful? 1
  • +
  • -

#6 ChrisDeCamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-November 10

Re: String toString() Help!

Posted 21 November 2010 - 08:07 PM

Hey pbl!

Thank you so much! This really helped me a lot!!

Kind regards,

Chris DeCamp





View Postpbl, on 21 November 2010 - 02:28 PM, said:

A lot lot lot too much static in your code
Beside the main() method nothing should be static

import javax.swing.JOptionPane;
public class Inheritence2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		//Declare variables
		int 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

		Point p = new Point(firstNumber, secondNumber);

		//Calculate the area of the square
		area = Square.getArea();

		//Calculate the volume of the square
		Cube c = new Cube(firstNumber, secondNumber);
		volume = c.getVolume();

		//Display results for point
		JOptionPane.showMessageDialog(null,"The length of your line is: " + p.toString());

		//Display results for square
		JOptionPane.showMessageDialog(null, " The area of  your square is: " + area);		
		//Display results for cube
		JOptionPane.showMessageDialog(null, " The volume of your cube is " + volume);
	}

}






class Point {

	int number1;
    int number2;


	public Point (int number1, int number2){
		this.number1 = number1;
		this.number2 = number2;
	}

	int getLength(){
		if (number1 > number2){

			return number1 - number2;

		}else

			return number2 - number1;
	}

	public String toString(){
		return " The length of your line is" + getLength();
	}

}


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;

	}		

}

class Cube extends Square {
    int volume;
    
	public Cube(int number1, int number2) {
		super(number1, number2);
		volume = sideLength * 3;

	}
	public int getVolume(){
		return volume;

	}

	public String toString(){

		return " The Volume of your cube is: " + volume;
	}


}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1