Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have written a JAVA program that creates a Drink object with two instance va

ID: 3690964 • Letter: Y

Question

You have written a JAVA program that creates a Drink object with two instance variables. String drink kind should not be blank or contain blanks only int size should be 1, 2 or 3 Scroll to the bottom of the assignment to get the code that you will need to test your assignment The code below can be used to test that your class works well. Copy and paste it into JGrasp. You should submit Drink.java and DrinkException.java. You are not going to write a main method (driver class) because I have already created it for you, as you can see below: You will create DrinkException.java and Drink.java To run with my code CoffeeShop.java. To see my code running : http://screencast-o-matic.com/watch/cDfjYE1a93 that is how it should look when you run my code with your code. You will submit only Drink.java and DrinkException.java. Do not submit CoffeeShop.java, I already have that code. Specifications for Drink.java. You will create the following methods

public class Drink{ Constructor:

public Drink(String kind, int size) throws Exception

public String toString( ) - must spell out drink size, 1=small, 2 = medium, 3= large. You may use if or switch in here.

public void setKind(String newKind) throws Exception

public void setSize(int newSize) throws Exception

public int getSize( )

public String getKind( )

For DrinkException.java

you will create the following methods public class

DrinkException extends Exception{ Constructor public DrinkException( )

public void setMessage(String newMessage) -20

public String getMessage( )

Here is my CoffeeShop.java code

import java.util.*;

public class CoffeeShop{
   public static void main (String[ ] arg) throws Exception{
      final double smPrice = 2.0;
      final double medPrice = 3.0;
      final double lgPrice = 3.5;
      Scanner sc = new Scanner(System.in);
      String input="";
      String kind;
      double total=0;
      int size=0;
         /*1 = small
           2 = medium
           3 = large
           */
      Drink[ ] order = new Drink[20];
      int count=0; //to count the number of items ordered   
      System.out.println("Welcome to CoffeeLand!"+
                         " Small drinks $2.00"+
                         " Medium drinks $3.00"+
                         " Large drinks $3.50"+
                         " What are you drinking today?");
    
      do{                 
           System.out.print("drink: ");
           sc = new Scanner(System.in);
           kind = sc.nextLine( );
           System.out.println("1=small, 2=medium, 3=large");
           System.out.print("size: ");
           size = sc.nextInt( );
           try{
              Drink drink = new Drink(kind, size); //calling constructor
              order[count] = drink;
              switch(size){
               case 1: total = total + smPrice;
               break;
               case 2: total = total + medPrice;
               break;
               case 3: total = total + lgPrice;
               break;            
              }
              count++;
           } //closes try
           catch(DrinkException de){
              System.out.print(de.getMessage( ));
           }
           sc = new Scanner(System.in);
           System.out.println(" Want to order another drink?");
           System.out.println("type y to continue anything else to end");
           input = sc.nextLine( );
           sc = new Scanner(System.in);
      }while (input.trim( ).equalsIgnoreCase("y"));
    
      System.out.println("you ordered");
      for(int i=0; i<count; i++){
          System.out.println(order[i].toString( ));
      }
      System.out.printf(" Total $ %.2f",total);
   }
} //closes class

Explanation / Answer

Drink.java

public class Drink
{
   private String kind;
   private int size;

   public Drink(String k,int s)throws Exception
   {
       kind=k;
       if(s>0 && s<4)
       {
           size=s;  
       }
       else
       {
           DrinkException ex=new DrinkException();
           ex.setMessage("Size must be 1,2 or 3");
           throw ex;
       }
   }

   public int getSize()
   {
       return size;
   }

   public String getKind()
   {
       return kind;
   }

   public void setKind(String k)
   {
       kind=k;
   }

   public void setSize(int s)throws Exception
   {
       if(s>0 && s<4)
       {
           size=s;  
       }
       else
       {
           DrinkException ex=new DrinkException();
           ex.setMessage("Size must be 1,2 or 3");
           throw ex;
       }
   }

   public String toString()
   {
       String s="Drink type: "+kind+" "+"Size: ";
       switch(size)
       {
           case 1:   s=s+"small";
           break;
           case 2:   s=s+"medium";
           break;
           case 3:   s=s+"large";
           break;
       }
       return s;
   }
}

DrinkException.java

public class DrinkException extends Exception
{
   private String message;

   public DrinkException()
   {
       super();
   }

   public void setMessage(String ex)
   {
       message=ex;
   }
   @Override
   public String getMessage()
   {
       return message;
   }
}

output:

Welcome to CoffeeLand!

   Small drinks $2.00

   Medium drinks $3.00

   Large drinks $3.50

What are you drinking today?

drink: coffee

1=small, 2=medium, 3=large

size: 2

Want to order another drink?

type y to continue anything else to end

y

drink: tea

1=small, 2=medium, 3=large

size: 7

Size must be 1,2 or 3

Want to order another drink?

type y to continue anything else to end

y

drink: mocha

1=small, 2=medium, 3=large

size: 3

Want to order another drink?

type y to continue anything else to end

n

you ordered

Drink type: coffee

Size: medium

Drink type: mocha

Size: large

   Total $ 6.50

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote