I want this program according to Chapter 11 \"Inheritance and Polymorphism\" fro
ID: 3633059 • Letter: I
Question
I want this program according to Chapter 11 "Inheritance and Polymorphism" from Introduction to Java Programming by Y.Daniel Liang. In this chapter there is almost similar Programming Excercises at the end of the chapter 11.1, but I didn't find the solution on this site. I need help with this program which is almost similar to that question. ThanksProject 4 The Triangle Class (Covers Chapter11)
Design a class named Triangle that extends GeometricObject. The class contains:
Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
A no-arg constructor that creates a default triangle.
A constructor that creates a triangle with the specified side1, side2, and side3.
The accessor methods for all three data fields.
A method named getArea() that returns the area of this triangle.
A method named getPerimeter() that returns the perimeter of this triangle.
A method named toString() that returns a string description for the triangle.
The formula to compute the area of a triangle:
s = side1 + side2 + side3)/2;
area = v(s(s - side1)(s - side2)(s -side3))
The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
Draw the UML diagram that involves the classes Triangle and GeometricObject. Implement the class. Write a test program that creates a Triangle object with sides 1, 1.5, 1, color yellow and filled true, and displays the area, perimeter, color, and whether filled or not. Write all three classes in three separated files: FirstInitialLastNameProject4.java, GeometricObject.java, and Triangle.java
Coding:
public class FirstInitialLastNameProject4 {
public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is "
+ triangle.getPerimeter());
System.out.println(triangle);
}
}
class GeometricObject {
// Copy it from the book
}
class Triangle extends GeometricObject {
// Implement it
}
Before submitting the program, you should test and run it, and make sure it works properly. Submit your source code (the file with the extension .java) to the Project 4 Folder.
WARNING: NEVER WRITES YOUR SOURCE CODE USING MICROSOFT WORD!
Instead, you should use either the editor come with the IDE or notepad.
Rubric for Project 4
60 total points possible.
(25 points) Carefully declare all the variables with appropriate data types. Use comments effectively to make the program more readable. Implement the calculations correctly. Design UML Diagrams.
(25 points) Compile and build the Java program. Test the program and make sure the program produces the desired answer.
(10 point) Submit your source code, algorithm, UML Diagrams and screenshots as an attached file to Project 4 Folder
Explanation / Answer
public class GeometricObject { private String color; private boolean filled; public GeometricObject(String color, boolean filled) { this.color = color; this.filled = filled; } /**Getter method for color*/ public String getColor() { return color; } /**Setter method for color*/ public void setColor(String color) { this.color = color; } /**Getter method for filled. Since filled is boolean, so, the get method name is isFilled*/ public boolean isFilled() { return filled; } /**Setter method for filled*/ public void setFilled(boolean filled) { this.filled = filled; } /** Return a string representation of this object*/ public String toString() { return "Color: " + color + "and filled: " + filled; } } class Triangle extends GeometricObject { double side1 = 1.0; double side2 = 1.0; double side3 = 1.0; public Triangle() { side1=0.0; side2=0.0; side3=0.0; } public Triangle(double a, double b, double c) { side1 = a; side2 = b; side3 = c; } public void show() { System.out.println(side1+","+side2+","+side3+","); } public double getArea(){ return( 1/2*(side1*side2*side3)); } public double getPerimeter(){ return (side1 + side2 + side3); } @Override public String toString(){ return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; } } public class Asg1 { public static void main(String[] args) { double a; double b; double c; Triangle my = new Triangle(1,1.5,1); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.