aspose file tools*
The moose likes Beginning Java and the fly likes Hierarchies, inheritance, polymorphism and interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hierarchies, inheritance, polymorphism and interfaces" Watch "Hierarchies, inheritance, polymorphism and interfaces" New topic
Author

Hierarchies, inheritance, polymorphism and interfaces

bitzack zack
Greenhorn

Joined: Nov 23, 2004
Posts: 2
Hi all,
I am trying to make some small changes to the version of this code:
I want to implement the changes listed below and run it as an application not an applet. I am kind of confused, any help or advice is highly apperecited

Bitzack!
The changes I wish to make are :

1.The edgeColor data member in class Shape is supplemented by a fillColor data member. For a Line, set the fillColor value to the edgeColor value (so edge and fill colors are the same for a Line.
2.A toString method is added to interface Displayable.
3.A drawFill method is added to interface Displayable. The old draw method should now draw a transparent shape (i.e., only a boundary for Rectangle and Oval) using the edgeColor while the drawFill method should draw a filled shape using the edgeColor for the edge and the fillColor for the body.
4.A perimeter method is added to the Computable interface. For a Line just use the length, for an Oval use PI*sqrt((width*width+height*height)/2). Return a double.
5.An interface Transformable provides methods translateTo( ) and scaleBy( ). The first method should move the shape parallel to itself. This involves moving the upperLeft to the specified location, and in the case of Line, moving the end by the same amount (if upperLeft were (100,50) originally and (200,25) were the specified new location, then end should also be moved 100 pixels to the right and 25 pixels up just like upperLeft). The second method should keep the center of the shape in the same location (rather than the upperLeft). Talk to me if you have trouble with this.
6.A driver program demonstrates the new functionality.



Here is a sample driver program followed by its output:

import java.awt.*;
import javax.swing.*;

public class ShapeApp extends JFrame
{
public static final int EXIT_ON_CLOSE = 3;

public void init()
{
setBounds(100,50,400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public void paint(Graphics g) //the objects are created and transformed here
//so that the drawing is maintained when the frame must be redrawn
{
Shape s;
s = new Line(10,10,100,80,Color.red);
s.draw(g);
s.scaleBy(1.5); // try scaleBy
s.draw(g);
s.translateTo(10,50); // try translateTo
s.draw(g);
s = new Rect(30,130,30,30,Color.blue,Color.green);
s.draw(g);
s.scaleBy(1.5);
s.draw(g);
s.translateTo(180,30);
s.drawFill(g);
s = new Oval (120,30,40,40,Color.blue,Color.red);
s.draw(g);
s.scaleBy(1.5);
s.draw(g);
s.translateTo(210,130);
s.drawFill(g);
}

public static void main(String args[])
{
ShapeApp f = new ShapeApp();
f.init();
Shape s;
s = new Line(10,10,100,80,Color.red);
s.print(); // see if print() works
s.scaleBy(1.5); // try scaleBy
System.out.println(s); // see if it worked
s.translateTo(10,50); // try translateTo
System.out.println(s); // see if it worked
s = new Rect(30,130,30,30,Color.blue,Color.green);
s.print();
s.scaleBy(1.5);
System.out.println (s);
s.translateTo(180,30);
System.out.println(s);
s = new Oval (120,30,40,40,Color.blue,Color.red);
s.print();
s.scaleBy(1.5);
System.out.println(s);
s.translateTo(210,130);
System.out.println(s);
}
} // end class ShapeApp

The Original Version of the Code
import java.awt.*;
import javax.swing.*;

// not a class, just an interface, abstract by default

interface Displayable
{
void draw(Graphics g);
void print();
}
// not a class, just an interface

interface Computable
{
double area();
Point center();
}

// the parent class Shape
// must be declared abstract because it does not define the methods draw, area and center
abstract class Shape implements Displayable, Computable
{
protected Point upperLeft; // public in child class, private to the outside world
protected Color edgeColor;

public Shape(int x, int y, Color c)
{
upperLeft = new Point(x,y);
edgeColor = c;
}

// this is Shape's version of print()
public void print()
{
System.out.println(getClass().toString().substring(6)+ ":\n" +
"\tupperLeft: "+upperLeft+" "+"\n\tedgeColor: "+ edgeColor);
// uses the correct area() and center() methods from a subclass
System.out.println("\tarea: "+area()+"\n\tcenter: "+center());
}

public Color getEdgeColor()
{
return new Color(edgeColor.getRGB());
}
}

// each particular kind of shape knows how to draw and print itself
class Line extends Shape
{
private Point end;

public Line(int xl, int yl, int x2, int y2, Color c)
{
super(xl,yl,c);
end = new Point(x2,y2);
}

public void draw(Graphics g)
{
g.setColor(edgeColor); //if edgeColor were private in Shape we could not get at it
g.drawLine(upperLeft.x, upperLeft.y, end.x, end.y);
}

public void print()
{
super.print();//print data common to all shapes
System.out.println("\tend: "+end);//print added line data
}

public double area()
{
return 0; // lines don't really have an area
}

public Point center()
{
return new Point((int)((end.x+upperLeft.x)/2.0),
(int)((end.y+upperLeft.y)/2.0));
}
}

class Rect extends Shape
{
private int width;
private int height;

public Rect(int x, int y, int w, int h, Color c)
{
super(x,y,c);
width = w;
height = h;
}

public void draw(Graphics g)
{
g.setColor(edgeColor);
g.drawRect(upperLeft.x, upperLeft.y, width, height);
}

public void print ()
{
super.print();//print data common to all shapes
System.out.println("\twidth: "+width+"\n\theight: "+height);
//print added rectangle data
}

public double area()
{
return width*height;
}

public Point center()
{
return new Point((int)(upperLeft.x+width/2.0),(int)(upperLeft.y+height/2.0));
}
}

class Oval extends Shape
{
private int width;
private int height;

public Oval(int x, int y, int w, int h, Color c)
{
super (x,y,c);
width = w;
height = h;
}

public void draw(Graphics g)
{
g.setColor(edgeColor);
g.drawOval(upperLeft.x, upperLeft.y, width, height);
}

public void print()
{
super.print();//print data common to all shapes
System.out.println("\twidth: "+width+"\n\theight: "+height);
//print added oval data
}

public double area()
{
return (Math.PI*width*height/4); // area of an ellipse
}

public Point center()
{
return new Point((int)(upperLeft.x+width/2.0),(int)(upperLeft.y+height/2.0));
}
}

public class ShapeApp extends JFrame
{
public static final int EXIT_ON_CLOSE = 3;

public void init()
{
setBounds(100,50,400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public void paint(Graphics g) //the objects are created and transformed here
//so that the drawing is maintained when the frame must be redrawn
{
Line l = new Line(100,200,40,70,Color.red);
l.draw(g);
Rect r = new Rect(70,30,100,150,Color.blue);
r.draw(g);
Oval o = new Oval(245,60,110,70,Color.green);
o.draw(g);
}

public static void main(String args[])
{
ShapeApp f = new ShapeApp();
f.init();
Line l = new Line(0,20,40,70,Color.red);
l.print();
System.out.println();

Rect r = new Rect(10,30,40,50,Color.blue);
r.print();
System.out.println();

Oval o = new Oval(45,60,10,20,Color.green);
o.print();
System.out.println();

//NOTE: you can cast an object both to its base class and
//its base interface
Shape s;
Displayable d;
Computable c;

s = l; // an implicit cast to l's base class
d = r; // an implicit cast to one of r's base interfaces
c = o; // an implicit cast to one of o's base interfaces

s.print(); //the correct version of print for l will be called
System.out.println();
d.print(); //the correct version of print for r will be called
System.out.println();
//c.print(); //can't be called because print is not a method in
//Computable
}
}
/* output...
Line:
upperLeft: java.awt.Point[x=0,y=20]
edgeColor: java.awt.Color[r=255,g=0,b=0]
area: 0.0
center: java.awt.Point[x=20,y=45]
end: java.awt.Point[x=40,y=70]

Rect:
upperLeft: java.awt.Point[x=10,y=30]
edgeColor: java.awt.Color[r=0,g=0,b=255]
area: 2000.0
center: java.awt.Point[x=30,y=55]
width: 40
height: 50

Oval:
upperLeft: java.awt.Point[x=45,y=60]
edgeColor: java.awt.Color[r=0,g=255,b=0]
area: 157.07963267948966
center: java.awt.Point[x=50,y=70]
width: 10
height: 20

Line:
upperLeft: java.awt.Point[x=0,y=20]
edgeColor: java.awt.Color[r=255,g=0,b=0]
area: 0.0
center: java.awt.Point[x=20,y=45]
end: java.awt.Point[x=40,y=70]

Rect:
upperLeft: java.awt.Point[x=10,y=30]
edgeColor: java.awt.Color[r=0,g=0,b=255]
area: 2000.0
center: java.awt.Point[x=30,y=55]
width: 40
height: 50

*/
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
1. JavaRanch prefers that you change your name to something real-sounding, like your real name.

2. Your code is very hard to read with no indentation. To fix this, please use the "CODE" UBB code for posting code. You can either use the CODE button at the bottom of the "Post A Reply" window or just surround your code with [>CODE] at the beginning and [>/CODE] at the end. Don't use the >, that was just to make the tags print in this note.

3. Please take a crack at doing this assignment yourself and ask questions when you have a problem. You don't really want us to do youre homework if we won't be there to take your exams.

4. With that said, please get back to us, we're here to help!


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hierarchies, inheritance, polymorphism and interfaces