1. Write a program to input a list of names (strings) from the
user and store them
in an ArrayList. The input can be terminated by entering the empty
string or
by
entering the string “quit”.
2. Add further functionality to your program so that it searches
the ArrayList to
find the first string and the last string according to dictionary
ordering and
then prints out these strings (names). Do this exercise without
sorting the
names in the ArrayList. For example, if the list contains the
following names:
Charles Darwin
Albert Einstein
Issac Newton
Tony Hoare
Grace Hopper
Edgar Dijkstra
Ada Lovelace
Charles Babbage
Stephen Hawking
Your program should output:
The first name in the list in alphabetical order is: Ada
Lovelace
The last name in the list in alphabetical order is: Tony
Hoare
3. Add to your program so that after completing question 2, it
sorts the ArrayList
to put all strings (names) in (alphabetical) order and prints them
all out in
order.
//TestArrayList.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class TestArrayList {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// creating a list
ArrayList list = new ArrayList();
System.out
.println("Enter list of names (blank line or quit to terminate)");
// reading first line of input
String input = sc.nextLine();
// looping as long as input is not empty or 'quit'
while (!input.isEmpty() && !input.equalsIgnoreCase("quit")) {
// adding input to list and reading next line
list.add(input);
input = sc.nextLine();
}
// code for question 2 begins here
// initializing two variables (to store first and last names in
// alphabetical order) to null
String first = null, last = null;
// looping through the list
for (String name : list) {
// if first is null (this is the first value on the list), setting
// name as both first and last
if (first == null) {
first = last = name;
} else {
// checking if name is alphabetically smaller than first
if (name.compareTo(first) < 0) {
// setting name as new first
first = name;
}
// checking if name is bigger than last
if (name.compareTo(last) > 0) {
// setting name as new last
last = name;
}
}
}
// displaying first and last
System.out
.println("The first name in the list in alphabetical order is: "
+ first);
System.out
.println("The last name in the list in alphabetical order is: "
+ last);
// code for question 3 begins..
// sorting list of names alphabetically
Collections.sort(list);
// looping and printing sorted list of names
System.out.println("Sorted list: ");
for (String name : list) {
System.out.println(name);
}
}
}
/*OUTPUT*/
Enter list of names (blank line or quit to terminate)
Charles Darwin
Albert Einstein
Issac Newton
Tony Hoare
Grace Hopper
Edgar Dijkstra
Ada Lovelace
Charles Babbage
Stephen Hawking
The first name in the list in alphabetical order is: Ada Lovelace
The last name in the list in alphabetical order is: Tony Hoare
Sorted list:
Ada Lovelace
Albert Einstein
Charles Babbage
Charles Darwin
Edgar Dijkstra
Grace Hopper
Issac Newton
Stephen Hawking
Tony Hoare
1. Write a program to input a list of names (strings) from the user and store...
1. Write a program to input a list of names (strings) from the user and store them in an ArrayList. The input can be terminated by entering the empty string or byentering the string “quit”.2. Add further functionality to your program so that it searches the ArrayList to find the first string and the last string according to dictionary ordering and then prints out these strings (names). Do this exercise without sorting the names in the ArrayList. For example, if...
In Java.
Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...
In this lab, you will write a program that reads a series name/value pairs, and stores them in a pair of vectors. After the name/value pairs are read, it will then read names (until the input is exhausted) and print out the corresponding value for that name. If the name is not found in the list, "name not found" should be printed. The names should be read as strings and stored as a vector<string>; the values should be read as...
Python program
- Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...
Solve all using Python
7. Create a list of strings, don't ask from the user, and return the count of the number of strings where the string length is 2 or more and the first and last chars of the string are the same (use for loop to go through the list). 8. The file State.txt contains the 50 U.S. states in the order in which they joined the union. Write a program to display the original 13 states in...
Write a program that has an array of at most 50 strings that hold people’s names and phone numbers. You can assume each string’s length is no more than 40. You may make up your own strings, or use the following: "Becky Warren, 555-1223" "Joe Looney, 555-0097" "Geri Palmer, 555-8787" "Lynn Presnell, 555-1212" "Holly Gaddis, 555-8878" "Sam Wiggins, 555-0998" "Bob Kain, 555-8712" "Tim Haynes, 555-7676" "Warren Gaddis, 555-9037" "Jean James, 555-4939" "Ron Palmer, 555-2783" The program should ask the user...
Implement a program that requests from the user a list of words (i.e., strings) and then prints on the screen, one per line, all four-letter strings in the list. >>> Enter word list: ['stop', 'desktop', 'top', 'post'] stop post An acronym is a word formed by taking the first letters of the words in a phrase and then making a word from them. For example, RAM is an acronym for random access memory. Write a function acronym() that takes a...
python code:
Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...
PYTHON: 14.7 LAB: All permutations of names PLEASE ANSWER IN PYTHON Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names, then use a recursive method to create and output all possible orderings of those names, one ordering per line. NAES MUST BE ALPHABETICAL ORDER AS IN THE EXAMPLE When the input is: Julia Lucas Mia then the output...
Write a Python program that keeps reading in names from the user, until the user enters 0. Once the user enters 0, you should print out all the information that was entered by the user. Use this case to test your program: Input: John Marcel Daisy Samantha Nelson Deborah 0 ================ Output: John Marcel Daisy 25 Samantha Nelson Deborah Hints: Create an array Names to hold the input names. Use the Names.append(x) function to add a new name to the...