Question

In: Computer Science

JAVA CODE, USE COMMENTS TO EXPLAIN PLEASE Write a Bottle class. The Bottle will have one...

JAVA CODE, USE COMMENTS TO EXPLAIN PLEASE

Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString() method will be given in class). All add, subtract, multiply and divide methods return a Bottle. This means the demo code b2 = b3.add(b1) brings into the add method a Bottle b1 which is added to b3. Bottle b3 is the this Bottle. The returned bottle is a new bottle containing the sum of b1 and b3. The value of b3 (the this bottle) is not changed. Your Bottle class must guarantee bottles always have a positive value and never exceed a maximum number chosen by you. These numbers are declared as constants of the class. Use the names MIN and MAX. The read() method should guarantee a value that does not violate the MIN or MAX value. Use a while loop in the read method to prompt for a new value if necessary. Each method with a parameter must be examined to determine if the upper or lower bound could be violated. In the case of the add method with a Bottle parameter your code must test for violating the maximum value. It is impossible for this add method to violate the minimum value of zero. The method subtract with a Bottle parameter must test for a violation of the minimum zero value but should not test for exceeding the maximum value. In the case of a parameter that is an integer, all methods must be examined to guarantee a value that does not violate the MIN or MAX values. Consider each method carefully and test only the conditions that could be violated.

(2 point) Further in the semester we will use a runtime exception class to guarantee these invariants.

public String toString()

{     

       return “” + this.pennies;

}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Bottle.java

import java.util.Scanner;

public class Bottle {

      // number of cookies or pennies or whatever you like

      private int cookies;

      // constants for minimum and maximum values

      private static int MIN = 0;

      private static int MAX = 50;

      // constructor sets cookies to minimum value

      public Bottle() {

            cookies = MIN;

      }

      // reads a valid value and sets the cookies count

      public void read() {

            Scanner scanner = new Scanner(System.in);

            System.out.print("Enter value for cookies: ");

            cookies = scanner.nextInt();

            // looping as long as cookies is invalid

            while (cookies < MIN || cookies > MAX) {

                  System.out.print("Error! Please enter a valid value between " + MIN

                              + " and " + MAX + ": ");

                  cookies = scanner.nextInt();

            }

      }

      // sets the cookies to given value if it is valid

      public void set(int cookies) {

            if (cookies >= MIN && cookies <= MAX) {

                  this.cookies = cookies;

            }

      }

      // sets the cookies to that of other bottle

      public void set(Bottle other) {

            this.cookies = other.cookies;

      }

      // returns the cookies

      public int get() {

            return cookies;

      }

      // adds cookies of two bottles and return the resultant Bottle

      public Bottle add(Bottle other) {

            int res = this.cookies + other.cookies;

            if (res > MAX) {

                  // capping to MAX

                  res = MAX;

            }

            Bottle result = new Bottle();

            result.cookies = res;

            return result;

      }

      // subtracts cookies of two bottles and return the resultant Bottle

      public Bottle subtract(Bottle other) {

            int res = this.cookies - other.cookies;

            if (res < MIN) {

                  // capping to MIN

                  res = MIN;

            }

            Bottle result = new Bottle();

            result.cookies = res;

            return result;

      }

      // multiplies cookies of two bottles and return the resultant Bottle

      public Bottle multiply(Bottle other) {

            int res = this.cookies * other.cookies;

            if (res > MAX) {

                  res = MAX;

            }

            Bottle result = new Bottle();

            result.cookies = res;

            return result;

      }

      // divides cookies of two bottles and return the resultant Bottle

      public Bottle divide(Bottle other) {

            int res = this.cookies / other.cookies;

            if (res < MIN) {

                  res = MIN;

            }

            Bottle result = new Bottle();

            result.cookies = res;

            return result;

      }

      // adds count more cookies to current bottle and return the new bottle

      public Bottle add(int count) {

            int res = this.cookies + count;

            // validating res (count could be negative, so checking for both MAX and

            // MIN)

            if (res > MAX) {

                  res = MAX;

            } else if (res < MIN) {

                  res = MIN;

            }

            Bottle result = new Bottle();

            result.cookies = res;

            return result;

      }

      // subtracts count cookies from current bottle and return the new bottle

      public Bottle subtract(int count) {

            int res = this.cookies - count;

            if (res > MAX) {

                  res = MAX;

            } else if (res < MIN) {

                  res = MIN;

            }

            Bottle result = new Bottle();

            result.cookies = res;

            return result;

      }

      // multiplies count more cookies to current bottle and return the new bottle

      public Bottle multiply(int count) {

            int res = this.cookies * count;

            if (res > MAX) {

                  res = MAX;

            } else if (res < MIN) {

                  res = MIN;

            }

            Bottle result = new Bottle();

            result.cookies = res;

            return result;

      }

      // divides count cookies from current bottle and return the new bottle

      public Bottle divide(int count) {

            int res = this.cookies / count;

            if (res > MAX) {

                  res = MAX;

            } else if (res < MIN) {

                  res = MIN;

            }

            Bottle result = new Bottle();

            result.cookies = res;

            return result;

      }

     

      //returns true if cookies in two bottles are same

      public boolean equals(Bottle other) {

            return this.cookies == other.cookies;

      }

      public String toString(){

            return "" + this.cookies;

      }

}

// Test.java

public class Test {

      public static void main(String[] args) {

            // creating two bottles, reading values and displaying both

            Bottle bottle1 = new Bottle();

            Bottle bottle2 = new Bottle();

            bottle1.read();

            System.out.println("Bottle 1: " + bottle1);

            bottle2.read();

            System.out.println("Bottle 2: " + bottle2);

            // testing all add, sub, multiply, division methods, please note that if

            // during any operation, the value of a bottle exceeds MAX, the value is

            // capped to MAX and if it goes below MIN, will be capped to MIN, also

            // the division yields integer division only

            System.out.println("\nBottle 1 + Bottle 2: " + bottle1.add(bottle2));

            System.out.println("Bottle 1 - Bottle 2: " + bottle1.subtract(bottle2));

            System.out.println("Bottle 1 * Bottle 2: " + bottle1.multiply(bottle2));

            System.out.println("Bottle 1 / Bottle 2: " + bottle1.divide(bottle2));

            // testing all add, sub, multiply, division methods taking int values

            System.out.println("\nBottle 1 + 10: " + bottle1.add(10));

            System.out.println("Bottle 1 - 10: " + bottle1.subtract(10));

            System.out.println("Bottle 1 * 10: " + bottle1.multiply(10));

            System.out.println("Bottle 1 / 10: " + bottle1.divide(10));

            // testing equals method

            System.out.println("\nBottle 1 = Bottle 2: " + bottle1.equals(bottle2));

      }

}

/*OUTPUT*/

Enter value for cookies: 30

Bottle 1: 30

Enter value for cookies: 20

Bottle 2: 20

Bottle 1 + Bottle 2: 50

Bottle 1 - Bottle 2: 10

Bottle 1 * Bottle 2: 50

Bottle 1 / Bottle 2: 1

Bottle 1 + 10: 40

Bottle 1 - 10: 20

Bottle 1 * 10: 50

Bottle 1 / 10: 3

Bottle 1 = Bottle 2: false


Related Solutions

JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word: banana dresser grammar potato revive uneven assess Write a program that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters as lowercase letters.
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. • A no-arg constructor that creates a default rectangle. • A constructor that creates a rectangle with specified width and height • A method name getWidth() return the value...
Write a Bottle class. The Bottle will have one private int that represents the countable value...
Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString() method will be given in class). All add, subtract, multiply...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments)...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments) // declare the constructor that sets the type and rate based in the sqft parm        // set values based on sqft <1000 is small with $100 per night, // sqft between 1000 and 2000, mid-sized $200 per night, and // over 2000 as a large cabin with $300 per night        //declare getRate        //declare getType        //declare setRate with int rate parm...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT