Lab 8
Lab 8
CLO3 -Build programs using the latest IDEs as per standard practices applicable to the
software industry.
Date: 15-04-2024
CMS: 470174
Reference Book: Java How to Program, 10th Ed, Deitel & Deitel (Available on LMS)
Task# 1:
Write an abstract class LivingThing.java followed by two concrete classes, Human.java and
Monkey.java, extending the abstract class.
Once done defining classes use the following client class to test it.
package myabstractclassproject;
Bonus. What happens when you create a LivingThing object in the Main class? For example
using the statement,
LivingThing z = new LivingThing();
Code:
package Lab8;
@Override
public void walk() {
System.out.println("Human " + this.getName() + " walks...");
}
}
@Override
public void walk() {
System.out.println("Monkey " + this.getName() + " walks...");
}
}
Output:
Bonus:
Task# 2:
Your task is to write MyOnlineShop program by referring to the UML class diagram below.
package myonlineshop;
// sale price.
double totalRegularPrice = 0;
double totalSalePrice = 0;
totalRegularPrice += pa[i].getRegularPrice();
// behavior.
totalSalePrice += pa[i].computeSalePrice();
Item number 0: Type = myonlineshop.TV, Regular price = 1000.0, Sale price = 800.0
Item number 1: Type = myonlineshop.TV, Regular price = 2000.0, Sale price = 1600.0
Item number 2: Type = myonlineshop.MP3Player, Regular price = 250.0, Sale price = 225.0
Item number 3: Type = myonlineshop.Book, Regular price = 34.0, Sale price = 17.0
Item number 4: Type = myonlineshop.Book, Regular price = 15.0, Sale price = 7.5
totalRegularPrice = 3299.0
totalSalePrice = 2649.5
Code:
package myonlineshop;
@Override
public double computeSalePrice() {
return this.getRegularPrice()-(0.10*this.getRegularPrice());
}
@Override
public double computeSalePrice() {
return this.getRegularPrice()-(0.20*this.getRegularPrice());
}
}
Output:
Modify the MyLine, MyOval and MyRectangle classes of GUI and Graphics Case Study
Exercise 8.1 and Exercise 9.1 of the reference book to create the class hierarchy in the below
Figure. Classes of the MyShape hierarchy should be “smart” shape classes that know how to
draw themselves (if provided with a Graphics object that tells them where to draw). Once the
program creates an object from this hierarchy, it can manipulate it polymorphically for the
rest of its lifetime as a MyShape.
In your solution, class MyShape must be abstract. Since MyShape represents any shape in
general, you cannot implement a draw method without knowing specifically what shape it is. The
data representing the coordinates and color of the shapes in the hierarchy should be declared as
private members of class MyShape. In addition to the common data, class MyShape should
declare the following methods:
a) A no-argument constructor that sets all the coordinates of the shape to 0 and the color to
Color.BLACK.
b) A constructor that initializes the coordinates and color to the values of the arguments
supplied.
c) Set methods for the individual coordinates and color that allow the programmer to set any
piece of data independently for a shape in the hierarchy.
d) Get methods for the individual coordinates and color that allow the programmer to
retrieve any piece of data independently for a shape in the hierarchy.
e) The abstract method
which the program’s paintComponent method will call to draw a shape on the screen.
To ensure proper encapsulation, all data in class MyShape must be private. This requires
declaring proper set and get methods to manipulate the data. Class MyLine should provide a no-
argument constructor and a constructor with arguments for the coordinates and color. Classes
MyOval and MyRectangle should provide a no-argument constructor and a constructor with
arguments for the coordinates, color and determining whether the shape is filled. The no-
argument constructor should, in addition to setting the default values, set the shape to be an
unfilled shape.
You can draw lines, rectangles and ovals if you know two points in space. Lines require x1, y1,
x2 and y2 coordinates. The drawLine method of the Graphics class will connect the two points
supplied with a line. If you have the same four coordinate values (x1, y1, x2 and y2) for ovals and
rectangles, you can calculate the four arguments needed to draw them. Each requires an upper-
left x-coordinate value (the smaller of the two x-coordinate values), an upper-left y-coordinate
value (the smaller of the two y-coordinate values), a width (the absolute value of the difference
between the two x-coordinate values) and a height (the absolute value of the difference between
Allow the user to specify (via an input dialog) the number of shapes to generate. The program
will then generate and display the shapes along with a status bar that informs the user how many
of each shape were created.
Code:
package Task3;
import java.awt.*;
import javax.swing.*;
public MyShape() {
this(0, 0, 0, 0, Color.BLACK);
}
public MyShape(double x1, double y1, double x2, double y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
public MyLine(double x1, double y1, double x2, double y2, Color color) {
super(x1, y1, x2, y2, color);
}
@Override
public void draw(Graphics g) {
g.setColor(getColor());
g.drawLine((int) getX1(), (int) getY1(), (int) getX2(), (int)
getY2());
}
}
public MyOval() {
super(0, 0, 50, 50, Color.RED);
this.filled = false;
}
public MyOval(double x1, double y1, double x2, double y2, Color color,
boolean filled) {
super(x1, y1, x2, y2, color);
this.filled = filled;
}
@Override
public void draw(Graphics g) {
g.setColor(getColor());
if (filled)
g.fillOval((int) Math.min(getX1(), getX2()), (int)
Math.min(getY1(), getY2()),
(int) Math.abs(getX2() - getX1()), (int) Math.abs(getY2()
- getY1()));
else
g.drawOval((int) Math.min(getX1(), getX2()), (int)
Math.min(getY1(), getY2()),
(int) Math.abs(getX2() - getX1()), (int) Math.abs(getY2()
- getY1()));
}
}
public MyRectangle() {
super(10, 10, 100, 100, Color.YELLOW);
this.filled = false;
}
public MyRectangle(double x1, double y1, double x2, double y2, Color
color, boolean filled) {
super(x1, y1, x2, y2, color);
this.filled = filled;
}
@Override
public void draw(Graphics g) {
g.setColor(getColor());
if (filled)
g.fillRect((int) Math.min(getX1(), getX2()), (int)
Math.min(getY1(), getY2()),
(int) Math.abs(getX2() - getX1()), (int) Math.abs(getY2()
- getY1()));
else
g.drawRect((int) Math.min(getX1(), getX2()), (int)
Math.min(getY1(), getY2()),
(int) Math.abs(getX2() - getX1()), (int) Math.abs(getY2()
- getY1()));
}
}
switch (type) {
case 0:
shapes[i] = new MyLine(x1, y1, x2, y2, color);
numLines++;
break;
case 1:
shapes[i] = new MyRectangle(Math.min(x1, x2), Math.min(y1,
y2), Math.abs(x2 - x1), Math.abs(y2 - y1), color, Math.random() > 0.5);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (MyShape shape : shapes) {
shape.draw(g);
}
}