Write a Bottle class. The Bottle will have one private int that reprrsents the c
ID: 3748103 • Letter: W
Question
Write a Bottle class. The Bottle will have one private int that reprrsents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&M, pennies, nickels, dimes or pebbles. The class has these 14 methods : read(), set(int), set(Bottle), get(), add(Bottle), substract(Bottle), multiply(Bottle), divide(Bottle), add(int), substract(int), multiply(int), divide(int), equals(Bottle), and toString() (toString() method will be given in class). All add, substract, 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 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. 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 mathod to violate the minimum value of zero. The method substract with a Bottle parameter must test for a violation of te minimum zero value but should not test for exceeding the maximum value. Consider each method carefully and test only the conditions that could be violated.
I have Bottle Demo as an attachment code that already given.
Explanation / Answer
/*package whatever //do not write package name here */
import java.util.*;
class Bottle{
private int pebbles;
int max=100 ,min=0;
Scanner scan = new Scanner(System.in);
void read(){
this.pebbles= scan.nextInt();
}
void set(int i){
this.pebbles=i;
}
void set(Bottle b){
this.pebbles=b.pebbles;
}
int get(){
return this.pebbles;
}
Bottle add(Bottle b){
int i =this.pebbles+b.pebbles;
if(i< min || i>max )
return null;
Bottle b1= new Bottle();
b1.set(i);
return b1;
}
Bottle add(int i){
i=this.pebbles+i;
if(i< min || i>max )
return null;
Bottle b1= new Bottle();
b1.set(i);
return b1;
}
Bottle substract(Bottle b){
int i =this.pebbles-b.pebbles;
if(i< min || i>max )
return null;
Bottle b1= new Bottle();
b1.set(i);
return b1;
}
Bottle substract(int i){
i=this.pebbles-i;
if(i< min || i>max )
return null;
Bottle b1= new Bottle();
b1.set(i);
return b1;
}
Bottle multiply(int i){
i=this.pebbles*i;
if(i< min || i>max )
return null;
Bottle b1= new Bottle();
b1.set(i);
return b1;
}
Bottle multiply(Bottle b){
int i=this.pebbles*b.pebbles;
if(i< min || i>max )
return null;
Bottle b1= new Bottle();
b1.set(i);
return b1;
}
Bottle divide(int i){
i=this.pebbles/i;
if(i< min || i>max )
return null;
Bottle b1= new Bottle();
b1.set(i);
return b1;
}
Bottle divide(Bottle b){
int i=this.pebbles/b.pebbles;
if(i< min || i>max )
return null;
Bottle b1= new Bottle();
b1.set(i);
return b1;
}
boolean equals(Bottle b){
if(b.pebbles==this .pebbles)
return true;
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.