Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Inheritance variables

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Inheritance variables

    Hello everyone,

    I have this assignment for my Java class:
    1. Make your Shapes comparable add: "implements Comparable" and create the method: public int compareTo(Object o){ ... }
    to the abstract class Shape (so all the Shapes inherit it). For the comparison use the Shapes' area.
    2. Create a driver (called Driver.java) that:
    a) creates an array of shapes (ask user for the size)
    b) in a loop, populate the array. (ask user what shape to create and the parameters need to create each shape)
    c) print out the unsorted array
    d) sort the array (I will post an implementation of SelectionSort in a file called SelectioSort.java)
    e) print out sorted array
    Extra credit
    Use the authors ReadInt and ReadDouble to get all users input (in a file called ReadData.java)

    My problem is populating the array to create each shape in class Driver. I can't figure out how to inherit the variables from each subclass using switch.

    // Driver.java
    // Driver for testing the sorting of simple Shapes (hierarchy: point, square, cube, circle, & cylinder)
    //   it creates an array of shapes 
    //   it populate the array
    //   print out the unsorted array (area & volume of each shape)
    //   sort the array using an implementation of SelectionSort (shapes need to implement Comparable)
    //   and lastly print out sorted array (area & volume of each shape)
    // Needs: Shape.java, Point.java, Square.java, Cube.java, Circle.java, Cylinder.java & SelectioSort.java
     
    import java.io.*;
    import java.text.*;
     
    public class Driver {
     
        public static void main(String[] args) throws IOException {
     
            Shape[] arrayOfShapes; //holds the list
            int choice; //code number for each type of figure
     
            System.out.println("How many data you want to input? ");
            int size = ReadData.readInt();
            System.out.println("Enter: " + size);
            System.out.println("\n");
            arrayOfShapes = new Shape[size];
     
            for (int i = 0; i < arrayOfShapes.length; i++) {
                Point point;
                Square square;
                Cube cube;
                Circle circle;
                Cylinder cylinder;
     
                System.out.println("What Shape do you want to create?");
                System.out.println("1. Point");
                System.out.println("2. Square");
                System.out.println("3. Cube");
                System.out.println("4. Circle");
                System.out.println("5. Cylinder");
                System.out.println("Choose one:");
                choice = ReadData.readInt();
                System.out.println("\n");
                switch (choice) {
                    case 1:
                        arrayOfShapes[i] = new Point(x, y);
                        System.out.println("Please enter Coordinate 1: ");
                        System.out.println("Please enter Coordinate 2: ");
                        s = ReadData.readDouble();
                        System.out.println(x);
                        y = ReadData.readDouble();
                        System.out.println(y);
                        System.out.println(point);
                        break;
                    case 2:
                        arrayOfShapes[i] = new Square(side, x, y);
                        System.out.println("Please enter the 3 size of square: ");
                        side = ReadData.readDouble();
                        x = ReadData.readDouble();
                        y = ReadData.readDouble();
                        System.out.println(square);
                        break;
                    case 3:
                        arrayOfShapes[i] = new Cube(depth, x, y);
                        System.out.println("Please enter the 3 size of Cube: ");
                        depth = ReadData.readDouble();
                        x = ReadData.readDouble();
                        y = ReadData.readDouble();
                        System.out.println(cube);
                        break;
                    case 4:
                        arrayOfShapes[i] = new Circle(radius, x, y);
                        radius = ReadData.readDouble();
                        System.out.println("Please enter the radius for circle: ");
                        radius = ReadData.readDouble();
                        System.out.println(circle);
                        break;
                    case 5:
                        arrayOfShapes[i] = new Cylinder(height, x, y, radius);
                        System.out.println("Please enter the height of Cylinder: ");
                        height = ReadData.readDouble();
                        System.out.println(cylinder);
                    default:
                        break;
                }
            }
     
            DecimalFormat precision2 = new DecimalFormat("#0.00");
     
            // Loop through arrayOfShapes and print the name, area, and volume of each object.
            System.out.println(" Before we sort on area we have :");
            for (int i = 0; i < arrayOfShapes.length; i++) {
                System.out.print(arrayOfShapes[i].getName() + ": " + arrayOfShapes[i].toString());
                System.out.print(" volume = " + precision2.format(arrayOfShapes[i].volume()));
                System.out.println(" AREA = " + precision2.format(arrayOfShapes[i].area()));
            }
     
            SelectionSort.sort(arrayOfShapes, arrayOfShapes.length);
     
            System.out.println(" After we sort the array we have :");
            for (int i = 0; i < arrayOfShapes.length; i++) {
                System.out.print(arrayOfShapes[ i].getName() + ": " + arrayOfShapes[ i].toString());
                System.out.print(" volume = " + precision2.format(arrayOfShapes[ i].volume()));
                System.out.println(" AREA = " + precision2.format(arrayOfShapes[ i].area()));
            }
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Inheritance variables

    I don't understand exactly what your problem is, but don't create the shape until the data has been gathered to create it. For example, "new Square()" should not be the first statement of the case clause but the last. And why does a square require 3 pieces of data to be defined?

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance variables

    That's the same question i ask myself. The teacher wants us to have a point that extends shape, and square extends point. So x and y are variables that square inherits from point. I don't know how to copy the same user input from x and y in point to case 2 .

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Inheritance variables

    Still a bit confused, but if every Shape has an (x, y) - a point that defines where they are in space - then those variables are part of the super class, Shape. Square doesn't inherit them from Point. Instead, all subclasses of Shape inherit (x, y) from Shape. I'm not sure you've interpreted the directions correctly, because based on what you've now said, I wouldn't think Point would be a separate class at all. Besides, a Point isn't really a shape.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance variables

    I understand what you are saying. This whole project confused me. We had been adding shapes to the package for 2 weeks, so he wanted to us implement Comparable to Shape, and do a selection sort to print area. Yes, point is not a shape, but professor wanted point as a shape.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Inheritance variables

    I don't have the 2 weeks of experience to rely on, but based on what you've said, either Shape has the instance variables (x, y) that all subclasses inherit, or Shape contains an instance variable Point that is either defined separately or as a Shape nested class, OR all subclasses of Shape have their own instance variable Point, a separate class. I'm leaning towards the second option as being the closest to what you've described.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance variables

    this is Shape

    public abstract class Shape implements Comparable{
       public double area() { return 0.0; }
       public double volume() { return 0.0; }
       public abstract String getName();  
     
       @Override
       public int compareTo(Object o){   
           if(this.area() < ((Shape)o).area()){
               return -1;
           }
           else if (this.area() == ((Shape)o).area()){
               return 0;
           }
           else
             return 1;
       }
    }

    Point
    public class Point extends Shape {
       protected double x, y; // coordinates of the Point
     
       public Point() { //no argument constructor
          setPoint( 0, 0); 
       }
       //constructor
       public Point(double a, double b){
           setPoint(a,b);
       }
     
       public void setPoint( double a, double b ) { 
          x = a; 
          y = b; 
       }
       public double getX() { 
          return x; 
       }
       public double getY() { 
          return y; 
       }
       public String toString() { 
          return "[" + x + ", " + y + "]"; 
       }
       public String getName() { 
          return "Point"; 
       }
    }

    Square

    public class Square extends Point{  
       protected double side;
       //private Point p;       // composition ---remove this, call super()
     
       public Square()  { 
          //call 3 arg constructor (side, x, y)
          this( 0.0, 0.0, 0.0 );  
       }
     
       public Square( double s, double x, double y ) {
          super(x,y);//call to the two-argument constructor of Point
          setSide(s); 
       }
     
       public void setSide( double s ) { 
          side = ( s >= 0 ? s : 0 ); // if (s>=0) side=s; else side=0;
       }
     
       public double getSide() { 
          return side; 
       }
     
       public double area() { 
          return Math.pow( side, 2 ); 
       }
     
       public String toString() { 
          return "Corner = " + super.toString() + "; Side = " + side; 
       }
     
       public String getName() { 
          return "Square"; 
       }
     
       public String getPointName() { 
          return super.getName(); 
       }
     
       public String getPointString() {  
          return super.toString(); 
       }
    }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Inheritance variables

    Square extends Point. Didn't see that coming (Edit: even though you stated it plainly enough - sorry I missed that), but now I understand why the Square() constructor requires 3 parameters.

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance variables

    If it was for me, i wouldn't have Point. Instead, i would have Square extend Shape.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,399
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: Inheritance variables

    Well, do what you gotta do. Now that I understand the prof's requirements better, what's your question? I think if you create the class hierarchy that you've described, all should fall into place.
    My problem is populating the array to create each shape in class Driver. I can't figure out how to inherit the variables from each subclass using switch.
    Is that still the question? If so, I don't understand what you're asking, except to my original point that you create the objects before you have their attributes.

Similar Threads

  1. Inheritance
    By vasanthjayaraman in forum Java Theory & Questions
    Replies: 5
    Last Post: July 10th, 2013, 12:28 PM
  2. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 07:34 PM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  5. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM