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

(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 GeometricObjectand implements Colorable. Design another class named Trianglethat extends GeometricObjectand implements Colorable. Implement howToColor inSquareto display the message Color all four sides. ImplementhowToColor inTriangleto display the message Color all three sides. Draw a UML diagram that involves Colorable, Square, Triangle, and GeometricObject. Write a test program that creates...
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, (include javadoc comments for each method) design a class named Contacts that has fields...
In java, (include javadoc comments for each method) design a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contacts objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to...
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...
Design a class named Message to represent a sentence or phrase. The class will contain: •...
Design a class named Message to represent a sentence or phrase. The class will contain: • a private string data field to hold the sentence or phrase. • A no-arg constructor with an empty string message. • A constructor that create a message object with the specified string sentence or phrase. • Accessor and mutator (getter/setter) for string data field. • A method named getVowels ( ) that returns the number of vowels in a sentence or phrase. • A...
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2....
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2. Create a class Car. Implements Vehicle. It's paint() method prints "Car Painted". It's drive( ) method prints "Car Driven". 3. Create a class Bus. Implements Vehicle. It's paint() method prints "Bus Painted" . It's drive( ) method prints "Bus Driven".   4. Create a method AutoWork which accepts an object of type Vehicle. The method makes a call to the vehicle's paint( ) and drive()...
Design a class named Account (put it in a package named accountspackages) with the following UML...
Design a class named Account (put it in a package named accountspackages) with the following UML diagram: Account -customerID: int -customerName: String -balance: double +setCustomerID(int): void +setCustomerName(String): void +setBalance(double):void +getCustomerID(): int +getCustomerName(): String +getBalance(): double +deposit(double): void +withdraw(double): void +printInformation():void The method withdraw(double) withdraws a specified amount from the account if the amount is less than or equal the balance, otherwise the method prints the message: Sorry! The account does not have sufficient funds. The method printInformation() prints:     the...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT