Modify the FrultBasket class as follows: - Fruit baskets should have one attribute basket: an array list that hold the Frult objects. - Build a constructor for FrultBasket - Create a addFrult() method that allows you to add another fruit to the fruit basket. ·A tostring() method is already provided. FruitBasket ArrayList
Java. Create a new class FruitBasket that will store fruit objects in a basket.
Starter code.
import java.util.*;
public class Fruit
{
//attributes
protected String name = null;
protected String colour = null;
//constructor
public Fruit(String name, String colour)
{
this.name = name;
this.colour = colour;
}
//Getters
public String getName(){return name;}
public String getColour(){return colour;}
//Setters
public void setName(String name){this.name = name;}
public void setColour(String colour){this.colour = colour;}
public String toString()
{
String fruitDetails = name+" ("+colour+")";
return fruitDetails;
}
}
import java.util.*;
public class FruitBasket
{
public String toString()
{
String basketContents = "FRUIT BASKET:\n";
for (Fruit fruit: basket)
{
basketContents += fruit.toString()+"\n";
}
return basketContents;
}
}
import java.util.*;
public class PoD
{
public static void main( String [] args ) {
Scanner in = new Scanner( System.in );
FruitBasket fruitBasket = new FruitBasket();
while(in.hasNextLine()){
String line = in.nextLine();
String[] fruitDetails = line.split(" ");
Fruit nextFruit = new Fruit(fruitDetails[0],fruitDetails[1]);
fruitBasket.addFruit(nextFruit);
}
System.out.println(fruitBasket);
in.close();
System.out.print("END OF OUTPUT");
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images