Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Activity of M251-Meeting 6

(Interfaces & Abstract Classes)

A. Write a Java interface Colorful, the interface has:


A method makeItRed() that takes no argument and return no
value. This method prints “now my color is red”, and sets color
to red value.
B. Write a Java abstract class Shape that has the following
properties:
1. a private attribute height of type integer.
2. Zero argument constructor that create and initialise height to its
default value. IT should invoke its one argument constructor.
3. One-argument constructor that initialise height to given value.
4. setter and getter methods for height attribute.
5. an abstract method calcArea() which has no arguments and it
calculates and returns the area of the shape.
6. Override toString() method to return a string representing
status of a Shape object as the below format:
Height = ….

C. Write a Java class Rectangle that has the following


properties:
1. The class Rectangle inherits Shape class and implements
Colorful interface, and implements Comparable Interface
2. It has a private attribute width of type integer
3. It has a private attribute color of type String.
4. It has multi-argument constructor to initialize its attributes and
the inherited one to given values. It should invoke super
constructor.
5. It has zero-argument constructor that initialize its attributes by
its default value, the color is initialised to “white”. It should
invoke its multi-argument constructor.
6. it has the getter and setter methods for its instance variable
width, and color.
7. Override toString() method to return a string representing the
status of a Rectangle Object as the below format:
Shape is: Rectangle height=… width =…. Color is …..
8. Implement any other necessary methods.

D. Write a Java class TestShape, that has a main method to do


the followings:

 Create two objects r1, and r2 of Rectangle class with different


heights and widths.

 Print out the area of r1.

 Print out the area of r2.

 Make color of r1 red.

 Print out the status of r1 object.

 Print out the status of r2 object.

 Print out the result of comparing r1 with r2 based on the rea.


Solution:

//A

public interface Colorful {

public void makeItRed();

//B

public abstract class Shape {

private int height;

public Shape(){

this(0);

public Shape(int a){

height = a;

public void setHeight(int a){

height = a;

public int getHeight(){

return height;

}
public abstract double calArea();

public String toString(){

return "Height= "+ height;

//C

public class Rectangle extends Shape implements Colorful,


Comparable<Rectangle>{

//2+3

private int width;

private String color;

//5

public Rectangle() {this(0,0,"white"); }

//4

public Rectangle(int h,int w, String s)

{ super(h); this.width = w; color = s; }


//6

public double getWidth()

{ return width; }

public void setWidth(int width)

{ this.width = width; }

//7

public String toString() {

return "The Shape is REctangle " + super.toString()+

"\t Width =" + width+"\t Color is "+ color;

//8

public void makeItRed(){

color = "Red";

System.out.println("Now my color is Red");

public double calArea()

{ return width * getHeight(); }

public int compareTo(Rectangle o){

if(this.calArea()> o.calArea()) return 1;

else if (this.calArea()< o.calArea()) return -1;

else

return 0;

} //End of class
//D

public class Meeting8_Activity {

public static void main(String[] args) {

Rectangle r1 = new Rectangle(4,5, "Pink");

Rectangle r2 = new Rectangle();

System.out.println("R1 area = "+ r1.calArea());

System.out.println("R2 area = "+ r2.calArea());

r1.makeItRed();

System.out.println("R1: "+ r1.toString());

System.out.println("R2: "+ r2.toString());

System.out.println("Comparing r1 with r2 based on area is "+


r1.compareTo(r2));

You might also like