Question

Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create...

Part I:

Problem: Code the following in Java:

Candy Class

-price

-number of pounds sold

Create the following methods:

-Two constructors – default and second

- accessor and mutator methods for each of the fields. - check for invalid price. Negative price is invalid (use if then

else and display error message)

- a method named userInput to get user input for all the fields. - check for invalid price. Negative price is invalid

(use a loop to prompt the user to enter correct price)

- method name displayAll to display all attributes

- a method named calcCashCollected to calculate and return the amount of cash collected by the selling the candy

– which is calculated by multiplying the price and number of pounds sold

-a method named calcDiscountPrice to calculate and return the discounted price of the candy. (application will send discount rate via parameter).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Candy.java

import java.util.Scanner;

public class Candy {
private double price;
private int poundsSold;
  
public Candy()
{
this.price = 0.0;
this.poundsSold = 0;
}
  
public Candy(double price, int poundsSold)
{
this.price = price;
this.poundsSold = poundsSold;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
if(price < 0)
System.out.println("Price cannot be negative!");
else
this.price = price;
}

public int getPoundsSold() {
return poundsSold;
}

public void setPoundsSold(int poundsSold) {
if(poundsSold < 0)
System.out.println("Pounds sold value cannot be negative!");
else
this.poundsSold = poundsSold;
}
  
public void userInput()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the price of the candy: $");
double candyPrice = Double.parseDouble(sc.nextLine().trim());
while(candyPrice < 0)
{
System.out.print("Invalid Input!\nEnter the price of the candy: $");
candyPrice = Double.parseDouble(sc.nextLine().trim());
}
  
System.out.print("Enter number of pounds candy sold: ");
int pSold = Integer.parseInt(sc.nextLine().trim());
while(pSold < 0)
{
System.out.print("Invalid Input!\nEnter number of pounds candy sold: ");
pSold = Integer.parseInt(sc.nextLine().trim());
}
  
setPrice(candyPrice);
setPoundsSold(pSold);
}
  
public void displayAll()
{
System.out.println("Price of candy: $" + String.format("%.2f", getPrice())
+ "\nNumber of pounds of candy sold: " + getPoundsSold());
}
  
public double calcCashCollected()
{
return(getPrice() * getPoundsSold());
}
  
public double calcDiscountPrice(double discountPercent)
{
return((discountPercent / 100) * calcCashCollected());
}
}

CandyClassTester.java (Main class)

public class CandyClassTester {
  
public static void main(String[] args)
{
Candy candy = new Candy();
  
candy.userInput();
  
System.out.println();
candy.displayAll();
  
System.out.println("Total cash collected: $" + String.format("%.2f", candy.calcCashCollected()) + "\n"
+ "Discount at 15% : " + String.format("%.2f", candy.calcDiscountPrice(15)) + "\n"
+ "Final price to pay: $"
+ String.format("%.2f", candy.calcCashCollected() - candy.calcDiscountPrice(15)));
}
}

************************************************************* SCREENSHOT **************************************************************

Add a comment
Know the answer?
Add Answer to:
Part I: Problem: Code the following in Java: Candy Class -price -number of pounds sold Create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Create the Python code for a program adhering to the following specifications. Write an Employee class...

    Create the Python code for a program adhering to the following specifications. Write an Employee class that keeps data attributes for the following pieces of information: - Employee Name (a string) - Employee Number (a string) Make sure to create all the accessor, mutator, and __str__ methods for the object. Next, write a class named ProductionWorker that is a subclass of the Employee class. The ProductionWorker class should keep data attributes for the following information: - Shift number (an integer,...

  • JAVA HELP Design a class named Employee. The class should keep the following information in fields:...

    JAVA HELP Design a class named Employee. The class should keep the following information in fields: Employee name Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. Hire date then, Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to...

  • Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main()...

    Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main() method. Within the same file, create another class named Employee, and do not declare it public. Create a field for each of the following pieces of information: employee name, employee number, hourly pay rate, and overtime rate. Create accessor and mutator methods for each field. The mutator method for the hourly pay rate should require the value to be greater than zero. If it...

  • C++ Visul Studio Create a class named Vehicle. The class has the following five member variables:...

    C++ Visul Studio Create a class named Vehicle. The class has the following five member variables: • Vehicle Name • Vehicle number • Sale Tax • Unit price • Total price Include set (mutator) and get (accessor) functions for each field except the total price field. The set function prompt the user for values for each field. This class also needs a function named computePrice() to compute the total price (quantity times unit price + salesTax) and a function to...

  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...

  • JAVA PLEASE Create a class called Account with the following instance data Integer id Double balance...

    JAVA PLEASE Create a class called Account with the following instance data Integer id Double balance Provides the following methods Default constructor (defaults balance to 100) Constructor with parameters for the ID and the starting balance Accessor and mutator methods for id and balance Method to perform a withdrawal Method to perform a deposit Create a class called Bank which has an array of Account objects. This class will manage the accounts. It must provide methods Do withdrawal Do deposits...

  • Using JAVA* Design a class named Person with fields for holding a person’s name, address, and...

    Using JAVA* Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator...

  • (To be written in Java code) Create a class named CollegeCourse that includes the following data...

    (To be written in Java code) Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display()...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT