Question

In: Computer Science

Complete the code below by finishing up the tasks detailed in the comments. ArrayList<String> fruits =...

Complete the code below by finishing up the tasks detailed in the comments.

ArrayList<String> fruits = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String fruit = "?";
while (!fruit.isEmpty()) {
    
    // prompt the user to enter a fruit
    
    
    // read the fruit using "sc", saving it into variable "fruit"
    
    
    // convert the fruit entered to lowercase
    
    
    // if the fruit is NOT empty and it is a new fruit, add it to ArrayList "fruits"
    
}

// display all fruits

Solutions

Expert Solution

Java Program to read fruits into Array List and display them:

//importing packages

import java.util.ArrayList;
import java.util.Scanner;

public class Main
{
   public static void main(String[] args) {
  
   ArrayList<String> fruits = new ArrayList<>();
  
Scanner sc = new Scanner(System.in);
  
String fruit = " ";
  
while (!fruit.isEmpty()) {
  
   // prompt the user to enter a fruit
   System.out.println("Enter a fruit");

// read the fruit using "sc", saving it into variable "fruit"     
   fruit=sc.nextLine();
     
   // convert the fruit entered to lowercase
   fruit=fruit.toLowerCase();
     
   // if the fruit is NOT empty and it is a new fruit, add it to ArrayList "fruits"
   if(!fruits.contains(fruit) && !fruit.isEmpty()){
  
fruits.add(fruit);     
     
   }
     
   }
     
   System.out.println("Displaying Fruits : ");   
  

//Displaying Fruits
   for(int i = 0; i < fruits.size(); i++) {

System.out.print(fruits.get(i) + "\n");
  
   }
  
}
}

Screenshot code:

Output:


Related Solutions

Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
Determine the outputs of the code: String[]array= {"Namath","Sauer","Namath","Namath","Snell","Snell","Namath","Namath","Namath", "Maynard","Namath","Namath"};        ArrayList<String>list=new ArrayList<String>(); &nbs
Determine the outputs of the code: String[]array= {"Namath","Sauer","Namath","Namath","Snell","Snell","Namath","Namath","Namath", "Maynard","Namath","Namath"};        ArrayList<String>list=new ArrayList<String>();        for(int i=0;i<array.length;i++)        {            list.add(array[i]);        }        System.out.println("OUTPUT 1");        System.out.println(list);        System.out.println("OUTPUT 2");        for(int i=0;i<list.size();i++)        {            if(list.get(i).equals("Namath"))            {                System.out.println(i+"\t"+ list.remove(i));            }        }        System.out.println("OUTPUT 3");        for(String s:list)        {           ...
What are the detailed tasks of each of the following traffic areas to complete the transport/traffic...
What are the detailed tasks of each of the following traffic areas to complete the transport/traffic part of the project?? Please provide all the breakdown of the tasks which are required to perform in the following areas to complete the project: I. traffic generation II. traffic distribution III. Traffic simulation III. Mode distribution What are the tasks breakdown of Traffic generation? What are the tasks breakdown of Traffic distribution? What are the tasks breakdown of Mode distribution ? What are...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket holds zero or more Products and can provide information * about the Products. One can add Products to a ShoppingBasket during its * lifetime, reset the ShoppingBasket, create a copy which contains Products of * at least a certain value, and make various queries to the ShoppingBasket. * (Thus, the number of Products that will be stored by a ShoppingBasket object * is not...
complete this code for me, this is java question. // Some comments omitted for brevity import...
complete this code for me, this is java question. // Some comments omitted for brevity import java.util.*; /* * A class to demonstrate an ArrayList of Player objects */ public class ListOfPlayers { // an ArrayList of Player objects private ArrayList players; public ListOfPlayers() { // creates an empty ArrayList of Players players = new ArrayList<>(); } public void add3Players() { // adds 3 Player objects players.add(new Player("David", 10)); players.add(new Player("Susan", 5)); players.add(new Player("Jack", 25)); }    public void displayPlayers()...
Write Matlab code (with detailed comments) to perform spectral analysis of 1KHz sine wave, noise and...
Write Matlab code (with detailed comments) to perform spectral analysis of 1KHz sine wave, noise and noise corrupted sine wave. Consider the sampling frequency Fs=10KHz.                                                                                                                                                   What should be the ideal frequency response of filter required to remove noise? Justify your answer                                                                                                                           
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the constructor for the class. Instantiate the testArrayList *and call the fillArrayList method to add objects to the testArrayList. * @param numElements The number of elements in the ArrayList */ public TestArrayLists(int numElements) { } /** * Complete the fillArrayList method. It should fill the testArrayList with * TestArrays objects consisting of 10 element int Array of random numbers. * @param numElements The number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT