Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project 3: Frozen Yogurt with Mix-ins Assignment For this project, you get to build off of the FrozenYogurt class from Project 2 and exercise using

Project 3: Frozen Yogurt with Mix-ins

Assignment

For this project, you get to build off of the FrozenYogurt class from Project 2 and exercise using loops in Java! A very common application of loops is input validation. You will get to do that in this project. Additionally, you get to use either a StringBuilder or StringBuffer object to store multiple toppings in a FrozenYogurt object.

FrozenYogurt class

For the FrozenYogurt class make the following changes to that class:

(Driver class will be separate)

1.Change the instance variable for the topping to be either a StringBuilder or StringBuffer object to appropriately support a comma-separated list of toppings (instead of just a single topping)

2.Change the setPrice method so that it returns a String. If the new price is negative, do not update the price and return a String with the following value: ALERT: Unable to set 's price to (negative values are not allowed) Where is the name of the frozen yogurt and is the parameter's value. If the price is 100.0 or greater, do not update the price and return a String with the following value: ALERT: Unable to set 's price to (value is too high (>=100.00)) Otherwise, return an empty string.

3.Change setYogurtFlavor method so that it returns a String. If the new flavor is not "chocolate" nor "vanilla", do not update the base flavor and return a String with the following value: ALERT: is not one of the valid yogurt flavors (chocolate or vanilla)! Where is the value of the parameter. If the new flavor is valid, then return an empty string.

4.Add an addTopping method that take a String as a parameter. If the instance variable already has a topping, then separate the previous value and the new value with ", ". Otherwise, just use the new topping as the value (without a ","). For each additional topping added, add $0.49 to the price.

import java.util.Scanner;

public class FrozenYogurt{

//private instance variables

private String name;

private double price;

private String flavor;

private String topping;

//constructor with no parameters

public FrozenYogurt(){

this.name = "";

this.price = 0.0;

this.flavor = "";

this.topping = "";

}

// constructor with parameters

public FrozenYogurt(String name, double price, String flavor, String topping){

this.name = name;

this.price = price;

this.flavor = flavor;

this.topping = topping;

}

// mutator methods

public void setName(String name){

this.name = name;

}

public void setPrice(double price){

this.price = price;

}

public void setYogurtFlavor(String flavor){

this.flavor = flavor;

}

public void setTopping(String topping){

this.topping = topping;

}

//Accessor methods

public String getName(){

return name;

}

public double getPrice(){

return price;

}

public String getYogurtFlavor(){

return flavor;

}

public String getTopping(){

return topping;

}

//toString method

public String toString(){

return "Frozen Yogurt: " + name + " Price: $" + price + " Yogurt: " + flavor + " Toppping: " + topping + " ";

}

//User inputs

public String valueEntered(String s){

return "You entered: " + s + " ";

}

}

//driver class

import java.util.Scanner;

public class FrozenYogurtDriver

{

public static void main(String[] args) {

//Scanner class

Scanner sc = new Scanner(System.in);

//object creation from FrozenYogurt

FrozenYogurt froyo1 = new FrozenYogurt();

FrozenYogurt froyo2 = new FrozenYogurt();

System.out.println("Welcome to the Frozen Yogurt Selector");

//User inputs for object

System.out.print("Please enter the name of the first frozen yogurt: ");

froyo1.setName(sc.nextLine());

System.out.println(froyo1.valueEntered(froyo1.getName()));

System.out.print("Please enter the name of the second frozen yogurt: ");

froyo2.setName(sc.nextLine());

System.out.println(froyo2.valueEntered(froyo2.getName()));

//User inputs for prices

System.out.print("Please enter the price for the "+ froyo1.getName() + ": ");

froyo1.setPrice(Double.parseDouble(sc.nextLine()));

System.out.println(froyo1.valueEntered(String.valueOf(froyo1.getPrice())));

System.out.print("Please enter the price for the "+ froyo2.getName() + ": ");

froyo2.setPrice(Double.parseDouble(sc.nextLine()));

System.out.println(froyo2.valueEntered(String.valueOf(froyo2.getPrice())));

//User inputs for flavors

System.out.print("Please enter the base yogurt flavor for " + froyo1.getName() +": ");

froyo1.setYogurtFlavor(sc.nextLine());

System.out.println(froyo1.valueEntered(froyo1.getYogurtFlavor()));

System.out.print("Please enter the base yogurt flavor for " + froyo2.getName() +": ");

froyo2.setYogurtFlavor(sc.nextLine());

System.out.println(froyo2.valueEntered(froyo2.getYogurtFlavor()));

//User inputs for toppings

System.out.print("Please enter the topping to add to " + froyo1.getName() +": ");

froyo1.setTopping(sc.nextLine());

System.out.println(froyo1.valueEntered(froyo1.getTopping()));

System.out.print("Please enter topping to add to " + froyo2.getName() +": ");

froyo2.setTopping(sc.nextLine());

System.out.println(froyo2.valueEntered(froyo2.getTopping()));

//display toSting

System.out.println(froyo1);

System.out.println(froyo2);

//Final message

System.out.print("Thank you!");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

2. How do business reports differ from business letters?

Answered: 1 week ago

Question

13-6 How will MIS help my career?

Answered: 1 week ago

Question

13-4 What are alternative methods for building information systems?

Answered: 1 week ago