Question

In: Computer Science

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

Solutions

Expert Solution

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-

  • javac AppleTester.java
  • java AppleTester

Make sure the filename is Appletester.java


Related Solutions

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...
use eclipse give me java codes Task III: Write a classCarInsurancePolicy The CarInsurancePolicy class will...
use eclipse give me java codes Task III: Write a class CarInsurancePolicy The CarInsurancePolicy class will describe an insurance policy for a car. 1. The data members should include all the data members of an Insurance Policy, but also the driver’s license number of the customer, whether or not the driver is considered a “good” driver (as defined by state law) , and the car being insured (this should be a reference to a Car object -- write a separate...
Write a Java Program to determine if a candidate qualifies for a FHA Mortgage. (using eclipse...
Write a Java Program to determine if a candidate qualifies for a FHA Mortgage. (using eclipse IDE) The qualifications given are: Residence: The home must be your primary residence. Down payment: They must have a credit score of at least 563. The Java Program needs to do the following. This can all be done in the main() method. Create a boolean variable to store the Residence requirement. Prompt the user to "Enter 1 if the Home will be the primary...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a postfix form, and then computes its value (by using stack-based algorithms). Assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.
Write a java program that creates a hashtable with 10 objects and 5 data members using...
Write a java program that creates a hashtable with 10 objects and 5 data members using mutator and accessor methods for each data member.
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class •...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class • uses proper design techniques including reading UML Class Diagrams • reads input from the keyboard using a Scanner Object and its methods • uses selection (if and if else) statements • uses iteration (while, do while or for) statements • uses String comparison methods. • follows standard acceptable programming practices. • handles integer overflow errors Prep Readings: Zybooks chapter 1 - 6 1. General...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT