Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modification to the following script to add encapsulation to it to include making all attributes private, adding constructor, and adding get and set methods. The

Modification to the following script to add encapsulation to it to include making all attributes private, adding constructor, and adding get and set methods. The main method should create instance of the class and demonstrate the correct functionality of all the methods:

package Wk1AfetseM;

import java.util.Scanner;

public class Wk1AfetseM { public static class Cookie { public String brand; public String type; public double price; public int numofCookies = 0; //default constructor public Cookie() { this.brand = "Gourmet Nestle"; this.type = "Chocolate Chip"; this.price = 4;

} //parameterized constructor public Cookie(String brand, String type, int price) { this.brand = brand; this.type = type; this.price = price; } //set methods public void setBrand(String brand) { this.brand = brand; } public void setType(String type) { this.type = type; } public void setPrice(double price) { System.out.println(" *** Gourmet Cookie Sale!! Price changed successfully from $" +String.format("%.2f", this.price) + " dollars to $" +String.format("%.2f", price) + " dollars. ***"); this.price = price; } //access methods public String getBrand() { return this.brand; } public String gettype() { return this.type; } public double getPrice() { return this.price; } //method for cost calculation public static double totalCost(int numofCookies, double price) { return (numofCookies * price); } }

public static void main(String[] args) { Scanner input = new Scanner(System.in); //prompt user for number of cookies they would like to bake in the oven System.out.print("Please Enter in the Number of Gourmet Nestle Chocolate Chip Cookies you would like to Order: "); int numofCookies = input.nextInt(); //create instance for the cookies Cookie cookie = new Cookie(); //information displayed System.out.println(" Brand: " + cookie.brand); System.out.println(" Type: " + cookie.type); System.out.println(" Original Price: $"+String.format("%.2f", cookie.price)); //update the price cookie.setPrice(2); System.out.println(" Gourmet Nestle Chocolate Chip Cookie Sale New Price: $"+String.format("%.2f", cookie.price)); System.out.println(" => Total Number of Gourmet Nestle Chocolate Chip Cookies Ordered: " + numofCookies); System.out.println(" ==> Total Cost for the Gourmet Nestle Chocoalte Chip Cookies Ordered is $" + String.format("%.2f", Cookie.totalCost(numofCookies, cookie.price)) + " dollars. "); input.close(); } }

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

Step: 3

blur-text-image

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

3. What sales techniques does Ann use when she closes?

Answered: 1 week ago