0% found this document useful (0 votes)
8 views6 pages

Project04 Triangle Class Inheritance

The document outlines the design and implementation of a Triangle class that extends a GeometricObject class, including methods for calculating area and perimeter, as well as constructors and accessor methods. It provides a UML diagram and pseudocode for the classes involved, along with a coding example demonstrating the creation and usage of a Triangle object. The Triangle class features three sides, default values, and methods for string representation, area, and perimeter calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Project04 Triangle Class Inheritance

The document outlines the design and implementation of a Triangle class that extends a GeometricObject class, including methods for calculating area and perimeter, as well as constructors and accessor methods. It provides a UML diagram and pseudocode for the classes involved, along with a coding example demonstrating the creation and usage of a Triangle object. The Triangle class features three sides, default values, and methods for string representation, area, and perimeter calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Project05: The Triangle Class

Problem Description:
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.

For the formula to compute the area of a triangle, see Exercise 5.19.
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.

1
Design:
Draw the UML class diagram here

GeometricObject
-color:String
-filled:boolean
+ GeometricObject()
+ GeometricObject(color:String,filled:bool)
+setColor(color:String):void
+setFilled(filled:boolean):viod
+getColor():String
+getFilled ():boolean

extends

Triangle
-side1:double
-side2:double
-side3:double
+ Triangle ()
+ Triangle (side1:double, side2:double, side3:double)
+setSide1 (s: double):void
+setSide2 (s: double):void
+setSide3 (s: double):void
+getSide1():double
+getSide2():double
+getSide3():double
+getArea ():double
+getPerimeter ():double
+toString ():String

2
Pseudocode:

CLASS GeometricObject
PRIVATE color : String
PRIVATE filled : boolean

METHOD GeometricObject() // No-arg constructor


SET [Link] = "white”
SET this. filled =true
END METHOD

METHOD GeometricObject(color : String, filled : boolean)


SET [Link] = color
SET [Link] = filled
END METHOD

METHOD getColor() : String


RETURN color
END METHOD

METHOD setColor(color : String)


SET [Link] = color
END METHOD

METHOD isFilled() : boolean


RETURN filled
END METHOD

METHOD setFilled(filled : boolean)


SET [Link] = filled
END METHOD

END CLASS

CLASS Triangle EXTENDS GeometricObject


PRIVATE side1 : double
PRIVATE side2 : double
PRIVATE side3 : double

METHOD Triangle() // No-arg constructor


// default sides = 1.0
END METHOD

METHOD Triangle(side1 : double, side2 : double, side3 : double)


SET this.side1 = side1
SET this.side2 = side2
SET this.side3 = side3
END METHOD

3
METHOD getSide1() : double
RETURN side1
END METHOD

METHOD getSide2() : double


RETURN side2
END METHOD

METHOD getSide3() : double


RETURN side3
END METHOD

METHOD getPerimeter() : double


RETURN side1 + side2 + side3
END METHOD

METHOD getArea() : double


SET s = getPerimeter() / 2
RETURN sqrt(s * (s - side1) * (s - side2) * (s - side3))
END METHOD

METHOD toString() : String


RETURN "Triangle: side1 = " + side1 +
" side2 = " + side2 +
" side3 = " + side3
END METHOD
END CLASS

Coding: (Copy and Paste Source Code here. Format your code using Courier 10pts)

public class Exercise11_01 {


public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
[Link]("yellow");
[Link](true);

[Link](triangle);
[Link]("The area is " + [Link]());
[Link]("The perimeter is "
+ [Link]());
[Link](triangle);
}
}

class GeometricObject {
// Copy it from the book
}

class Triangle extends GeometricObject {


// Implement it
}

4
Solution Code:

public class Test {


public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
[Link]("yellow");
[Link](true);

[Link](triangle);
[Link]("The area is " + [Link]());
[Link]("The perimeter is "
+ [Link]());
[Link](triangle);
}
}
public class GeometricObject {
private String color = "white";
private boolean filled;

// No-arg constructor
public GeometricObject() {
}

// Constructor with parameters


public GeometricObject(String color, boolean filled) {
[Link] = color;
[Link] = filled;
}

// Getter and Setter for color


public String getColor() {
return color;
}

public void setColor(String color) {


[Link] = color;
}

// Getter and Setter for filled


public boolean isFilled() {
return filled;
}

public void setFilled(boolean filled) {


[Link] = filled;
}
}

class Triangle extends GeometricObject {


private double side1 = 1.0, side2 = 1.0, side3 = 1.0;

/** Constructor */
public Triangle() {
}

/** Constructor */
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

5
/** Override method findArea in GeometricObject */
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return [Link](s * (s - side1) * (s - side2) * (s - side3));
}

/** Override method findPerimeter in GeometricObject */


public double getPerimeter() {
return side1 + side2 + side3;
}

/** Override the toString method */


public String toString() {
// Implement it to return the three sides
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
}
}

You might also like