Question

In: Computer Science

Task: Write a program that creates a class Apple and a tester to make sure the...

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 non­negative 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.

Solutions

Expert Solution

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


Related Solutions

Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
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...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
Write a program that creates a Singleton class to connect to two databases. The program must...
Write a program that creates a Singleton class to connect to two databases. The program must provide two instances of this class. One instance for connecting to MySQL database and the other for connecting to Oracle database.
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
Write a program to have a Car class which is comparable. Make the Car class comparable...
Write a program to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling a static...
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint:...
C++ questions, please make sure to dividet he program into functions which perform each major task,...
C++ questions, please make sure to dividet he program into functions which perform each major task, A leap year is defined as any calendar year which meets the following criteria: If the year is not divisible by 4, it is a common year If the year is not divisible by 100, it is a leap year If the year is not divisible by 400, it is a common year Otherwise it is a leap year For your program you will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT