❓ Help Code

Yelan

Eternal Poster
Create a generic class called TriPrism. It shall store the dimensions of a triangular prism and calculate its volume. The type of the dimensions will be decided upon instantiation of the TriPrism objects. Use the formula: volume = length * (0.5 * base * height)

  1. Create a generic class called TriPism.
  2. Declare three (3) private variables named length, base, and height.
  3. Add three (3) pairs of getters and setters to retrieve and set the values of the variables mentioned above.
  4. Create another class named TriPrismDemo.
  5. In its main method, instantiate two (2) TriPrism objects named tp1 (Integer) and tp2 (Double).
  6. Use the setter methods to set the values of the dimensions.
  7. Calculate and display the volume of the two (2) TriPrism objects using the getter methods. Refer to the formula provided above.
 
Here is the implementation of the requested classes:

Java:
class TriPrism<T extends Number> {
    private T length;
    private T base;
    private T height;

    public T getLength() {
        return length;
    }

    public void setLength(T length) {
        this.length = length;
    }

    public T getBase() {
        return base;
    }

    public void setBase(T base) {
        this.base = base;
    }

    public T getHeight() {
        return height;
    }

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

    public double calculateVolume() {
        return length.doubleValue() * (0.5 * base.doubleValue() * height.doubleValue());
    }
}

class TriPrismDemo {
    public static void main(String[] args) {
        TriPrism<Integer> tp1 = new TriPrism<>();
        tp1.setLength(5);
        tp1.setBase(2);
        tp1.setHeight(3);

        TriPrism<Double> tp2 = new TriPrism<>();
        tp2.setLength(4.5);
        tp2.setBase(3.2);
        tp2.setHeight(2.8);

        double volume1 = tp1.calculateVolume();
        double volume2 = tp2.calculateVolume();

        System.out.println("Volume of tp1: " + volume1);
        System.out.println("Volume of tp2: " + volume2);
    }
}

In this implementation, the TriPrism class is a generic class that uses a type parameter T which extends Number. This allows us to use different types of numbers such as Integer and Double for the dimensions.

The class has three private variables length, base, and height, and corresponding getter and setter methods for each of them.

The calculateVolume method uses the formula provided to calculate the volume of the triangular prism.

In the TriPrismDemo class, we create two instances of TriPrism, one with Integer dimensions and one with Double dimensions. We set the values using the setter methods and then calculate and display the volumes using the getter methods.
 

About this Thread

  • 1
    Replies
  • 1K
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
998
Guests online
1,095
Total visitors
2,093

Forum statistics

Threads
2,276,161
Posts
28,967,972
Members
1,231,145
Latest member
kennethCASTRO2012
Back
Top