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 4 of 4

Thread: Polymorphism test

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Polymorphism test

    I have a program i'm trying to write that is showing polymorphism and it isn't working as expected. I expect the output to have an extra line that will have the information that is unique to the subclasses.

    Here is the main program:
    /**
     *
     * @author Derek Allan
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            CameraDriver test = new CameraDriver();
     
            test.printMe();
        }
     
    }
    Then I have a driver program:
    public class CameraDriver {
     
        public Camera2[] cameraList;
     
        public CameraDriver()
        {
           cameraList = new Camera2[3];
     
           cameraList[0] = new Camcorder("Samsung", 12, 2.1, 2.2, "black");
           cameraList[1] = new PointAndShoot("Canon", 10, 3.9, 0.2, 3.8);
           cameraList[2] = new ProfesionalVideo("Panasonic", 3, 2.6, 2.7, true);
        }
     
        public void printMe()
        {
            for(int count = 0; count < cameraList.length; count++)
            {
                System.out.println(cameraList[count]);
    				System.out.println();
            }
        }
     
    }
    Then I have the main class:
    public class Camera2 {
     
        protected String brandName;
        protected int megapixel;
        protected double width;
        protected double height;
     
        public Camera2()
        {
            brandName = "";
            megapixel = 0;
            width = 0;
            height = 0;
        }
        /**
         * Constructor for the data of a camera
         * @param bn brand name
         * @param mp megapixel
         * @param w width
         * @param h height
         */
        public Camera2(String bn, int mp, double w, double h)
        {
            brandName = bn;
            megapixel = mp;
            width = w;
            height = h;
        }
        /**
         * Sets the camera brand name
         * @param bn camera brand name
         */
        public void setBrand(String bn)
        {brandName = bn;}
        /**
         * returns the camera brand name
         * @return camera brand name
         */
        public String getBrand()
        {return brandName;}
        /**
         * sets the camera megapixel
         * @param mp megapixel
         */
        public void setMega(int mp)
        {megapixel = mp;}
        /**
         * returns the camera megapixel
         * @return megapixel
         */
        public int getMega()
        {return megapixel;}
        /**
         * sets the camera width
         * @param w width
         */
        public void setWidth(double w)
        {width = w;}
        /**
         * returns the camera width
         * @return width
         */
        public double getWidth()
        {return width;}
        /**
         * sets the camera Height
         * @param h Height
         */
        public void setHeight(double h)
        {height = h;}
        /**
         * returns the camera Height
         * @return Height
         */
        public double getHeight()
        {return height;}
        /**
         * Method created to show the set values for all variables we are interested in.
         * @return Brand, megapixel, height and width.
         */
        public String toString()
        {
            return("Brand is " + getBrand())
              + ("\nMegapixel is " + getMega())
              + ("\nHeight is " + getHeight())
              + ("\nWidth is " + getWidth());
        }
     
     
    }
    Then I have the subclasses:
    /**
     *Subclass of camera
     * @author Derek Allan
     */
    public class Camcorder extends Camera2{
     
        protected String color;
     
        public Camcorder()
        {
            brandName = "";
            megapixel = 0;
            width = 0;
            height = 0;
            color = "";
     
        }
        public Camcorder(String bn, int mp, double w, double h, String col)
        {
            super(bn, mp, w, h);
            color = col;
        }
     
        public void setColor(String col)
        {
            color = col;
        }
     
        public String getColor()
        {
            return color;
        }
     
        public String tostring()
        {
            String result = super.toString();
            result += "\nThe Came corder is " + color + " in color.";
     
    	return result;
        }
    }
    /**
     *Subclass of camera
     * @author Derek Allan
     */
    public class PointAndShoot extends Camera2{
     
        protected double opticalZoom;
     
        public PointAndShoot(String bn, int mp, double w, double h, double opZo)
        {
            super(bn, mp, w, h);
            opticalZoom = opZo;
        }
     
        public void setOpZo(double opZo)
        {
            opticalZoom = opZo;
        }
     
        public double getOpZo()
        {
            return opticalZoom;
        }
     
        public String tostring()
        {
            String result = super.toString();
            result += "\nThe Point and Shoot camera has " + opticalZoom + " opticalZoom.";
     
    	return result;
        }
    }

    /**
     *Subclass of camera
     * @author Derek Allan
     */
    public class ProfesionalVideo extends Camera2{
     
        protected boolean hdvideo1080p;
     
        public ProfesionalVideo(String bn, int mp, double w, double h, boolean hd)
        {
            super(bn, mp, w, h);
            hdvideo1080p = hd;
        }
     
        public void setHd(boolean hd)
        {
            hdvideo1080p = hd;
        }
     
        public boolean getHd()
        {
            return hdvideo1080p;
        }
     
        public String tostring()
        {
            String result = super.toString();
            result += "\nIt is  " + hdvideo1080p + " that there is hd 1080p video in the video camera.";
     
    	return result;
        }
    }
    The output is :
    Brand is Samsung
    Megapixel is 12
    Height is 2.2
    Width is 2.1

    Brand is Canon
    Megapixel is 10
    Height is 0.2
    Width is 3.9

    Brand is Panasonic
    Megapixel is 3
    Height is 2.7
    Width is 2.6


    but i expect the "black", 3.8 and true string portions to be showing in the output at the last lines. What do I need to do to correct this code?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Polymorphism test

    Your problem is that your array holds Camera2 objects so it will call the toString method in the Camera2 class. You could use the instanceof operator and cast to the subclasses and then print them.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Polymorphism test

    I've never used the instanceOf operator before could you show me what code I would need to use to make this work? I tried cameraList[0] instanceOf Camcorder; but it doesn't work, I'm sure I'm using it wrong.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Polymorphism test

    1. Check whether your code is executing all functions you need it to execute?
    2. Your toString() is returning only the concatenation of four values but not the fifth value (black,3.8,true), check this.

Similar Threads

  1. Polymorphism issue
    By LDM91 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2010, 03:26 PM
  2. JUnit test for ER
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 10:26 PM
  3. JDBC API Test suite 1.3.1
    By prabhuaakannan in forum JDBC & Databases
    Replies: 0
    Last Post: March 26th, 2010, 10:57 AM
  4. Test and Set Explanation
    By bananasplitkids in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 6th, 2010, 01:10 PM
  5. A simple test for you all
    By Maximillian in forum The Cafe
    Replies: 6
    Last Post: December 2nd, 2009, 04:15 PM