Question

In: Computer Science

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 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;}

I also have a demo to go with this one...

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

Thanks for the question.

Below is the implementation of the Bottle.java class you will be needing  Let me know if you have any doubts or if you need anything to change. I ran your driver class and its running fine without any error.

Below is the screenshot of a sample run.

Let me know for any changes ..

Thank You !!

===========================================================================


import java.util.Scanner;

public class Bottle {

    private int cookies;
    private static final int MIN = 0;
    private static final int MAX = 10;

    public Bottle() {
        cookies = 0;
    }

    public void read() {

        Scanner scanner = new Scanner(System.in);
        cookies = scanner.nextInt();
        if (cookies < MIN) cookies = 0;
        if (cookies > MAX) cookies = MAX;
        scanner.nextLine();
    }

    public void set(int i) {

        cookies = i;
        if (cookies < MIN) cookies = MIN;
        if (cookies > MAX) cookies = MAX;
    }

    public void set(Bottle bottle) {

        cookies += bottle.cookies;
        if (cookies < MIN) cookies = MIN;
        if (cookies > MAX) cookies = MAX;
    }

    public Bottle add(Bottle bottle1) {

        Bottle bottle = new Bottle();
        if (cookies + bottle1.cookies > MAX) {
            bottle.set(MAX);
        } else {
            bottle.set(cookies + bottle1.cookies);
        }
        return bottle;
    }


    public Bottle subtract(Bottle bottle1) {

        Bottle bottle = new Bottle();
        if (cookies - bottle1.cookies < MIN) {
            bottle.set(MIN);
        } else {
            bottle.set(cookies - bottle1.cookies);
        }
        return bottle;
    }

    public Bottle divide(Bottle bottle1) {
        Bottle bottle = new Bottle();
        bottle.set(cookies / bottle1.cookies);
        if (cookies / bottle1.cookies < MIN) bottle.set(MIN);
        if (cookies / bottle1.cookies > MAX) bottle.set(MAX);
        return bottle;
    }

    public Bottle divide(int count) {

        Bottle bottle = new Bottle();
        bottle.set(cookies / count);
        if (cookies / count < MIN) bottle.set(MIN);
        if (cookies / count > MAX) bottle.set(MAX);
        return bottle;
    }

    public Bottle multiply(Bottle bottle4) {
        Bottle bottle = new Bottle();
        if (cookies * bottle4.cookies > MAX) {
            bottle.set(MAX);
        } else {
            bottle.set(cookies * bottle4.cookies);
        }
        return bottle;
    }

    public Bottle multiply(int count) {
        Bottle bottle = new Bottle();
        if (cookies * count > MAX) {
            bottle.set(MAX);
        } else {
            bottle.set(cookies * count);
        }
        return bottle;
    }

    public Bottle add(int x) {
        Bottle bottle = new Bottle();
        if (cookies + x > MAX) {
            bottle.set(MAX);
        } else {
            bottle.set(cookies + x);
        }
        return bottle;
    }

    public Bottle subtract(int x) {
        Bottle bottle = new Bottle();
        if (cookies - x < MIN) {
            bottle.set(MIN);
        } else {
            bottle.set(cookies - x);
        }
        return bottle;
    }

    @Override
    public String toString() {
        return "Bottle Count: " + cookies;
    }

    public int get() {
        return cookies;
    }

    public boolean equals(Bottle obj) {
        if (obj == null) return false;
        return cookies == obj.cookies;
    }
}

====================================================================

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());
    }
}

====================================================================



===========================================================================

Thank you so much !

Please do appreciate with an up vote : )


Related Solutions

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...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
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()...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
Write a class Electionresults. Write the input inData (int a, int b) that creates (for example...
Write a class Electionresults. Write the input inData (int a, int b) that creates (for example by random method) polls and returns two-dimensional arrays containing polls, where as the first dimension is the number of a political party and the second dimension is the constituency code. The inData entry method includes integers, number of categories and number of constituencies. Write the program countingCategory that includes a two-dimensional state with data on the number of votes, and returns the total number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT