Question

In: Computer Science

Write a class named GasTank containing: An instance variable named amount of type double, initialized to...

Write a class named GasTank containing:

  • An instance variable named amount of type double, initialized to 0.
  • An instance variable named capacity of type double.
  • A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity.
  • A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is increased beyond the value of capacity, amount is set to capacity.
  • A method named useGas that accepts a parameter of type double. The value of the amount instance variable is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 0.
  • A method named isEmpty that accepts no parameters. isEmpty returns a boolean value: true if the value of amount is less than 0.1, and false otherwise.
  • A method named isFull that accepts no parameters. isFull returns a boolean value: true if the value of amount is greater than capacity-0.1, and false otherwise.
  • A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instance variable.

Solutions

Expert Solution

class GasTank {
        private double amount = 0;
        private double capacity;

        public GasTank(double aCapacity) {
                super();
                capacity = aCapacity;
        }

        public void addGas(double d) {
                amount += d;
                if (amount > capacity)
                        amount = capacity;
        }

        public void useGas(double d) {
                amount -= d;
                if (amount < 0)
                        amount = 0;
        }

        public boolean isEmpty() {
                return amount == 0;
        }

        public boolean isFull() {
                return amount == capacity;
        }

        public double getGasLevel() {
                return amount;
        }

}

public class TestGasTank {
        public static void main(String[] args) {
                GasTank gt = new GasTank(100);
                gt.addGas(50);
                System.out.println("Gas : " + gt.getGasLevel());
                gt.addGas(50);
                System.out.println("Gas : " + gt.getGasLevel());
                System.out.println("Is Full: " + gt.isFull());
                gt.useGas(100);
                System.out.println("Gas : " + gt.getGasLevel());
                System.out.println("Is Empty: " + gt.isEmpty());

        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
RUBY Write a DNA class that gets initialized with a string of nucleotide characters. An instance...
RUBY Write a DNA class that gets initialized with a string of nucleotide characters. An instance of DNA responds to the to_s, length, hamming_distance, contains, positions, and frequencies messages. Where a and b are DNA instances of the same length, the message send a.hamming_distance(b) returns the Hamming distance between a and b. a.contains(b) returns true if b appears as a substring in a, and returns false otherwise. a.positions(n) returns an array of the (zero-based) positions in which the nucleotide n...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JavaDefine a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
Code in Java Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JAVADefine a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Given a String variable named sentence that has been initialized, write an expression whose value is...
Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence. write it in python
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named...
Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT