Question

In: Computer Science

Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....

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.

Solutions

Expert Solution

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:

=====


Related Solutions

JAVA There is a folder named Recursive folder at the same location as these below files,...
JAVA There is a folder named Recursive folder at the same location as these below files, which contains files and folders. I have read the data of the Recursive folder and stored in a variable. Now they are required to be encrypted and decrypted. You may copy past the files to three different files and see the output. I am confused on how to write code for encrypt() and decrypt() The names of files and folders are stored in one...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read and follow the instructions below, placing the code statements needed just after each instruction. Leave the instructions as given in the code. Declare and create (instantiate) an integer array called List1 that holds 10 places that have the values: 1,3,4,5,2,6,8,9,2,7. Declare and create (instantiate) integer arrays List2 and List3 that can hold 10 values. Write a method called toDisplay which will display the contents...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
I. At the beginning, create a folder named "test" and add the folder into the version...
I. At the beginning, create a folder named "test" and add the folder into the version control tool tracking system. Then create one file named "001" under this folder. Commit any change if necessary. II. Next, add another file named "002" in the same folder and commit the change ("adding") in the version control tool. III. Followed by adding that file, create a branch (namely, "branch-A") in the version control tool and make some changes to the contents in file...
In the root of the project create a folder called res. In that folder create a...
In the root of the project create a folder called res. In that folder create a file called DemoFile.txt Create a class with a static main that tests the ability to resolve and print a Path: Create an instance of a FileSystem class. Create an instance of the Path interface for the DemoFile.txt file. Print the constructed Path with System.out.println() method. Create a class that does the following: Using a pre-Java 7 solution, create a class that tests streams in...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program that performs the following:  Define a function called “Find_Min” that takes an array, and array size and return the minimum value on the array.  Define a function called “Find_Max” that takes an array, and array size and return the maximum value on the array.  Define a function called “Count_Mark” that takes an array, array size, and an integer value (mark) and...
Inside “Lab1”folder, create a project named “Lab1Ex1”. Use this project to write and run a C++...
Inside “Lab1”folder, create a project named “Lab1Ex1”. Use this project to write and run a C++ program that produces:  Define a constant value called MAX_SIZE with value of 10.  Define an array of integers called Class_Marks with MAX_SIZE which contains students Mark. Define a function called “Fill_Array” that takes an array, and array size as parameters. The function fills the odd index of the array randomly in the range of [50- 100] and fills the even index of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT