In: Computer Science
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;
}
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