Question

In: Computer Science

Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s...

Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s flavour and the number of scoops Create an IceCreamCone class with two fields - flavor and scoops The IceCreamCone constructor calls two data entry methods — getFlavor() and getScoops() The getScoops() method throws an IceCreamConeException when the scoop quantity exceeds 3 Write a program that establish three TceCreamCone objects and handles the Exception

Solutions

Expert Solution

//Java program

import java.util.Scanner;

class IceCreamConeException extends Exception
{
public IceCreamConeException(String s)
{
super(s);
}
}

class IceCreamCone{
   private String flavor;
   private int scoops;
  
   public IceCreamCone() throws IceCreamConeException {
       flavor = getFlavor();
       scoops = getScoops();
   }

   private int getScoops() throws IceCreamConeException {
       Scanner in = new Scanner(System.in);
       System.out.print("Enter number of Scoops : ");
       int scoop = in.nextInt();
       if(scoop > 3)throw new IceCreamConeException("out of limit");
       in.close();
       return scoop;
   }

   private String getFlavor() {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter Flavor name : ");
       String flavor = input.nextLine();
       input.close();
       return flavor;
   }
}

public class IceCream {
   public static void main(String args[]) {
       try {
           IceCreamCone cone1 = new IceCreamCone();
       } catch (IceCreamConeException e) {
           e.printStackTrace();
       }
       try {
           IceCreamCone cone2 = new IceCreamCone();
       } catch (IceCreamConeException e) {
           e.printStackTrace();
       }
       try {
           IceCreamCone cone3 = new IceCreamCone();
       } catch (IceCreamConeException e) {
           e.printStackTrace();
       }
      
   }
}


Related Solutions

Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment...
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment number, a number of bedrooms, and a rent value for an apartment. Upon construction, throw an ApartmentException if any of the following occur: • The apartment number does not consist of 3 digits • The number of bedrooms is less than 1 or more than 4 • The rent is less than $500.00 or over $2500 Write a driver class that demonstrates creating valid...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that...
Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that will take in a string with the format h:m:s and parse the h, m and s into separate int values that can be used to set the time of a new time object. int getSecond() const - return the value of the second void setSecond() - set the value of the second, making sure the new second value is valid before changing it. std::string...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
Create a class that calls for user to input a string. Pass the string from that...
Create a class that calls for user to input a string. Pass the string from that class to another and count how many words the string has, count how many vowels are in the string and count how many letters end with d. java language
What can be done to improve the texture and mouthfeel of ice creams?
What can be done to improve the texture and mouthfeel of ice creams?
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method 2: badFriend - return string value, "I don't like you!" Main Method: - create new Friend object - call goodFriend and badFriend using Friend object /* class header */ { /* constructor method header */ { } public static void main (String [] args) { Friend f = /* new Friend object */ // call goodFriend using Friend object // call badFriend using Friend...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT