Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Dr. Java Objective: Write a program that creates a class Apple and a teste

ID: 3797147 • Letter: U

Question

Using Dr. Java

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 Concert are

Type: 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 Methods

toString: 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 Class

import java.util.ArrayList;

public class Apple {

   private String Type;
   private float Weight;
   private float Price;
   private ArrayList<String> appleType;
   private boolean isValid = true;

   public Apple(){

       this.Type = "Gala";
       this.Weight = 0.5f;
       this.Price = 0.89f;

       appleType = new ArrayList<String>();
       appleType.add("Red Delicious");
       appleType.add("Golden Delicious");
       appleType.add("Gala");
       appleType.add("Granny Smith");


   }

   public String getType() {
       return Type;
   }

   public void setType(String type) {
       if(!appleType.contains(type)){
           System.out.println("Invalid Name");
           isValid = false;
       }
       else
           Type = type;
   }

   public float getWeight() {
       return Weight;
   }

   public void setWeight(float weight) {
       if(weight > 0 && weight < 2){
           Weight = weight;
       }
       else{
           System.out.println("Invalid Weight");
           isValid = false;
       }
   }

   public float getPrice() {
       return Price;
   }

   public void setPrice(float price) {
       if(price > 0){
           Price = price;
       }
       else{
           System.out.println("Invalid Price");
           isValid = false;
       }
   }

   public String toString(){
       return "Name: " + Type + " Weight: " + Weight + " Price: " + Price;
   }

   public boolean equals(Apple newApple){
       if(newApple.getType().equalsIgnoreCase(Type) && (newApple.getWeight() == Weight) && (newApple.getPrice() == Price))
           return true;
       else
           return false;


   }

   public boolean isValid() {
       return isValid;
   }

}

-----------------------------------------------------------------------------------------------------------------------------------------------

AppleTester Class

public class AppleTester {

   public static void main(String[] args) {
      
       System.out.println("Welcome to the apple tester" + " ");
      
       // Default apple
       System.out.println("Creating a default apple");
      
       Apple defaultApple = new Apple();
      
       System.out.println("Printing the default apple's values");
       System.out.println(defaultApple.toString() + " ");
      
       // Another apple
       System.out.println("Creating another apple");
       Apple secApple = new Apple();
       System.out.println("Setting the new apple's values to the following"
               + " valid values "Granny Smith" 0.75 0.99");
       secApple.setType("Granny Smith");
       secApple.setWeight(0.75f);
       secApple.setPrice(0.99f);
      
       System.out.println("Printing the new apple's values");
       System.out.println(secApple.toString() + " ");
      
       // Another apple with invalid values
       System.out.println("Creating another apple");
       Apple thirdApple = new Apple();
      
       System.out.println("Setting the new apple's values to the following"
               + " valid values "iPad" 2.5 -200");
       thirdApple.setType("iPad");
       thirdApple.setWeight(2.5f);
       thirdApple.setPrice(-200f);
      
       if(!thirdApple.isValid()){
           thirdApple = new Apple();
          
           System.out.println("Printing the apple's values which should have not changed from the default values");
           System.out.println(thirdApple.toString() + " ");
       }else{
           System.out.println("Printing the new apple's values");
           System.out.println(thirdApple.toString() + " ");
       }
      
      
      

   }

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote