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:
Arrays
Exercise:
Programming Projects
Question:18 | ISBN:9780132830317 | Edition: 5

Question

Programming Project 4.13 asked you to create a BoxOfProduce class representing a box of produce to deliver from a CSA farm. The box contained exactly three items. Modify the class so it uses an array of type String to represent the items in the box. You can still start with three random items to place in the box, but your menu should be modified to allow the user to add additional items and still substitute one item for another. You will likely need to modify the constructor of the BoxOfProduce class and also add new methods.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Modified BoxOfPRoduce class:

ublic class BoxOfProduce 
{
	
	private final int SIZE_LENGTH=20;
	int size;
	private String[] itemType=new String[SIZE_LENGTH];
	public BoxOfProduce(String[] items) 
	{
		size=0;
		setFirstFruit(items[0]);
		setSecondFruit(items[1]);
		setThirdFruit(items[2]);

	}
	public void add(String fruit) 
	{
		if(this.size()<SIZE_LENGTH)
		{
			itemType[size]=fruit;
			size=size+1;
		}
	}
	public void insertAt(int index,String fruit)
	{
		itemType[index-1]=fruit;
	}
	public void setFirstFruit(String firstitemType) 
	{
		itemType[size]=firstitemType;
		size=size+1;
	}
	public void setSecondFruit(String seconditemType) 
	{
		itemType[size]=seconditemType;
		size=size+1;
	}
	public void setThirdFruit(String thirditemType) 
	{	
		itemType[size]=thirditemType;
		size=size+1;
	}
	public int size()
	{
		return size;
	}
	public String toString() 
	{

		String items="";
		for (int index = 0; index < size; index++) 
			items+=itemType[index]+"\n";
		return items;

	}
}

 

0 0

Discussions

Post the discussion to improve the above solution.