Results 1 to 5 of 5
Thread: Inheritance Problem
- 03-25-2010, 07:44 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
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:
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: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) } }
- 03-25-2010, 08:07 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Inheritance works quite differently for static fields and methods.
Remove the "static" modifier EVERYWHERE and you should be much better off
- 03-25-2010, 08:08 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 03-25-2010, 08:21 PM #4
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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) } }
- 03-25-2010, 08:23 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
These comments apply for wrong==!(how I would do it), so they might be a little opinionated...Any hints on what I'm doing wrong?
(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).
(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.Java Code:/** Creates a point with given coordinates. */ public Point(double x, double y) { this.x = x; this.y = y; }
-----
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
-
inheritance problem
By er1c550n20 in forum New To JavaReplies: 2Last Post: 03-10-2010, 06:01 PM -
Inheritance method problem
By Chasingxsuns in forum New To JavaReplies: 3Last Post: 11-10-2009, 08:30 PM -
Problem with Inheritance
By KronikAlkoholik in forum New To JavaReplies: 4Last Post: 08-25-2009, 12:13 AM -
Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-07-2007, 06:08 PM -
Inheritance in GUI
By Marty in forum SWT / JFaceReplies: 2Last Post: 05-11-2007, 12:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks