In: Computer Science
Task:
Write a program that creates a class Apple and a tester to make
sure the Apple class is crisp
and delicious.
Instructions:
First create a class called Apple
The class Apple DOES NOT HAVE a main method
Some of the attributes of Apple are
o Type: A string that describes the apple. It may only be of the
following types:
Red Delicious
Golden Delicious
Gala
Granny Smith
o Weight: A decimal value representing the apple’s weight in
kilograms. The
weight must be between 0kg and 2kg
o Price: The price per apple. This must be a nonnegative decimal
value.
Create the Default Constructor – sets everything to default
values and has no
parameters.
Create Accessors and Mutators for each instance variable
o MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following Methods
o toString() : Displays the values of the instance variables on the
console.
Finally create a class called AppleTester
This class DOES HAVE a main method
Create at least 3 different types of apples
Test if the accessors, mutators, and other methods work as
intended.
Sample Output:
Welcome to the apple tester!!!
Creating the first apple!
Default values of the first apple object:
Type: Gala
Weight: 0.5 kg
Price: $0.88
Creating the second apple object!
Enter the type of the second apple object:
Granny Smith
Enter the weight of the second apple object:
0.7
Enter the price of the second apple object:
1.45
Values of the second apple object:
Type: Granny Smith
Weight: 0.7 kg
Price: $1.45
Creating the third apple object!
Enter the type of the third apple object:
Banana
Invalid value for type!
Enter the weight of the third apple object:
3.5
Invalid value for weight!
Enter the price of the third apple object:
2.22
Invalid value for price!
Printing the third apple’s values which should have not changed
from the default values
Retrieving the third apple object's type: Gala
Retrieving the third apple object's weight:0.5 kg
Retrieving the third apple object's price:$0.88
Note: may you copy and paste the code once you are done, thanks,
also may you complete it within 4 hours please.
Immediate upvote.
Below is the source code for the two classes:
1. Apple class:
public class Apple
{
private String type;
private double weight;
private double price;
//default constructor
public Apple ()
{
this.type = "Gala";
this.weight = 0.5;
this.price = 0.88;
}
//getters
public String getType()
{
return this.type;
}
public double getWeight()
{
return this.weight;
}
public double getPrice()
{
return this.price;
}
//Setters
public void setType (String des)
{
System.out.println("des: " +
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 value for type!");
return;
}
type = des;
}
public void setWeight (double weight)
{
if (weight < 0 || weight >
2)
{
System.out.println("Invalid value for weight!");
return;
}
this.weight = weight;
}
public void setPrice (double price)
{
if (price < 0)
{
System.out.println("Invalid value for price!");
return;
}
this.price = price;
}
public String toString()
{
return "Type: " + this.type +
"\nWeight: " + this.weight + " kg\nPrice: $" + this.price +
"\n";
}
}
2. AppleTester Class
import java.util.Scanner;
public class AppleTester
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Apple Tester!!!");
System.out.println("Creating a first Apple!");
Apple apple1 = new Apple();
System.out.println("Default values of the first apple
object:");
System.out.println(apple1.toString());
System.out.println("Creating the second apple object!");
Apple apple2 = new Apple();
System.out.println("Enter the type of the second apple
object:");
apple2.setType(sc.nextLine());
System.out.println("Enter the weight of the second apple
object:");
apple2.setWeight (sc.nextDouble());
System.out.println("Enter the price of the second apple
object:");
apple2.setPrice (sc.nextDouble());
System.out.println("Values of the second apple object:");
System.out.println(apple2.toString());
System.out.println("Creating the third apple object!");
Apple apple3 = new Apple();
System.out.println("Enter the type of the third apple
object:");
apple3.setType(sc.next());
System.out.println("Enter the weight of the third apple
object:");
apple3.setWeight (sc.nextDouble());
System.out.println("Enter the price of the third apple
object:");
apple3.setPrice (sc.nextDouble());
System.out.println("Printing the third apple's value which should
not have changes from the default values");
System.out.println("Retrieving the third apple object's type: " +
apple3.getType());
System.out.println("Retrieving the third apple object's weight: " +
apple3.getWeight() + " kg");
System.out.println("Retrieving the third apple object's price: $" +
apple3.getPrice());
sc.close();
}
}
Note:
There is a mistake in the sample output as below:
Enter the price of the third apple object:
2.22
Invalid value for price!
Here the value 2.22 cannot be an invalid price because
the price should be a nonnegative decimal number as per the given
question. Assuming the price value given for the third object as
-2.22, I have generated the code.
Below is the execution output:
Please comment for any clarifications. Thanks