SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes I
Exercise:
Programming Projects
Question:13 | ISBN:9780132830317 | Edition: 5

Question

Your Community Supported Agriculture (CSA) farm delivers a box of fresh fruits and vegetables to your house once a week. For this Programming Project, define the class BoxOfProduce that contains exactly three bundles of fruits or vegetables. You can represent the fruits or vegetables as three instance variables of type String .Add an appropriate constructor, accessor, and mutator methods. Also write a toString() method that returns as a String the complete contents of the box.

Next, write a main method that creates a BoxOfProduce with three items randomly selected from this list:

Broccoli

Tomato

Kiwi

Kale

Tomatillo

This list should be stored in a text file that is read in by your program. For now you can assume that the list contains exactly five types of fruits or vegetables.

Do not worry if your program randomly selects duplicate produce for the three items. Next, the main method should display the contents of the box and allow the user to substitute any one of the five possible fruits or vegetables for any of the fruits or vegetables selected for the box. After the user is done with substitutions, output the final contents of the box to be delivered. If you create additional methods to select the random items and to select valid substitutions, then your main method will be simpler to write.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

public class BoxOfProduce
{
    private String f1;
    private String f2;
    private String f3;

    public BoxOfProduce(String f1, String f2, String f3) {

        setFruit1(f1);
        setFruit2(f2);
        setFruit3(f3);
    }

    public void setFruit1(String f1)
    {
        this.f1 = f1;
    }

    public void setFruit2(String f2)
    {
        this.f2 = f2;
    }

    public void setFruit3(String f3)
    {
        this.f3 = f3;
    }

    public String getFruit1()
    {
        return f1;
    }

    public String getFruit2()
    {
        return f2;
    }

    public String getFruit3() {
        return f3;
    }

    public String toString()
    {
        return "1 : " + f1 + "\n" + "2 : " + f2 + "\n" + "3 : " + f3;
    }

}


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

import java.util.Random;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class CSADemo
{

    public static void main(String[] args)
    {

        int LENGTH = 5;
        Scanner scn = new Scanner(System.in);
        Scanner fileScn = null;
        String fruit = null;
        String[] items = new String[LENGTH];
        try
        {
            fileScn = new Scanner(new FileInputStream("items.txt"));
        } catch (FileNotFoundException e)
        {
            System.out.println("File not found");
            System.exit(0);
        }
        int index = 0;

        while (fileScn.hasNext())
        {

            fruit = fileScn.nextLine();

            items[index] = fruit;

            index = index + 1;
        }

        Random random = new Random();
        String firstFruitType = items[random.nextInt(LENGTH)];
        String secondFruitType = items[random.nextInt(LENGTH)];
        String thirdFruitType = items[random.nextInt(LENGTH)];
        BoxOfProduce boxOfProduce = new BoxOfProduce(firstFruitType, secondFruitType, thirdFruitType);

        System.out.print("****Random of 3 items*****\n");
        System.out.println(boxOfProduce.toString());

        System.out.println("******Total items:*******");

        for (int position = 0; position < items.length; position++)
            System.out.println(position + 1 + ": " + items[position]);
        System.out.println("----------------------");
        System.out.print("Enter your choice:");

        int listChoice = scn.nextInt();
        System.out.print("List of replace fruit items:\n ");
        System.out.println(boxOfProduce.toString());
        System.out.print("Choice of replace fruit item number(1-3): ");
        int boxChoice = scn.nextInt();

        switch (boxChoice)
        {

        case 1:
            boxOfProduce = new BoxOfProduce(items[listChoice - 1], secondFruitType, thirdFruitType);
            break;

        case 2:
            boxOfProduce = new BoxOfProduce(firstFruitType, items[listChoice - 1], thirdFruitType);
            break;
        case 3:

            boxOfProduce = new BoxOfProduce(firstFruitType, secondFruitType, items[listChoice - 1]);
            break;
        default:
            System.out.println("Invalid choice");
        }

        System.out.println("Items:");
        System.out.println(boxOfProduce.toString());

    }
}

items.txt file data:

Broccoli
Tomato
Kiwi
Kale
Tomatillo


OUTPUT:

0 0

Discussions

Post the discussion to improve the above solution.