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

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...
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.
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...
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...
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]
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT