Question

In: Computer Science

(Java Problem) Create a Produce class that have an instance variable of type String for the...

(Java Problem)

Create a Produce class that have an instance variable of type String for the

name, appropriate constructors, appropriate accessor and mutator methods,

and a public toString() method. Then create a Fruit and a Vegetable class

that are derived from Produce. These classes should have constructors that

take the name as a String, the price (this is the price per box) as a double,

the quantity as an integer, and invoke the appropriate constructor from the

base class to set the name. Also, they should override toString method to

display the name of the produce, the price, and its type. For instance, Mango

is a Fruit and Cauliflower is a Vegetable.

Finally, create a class called TruckOfProduce that will keep track of the boxes

of Vegetables and Fruits added in to the truck. This class should use an array

of Produce to store both vegetables and fruits. Also, it should have the

following:

Constructor

that accepts an integer

to initialize the array of Produce

addProduce

method that

adds either fruit or vegetable

to the array

search

method that accepts

a name string

, which can be either the

name of a fruit or vegetable, and returns true if the name exists.

Otherwise, it returns false.

remove

method that accepts

a produce object

and returns true if the

produce is found and removed successfully. Otherwise, it returns false.

computeTotal

method that will return the total cost of all the produce

in the truck.

toString

method that returns all the produce from in the truck.

Solutions

Expert Solution

Here is the solution for your answer. I have also created main() for better understanding and it is very much user interactive. Also, Comments has been given where you can find some code miscellaneous. Still if you have any doubt feel free to ask.

/*file: TruckOfProduce.java*/

import java.util.*;

class Produce

{

    private String name;

    public Produce(String s) {

        name=s;

    }

    public String getName(){

        return name;

    }

    public void setName(String s){

        name=s;

    }

    public String toString(){

        return ("Name is: "+name);

    }

    /*following methods are added because base class refernce was unable to figure out these methods in derived class*/

    public int getQuantity()

    {

        return 0;

    }

    public double getPrice()

    {

        return 0;

    }

    public void setQuantity(int quantity)

    {

        return;

    }

    public void setPrice(double price)

    {

        return;

    }

}

class Fruits extends Produce

{

    private double price;

    private int quantity;

    public Fruits(String s,double price,int quantity)

    {

        super(s);

        this.price=price;

        this.quantity=quantity;

    }

    public int getQuantity()

    {

        return quantity;

    }

    public double getPrice()

    {

        return price;

    }

    public void setQuantity(int quantity)

    {

        this.quantity=quantity;

    }

    public void setPrice(double price)

    {

        this.price=price;

    }

    public String toString()

    {

        return (super.getName()+" is a fruit. It cost is: "+price+" per box.");

    }

}

class Vegetables extends Produce

{

    private double price;

    private int quantity;

    public Vegetables(String s,double price,int quantity)

    {

        super(s);

        this.price=price;

        this.quantity=quantity;

    }

    public int getQuantity()

    {

        return quantity;

    }

    public double getPrice()

    {

        return price;

    }

    public void setQuantity(int quantity)

    {

        this.quantity=quantity;

    }

    public void setPrice(double price)

    {

        this.price=price;

    }

    public String toString()

    {

        return (super.getName()+" is a vegetable. It cost is: "+price+" per box.");

    }

}

class TruckOfProduce

{

    private Produce arrayOfProduce[];

    private int i=0,n;

    public TruckOfProduce(int n)

    {

        arrayOfProduce=new Produce[n];

        this.n=n;

    }

    void addProduce()

    {

        if(i>=n)

            System.out.println("No more items can be added.");

        else

            {

                System.out.println("1.Add a fruit.\n2.Add a vegetable.\nEnter your choice:");

                int ch;

                Scanner sc=new Scanner(System.in);

                ch=sc.nextInt();

                String s;

                int q;

                double p;

                switch(ch)

                {

                    case 1:     System.out.println("Enter Fruit name: ");

                                s=sc.next();

                                System.out.println("Enter Quantity(No. of Boxes): ");

                                q=sc.nextInt();

                                System.out.println("Enter Price per box:");

                                p=sc.nextDouble();

                                arrayOfProduce[i]=new Fruits(s,p,q);

                                i++;

                                break;

                    case 2:     System.out.println("Enter Vegetable name: ");

                                 s=sc.next();

                                System.out.println("Enter Quantity(No. of Boxes): ");

                                q=sc.nextInt();

                                System.out.println("Enter Price per box:");

                                p=sc.nextDouble();

                                arrayOfProduce[i]=new Vegetables(s,p,q);

                                i++;

                                break;

                }

            }

    }

    public boolean Search(String s)

    {

        for(int j=0;j<i;j++)

        {

                if(s.equalsIgnoreCase(arrayOfProduce[j].getName())==true)

                    return true;

        }

        return false;

    }

    public double computeTotal()

    {

        double cost=0;

        for(int j=0;j<i;j++)

        {

            //calculate cost

            cost+=(arrayOfProduce[j].getPrice()*arrayOfProduce[j].getQuantity());

        }

        return cost;

    }

    public boolean removed(Produce p)

    {

        for(int j=0;j<i;j++)

        {

            if(p.getName().equalsIgnoreCase(arrayOfProduce[j].getName())==true)

            {   

                /*shifting back the remaining items. I overwrite the element that we want to remove.*/

                for(int k=j+1;k<i;k++)

                    arrayOfProduce[k-1]=arrayOfProduce[k];

                i--;

                return true;

            }

        }

        return false;

    }

    public String toString()

    {

        String s=" ";

        for(int j=0;j<i;j++)

            s=s+"Item "+(j+1)+": "+arrayOfProduce[j].toString();

        return s;

    }

    public static void main(String []args)

    {

        TruckOfProduce object=new TruckOfProduce(5);

        Scanner s=new Scanner(System.in);

        int ch;

        do{

            System.out.println("Eneter your choice:\n1.Add item\n2.Search\n3.removed\n4.Compute total\n5.display\n6.exit");

            ch=s.nextInt();

            switch(ch)

            {

                case 1:     object.addProduce();

                            break;

                case 2:     System.out.println("Enter item you want to search ");

                            if(object.Search(s.next())==true)

                                System.out.println("Element Found");

                            else

                                System.out.println("Element not Found");

                            break;

                case 3:     System.out.println("Enter item you want to remove");

                            Produce p=new Produce(s.next());

                            if(object.removed(p)==true)

                                System.out.println("Element removed");

                            else

                                System.out.println("Element not Found");

                            break;

                case 4:     System.out.println("Total amount is: "+object.computeTotal());

                            break;

                case 5:     System.out.println(object);

                            break;

                case 6:     return;

            }

        }while(true);   

    }

}

/*feel free to ask if you have any query.*/


Related Solutions

Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have a constructor Sentence(int size), where size determines the length of the sentence field of a given Sentence. The class should have an instance method public boolean isValid() that determines the validity of a sentence according to the rules detailed below. Also create a public nested class Word, with instance variables String value and Type type. Within this class, you must create: A public enum...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JavaDefine a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
Code in Java Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JAVADefine a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT