Question

In: Computer Science

Using an ArrayList, create a program which does the following: If the user enters quit, the...

Using an ArrayList, create a program which does the following: If the user enters quit, the program ends. If the user enters add followed by a string, program adds the string only to your ArrayList. If the user enters print, the program prints out the ArrayList contents. If the user enters size, the program prints out size of ArrayList, If the user enters remove followed by string, it removes all strings.

interface example:

**** Welcome to my List Program ****

Options: quit, add and a string, size, and print

Enter option: add bill jackson

bill jackson added to list

Enter option: print

[bill jackson]

Enter option: size

Size of List is: 1

Enter option: add jim jones

jim jones added to list

Enter option: print

[bill jackson, jim jones]

Enter option: search jim jones

jim jones found in index 1

Enter option: search billy billkins

billy billkins not found

Enter option: remove jim jones

jim jones removed

Enter option: print

[bill jackson]

Enter option: remove nick

nick not found - not removed

Enter option: remove all nick

all nick removed

Enter option: quit

Thank You!

Solutions

Expert Solution

please give thumbs up, thanks

code:


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

/**
*
* @author VISHAL
*/
public class WordProgram {
ArrayList A;
public WordProgram()
{
A=new ArrayList();
}
public void add(String str)
{
A.add(str);
}
public int search(String str)
{
for(int i=0; i<A.size();i++)
{
if(A.get(i)==str)
{
return i;
}
}
return -1;
}
public int size()
{
return A.size();
}
public void print()
{
for(int i=0; i<A.size(); i++)
{
System.out.println(A.get(i));
}
}
public static void main(String[]args)
{
WordProgram P=new WordProgram();
Scanner scan=new Scanner(System.in);
String choice;
while(true)
{
System.out.println("Options: quit, add and a string, size, and print");
choice=scan.nextLine();
String[]data=choice.split(" ");
if("add".equals(data[0]))
{
String word = "";
for(int i=1; i<data.length; i++)
word+=data[i];
P.add(word);
}
else if("size".equals(data[0]))
{
System.out.println("Size of the List is = "+P.size());
}
else if("print".equals(data[0]))
{
P.print();
}
else if("search".equals(data[0]))
{
String word = "";
for(int i=1; i<data.length; i++)
word+=data[i];
if(P.search(word)!=-1)
{
System.out.println(word+" found in index "+P.search(word));
}
else
{
System.out.println("not found");
}
}
else if("quit".equals(data[0]))
{
return;
}
}
}
  
}


Related Solutions

Python! Create a program that does the following: Reads a number from the user. Calls a...
Python! Create a program that does the following: Reads a number from the user. Calls a function that finds all the divisors of that number. Calls another function to see if the number 7 is a divisor of the original number. Keeps reading input from the user and calling the function above until the user enters the letter ‘q’. Create 2 functions findDivisors() and lucky7(). Use docstrings to explain what each function does. Use the help() function to output the...
Using Visual Basic 2017, Create a form with two textboxes and one button. A user enters...
Using Visual Basic 2017, Create a form with two textboxes and one button. A user enters the course she is taking in the first text box and then clicks on the button. Then if the user entered "Program Language" in the first text box, the second text box will show "Awesome pick!". If the user entered something else, the text box shows "Ok." Please write the correct code.
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
write a program using the main method where the user enters a number greater than 0,...
write a program using the main method where the user enters a number greater than 0, and the program prints out a set of stairs. The stairs consist of 3 boxes stacked on top of each other where the length and width of the first box is the number, the length and width of the second box is the number plus 1, and the length and width of the third box is the number plus 2. The stairs should be...
Using Python Write a program that does the following in order: 1.     Asks the user to enter...
Using Python Write a program that does the following in order: 1.     Asks the user to enter a name 2.     Asks the user to enter a number “gross income” 3.     Asks the user to enter a number “state tax rate” 4.     Calculates the “Federal Tax”, “FICA tax” and “State tax” 5.     Calculates the “estimated tax” and round the value to 2 decimal places 6.     Prints values for “name”, “gross income” and “estimated tax” The program should contain three additional variables to store the Federal tax, FICA...
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
5) Create the following in a Java program Create a scanner Prompt the user to enter...
5) Create the following in a Java program Create a scanner Prompt the user to enter the name where the box of mail is shipping from and create the variable and relate to scanner Prompt the user to enter the name of destination where the box of mail will be shipped and create the variable and relate to scanner Prompt the user to enter the weight of the package and create variable to relate to scanner Calculate cost of shipping...
Using RAPTOR create a program that allows the user to input a list of first names...
Using RAPTOR create a program that allows the user to input a list of first names in on array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. the output should be a list of email address where the address is of the following form: [email protected]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT