Question

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

Homework Answers

Answer #1

If you have any program with code feel free to comment

Program

package funWithBottle;

import java.util.Scanner;

/*since no instruction was given on what to do when the violation occurs
* other than read() I have simply put a message to show the violation*/

public class Bottle {
  
   private int pebbles=0;
   //hardcoded min and max
   private final int max = 15;
   private final int min = 1;

   public void read() {
       Scanner sc= new Scanner(System.in);
       pebbles = sc.nextInt();sc.nextLine();
       while(pebbles< min || pebbles > max) {//re entering the values when violation occures
           System.out.print("Invalid Input! try again: ");
           pebbles = sc.nextInt();sc.nextLine();
       }
   }
   //setters
   public void set(int val) {
       if(val< min || val > max) {
           System.out.print("Violation of value in set(int)\n");
       }
       pebbles = val;
   }
  
   public void set(Bottle b) {
       if(b.pebbles< min || b.pebbles > max) {
           System.out.print("Violation of value set(Bottle)\n");
       }
       pebbles = b.pebbles;
   }
   //getter
   public int get() {
       return pebbles;
   }
   //adding bottles
   public Bottle add(Bottle b) {
       Bottle ans = new Bottle();
       int sum = this.pebbles + b.pebbles;
       ans.set(sum);
       if(ans.pebbles< min || ans.pebbles > max) {
           System.out.print("Violation of value in add(bottle)\n");
       }
       return ans;
   }
   public Bottle add(int val) {
       Bottle ans = new Bottle();
       int sum = this.pebbles + val;
       ans.set(sum);
       if(ans.pebbles< min || ans.pebbles > max) {
           System.out.print("Violation of value in add(int)\n");
       }
       return ans;
   }
   //subtracting bottles
   public Bottle subtract(Bottle b) {
       Bottle ans = new Bottle();
       int sum = this.pebbles - b.pebbles;
       ans.set(sum);
       if(ans.pebbles< min || ans.pebbles > max) {
           System.out.print("Violation of value in subtract(bottle)\n");
       }
       return ans;
   }
   public Bottle subtract(int val) {
       Bottle ans = new Bottle();
       int sum = this.pebbles - val;
       ans.set(sum);
       if(ans.pebbles< min || ans.pebbles > max) {
           System.out.print("Violation of value in subtract(int)\n");
       }
       return ans;
   }
   //multiplying bottles
   public Bottle multiply(Bottle b) {
       Bottle ans = new Bottle();
       int sum = this.pebbles * b.pebbles;
       ans.set(sum);
       if(ans.pebbles< min || ans.pebbles > max) {
           System.out.print("Violation of value in multiply(bottle)\n");
       }
       return ans;
   }
   public Bottle multiply(int val) {
       Bottle ans = new Bottle();
       int sum = this.pebbles * val;
       ans.set(sum);
       if(ans.pebbles< min || ans.pebbles > max) {
           System.out.print("Violation of value in multiply(int)\n");
       }
       return ans;
   }
   //dividing bottles
   public Bottle divide(Bottle b) {
       Bottle ans = new Bottle();
       int sum = this.pebbles / b.pebbles;
       ans.set(sum);
       if(ans.pebbles< min || ans.pebbles > max) {
           System.out.print("Violation of value in divide(bottle)\n");
       }
       return ans;
   }
   public Bottle divide(int val) {
       Bottle ans = new Bottle();
       int sum = this.pebbles / val;
       ans.set(sum);
       if(ans.pebbles< min || ans.pebbles > max) {
           System.out.print("Violation of value in divide(int)\n");
       }
       return ans;
   }
   //comparing bottles
   public boolean equals(Bottle obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       if (pebbles != obj.pebbles)
           return false;
       return true;
   }

   @Override//string representation of the class
   public String toString() {
       return pebbles+"";
   }
  
}

Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a numeric object (Integer, Double or any class that extends java.lang.number) Add a method standardDeviation (stdev) that takes an ArrayList of type T and returns a standard deviation as type double. Use a for each loop where appropriate. Hard code a couple of test arrays into your Demo file. You must use at least 2 different types such as Double and Integer. Your call will...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B()...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
Design a class that can be used to simulate rolling a multi-faced die. 2. Write a...
Design a class that can be used to simulate rolling a multi-faced die. 2. Write a Java class called Die in a class file called Die.java.    3. The Die class will have three private class attributes: a. The first attribute, called numSides, is of type int and represents the number of sides on the die. b. The second attribute, called sumRolls, is static of type int and will keep track of the sum of multiple die rolls by multiple...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
I have a 2 class code and it works everything is fine and runs as it...
I have a 2 class code and it works everything is fine and runs as it supposed too. What will the UML be for both classes. Here's the code, any help will be awsome, Thanks. import java.util.Scanner; public class PayRollDemo { public static void main(String[] args) { double payRate; int hours; PayRoll pr = new PayRoll(); Scanner keyboard = new Scanner(System.in); for (int index = 0; index < 7 ; index++ ) { System.out.println(); System.out.println("EmployeeID:" + pr.getEmployeeID(index)); System.out.println(); System.out.println("Enter the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT