I need it by drjava program Lab 13 Apple Maker Objective: Write a program that c
ID: 3590827 • Letter: I
Question
I need it by drjava program
Lab 13
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 Apple areType: 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’s weight in kilograms. The weight must be between 0kg and 2kg
Price: The price per apple. This must be a non-negative decimal value.
Create the following Constructors
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 MethodstoString: Returns a string with all of the instance variable values
Ex: “Name: <<apple’s name>> Weight <<apple’s weight>> Price <<apple’s price>>”
equals: This method returns a Boolean and takes in another instance of an apple. It returns true if all of the instance variables equal the instance variables in the other apple
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.
Example Dialog:
Welcome to the apple tester
Creating a default apple
Printing the default apple’s values
Name: Gala Weight: 0.5 Price: 0.89
Creating another apple
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 Price:0.99
Creating another apple
Setting the new apple’s values to the following invalid values “iPad” 2.5 -200
Invalid Name
Invalid Weight
Invalid Price
Printing the apple’s values which should have not changed from the default values
Name: Gala Weight: 0.5 Price 0.89
Explanation / Answer
Apple.java
public class Apple {
//Declaring instance variables
private String type;
private double weight;
private double price;
//Zero argumented constructor
public Apple() {
this.type = "Gala";
this.weight = 0.5;
this.price = 0.89;
}
//Parameterized constructor
public Apple(String type, double weight, double price) {
super();
setType(type);
setWeight(weight);
setPrice(price);
}
//getters and setters
public String getType() {
return type;
}
public void setType(String type) {
if (type.equalsIgnoreCase("Red Delicious") || type.equalsIgnoreCase("Golden Delicious") || type.equalsIgnoreCase("Gala") || type.equalsIgnoreCase("Granny Smith")) {
this.type = type;
} else {
System.out.println("Invalid Name");
this.type = "Gala";
}
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
if (weight >= 0 && weight <= 2) {
this.weight = weight;
} else {
System.out.println("Invalid Weight");
this.weight = 0.5;
}
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if (price >= 0) {
this.price = price;
} else {
System.out.println("Invalid Price");
this.price = 0.89;
}
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Name :" + type + " Weight:" + weight + " Price:" + price;
}
}
____________________
AppleTester.java
public class AppleTester {
public static void main(String[] args) {
//Creating an Apple class object by using the default constructor
Apple a1 = new Apple();
//Displaying the apple lass info
System.out.println(a1.toString());
//Creating an Apple class object by using the parameterized constructor
Apple a2 = new Apple("Granny Smit", 0.75, 0.99);
//Displaying the apple lass info
System.out.println(a2.toString());
//Creating an Apple class object by using the parameterized constructor
Apple a3 = new Apple("iPad", 2.5, -200);
//Displaying the apple lass info
System.out.println(a3.toString());
}
}
___________________
Output:
Name :Gala Weight:0.5 Price:0.89
Invalid Name
Name :Gala Weight:0.75 Price:0.99
Invalid Name
Invalid Weight
Invalid Price
Name :Gala Weight:0.5 Price:0.89
_____________Could you rate me well.Plz .Thank You