Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives The goals of this lab are to review some very important OOP concepts that come up in job interviews: o inheritance o method overloading,

Objectives

The goals of this lab are to review some very important OOP concepts that come up in job interviews:

o inheritance

o method overloading, overriding

o constructor chaining

o polymorphism

o abstract classes

Lab 8 Random Shape Generator

Part 1 Introducing abstract Shape class

Using your shape class - Add an abstract draw(Graphics g) method to your abstract Shape class

Create a Rectangle, Oval, Triangle, Pentagon & Hexagon class that are all extended from an abstract Shape class (given below). Extend the Rectangle class to make a Square class. Extend the Oval class to make a Circle class. The shape classes will contain state information for the object to be drawn

o All the different shapes will need a draw method but each one will be slightly different

o All extended classes should also implement a toString method

it is up to you how you want to represent the Shape as a String

o All extended classes should also implement a equals method to compare two shapes

o The Oval, Circle, Triangle, Rectangle, Square, Hexagon and Pentagon classes should have the same number of constructors with the same arguments, in addition to any other fields that are needed to create those shapes To initialize the private instance fields of the Shape class you will need to make calls to the super class constructor. Private fields and methods of a super class are not directly accessible in the subclasses as if you were in the same class

Then initialize fields specific to the particular Shape class you are implementing

- If done correctly the Square classes will have the least amount of code because it can reuse most of the Rectangle class methods. Similarly Circle class should reuse code from Oval class. Hint: use super class constructors correctly and all this class should have is constructors and a toString method

Using your classes, implement the Random Shape Generator application which runs for 7 seconds and draws one random shape every 100 milliseconds. No shape can be drawn more than 10 times.

Write Junit tests for testing toString and equals methods

Other requirements are: Your background should be random color than black

o Shapes should be of random sizes

o Shapes should be of random colors different than background.

o Shapes should be drawn in random locations

o Shapes may overlap a little bit but try to avoid overlaps that hide half of any shape!

Note: Some of the methods will not be used now and we will come back to them to add more functionality. The way I designed this lab might feel somewhat contrived, which was intentional to emphasize the concepts.

Below is the API for the shape class: all methods that are not abstract are common to all shapes

- the abstract methods are methods all shapes will have, but will be different for each shape

import java.awt.Color;

abstract class Shape {

private Color fillColor;

private Color borderColor;

private Boolean isFilled;

private Point Location;

// the three constructors initialize the instance fields

public Shape(Color fillColor, borderColor, int x, int y) { }

// set borderColor to Black since not provided

public Shape(Color fillColor, int x, int y) { }

// set fillColor to white and border color to black

public Shape(int x, int y) { }

// will fill the shape with some random image. You can select any image for larger shapes

public void setFillColor(Color c) { }

public Color getFillColor() { }

public void setBorderColor(Color c) { }

public Color getBorderColor() { }

public void setLocation(Point pt) { }

public Point getLocation() { }

// Note: subclasses of Shape do not inherent private members so we need methods the subclasses

// can use to get the x and y values from the private Point instance field

public int getX() { }

public void setX(int x) { }

public int getY() { }

public void setY(int y) { }

// if fillColor is white returns true, else returns false

public boolean isFilled() { }

// moves location by dx and dy

private void moveLocation(int dx, int dy) { }

abstract double getArea();

abstract double getPerimeter();

}

Few things to keep in mind for Lab 8.

  • You dont need to implement below methods in ShapeDriver Class for this lab.
    • keyPressed(KeyEvent e)
    • keyReleased(KeyEvent e)
    • keyTyped(KeyEvent e)
  • Implement Junit only for toString() and equals() method.

Tips:

  • ShapeWindow class should create a JFrame and make it visible.
    • JFrame shapeWindow = new ShapeWindow();
  • ShapeDriver should implement paintComponent() method.
    • Use the different Shapes draw methods here. The draw methods in the different Shapes class takes a Graphics object as an argument.
    • The logic to implement the timer goes here.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

2. (1 point) Given AABC, tan A b b

Answered: 1 week ago