Results 1 to 5 of 5
  1. #1
    g2beastie is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default Inheritance Problem

    Hello. I am fairly new to java and programming in general. I'm having a pronlem with an inheritance assignment. Here are instructions:

    "Write a program called InheritanceTest.java to support an inheritance hierarchy for class Point–Square–Cube. Use Point as the superclass of the hierarchy. Specify the instance variables and methods for each class. The private data of Point should be the x-y coordinates, the private data of Square should be the sideLength, the private data of Cube should be depth. Provide applicable accessor methods, mutator methods, toString() methods, area() method and volume() method to all classes. Write a program that instantiates objects of your classes, test all instance methods and outputs each object’s area and volume when appropriate. "

    I just don't know what else to do at this point, and I believe is to my lack of understanding of constructor methods and inhertiance I think. Here's what I have so far for the three classes:

    Java Code:
    public class Point {
    	
    	private static int x;
    	private static int y;
    	
    	public Point(){
    		int ex = 0;
    		int why = 0;
    		x = ex;
    		y = why;
    	}
    	
    	public static int getX(){
    		
    		return x;
    	}
    	public static int getY(){
    		
    		return y;
    	}
    	
    	public String toString(){
    		
    		String output = "Point class X coordinate = "+ x +", Y coordinate = "
    		+y;
    		
    		return output;
    		
    	}
    
    }
    
    public class Square extends Point {
    	
    	private static double sideLength;
    	
    	public Square(){
    		
    		this.sideLength = 
    	}
    	
    	public static double getSideLength(int a, int b){
    		double c;
    		c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    		
    		return c;
    	}
    	
    	public static double getArea(){
    		
    		double area = sideLength * sideLength;
    		
    		return area;
    	}
    	
    	public String toString(){
    		
    		String output = "Square class sidelength = "+ sideLength;
    		
    		return output;
    	}
    	
    	
    
    }
    
    
    public class Cube extends Square {
    
    	private static double depth, volume, side;
    	
    	
    	
    	public static double getVolume(){
    		
    		volume = (sideLength * sideLength)
    	}
    }
    Any hints on what I'm doing wrong? I've tried so many things but I really don't know what I'm doing. Point me in the right direction please. Thank you.:confused:

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Inheritance works quite differently for static fields and methods.

    Remove the "static" modifier EVERYWHERE and you should be much better off

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Ok, the first thing you're doing wrong is your variables are defined as static, i.e. class variables. That means that every Point object (or a subclass object) you create will share the same value of x and y. What you need to do is take static out of the definition of x and y and the methods that use them, as you want to work with different Points, Squares and Cubes that have different properties.
    Now, the getSideLength() method in Square class is a good example of a method that should not be static, and it shouldn't be used to calculate the side length, but return the value that is set at object creation.

  4. #4
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Understand "static" and use it purposefully.

    This should help you a little.
    You are over using "static".
    Check out its purpose, and you will see this is true.
    But then, your code will probably compile,
    and run with these static values as they are.
    They just look obviously amature.

    Get your understanding of "static" clear in your mind
    early because there are some challenging compiler
    error messages that are frustrating to the beginner
    who lacks this knowledge.

    Java Code:
    public class Point {
    	
    	private static int x;         //   THIS SHOULD NOT BE STATIC
    	private static int y;         //   THIS SHOULD NOT BE STATIC
    	
    	public Point(){
    		int ex = 0;
    		int why = 0;
    		x = ex;
    		y = why;
    	}
    	
    	public static int getX(){         //   THIS SHOULD NOT BE STATIC
    		
    		return x;
    	}
    	public static int getY(){         //   THIS SHOULD NOT BE STATIC
    		
    		return y;
    	}
    	
    	public String toString(){
    		
    		String output = "Point class X coordinate = "+ x +", Y coordinate = "
    		+y;
    		
    		return output;
    		
    	}
    
    }
    
    public class Square extends Point {
    	
    	private static double sideLength;         //   THIS SHOULD NOT BE STATIC
    	
    	public Square(){
    		
    		this.sideLength = 
    	}
    	
    	public static double getSideLength(int a, int b){         //   THIS SHOULD NOT BE STATIC
    		double c;
    		c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    		
    		return c;
    	}
    	
    	public static double getArea(){         //   THIS SHOULD NOT BE STATIC
    		
    		double area = sideLength * sideLength;
    		
    		return area;
    	}
    	
    	public String toString(){
    		
    		String output = "Square class sidelength = "+ sideLength;
    		
    		return output;
    	}
    	
    	
    
    }
    
    
    public class Cube extends Square {
    
    	private static double depth, volume, side;         //   THIS SHOULD NOT BE STATIC
    	
    	
    	
    	public static double getVolume(){         //   THIS SHOULD NOT BE STATIC
    		
    		volume = (sideLength * sideLength)
    	}
    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Any hints on what I'm doing wrong?
    These comments apply for wrong==!(how I would do it), so they might be a little opinionated...

    (1) Remove all the "static" keywords. If anything (other than the main() method) in your code is marked static be prepared to explain why you are using static rather than doing it in a more sensible way.

    (2) Make x and y in the Point class double rather than int. It will keep things simple if all the dimensional values are of the same type. Likewise the other ints.

    (3) Provide arguments to the constructors. The constructor will need the information that goes into defining whatever it is that the instance represents. (The point (0,0) is not particularly special and does not deserve its own, no argument, constructor).

    Java Code:
        /** Creates a point with given coordinates. */
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    (4) Work one step at a time. (1)->(3) apply to the other classes as well (and some other things!) but stop here and write your test program to create a point and use its accessor methods. The idea is to be able to use the compiler to alert you of any bugs it can find so you can fix them, and let you check the output is what you expect. Working in this way allows to ask very focused questions here: what a compiler message means, or why you get the output you do.

    -----

    It has to be said that there are problems with this assignment. A square IS-NOT-A point. Squares and cubes differ in the number of dimensions they have but not (when I went to school) in the extent they occupy along those dimensions. There's nothing you can do about this, of course. You've been given a pig's ear: good luck!


    [Edit] Very slow ...;( Sorry for the repetition.
    Last edited by pbrockway2; 03-25-2010 at 08:25 PM.

Similar Threads

  1. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  2. Inheritance method problem
    By Chasingxsuns in forum New To Java
    Replies: 3
    Last Post: 11-10-2009, 08:30 PM
  3. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 12:13 AM
  4. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:08 PM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 AM

Posting Permissions

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