Question

for Java define a class for a triangle of any size: including two constructors 1) default...

for Java

define a class for a triangle of any size: including two constructors

1) default color

2) accepts 3 parameters (don't forget set and get methods) and include a method for the area of the triangle

create a driver program that instantiates two triangles:

1) using the default color

2) using the second constructor

sketch the UML diagram

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java code

public class Triangle {

    private String color;
    private double height,base;

    //default constructor
    public Triangle()
    {
        height=1;
        base=1;
        color="White";
    }
    //Constructor taking three parameters

    /**
     *
     * @param height
     * @param base
     * @param color
     */
    public Triangle(double height,double base, String color)
    {
        this.height= height;
        this.base= base;
        this.color= color;
    }
    //getters

    public double getBase() {
        return base;
    }

    public double getHeight() {
        return height;
    }

    public String getColor() {
        return color;
    }
    //setters

    public void setHeight(double height) {
        this.height = height;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setBase(double base) {
        this.base = base;
    }
    //Calculate area

    /**
     *
     * @return area of triangle
     */
    public double getArea()
    {
        return (base*height)/2;
    }

    //override toString() to print Triangle Object info

    @Override
    public String toString() {
        return "Triangle: Height: "+height+" base: "+base+" Color: "+color+" Area: "+getArea();
    }
}
public class Main {
    public static void main(String[] args)
    {
        //create object of Triangle using default constructor
        Triangle triangle= new Triangle();
        //create object of triangle using parameterised constructor
        Triangle triangle1= new Triangle(2,3,"Red");
        //Print info of both object
        System.out.println(triangle);
        System.out.println(triangle1);
    }
}

//Output

//UML Diagram

//if you need any help regarding this solution............ please leave a comment........... thanks

Add a comment
Know the answer?
Add Answer to:
for Java define a class for a triangle of any size: including two constructors 1) default...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 3: (15 pts) Modify the Patient class with two overloaded constructors: A new default constructor...

    Problem 3: (15 pts) Modify the Patient class with two overloaded constructors: A new default constructor which initializes the data items for a patient object using code within the constructor (use any data). A second constructor with parameters to pass all the data items into the object at the time of instantiation. Create a test main class which instantiates two objects. Instantiate the first object using the default constructor and the second object using the constructor with the parameters.

  • Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class....

    Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. Write 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. The program should create a Triangle object with these sides and set the color and filled properties using the input....

  • Check all statements that are true regarding constructors in Java 1. A default empty constructor will...

    Check all statements that are true regarding constructors in Java 1. A default empty constructor will be generated if you don't provide any. This default constructor calls super() and initializes all instance variables to default value like 0, null. 2. Constructor name should be the exact same same name as the class name 3. A class can only have one or more constructors 4. It is not required to explicitly define a constructor 5. A class can only have one...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Define a DNA class: Please consider the features of Java classes, and then define a Java...

    Define a DNA class: Please consider the features of Java classes, and then define a Java class for a DNA sequence object to include instance variable names and methods that you can use to define a piece of DNA sequence. You can use a UML (Universal Modeling Language) diagram to define your class, or you can simply list the variable, and method's names, data type, and parameters the methods will take in.

  • IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...

    IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • Define an example of inheritance with a base class that has 2 attributes, 2 derived classes...

    Define an example of inheritance with a base class that has 2 attributes, 2 derived classes that each have 2 attributes. Implement this in C++. Create a driver program that creates instances of both of the derived classes and exercises the classes’ member functionsl. Student answer should include the following: 1 base class with 2 attributes, 2 constructors (default and with parameters), get/set methods for each attribute, and display methods 2 derived classes with 2 attributes, 2 constructors (default and...

  • LW: Class Constructors Objectives Write a default constructor for a class. Note that this const...

    LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT