In: Computer Science
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own
folder.
3. Import Scanner and Stacks from the java.util package.
4. Create a Stack object named basket.
5. The output shall:
5.1.Ask the user to input the number of fruits s/he would like to catch.
5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G
for guava.
5.3.Display all the fruits that the basket has.
5.4.Ask the user to enter E to start eating a fruit.
5.5.Display the fruits remaining each time E is entered and "No more fruits" when the basket
becomes empty.
6. Convert your code into a Python script. Use the range() function to allow the user to input multiple
times. For example, if the user has to enter input five (5) times, the code will be for i in range(5).
The variable i represents numbers 1 to 5. Since the user input is only a one-character string, refer
to the sample code below to add a fruit to the basket. The variable i represents all the one-character
strings entered by the user. The functions upper() and lower() allow case-insensitive input.
for i in keys:
if i.upper() == "A":
basket.append("apple")
7. Save the script as fruit_basket.py to your folder.
Code:
=====
import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;
public class FruitBasket {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
Stack<String> basket = new
Stack<String>();
System.out.print("How many fruits
do you want catch: ");
int num =
Integer.parseInt(sc.nextLine());
for(int i=0; i<num; i++) {
System.out.print("choose a fruit to catch by pressing "+
"A for apple, O for orange, M
for mango,"
+ " or G for guava: ");
basket.push(sc.nextLine());
}
Iterator<String> it =
basket.iterator();
System.out.println("Fruits in
basket are: ");
while(it.hasNext()) {
String fruit =
it.next();
if(fruit.equals("A")) {
System.out.print("Apple ");
}
else
if(fruit.equals("O")) {
System.out.print("Orange ");
}
else
if(fruit.equals("M")) {
System.out.print("Mango ");
}
else
if(fruit.equals("G")) {
System.out.print("Guava ");
}
}
System.out.println();
while(true) {
if(!basket.isEmpty()) {
System.out.print("Enter E to consume a fruit:
");
String inp = sc.nextLine();
if(inp.equals("E")) {
basket.pop();
}
else {
System.out.println("Invalid
input");
}
}
else {
System.out.println("No more fruits");
break;
}
}
sc.close();
}
}
output
======
Implemented the code as per the requirement. As python is
indentation specific, you may not get the formatted text while
copying the code,
so I'm attaching the screenshots of the code for reference. Please
make sure when you are executing the below code you have same
format, especially tabs.
Please comment if any modification required or if you need any help.
Code:
====
num = int(input("How many fruits do you want catch: ")) fruits = [] for i in range(num): inp = input("choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava: ") fruits.append(inp) print("Fruits in basket are: ", end="") for fruit in fruits: if (fruit.lower()=="a"): print("Apple ",end="") elif (fruit.lower()=="o") : print("Orange ", end="") elif(fruit.lower()=="m") : print("Mango ",end="") elif(fruit.lower()=="g"): print("Guava ",end="") print(); while (True) : if (len(fruits)!=0): inp = input("Enter E to consume a fruit: ") if (inp.lower()=="e"): fruits.pop() else: print("Invalid input") else: print("No more fruits") break
code screenshot:
=============
output:
=====