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:
Then I have a driver 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 the main class: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 subclasses: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()); } }
/** *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; } }
The output is :/** *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; } }
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?