In: Computer Science
Using Java preferably with Eclipse
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
Type: A string that describes the apple. It may only be of the 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 nonnegative decimal value
Create the Default Constructor – sets everything to default values and has no parameters
Create Accessors and Mutators for each instance variable
MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
Create the following Methods
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
The code for Apple Class and Appletester class is given below with comments -
import java.util.*;
class Apple
{
String type;
double weight;
double price;
Apple()//Default constructor
{
type = "Gala";
weight = 0.5;
price = 0.88;
}
//Accessor methods
public String getType()
{
return type;
}
public double getWeight()
{
return weight;
}
public double getPrice()
{
return price;
}
//Mutator methods
public void setType(String type)
{
if(type.equals("Red Delicious") || type.equals("Golden Delicious") || type.equals("Gala") || type.equals("Granny Smith"))
this.type = type;
else
System.out.println("Invalid value for type!");
}
public void setWeight(double weight)
{
if(weight>0 && weight<=2)
this.weight = weight;
else
System.out.println("Invalid value for weight!");
}
public void setPrice(double price)
{
if(price>0)
this.price = price;
else
System.out.println("Invalid value for price!");
}
//toString method
public String toString()
{
return ("Type: " + type + "\nWeight: " + weight + " kg\nPrice: $" + price);
}
}
//AppleTester class
public class AppleTester
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//Creating 3 different apple objects and inputting values
System.out.println("Welcome to the Apple Tester!");
System.out.println("Creating the first apple!");
Apple apple1 = new Apple();
System.out.println("Default values of the first apple object:");
System.out.println(apple1);
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);
//Clear extra carriage character.
sc.nextLine();
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.nextLine());
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("Values of the third apple object:");
System.out.println(apple3);
}
}
Code Screenshot-
Explanation - The Apple class is self-explanatory where all the accessor and mutator functions have been added which check the conditions given. If conditions do not match, the default value which is set by the constructor is not changed.
AppleTester class I have tried to replicate the given input as in the question and create 3 apple objects and take inputs and set the apple attributes as per the input given.
OUTPUT Screenshot -
The code can be run by compiling the program and running it using these 2 commands-
Make sure the filename is Appletester.java