Question

In: Computer Science

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() 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.

import java.util.Scanner;
// test driver for the Bottle class
                public class BottleDemo3
{
        public static void main(String[] args)
        {
                Scanner scan = new Scanner(System.in);
                int x;
                Bottle bottle1 = new Bottle();
                Bottle bottle2 = new Bottle();
                Bottle bottle3 = new Bottle();
                Bottle bottle4 = new Bottle();
                Bottle bottle5 = new Bottle();
                System.out.println("please enter a number for bottle1:");
                bottle1.read();
                System.out.println("Bottle1 is this value " + bottle1 + ".");
                System.out.println("Please enter a number for bottle2:");
                bottle2.read();
                bottle3 = bottle2.add(bottle1);
                System.out.println("The sum of bottle2 and bottle1 is: " + bottle3 + ".");
                bottle4 = bottle3.divide(2);
                System.out.println("The 2 bottle average is: " + bottle4 + ".");
                System.out.print("Subtracting bottle1 from bottle2 is: " );
                bottle3 = bottle2.subtract(bottle1);
                System.out.println( bottle3);
                bottle3 = bottle2.divide(bottle1);
                System.out.println("Dividing bottle2 with bottle1 is: " + bottle3 + ".");
                if (bottle1.equals(bottle2))
                {
                        System.out.println("Bottle1 and bottle2 are equal.");
                }
                else
                {
                        System.out.println("Bottle1 and bottle2 are not equal.");
                }
                System.out.println("Bottle4 is now given the value of 10 with the set() method.");
                bottle4.set(10);
                System.out.println("The value of bottle4 is " + bottle4 + ".");
                System.out.println("Bottle4 is now multipled with bottle1.  The value is placed in " +
                                "bottle5.");
                bottle5 = bottle1.multiply(bottle4);
                System.out.println("The value of bottle5 is " + bottle5 + ".");
                System.out.println("Enter an integer to add to the value bottle1 has.");
                System.out.println("The sum will be put in bottle3.");
                x = scan.nextInt();
                bottle3 = bottle1.add(x);
                System.out.println("Adding your number " + x +
                        " to bottle1 gives a new Bottle with " + bottle3 + " in it.");
                System.out.print("Adding the number " + bottle2 + " which is the number" +
                                " in bottle2 to the\nnumber in ");
                bottle2 = bottle1.add(bottle2);
                System.out.println("bottle1 which is " + bottle1 +" gives " + bottle2 + ".");
                bottle2.set(bottle2.get());
        }
}

Solutions

Expert Solution

Bottle.java

import java.util.Scanner;

public class Bottle {
private static int MIN = 0;
private static int MAX = 50;
private int marbles;
  
public Bottle()
{
this.marbles =0 ;
}
  
public Bottle(int marbles)
{
this.marbles = marbles;
}

public int get() {
return marbles;
}

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

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Bottle other = (Bottle) obj;
if (this.marbles != other.marbles) {
return false;
}
return true;
}
  
public void read()
{
Scanner sc = new Scanner(System.in);
int num = Integer.parseInt(sc.nextLine().trim());
while(num <= MIN || num > MAX)
{
System.out.print("Enter a number between " + MIN + " and " + MAX + ": ");
num = Integer.parseInt(sc.nextLine().trim());
}
set(num);
}
  
@Override
public String toString()
{
return(this.get() + "");
}
  
public Bottle add(Bottle bottle)
{
int sum = this.get() + bottle.get();
if(sum > MAX)
{
System.out.println("Addition not allowed as " + sum + " > " + MAX);
return null;
}
else
{
Bottle newBottle = new Bottle(sum);
return newBottle;
}
}
  
public Bottle subtract(Bottle bottle)
{
int diff = this.get() - bottle.get();
if(diff < MIN)
{
System.out.println("Subtraction not allowed as " + diff + " < " + MIN);
return null;
}
else
{
Bottle newBottle = new Bottle(diff);
return newBottle;
}
}
  
public Bottle multiply(Bottle bottle)
{
int prod = this.get() * bottle.get();
if(prod > MAX)
{
System.out.println("Multiplication not allowed as " + prod + " > " + MAX);
return null;
}
else
{
Bottle newBottle = new Bottle(prod);
return newBottle;
}
}
  
public Bottle divide(Bottle bottle)
{
if(bottle.get() == 0)
{
System.out.println("Division by 0 is not allowed.");
return null;
}
  
int quot = this.get() / bottle.get();
if(quot < MIN)
{
System.out.println("Division not allowed as " + quot + " < " + MIN);
return null;
}
else
{
Bottle newBottle = new Bottle(quot);
return newBottle;
}
}
  
public Bottle add(int num)
{
int sum = this.get() + num;
if(num > MAX)
{
System.out.println("Addition not allowed as " + sum + " > " + MAX);
return null;
}
else
{
Bottle newBottle = new Bottle(sum);
return newBottle;
}
}
  
public Bottle subtract(int num)
{
int diff = this.get() - num;
if(diff < MIN)
{
System.out.println("Subtraction not allowed as " + diff + " < " + MIN);
return null;
}
else
{
Bottle newBottle = new Bottle(diff);
return newBottle;
}
}
  
public Bottle multiply(int num)
{
int prod = this.get() * num;
if(prod > MAX)
{
System.out.println("Multiplication not allowed as " + prod + " > " + MAX);
return null;
}
else
{
Bottle newBottle = new Bottle(prod);
return newBottle;
}
}
  
public Bottle divide(int num)
{
if(num == 0)
{
System.out.println("Division by 0 is not allowed.");
return null;
}
  
int quot = this.get() / num;
if(quot < MIN)
{
System.out.println("Division not allowed as " + quot + " < " + MIN);
return null;
}
else
{
Bottle newBottle = new Bottle(quot);
return newBottle;
}
}
}

BottleDemo3.java (Main class)

import java.util.Scanner;

public class BottleDemo3 {
  
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
int x;
Bottle bottle1 = new Bottle();
Bottle bottle2 = new Bottle();
Bottle bottle3 = new Bottle();
Bottle bottle4 = new Bottle();
Bottle bottle5 = new Bottle();
System.out.print("Please enter a number for bottle1: ");
bottle1.read();
System.out.println("Bottle1 is this value: " + bottle1 + ".");
System.out.print("Please enter a number for bottle2: ");
bottle2.read();
bottle3 = bottle2.add(bottle1);
System.out.println("The sum of bottle2 and bottle1 is: " + bottle3 + ".");
bottle4 = bottle3.divide(2);
System.out.println("The 2 bottle average is: " + bottle4 + ".");
System.out.print("Subtracting bottle1 from bottle2 is: ");
bottle3 = bottle2.subtract(bottle1);
System.out.println(bottle3);
bottle3 = bottle2.divide(bottle1);
System.out.println("Dividing bottle2 with bottle1 is: " + bottle3 + ".");
if (bottle1.equals(bottle2)) {
System.out.println("Bottle1 and bottle2 are equal.");
} else {
System.out.println("Bottle1 and bottle2 are not equal.");
}
System.out.println("Bottle4 is now given the value of 10 with the set() method.");
bottle4.set(10);
System.out.println("The value of bottle4 is " + bottle4 + ".");
System.out.println("Bottle4 is now multipled with bottle1. The value is placed in bottle5.");
bottle5 = bottle1.multiply(bottle4);
System.out.println("The value of bottle5 is " + bottle5 + ".");
System.out.println("Enter an integer to add to the value bottle1 has.");
System.out.println("The sum will be put in bottle3.");
x = scan.nextInt();
bottle3 = bottle1.add(x);
System.out.println("Adding your number " + x
+ " to bottle1 gives a new Bottle with " + bottle3 + " in it.");
System.out.print("Adding the number " + bottle2 + " which is the number"
+ " in bottle2 to the\nnumber in ");
bottle2 = bottle1.add(bottle2);
System.out.println("bottle1 which is " + bottle1 + " gives " + bottle2 + ".");
bottle2.set(bottle2.get());
}
}

******************************************************************* SCREENSHOT ********************************************************


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 CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
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...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
please write a java code, one for method and another for the Demo to: -Compute the...
please write a java code, one for method and another for the Demo to: -Compute the average age of all female students. -Compute the least amount of credits completed among males. Store the three arrays in the demo file. Print the results within the demo file. String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"}; int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21,...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for e.g., 1980), except that it is not a leap year if it is also divisible by 100 (for e.g., 1900); however, it is a leap year if it is further divisible by 400 (for e.g., 2000). Write a program that prompts the...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
write a java code, please do not use method and demo Consider a four digit number...
write a java code, please do not use method and demo Consider a four digit number such as 6587. Split it at two digits, as 65 and 87. Sum of 65 and 87 is 152. Sum of the digits of 152 is 8. Given the starting and ending four digit numbers and a given target number such as 10, write a program to compute and print the number of instances where the sum of digits equals the target.
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT