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...
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...
Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
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?
A constructor, with a String parameter representing the name of the item.
USE JAVA Item classA 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 ClassIt 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.Inventory classA constructor which takes a String parameter indicating the item type, an int parameter indicating the initial...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this class is KeywordsInFile( filename_with_keywords, filename_with_text) filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N. filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT