Question

In: Computer Science

Using your Barrel class and Temperature class create a VacuumBarrel class that extends the Barrel class...

Using your Barrel class and Temperature class create a VacuumBarrel class that extends the Barrel class and incorporates a Temperature as the private data of the VacuumBarrel. Provide constructors, add(VacuumBarrel), subtract(VacuumBarrel), divide(int), equals(VacuumBarrel), greaterThan(VacuumBarrel) (that has one parameter and returns true if the this is greater than the parameter), toKelvin(), toFahrenheit(), toCelsius(), and toString() methods. Use a similar demo to the one you used with the Temperature class. This demo will put instances of VacuumBarrel in three arrays and manipulates them.

The VacuumBarrel class extends Barrel so it is a Barrel. The data of the Barrel class should be private so the code answer.marbles = this.marbles + other.marbles; will not compile. The Barrel methods are inherited however and they can be used to make the addition. The Barrel class should have method set(int) that can be used to set the value of marbles to the integer parameter. The Barrel class should have method get() that will return the value of a Barrel. If the values of the two Barrels can be found then an addition is easy. It is also possible that the Barrel class has method set(Barrel) that will set the value of marbles to the parameter’s value. The method add(Barrel) of the Barrel class returns a Barrel. Using these two methods will also give the value for VacuumBarrel answer.

The VacuumBarrel has a private Temperature that can use the methods from the Temperature class. In the VacuumBarrel the private data degree and scale of the Temperature class cannot be used. If the private data in the VacuumBarrel is called temp then this value can be accessed with this code: answer.temp, this.temp and other.temp.

Solutions

Expert Solution

Here is the code,

The question is not completely clear, but I have tried to what I understood, If you feel there need some changes required then let me know I will try to fix that as well.

One thing that was not mentioned was the return types of the given method to be implemented like: add(VacuumBarrel), subtract(VacuumBarrel), divide(int), equals(VacuumBarrel), etc.

Barrel.java

public class Barrel {
    private int marbles;

    public void setMarbles(int marbles) {
        this.marbles = marbles;
    }

    public int getMarbles() {
        return marbles;
    }

    public void set(Barrel barrel){
        this.marbles = barrel.getMarbles();
    }

    public Barrel add(Barrel barrel){
        Barrel addBarrel = new Barrel();
        addBarrel.setMarbles(this.marbles + barrel.getMarbles());
        return addBarrel;
    }
}

Temperature.java

public class Temperature {
    private double degree;
    private String scale;

    public Temperature(double degree, String scale) {
        this.degree = degree;
        this.scale = scale;
    }

    public double getDegree() {
        return degree;
    }

    public void setDegree(double degree) {
        this.degree = degree;
    }

    public String getScale() {
        return scale;
    }

    public void setScale(String scale) {
        this.scale = scale;
    }
}

VacuumBarrel.java

public class VacuumBarrel extends Barrel{
    private Temperature temperature;

    public VacuumBarrel(Temperature temperature){
        this.temperature = temperature;
        if(!this.temperature.getScale().equals("celsius")){
            this.temperature = toCelsius();
            this.temperature.setScale("celsius");
        }
    }

    public VacuumBarrel add(VacuumBarrel obj){
        VacuumBarrel additionBarrel = new VacuumBarrel(this.temperature);
        if(!obj.temperature.getScale().equalsIgnoreCase("celsius")){
            obj.temperature = obj.toCelsius();
            obj.temperature.setScale("celsius");
        }
        additionBarrel.temperature.setDegree(additionBarrel.temperature.getDegree() + obj.temperature.getDegree());
        additionBarrel.temperature.setScale("celsius");
        return additionBarrel;
    }

    public VacuumBarrel subtract(VacuumBarrel obj){
        VacuumBarrel additionBarrel = new VacuumBarrel(this.temperature);
        if(!obj.temperature.getScale().equalsIgnoreCase("celsius")){
            obj.temperature = obj.toCelsius();
            obj.temperature.setScale("celsius");
        }
        additionBarrel.temperature.setDegree(additionBarrel.temperature.getDegree() - obj.temperature.getDegree());
        additionBarrel.temperature.setScale("celsius");
        return additionBarrel;
    }

    public VacuumBarrel divide(VacuumBarrel obj){
        VacuumBarrel additionBarrel = new VacuumBarrel(this.temperature);
        if(!obj.temperature.getScale().equalsIgnoreCase("celsius")){
            obj.temperature = obj.toCelsius();
            obj.temperature.setScale("celsius");
        }
        additionBarrel.temperature.setDegree(additionBarrel.temperature.getDegree() / obj.temperature.getDegree());
        return additionBarrel;
    }

    public boolean equals(VacuumBarrel obj){
        return this.temperature == obj.temperature;
    }

    public boolean greaterThan(VacuumBarrel obj){
        if(!obj.temperature.getScale().equalsIgnoreCase("celsius")){
            obj.temperature = obj.toCelsius();
            obj.temperature.setScale("celsius");
        }
        return this.temperature.getDegree() > obj.temperature.getDegree();
    }

    public Temperature toKelvin(){
        Temperature mTemp;
        if(this.temperature.getScale().equalsIgnoreCase("celsius")){
            double tempInKelvin = this.temperature.getDegree() + 273.15;
            return new Temperature(tempInKelvin, "kelvin");
        }
        else{
            double tempInKelvin = 273.5f + ((this.temperature.getDegree() - 32.0f) * (5.0f/9.0f));
            return new Temperature(tempInKelvin, "kelvin");
        }
    }

    public Temperature toFahrenheit(){
        Temperature mTemp;
        if(this.temperature.getScale().equalsIgnoreCase("celsius")){
            double tempInFahrenheit = this.temperature.getDegree() * (9f / 5) + 32;
            return new Temperature(tempInFahrenheit, "fahrenheit");
        }
        else{
            double tempInFahrenheit = ((this.temperature.getDegree() - 273.15) * 1.8) + 32;
            return new Temperature(tempInFahrenheit, "fahrenheit");
        }
    }

    public Temperature toCelsius(){
        Temperature mTemp;
        if(this.temperature.getScale().equalsIgnoreCase("fahrenheit")){
            double tempInCelsius =(( 5 *(this.temperature.getDegree() - 32.0)) / 9.0);
            return new Temperature(tempInCelsius, "celsius");
        }
        else{
            double kelvin = this.temperature.getDegree();
            double tempInCelsius = kelvin - 273.15F;
            return new Temperature(tempInCelsius, "celsius");
        }
    }

    @Override
    public String toString() {
        return "VacuumBarrel{" +
                "temperature=" + temperature +
                '}';
    }
}

If you have any doubts, then plz put in the comments. Also, do upvote the solution if it helped you in any way.


Related Solutions

In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
4.) Create a Java program using the “extends” and “runnable” methods. You will be creating your...
4.) Create a Java program using the “extends” and “runnable” methods. You will be creating your own threaded program using the threadExtend.java code and the threadRunnable.java code. A.) Focus the threadExtend.java code DETAILS TO NOTE: - It creates 4 instances of the same thread. This means that the thread code is actually running 4 separate times - The thread accepts 2 parameters, an INTEGER from 1 to 4, as well as an ID - Note the parameters are hard coded...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override the extract()method in your class. You can use your main() method for testing your method (although you do not need to provide a main method). Recall that a list is an ordered collection of data X_0, X_1, X_2, ..., X_n-1 The extract(int start, int end) method removes all elements X_start, X_start_1, ..., X_end-1 from the list. It also returns all removed elements as a...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View javadoc for the Mammal class and updated Animal class in homework 4 http://comet.lehman.cuny.edu/sfakhouri/teaching/cmp/cmp326/s19/hw/hw4/ Use the description provided below in UML. Mammal - tailLength : double - numLegs : int + Mammal() + Mammal(double, int) + Mammal(String, int, double, double, char, double, int) //pass values to parent’s overloaded constructor //and assign valid values to tailLength, numLegs or -1 if invalid + setTailLength(double) : void //if...
using java Design a class named Triangle that extends GeometricObject. The class contains: • Three double...
using java Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 to denote three sides of a triangle. • A no-arg constructor that creates a default triangle with default values 1.0. • A constructor that creates a triangle with the specified side1, side2, and side3. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule...
Create a Square Class and Create a graphical representation of your Square class - your class...
Create a Square Class and Create a graphical representation of your Square class - your class will have the following data fields: double width, String color. - provide a no-args constructor. - provide a constructor that creates a square with the specific width - implement method getArea() - implement method getPerimeter() - implement method setColor(). - draw a UML diagram for your class - write a test program that will create a square with the width 30, 40 and 50....
Write an Application that extends the Thread class, call it MyThread.java. Have your new class accept...
Write an Application that extends the Thread class, call it MyThread.java. Have your new class accept an integer(i.e. 200) when it is instantiated. (MyThread mt = new MyThread(200); ) This integer number will be the number of times that this class loops and prints a message. The message should read “Thread Running...200”. The 200 would be whatever number is passed into the constructor. If the number was 300, then the output should read “ Thread Running ..... 300”. Use a...
The questions in this assessment use the following. class R { ... } class A extends...
The questions in this assessment use the following. class R { ... } class A extends R { ... } abstract class B extends R { ... } final class C extends R { ...} class D extends A { ... } class E extends B { ... } class F extends B { ... } // none of the classes implement a toString() method [0] Draw a class hierarchy for the classes defined above. [1] No or Yes: class...
Write a class that extends the LeggedMammal class from the previous laboratory exercise.
C++ code on Visual Studio Code:Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators....
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT