Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester...

80.2K

Verified Solution

Question

Programming

Using Java preferably with Eclipse

Task:

Write a program that creates a class Apple and a tester to makesure 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 onlybe of the following types:

 Red Delicious

 Golden Delicious

 Gala

 Granny Smith

Weight: A decimal value representing the apple’s weight inkilograms.  The weight must be between 0kg and 2kg

Price: The price per apple.  This must be anon­negative decimal value

Create the Default Constructor – sets everything to defaultvalues 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 onthe 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 asintended.

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 changedfrom 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

Answer & Explanation Solved by verified expert
3.6 Ratings (294 Votes)
The code for Apple Class and Appletester class is given belowwith comments import javautilclass Apple String type double weight double price AppleDefault constructor type Gala weight 05 price 088 Accessor methods public String getType return type public double getWeight return weight public double getPrice return price Mutator methods public void setTypeString type iftypeequalsRed Delicious typeequalsGolden Delicious typeequalsGala typeequalsGranny Smith thistype type else    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students