Results 1 to 4 of 4
  1. #1
    speedycerv is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default How do I fix my code - 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?

    I'm thinking it has something to do with me declaring the array objects as Camera2, however there is an example in my book for Firm (book by lewis/loftus) where it does the same thing that I did and it works on their program, but not mine. What code do I need to put in the change it? Please be specific what I need to type as this is confusing me greatly. Thank you in advance.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Please use code tags next time when posting code.
    You typed toString wrong. Method names are case sensitive so created a new method called tostring rather than override the toString method.

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

    Default

    What code do I need to put in the change it? Please be specific what I need to type as ...

    I'm not sure this is a particularly effective attitude.

    If you are not getting the output you expect then methods producing that output are not being called when you expect them to be. There are lots of reasons why this might be the case, but some are caught by using the @Override annotation. This tells the compiler that you are overriding some behaviour in the parent class. And the compiler will help by alerting you to problems.

    For usage and rationale see the Annotations page in Oracle's Tutorial.

  4. #4
    speedycerv is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you for the help! I was trying to use the code tags and I wasn't sure how to use them. I now see that there is a button to help me with

    Java Code:
    this

Similar Threads

  1. Polymorphism
    By blug in forum New To Java
    Replies: 3
    Last Post: 10-11-2010, 11:35 AM
  2. What's an easy way to test this code?
    By twiggy62 in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 10:41 AM
  3. Polymorphism Help
    By AWPtic in forum New To Java
    Replies: 5
    Last Post: 04-06-2009, 05:13 PM
  4. Replies: 0
    Last Post: 04-05-2008, 01:38 AM
  5. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 04:14 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
  •