Apple Maker objective: Write a program that creates a class Apple and a tester t
ID: 3797077 • Letter: A
Question
Apple Maker objective: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. First create a class called Apple Write a class file called Apple that DOES NOT HAVE a main method Some of the attributes of Concert are Type A string that describes the apple. It may only be of these following types Red Delicious Golden Delicious Gala Granny Smith Weight: A decimal value representing the apple weight in kilograms. The weight must be between 0kg and 2kg o Price: The price per apple. This must be a non-negative decimal value. Create the following Constructors o Default sets everything to default values and has no parameters Accessors and Mutators for each variable MAKE SURE THE MUTATORS CHECK FOR VALID VALUES Create the following Methods o toString Retums a string with all of the instance variable values Ex: "Name:Explanation / Answer
// Apple.java
public class Apple
{
private String description;
private double appleWeight;
private double appleprice;
//Default apple values (Constructors)
public Apple ()
{
this.description = "Gala";
this.appleWeight = 0.5;
this.appleprice = 0.89;
}
//Accessors
public String getDescription()
{
return this.description;
}
public double getAppleWeight()
{
return this.appleWeight;
}
public double getApplePrice()
{
return this.appleprice;
}
//Mutators
public void setDescription (String des)
{
if (des.equalsIgnoreCase("Red Delicious") == false && des.equalsIgnoreCase("Golden Delicious") == false && des.equalsIgnoreCase("Gala") == false && des.equalsIgnoreCase("Granny Smith") == false)
{
System.out.println ("Invalid name");
return;
}
description = des;
}
public void setAppleWeight (double weight)
{
if (weight < 0 || weight > 2)
{
System.out.println("Invalid Weight");
return;
}
appleWeight = weight;
}
public void setApplePrice (double price)
{
if (price < 0)
{
System.out.println("Invalid Price");
return;
}
appleprice = price;
}
public String toString()
{
return "Name: " + description + " Weight: " + appleWeight + " ApplePrice: " + appleprice + " ";
}
public boolean equals (Apple other)
{
return this.description.equalsIgnoreCase(other.getDescription()) && this.appleWeight == other.getAppleWeight() && this.appleprice == other.getApplePrice();
}
}
// AppleTester.java
public class AppleTester
{
public static void main(String[] args)
{
System.out.println("Welcome to Apple Tester ");
System.out.println("Creating a default Apple Printing the default apple's value");
Apple apple1 = new Apple();
System.out.println(apple1.toString() + " ");
System.out.println("Setting the new apple's values to the following valid values: 'Granny Smith' 0.75 0.99 Printing the new apple's values");
Apple apple2 = new Apple();
apple2.setDescription("Granny Smith");
apple2.setAppleWeight (0.75);
apple2.setApplePrice (0.99);
System.out.println(apple2.toString() + " ");
System.out.println("Creating another apple Setting the new apple's values to the following valid values: 'iPad' 2.5 -200");
Apple apple3 = new Apple();
apple3.setDescription("iPad");
apple3.setAppleWeight (2.5);
apple3.setApplePrice (-200);
apple3 = apple1;
System.out.println("Printing the apple's value which should not have changes from the default values");
System.out.println(apple3.toString());
}
}
/*
output:
Welcome to Apple Tester
Creating a default Apple
Printing the default apple's value
Name: Gala Weight: 0.5 ApplePrice: 0.89
Setting the new apple's values to the following valid values: 'Granny Smith' 0.75 0.99
Printing the new apple's values
Name: Granny Smith Weight: 0.75 ApplePrice: 0.99
Creating another apple
Setting the new apple's values to the following valid values: 'iPad' 2.5 -200
Invalid name
Invalid Weight
Invalid Price
Printing the apple's value which should not have changes from the default values
Name: Gala Weight: 0.5 ApplePrice: 0.89
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.