(Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject then implement the Triangle class. Write a test
Program to create Triangle class
Program Plan:
- Include a class name named “Exercise_13_01”.
- Include the scanner package.
- Declare the main() function.
- Create a scanner object.
- Prompt the user to enter three sides of the triangle.
- Prompt the user to enter a color.
- Prompt the user to enter a Boolean value on whether the triangle is filled.
- Create triangle object with the entered user input.
- Set color and set filled in the triangle.
- Print the output.
- Close the main() function.
- Close the class “Exercise_13_01”.
- Include an abstract class “GeometricObject”.
- Declare all the data types of the variables.
- Construct a default geometric object.
- Construct a geometric object with color and filled value.
- Assign color and fill to the object.
- Set a new color.
- Define an isFilled method returns filled and since filled is Boolean; the get method is named isFilled.
- Set a new filled and getDateCreated method.
- Override toString function to return output.
- Define Abstract method getArea.
- Define Abstract method getPerimeter.
- Close class.
- Include a class “Triangle” that extends the class GeometricObject.
- Declare the three sides of the triangle.
- Include a triangle constructor.
- Assign values to all three sides of the triangle.
- Assign triangle values to all attributes.
- Return side1 and Set side1 to a new length.
- Return side2 and Set side2 to a new length.
- Return side3 and Set side3 to a new length.
- Create a Function to return area of the Triangle.
- Create a function to return a string description of the object.
- Close class.
The java code to design a new Triangle class that extends the abstract GeometricObject class and to design a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled or not.
Explanation of Solution
Program:
File name: Exercise_13_01.java
//package to have user input
import java.util.Scanner;
//class Definition
public class Exercise_13_01
{
// Main method
public static void main(String[] args)
{
// Create a Scanner object
Scanner input = new Scanner(System.in);
/* Prompt the user to enter three sides of the triangle */
System.out.print("Enter three side of the triangle: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
// Prompt the user to enter a color
System.out.print("Enter a color: ");
String color = input.next();
/*Prompt the user to enter a boolean value on whether the triangle is filled*/
System.out.print("Is the triangle filled (true / false)? ");
boolean filled = input.nextBoolean();
/* Create triangle object with the entered user input */
Triangle triangle = new Triangle(side1, side2, side3);
// set color
triangle.setColor(color);
// set filled
triangle.setFilled(filled);
// print the output
System.out.println(triangle.toString());
System.out.println("Area: " + triangle.getArea());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Color: " + triangle.getColor());
System.out.println("Triangle is" + (triangle.isFilled() ? "" : " not ")
+ "filled");
}
}
Filename: GeometricObject.java
//abstract class GeometricObject definition
public abstract class GeometricObject
{
//data type declaration
private String color = "while";
private boolean filled;
private java.util.Date dateCreated;
// Construct a default geometric object
protected GeometricObject()
{
dateCreated = new java.util.Date();
}
/* Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled)
{
dateCreated = new java.util.Date();
//assign color and fill
this.color = color;
this.filled = filled;
}
// Return color
public String getColor()
{
return color;
}
// Set a new color
public void setColor(String color)
{
this.color = color;
}
/* isFilled method returns filled and Since filled is boolean,the get method is named isFilled */
public boolean isFilled()
{
return filled;
}
// Set a new filled
public void setFilled(boolean filled)
{
this.filled = filled;
}
// Get dateCreated
public java.util.Date getDateCreated()
{
return dateCreated;
}
//override toString function to return output
@Override
public String toString()
{
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
// define Abstract method getArea
public abstract double getArea();
//Define Abstract method getPerimeter
public abstract double getPerimeter();
}
Filename: Triangle.Java
/*Class definition of Triangle that extends the class GeometricObject */
public class Triangle extends GeometricObject
{
//declare the three sides of the triangle
private double side1;
private double side2;
private double side3;
//triangle constructor
public Triangle()
{
}
//assign values to all three sides of the triangle
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
//assign triangle values to all attributes
public Triangle(double side1, double side2, double side3, String color, boolean filled)
{
this(side1, side2, side3);
setColor(color);
setFilled(filled);
}
// Return side1
public double getSide1()
{
return side1;
}
// Set side1 to a new length
public void setSide1(double side1)
{
this.side1 = side1;
}
// Return side2
public double getSide2()
{
return side2;
}
// Set side2 to a new length
public void setSide2(double side2)
{
this.side2 = side2;
}
// Return side3
public double getSide3()
{
return side3;
}
// Set side3 to a new length
public void setSide3(double side3)
{
this.side3 = side3;
}
// Function to Return area of the Triangle
@Override
public double getArea()
{
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
/* override function to Return perimeter of the triangle */
@Override
public double getPerimeter()
{
return side1 + side2 + side3;
}
/*override function to Return a string description of the object*/
@Override
public String toString()
{
return super.toString() + "\nArea: " + getArea() + "\nPerimeter: " + getPerimeter();
}
}
UML Diagram:
Explanation:
- Here, GeometricObject is an abstract class that extends the triangle class which is represented using an arrow. The triangle class then contains a rectangle that represents the objects used to input the three sides of the triangle.
- The remaining part is used to represent all the methods and functions used in the triangle class.
- The Triangle() method creates a triangle with default sides. The method Triangle(side1:double, side2; double,side3;double) creates a triangle of the specified sides.
- Then the respective sides of the triangles are returned using getSide() methods.
- Then the method getArea() returns the area of the triangle and the method getPerimeter() returns the perimeter of the triangle and the function toString() returns a string description of the object.
Enter three side of the triangle: 3
4
5
Enter a color: red
Is the triangle filled (true / false)? false
created on Sun Jul 08 13:24:03 IST 2018
color: red and filled: false
Area: 6.0
Perimeter: 12.0
Area: 6.0
Perimeter: 12.0
Color: red
Triangle is not filled
Want to see more full solutions like this?
Chapter 13 Solutions
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Elementary Surveying: An Introduction To Geomatics (15th Edition)
Modern Database Management
SURVEY OF OPERATING SYSTEMS
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Starting Out with C++ from Control Structures to Objects (9th Edition)
- Exercise: 1) Design a class ComboLock that works like the combination lock in a gym locker, as shown below. The lock is constructed with a combination-three numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right. The getStatus method returns a Boolean indicating whether the lock is opened(true) or closed(false). The lock opens if the user first turned it right to the first number in the combination, then left to the second, and then right to the third. 02 36 2) Document your class. 3) Provide a tester class and test a scenario in which you create a comboLock having the code (25, 36, 19), unlock it successfully, and print the status of the lock after each turn. 4) Put the ComboLock class and the tester class in two distinct packagesarrow_forwardClasses and Objects) Hand-write a complete Java class that can be used to create a Car object as described below. a. A Vehicle has-a: i. Registration number ii. Owner name iii. Price iv. Year manufactured b. Add all instance variables C. The class must have getters and setters for all instance variables d. The class must have two constructors, a no-args and a constructor that receives input parameters for each instance variable.arrow_forward(java) The Painting Subclass Write class as follows: The class is named Painting, and it inherits from the Art class. It has a private String member variable named medium It has a default constructor that assigns the values "No name" to name, "No artist" to artist, -1 to year, and "No medium" to the medium variable. This default constructor calls the four-argument constructor. It has a four-argument constructor to assign values to the name, artist, year, and medium variables. It has a getter and settersfor the medium variable. It has a toString() method This class contains no other methods Make sure to include your name, the name of this class, our course number, and the Activity number in a Javadoc comment at the top. Make sure to write a Javadoc comment for each of these methods.arrow_forward
- Please help me with this , I am stuck ! PLEASE WRITE IT IN C++ Thanks 1) bagUnion: The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method union for the ArrayBag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method's parameter. The method signature (which should appear in the .h file and be implemented in the .cpp file is: ArrayBag<T> bagUnion(const ArrayBag<T> &otherBag) const; This method would be called in main() with: ArrayBag<int> bag1u2 = bag1.bagUnion(bag2); Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. The union does not affect the contents of the original bags. Here is the main file: #include <cstdlib>#include <iostream>#include "ArrayBag.h"using namespace std; template…arrow_forward(After reading the instructions given in the pictures attached, read the following) Class membersdoctorType Class must contain at least these functionsdoctorType(string first, string last, string spl); //First Name, Last Name, Specialty void print() const; //Formatted Display First Name, Last Name, Specialtyvoid setSpeciality(string); //Set the doctor’s Specialtystring getSpeciality(); //Return the doctor’s SpecialtypatientType Class must contain at least these functionsvoid setInfo(string id, string fName, string lName,int bDay, int bMth, int bYear,string docFrName, string docLaName, string docSpl,int admDay, int admMth, int admYear,int disChDay, int disChMth, int disChYear);void setID(string);string getID();void setBirthDate(int dy, int mo, int yr);int getBirthDay();int getBirthMonth();int getBirthYear();void setDoctorName(string fName, string lName);void setDoctorSpl(string);string getDoctorFName();string getDoctorLName();string getDoctorSpl();void…arrow_forward(The Account class) Account -id: int -balance: double -annualInterestRate : double -dateCreated : Date +Account( ) +Account(someId : int, someBalance : double) +getId() : int +setId(newId : int) : void +getBalance() : double +setBalance(newBalance : double) : void +getAnnualInterestRate( ) : double +setAnnualInterestRate(newRate : double) : void +getDateCreated( ) : Date +getMonthlyInterestRate( ) : double +getMonthlyInterest( ) : double +withdraw(amt : double) : void +deposit(amt : double) : void Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. Make it static. ■ A private Date data field named dateCreated that stores the date when the account was created. (private Date dateCreated)…arrow_forward
- (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 denotethe fan speed.■ A private int data field named speed that specifies the speed of the fan (the default isSLOW).■ 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 thatspecifiesthe radius of the fan (the default is5).■ A string data field named color thatspecifiesthe color of the fan (the default is blue).■ The accessor and mutator methodsfor all four data fields.■ A no-arg constructorthat creates a default fan.■ A method named toString() that returns a string description for the fan. If the fan is on, the method returns the fan speed, color, and radius in one combined string. If the fan is not on, the method returns the fan color and radius along with the string “fan is off” in onecombined string.Draw the UML diagram for the class and…arrow_forwardThis program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. item_description (string) - Set to "none" in default constructorImplement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs (empty methods) initially, to be completed in later steps. Parameterized constructor which takes the customer name and date as parameters Attributescustomer_name (string) - Initialized in default constructor to "none"current_date (string) - Initialized in default constructor to "January 1, 2016"cart_items (list)Methodsadd_item()Adds an item to cart_items list. Has a parameter of…arrow_forwardThis program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. item_description (string) - Set to "none" in default constructorImplement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs (empty methods) initially, to be completed in later steps. Parameterized constructor which takes the customer name and date as parameters Attributescustomer_name (string) - Initialized in default constructor to "none"current_date (string) - Initialized in default constructor to "January 1, 2016"cart_items (list)Methodsadd_item()Adds an item to cart_items list. Has a parameter of…arrow_forward
- (The Triangle class) Design a class named Triangle that extends GeomericObject. The class contains: Three double data fields named side1, side2, and side 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 specific side1, side2, and side3. The accessor method for the 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. Note:- Please write a java code and also need an output for this program. (Also, let me know with what name file should be saved to get output)arrow_forwardNote: It`s python codingarrow_forwardNote: It`s python codingarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education