I need help inputing the following compiling the following JAVA code in NETBEANS
ID: 2247136 • Letter: I
Question
I need help inputing the following compiling the following JAVA code in NETBEANS. it is for How to program Java tenth edition chapter 3.
Create a Candy class
Instance variables: name (String), price (double), quantity (int)
Constructor – take a String to set the name
Get and set methods for each instance variables
If attempt to set quantity to a negative number, print a message and set it to 1
display() will print the data in the format name – quantity at price
Print price with 2 decimal places
getCost() – returns quantity * price
TestCandy class should contain main()
Display the Candy object
Prompt for the name, price, quantity (all on separate lines)
Use nextLine for name
Set the data in a Candy object
Display the Candy object
Display the cost of for the Candy object
Requirements:
Put System.out.println() after you finish each call to a next Scanner methodThis will help you match the Test Program and make it easier to use it.
Sample Run #1: (the highlighted text is what the user types)
Default: Kit Kat - 0 at 0.00
Enter new name: Reese
Price: 1.50
Quantity: 2
Updated: Reese - 2 at 1.50
Total cost = $3.00
Sample Run #2: (the highlighted text is what the user types)
Default: Kit Kat - 0 at 0.00
Enter new name: Snickers
Price: 2.75
Quantity: -3
Sorry no negative quantities -- setting to 1
Updated: Snickers - 1 at 2.75
Total cost = $2.75
Extra Notes:
Did you correctly name the package/folder?
Did you correctly name the class/file?
Did you include comments?
Explanation / Answer
Candy.java
package candy;
public class Candy {
private String name;
private double price;
private int quantity;
public Candy(String name) {
super();
this.name = name;
}
public Candy() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
if (quantity<0){
System.out.println("Sorry no negative quantities -- setting to 1");
this.quantity = 1;
}
else{
this.quantity = quantity;
}
}
public double getcost(Candy c){
return quantity * price;
}
public void display(Candy c){
System.out.println("Updated: " + c.name + " at " + c.price );
System.out.println("Total Cost = $" + getcost(c) );
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------
TestCandy.java
package candy;
import java.util.Scanner;
public class TestCandy {
public static void main(String[] args) {
// TODO Auto-generated method stub
Candy c = new Candy();
Scanner sc = new Scanner(System.in);
System.out.println("Enter new name:");
c.setName(sc.nextLine());
System.out.println("Price:");
c.setPrice(sc.nextDouble());
System.out.println("Quantity:");
c.setQuantity(sc.nextInt());
c.display(c);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.