JAVA
In this PoD you will use an ArrayList to store different cities). This PoD can be done in one file.
Details
Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many cities you will add to the arraylist. You should make sure as your read in cities, that you do not add cities that are already in the list (e.g., don’t allow any repeats).
Once you have added all the cities, print the list.
Then read in one more city. If this is city already exists, do nothing but if the city isn’t in the list, add it to the end of the list (e.g., the last position).
Print the list again.
Find and print the city that has the longest name and it’s index in the arraylist (if there is more than one city with the longest name, print the first)
Input
Example Input:
5 Toronto Vancouver Ottawa Montreal Halifax Moncton
Output
Example Output:
[Toronto, Vancouver, Ottawa, Montreal, Halifax]
[Toronto, Vancouver, Ottawa, Montreal, Halifax, Moncton]
Longest: Vancouver Index: 1
package test;
import java.util.Scanner; //scanner class import for input
import java.util.ArrayList; //ArrayList class import
public class cities {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in); //making an object of scanner
class
// below the input is being taken as
a string and the string is then
// split into multiple string using
the regx=" " and they are stored
// in a string array s
String s[] = sc.nextLine().split("
");
ArrayList<String> a = new
ArrayList<String>(); //a is an object of arraylist
class
int arraylist_length=0; //initially
the length of the array list is zero
int n = Integer.parseInt(s[0]);
//the first element of the array is the length given
// adding all the words except the
last one to the array list
for(int i=1;i<=n;i++) {
int
flag=0;
// below loop
checks if the city name already exists in the arraylist
for(int
j=0;j<arraylist_length;j++) {
if(s[i].equals(a.get(j))) {
flag=1;
break;
}
}
// if the city
name is not present in the arraylist then it is added
if(flag==0)
{
a.add(s[i]);
arraylist_length+=1;
}
}
System.out.println(a); //printing
the arraylist
// checking for the last city
name
int flag=0;
for(int
j=0;j<arraylist_length;j++) {
if(s[n+1].equals(a.get(j))) {
flag=1;
break;
}
}
// if the extra city name is not in
the arraylist then it is added and arryalist
// is printed again
if(flag==0) {
a.add(s[n+1]);
arraylist_length+=1;
System.out.println(a);
}
int max_length=0;
int index=0; //for storing the
index of city name with max length
// this loop is for finding the
maximum length and it is started from the last index of
// the arraylist because if a name
with same maximum length is repeated then we want
// the one occurring first
for(int
i=arraylist_length-1;i>=0;i--) {
// the city name
is split into array of letters
String temp[] =
a.get(i).split("");
int
temp_length=0;
// this loops calculates the length
of the string by accessing items in the 'temp' array
for(String c :
temp) {
temp_length++;
}
// if the length
of the current city name is greater than the maximum length
// then the
max_length variable is updated and the index of it is also
stored
if(temp_length>=max_length) {
max_length=temp_length;
index = i;
}
}
// now finally the longest city name
is printed along with its index position in the
// arraylist
System.out.println("Longest:"+a.get(index)+" Index :"+index);
}
}
Sample Output1

Sample Output2

In the above example a city with longest name has repeated i.e. 'Montrealas'. it has the same length 9 as 'vancouver' but as 'Vancouver' is present before "Montreals" in the arraylist so it is printed finally. Moreover as the extra city name is already available in the arraylist so its not added and the arraylist is not printed again.
JAVA In this PoD you will use an ArrayList to store different cities). This PoD can...
JAVA In this PoD you will use an ArrayList to store different pet names (there are no repeats in this list). This PoD can be done in your demo program (where your main method is) – you don’t have to create a separate class for today. Details Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many pet names (one word) you will add to the arraylist....
JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...
JAVA This PoD, builds off the Book class that you created on Monday (you may copy your previously used code). For today’s problem, you will add a new method to the class called lastName() that will print the last name of the author (you can assume there are only two names in the author name – first and last). You can use the String spilt (“\s”) method that will split the String by the space and will return an array...
Please Write in Java 15 salesmen have performed sales in 10 different cities and the information is stored in a two-dimensional array. For example: City 0 City 1 City 2 City … City 9 salesman 0 3.4 4.0 2.6 …… 3.5 salesman 1 3.7 4.44 1.90 …… 5.9 salesman … 1.5 5.9 7.4 …… 0.0 salesman 14 44.4 5.6 7.8 ….. 0.9 Write a class called Employee that contains the following methods A static method called totalSales() (String name, double...
For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...
JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...
*****Requiremrnt:
1. Please do not use any array or arraylist
2. collects only a single String input
3. uses at least 3 String methods
1) Just Initials: Print just the initials in upper case letters separated by periods 2) Last Name First With Middle Initial: Print the last name in lower case with the first letter capitalized, followed by a comma and the first name in in lower case with the first letter capitalized and then the middle initial capitalized...
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...
Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...
Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...