Apple Maker objective: Write a program that creates a class Apple and a tester t
ID: 3797176 • Letter: A
Question
Apple Maker 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 Red Delicious Golden Delicious Gala Granny Smith o Weight: A decimal value representing the apple 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 following Constructors o Default sets everything to default values and has no parameters Accessors and Mutators for each variable o MAKE SURE THE MUTATORS CHECK FOR VALID VALUES! Create the following Methods o toString Retums a string with all of the instance variable values Ex: "Name:Explanation / Answer
import java.util.*;
class Apple
{
private String Type;
private double Weight;
private double Price;
public Apple() //default constructor
{
Type = " ";
Weight = 0.0;
Price = 0.0;
}
public Apple(String Type,double Weight,double Price) //parameter constructor
{
this.Type = Type;
if(Type != "Red Delicious" && Type != "Golden Delicious" && Type != "Gala" && Type != "Granny Smith")
{
System.out.println("Invalid Name");
}
this.Weight = Weight;
if(Weight <0 || Weight > 2)
{
System.out.println("Invalid Weight");
}
this.Price = Price;
if(Price < 0)
{
System.out.println("Invalid Price");
}
}
//set and get methods
public void setType(String Type)
{
this.Type = Type;
if(Type != "Red Delicious" && Type != "Golden Delicious" && Type != "Gala" && Type != "Granny Smith")
{
System.out.println("Invalid Name");
}
}
public void setWeight(double Weight)
{
this.Weight = Weight;
if(Weight <0 || Weight > 2)
{
System.out.println("Invalid Weight");
}
}
public void setPrice(double Price)
{
this.Price = Price;
if(Price < 0)
{
System.out.println("Invalid Price");
}
}
public String getType()
{
return Type;
}
public double getWeight()
{
return Weight;
}
public double getPrice()
{
return Price;
}
public String toString() //override toString()
{
return "Name : "+Type +" Weight : "+Weight +" Price : "+Price;
}
public boolean Equals(Apple obj) //override Equals()
{
if(this.Type == obj.Type && this.Weight == obj.Weight && this.Price == obj.Price)
return true;
else
return false;
}
}
class AppleTester
{
public static void main (String[] args)
{
System.out.println("Welcome to the apple tester");
System.out.println(" Creating a default apple");
Apple app1 = new Apple("Gala",0.5,0.89);
System.out.println("printing the default apple's value");
System.out.println(app1.toString());
System.out.println(" Creating another apple");
Apple app2 = new Apple();
app2.setType("Granny Smith");
app2.setWeight(0.75);
app2.setPrice(0.99);
System.out.println("printing the default apple's value");
System.out.println(app2.toString());
System.out.println(" Creating another apple");
Apple app3 = new Apple();
app2.setType("ipad");
app2.setWeight(2.5);
app2.setPrice(-200);
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.