Title: Polymorphism Examples
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Polymorphism Examples

Drawing Program

Shape.java


import java.awt.Color;
import java.awt.Graphics;

public abstract class Shape {
    private int x, y;
    private String color;
    public abstract void draw(Graphics g);

    public void setColor(Graphics g)
    {
        if (color.equals("red"))
            g.setColor(Color.red);
        else if (color.equals("blue"))
            g.setColor(Color.blue);
        else if (color.equals("green"))
            g.setColor(Color.green)
;    }

    public int getX()
    {
        return x;
    }
    public int getY()
    {
        return y;
    }

    public Shape(int x, int y, String color)
    {
        this.x = x;
        this.y =y;
        this.color = color;
    }



}

class Rectangle extends Shape
{
    private int width, height;

    public Rectangle(int x, int y, String color, int width, int height)
    {
        super(x,y,color);
        this.width = width;
        this.height = height;
    }

    public void draw(Graphics g)
    {
        setColor(g);
        g.drawRect(getX(), getY(), width, height);
    }
}

class Oval extends Shape
{
    private int width, height;

    public Oval(int x, int y, String color, int width, int height)
    {
        super(x,y,color);
        this.width = width;
        this.height = height;
    }
    public void draw(Graphics g)
    {
        setColor(g);
        g.drawOval(getX(), getY(), width, height);
    }


}


MyDrawing.java


package shape;

import java.awt.Graphics;
import java.util.Scanner;

import javax.swing.JFrame;

public class MyDrawing extends JFrame{

    Shape[] shapeArr = new Shape[1000];
    int count =0;

    public void addShape(Shape s)
    {
        shapeArr[count++] = s;
        repaint();
    }
    MyDrawing()
    {
        super("My First GUI Example");
        setSize(800, 700);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void paint(Graphics g)
    {
        super.paint(g);

        for (int i=0; i < count; i++)
        {
            shapeArr[i].draw(g);
        }
    }
    public static void main(String[] args) {
        MyDrawing md = new MyDrawing();
        md.addShape( new Rectangle(100,200, "red", 40,50) );

        Scanner keyboard = new Scanner(System.in);

        while (true)
        {
            System.out.println("Enter object to draw [r/o] color, x, y, width, height");
            String type = keyboard.next();
            String color = keyboard.next();
            int x = keyboard.nextInt();
            int y = keyboard.nextInt();
            int width= keyboard.nextInt();
            int height = keyboard.nextInt();
            if ("r".equals(type))
                md.addShape(new Rectangle(x,y, color, width, height));
            else if ("o".equals(type))
                md.addShape(new Oval(x,y, color, width, height));

        }


    }

}


Music Examples

Abstract Version

MakeMusic.java


package music_abstract;

public abstract class MakeMusic {
    abstract String getType();

    abstract String play();

}

class Drum extends MakeMusic
{
    String getType()
    {
        return "drum";
    }
    String play()
    {
        return "rat a tat tat";
    }
}

class Brass extends MakeMusic
{
    String getType()
    {
        return "Brass";
    }
    String play()
    {
        return "some brass sound";
    }
}
class Tuba extends Brass
{
    String getType()
    {
        return "Tuba";
    }
    String play()
    {
        return "Oomp Pa Pa";
    }
}


Orchestra.java


package music_abstract;

public class Orchestra {


    public static void main(String[] args)
    {
        MakeMusic[] orchestra = new MakeMusic[4];

        orchestra[0] = new Drum();
        orchestra[1] = new Tuba();
        orchestra[2] = new Drum();
        orchestra[3] = new Brass();

        for (int i=0; i < orchestra.length; i++)
        {
            MakeMusic m = orchestra[i];
            System.out.println(m.getType() + " makes the sound " + m.play());
        }
    }
}



Interface Version

MakeMusic.java

public interface MakeMusic {
    public String getType();
    public String play();
}


Instruments.java


class Drum implements MakeMusic
{
    public String getType()
    {
        return "drum";
    }
    public String play()
    {
        return "rat a tat tat";
    }
}

class Brass implements MakeMusic
{
    public String getType()
    {
        return "Brass";
    }
    public String play()
    {
        return "some brass sound";
    }
}
class Tuba extends Brass
{
    public String getType()
    {
        return "Tuba";
    }
    public String play()
    {
        return "Oomp Pa Pa";
    }
}



Singer.java


class Person
{
    String name;
    int age;
    Person (String n, int a)
    {
        age =a;
        name =n;
    }

}

public class Singer extends Person  implements MakeMusic{

    Singer(String n, int a)
    {
        super(n,a);
    }
    public String getType() {
        return "Singer";
    }
    public String play() {
        return "la la la la";
    }
}




Orchestra.java

public class Orchestra {


    public static void main(String[] args)
    {
        MakeMusic[] orchestra = new MakeMusic[5];

        orchestra[0] = new Drum();
        orchestra[1] = new Tuba();
        orchestra[2] = new Drum();
        orchestra[3] = new Brass();
        orchestra[4] = new Singer("Grace",25);

        for (int i=0; i < orchestra.length; i++)
        {
            MakeMusic m = orchestra[i];
            System.out.println(m.getType() + " makes the sound " + m.play());
        }
    }
}


Last Modified: October 27, 2013 9:05 PM