Question

In: Computer Science

(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class...

(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObject and implements Colorable. Design another class named Triangle that extends GeometricObject and implements Colorable. Implement howToColor in Square to display the message Color all four sides. Implement howToColor in Triangle to display the message Color all three sides.

Draw a UML diagram that involves Colorable, Square, Triangle, and GeometricObject.

Write a test program that creates an array of five GeometricObjects. For each object in the array, display its area and invoke its howToColor method if it is colorable.

Hint: In Java, you can use

if (object instanceof Interface) {

//Do some thing here.

}

to check if object is an instance of a class that implements the Interface https://stackoverflow.com/questions/13487765/how-instanceof-will-work-on-an-interface

Submission:

Print your UML diagram and print out your code.

You should have following source codes:

  • one UML diagram
  • one Colorable interface
  • one GeometricObject class
  • one Square class
  • one Triangle class
  • one class (you can name this class yourself) that has a main function to proceed the howToColor function call.

Solutions

Expert Solution

interface Colorable
{
public void howToColor();
}

abstract class GeometricObject implements Colorable// abstract base class
{

//abstract methods
abstract public double calculateArea();
abstract public double calculatePerimeter();
abstract public String toString();

}

class Triangle extends GeometricObject
{
private double side1,side2,side3;
public Triangle()
{
side1 = 1.0;
side2 = 1.0;
side3 = 1.0;
}
public Triangle(double side1,double side2,double side3)
{
if ((side1 >= side2 + side3) || (side2 >= side1 + side3)||(side3 >= side2 + side1))
{
this.side1 = 1.0;
this.side2 = 1.0;
this.side3 = 1.0;
}
else
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
}
//The accessor(getters and setters) methods for all three data fields.
public void setSide1(double side1)
{
this.side1 = side1;
}
public double getSide1()
{
return side1;
}
public void setSide2(double side1)
{
this.side2 = side2;
}
public double getSide2()
{
return side2;
}
public void setSide3(double side3)
{
this.side3 = side3;
}
public double getSide3()
{
return side3;
}
//A method named getArea() that returns the area of this triangle.
public double calculateArea()
{
double s = (side1 +side2+side3)/2;
double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
//A method named getPerimeter() that returns the perimeter of this triangle.
public double calculatePerimeter()
{
return(side1+side2+side3);
}
//A method named toString() that returns a string description for the triangle.
public String toString()
{
return "Triangle side1 : "+side1 +" side2 : "+side2+" side3 : "+side3 +" Area : "+calculateArea()+" Perimeter : "+calculatePerimeter();
}

public void howToColor()
{
System.out.println("Color all three sides");
}
}

class Square extends GeometricObject
{
private double side;

public Square(double side)
{
this.side = side;
}
public String toString()
{
return "Square side : "+side+" Area : "+calculateArea()+" Perimeter : "+calculatePerimeter();
}

public void howToColor()
{
System.out.println("Color all four sides");
}

public double calculateArea()
{
return side*side;
}
//A method named getPerimeter() that returns the perimeter of this triangle.
public double calculatePerimeter()
{
return 4*side;
}
}

class TestGeometricObject
{
public static void main (String[] args)
{
  
GeometricObject[] f = new GeometricObject[5];
f[0] = new Square(20);
f[1] = new Triangle(6.5, 14.9, 11.3);
f[2] = new Square(4.5);
f[3] = new Triangle(3.0,4.0,5.0);
f[4] = new Square(2.5);

for(int i=0;i<f.length;i++)
{
System.out.println(f[i]);
if(f[i] instanceof Colorable)
f[i].howToColor();
}


}
}

Output:

Square side : 20.0 Area : 400.0 Perimeter : 80.0
Color all four sides
Triangle side1 : 6.5 side2 : 14.9 side3 : 11.3 Area : 34.340505510985146 Perimeter : 32.7
Color all three sides
Square side : 4.5 Area : 20.25 Perimeter : 18.0
Color all four sides
Triangle side1 : 3.0 side2 : 4.0 side3 : 5.0 Area : 6.0 Perimeter : 12.0
Color all three sides
Square side : 2.5 Area : 6.25 Perimeter : 10.0
Color all four sides

UML

Do ask if any doubt. Please upvote.


Related Solutions

A JavaFX UI class has a button named 'btnOk' that needs to call a void method...
A JavaFX UI class has a button named 'btnOk' that needs to call a void method with no parameters named 'displayResults' when it is clicked. Write the complete lambda-expression statement to set up the btnOk event handler to do this.
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
Design a class named ArraySort and it contains the following method: Public int search(int target): if...
Design a class named ArraySort and it contains the following method: Public int search(int target): if the target is found in the array, return the number of its showing up. If the target is not found in the array, return -1. If the array is empty, return -1; Public int maximum():Return the maximum number in the array if the array is nonempty, otherwise return -1; Public int minimum(): Return the minimum number in the array if the array is nonempty,...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk();...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk(); public void crash(); public void drive(); } public interface Boat{ public String getMake (); public void setMake (); public void blast_horn(); public void sink(); public void move(); } 1. Create a concrete FamilyCar class from the Car interface.
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
Given a class Stack with the interface public void push(char n) // pushes n onto stack...
Given a class Stack with the interface public void push(char n) // pushes n onto stack public char pop() // return the top of the stack, removing element from stack public boolean isEmpty() // return true if stack is empty Write a method public int removeX(Stack<Character> stack) which takes a stack of Characters, removes the occurrences of ‘X’ and returns the count of the number of Xs removed. It must restore the stack to its original order (less the Xs)....
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT