Related questions
Your program will read in a list of high and low integer temperatures and city names from System.in using the Scanner class, then sort the information three different ways: by high temperature, low temperature, and by city name. It is a requirement of the assignment that an ArrayList is used to store the data internally, and the sort method that ArrayList implements for the List interface is used to do the sorting.
Here is some sample input/output. The program would first print out a prompt:
Enter high/low temperatures and city:
The user might then type or paste the following into the terminal or Netbeans' output window:
60 37 Dallas, TX 61 42 Kansas City, KS 75 57 Cleveland, OH 81 70 Fort Lauderdale, FL 79 55 Seattle, WA .
Notice that the input ends with a line starting with a period; as in Programming Project 1, the last line could start with any non-digit character. There is no limit on the number of lines of input the program might receive; since your program is using an ArrayList to store the temperature information for each city, it isn't necessary to know in advance how many lines there could be.
Then the program would produce the following output for the above input:
***List in original order*** Dallas, TX 37 60 Kansas City, KS 42 61 Cleveland, OH 57 75 Fort Lauderdale, FL 70 81 Seattle, WA 55 79 ***List sorted by low temperature*** Dallas, TX 37 60 Kansas City, KS 42 61 Seattle, WA 55 79 Cleveland, OH 57 75 Fort Lauderdale, FL 70 81 ***List sorted by high temperature*** Dallas, TX 37 60 Kansas City, KS 42 61 Cleveland, OH 57 75 Seattle, WA 55 79 Fort Lauderdale, FL 70 81 ***List sorted by city name*** Cleveland, OH 57 75 Dallas, TX 37 60 Fort Lauderdale, FL 70 81 Kansas City, KS 42 61 Seattle, WA 55 79
Notice that the city names come first on output but last on input, and that the temperatures are all lined up in a column. This will make it easier for your program to read the data, but it will be printed in a manner that is easier for a human to read
HERE IS THE CODE
Please follow the comments and please do exactly according to the comments.
// You will need to provide an appropriate header comment.
import java.util.ArrayList;
import java.util.Scanner;
public class ThreeWaySortTemperatures
{
public static void main(String[] args)
{
ArrayList<CityTemperatures> cityTempsList = new ArrayList<>();
Scanner inScanner = new Scanner(System.in);
System.out.println("Enter high/low temperatures and city:");
while (inScanner.hasNextInt())
{
int hiTemp = inScanner.nextInt();
int loTemp = inScanner.nextInt();
String cityName = inScanner.nextLine().trim();
cityTempsList.add(new CityTemperatures(cityName, loTemp, hiTemp));
}
// 1. Print the list out in the same order it came in
printList(cityTempsList, "***List in original order***");
// 2. Use cityTempsList.sort with the appropriate parameter
// to sort the list by low temperature in increasing order
// and then print the list
// Sort here
printList(cityTempsList, "***List sorted by low temperature***");
// 3. Use cityTempsList.sort with the appropriate parameter
// to sort the list by high temperature in increasing order
// and then print the list
// Sort here
printList(cityTempsList, "***List sorted by high temperature***");
// 4. Use cityTempsList.sort with the appropriate parameter
// to sort the list by city name in increasing order
// and then print the list
// Sort here
printList(cityTempsList, "***List sorted by city name***");
}
// Replace this comment with your own comment describing this method.
public static void printList(ArrayList<CityTemperatures> list, String title)
{
System.out.println(title);
// Write the code here to print the contents of list so that the
// city names, high temperatures and low temperatures line up
// in columns. The order of the data on each row should be
// city name, then low temperature, then high temperature.
// You will need to calculate the length of the longest
// city name to line things up properly.
// Once you have followed these directions,
// DELETE THIS 8 LINE COMMENT.
}
}
CityTemp.java File
// You will need to replace this comment with an appropriate header comment
// and you will need to flesh out this bare-bones class declaration
// that does very little as currently declared.
public class CityTemperatures
{
public CityTemperatures(String cityName, int loTemp, int hiTemp)
{
}
}
ThreeWaySortTemperatures.java:
//This class is used to print the cityName lowest temperature and highest temperature in an organized order
import java.util.*;
public class ThreeWaySortTemperatures
{
public static void main(String[] args)
{
ArrayList<CityTemperatures> cityTempsList = new ArrayList<>();
Scanner inScanner = new Scanner(System.in);
System.out.println("Enter high/low temperatures and city:");
while (inScanner.hasNextInt())
{
int hiTemp = inScanner.nextInt();
int loTemp = inScanner.nextInt();
String cityName = inScanner.nextLine().trim();
cityTempsList.add(new CityTemperatures(cityName, loTemp, hiTemp));
}
// 1. Print the list out in the same order it came in
printList(cityTempsList, "***List in original order***");
// 2. Use cityTempsList.sort with the appropriate parameter
// to sort the list by low temperature in increasing order
// and then print the list
// Sort here
cityTempsList.sort(new Comparator<CityTemperatures>(){
@Override
public int compare(CityTemperatures c1,CityTemperatures c2){
return c1.loTemp.compareTo(c2.loTemp);
}
});
printList(cityTempsList, "***List sorted by low temperature***");
// 3. Use cityTempsList.sort with the appropriate parameter
// to sort the list by high temperature in increasing order
// and then print the list
// Sort here
cityTempsList.sort(new Comparator<CityTemperatures>(){
@Override
public int compare(CityTemperatures c1,CityTemperatures c2){
return c1.hiTemp.compareTo(c2.hiTemp);
}
});
printList(cityTempsList, "***List sorted by high temperature***");
// 4. Use cityTempsList.sort with the appropriate parameter
// to sort the list by city name in increasing order
// and then print the list
// Sort here
cityTempsList.sort(new Comparator<CityTemperatures>(){
@Override
public int compare(CityTemperatures c1,CityTemperatures c2){
return c1.cityName.compareTo(c2.cityName);
}
});
printList(cityTempsList, "***List sorted by city name***");
}
// method to print the elements in the arraylist
public static void printList(ArrayList<CityTemperatures> list, String title)
{
ArrayList<String> names = new ArrayList<>();
System.out.println(title);
for(CityTemperatures ct : list){
names.add(ct.getCityname());
}
//Find the max length of city name
int max=names.get(0).length();
for(int i=0;i<names.size();i++){
if(names.get(i).length() > max){
max=names.get(i).length();
}
}
for(CityTemperatures ct : list){
System.out.println(ct.toString(ct.getCityname(),max));
}
System.out.println();
System.out.println();
}
}
CityTemperatures.java:
// This class is the model or the pojo class
// which specifies the structure of the array list data
import java.util.ArrayList;
public class CityTemperatures
{
Integer loTemp,hiTemp;
String cityName;
//Default Constructor
public CityTemperatures(){}
public CityTemperatures(String cityName, int loTemp, int hiTemp)
{
this.cityName=cityName;
this.loTemp=loTemp;
this.hiTemp=hiTemp;
}
//cityName getter method
public String getCityname(){
return cityName;
}
//toString() method used to print the object
public String toString(String cityname,int length){
int len=cityname.length();
String val=cityName;
for(int i=0;i<=(length-len);i++){
val+=" ";
}
return val+" "+loTemp+" "+hiTemp;
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 5 images
- use java to create a card game with the implements listed for the program. If you are able to do the first couple of steps that would be helpful and for step one use an array list with the following code. The code below should create 52 cards. String[] suits = {"Hearts", "Clubs", "Spades", "Diamonds"}; String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; for(String oneSuit : suits){ for(String num : numbers){ System.out.println(oneSuit + " " + num); } }arrow_forward1. Write a Java program to create a new array list, add some colors (string) and print out the collection. The list should contain color "green". 2. Change the previous program to insert an element into the array list at the first position. Change the application to print out the index of color "green". Change the previous application to replace "green" value with "yellow". Change the previous application to remove the third element from the list. Change the application to sort the list of colors. Change the application to shuffle the list of colors. Change the application such that it swaps colors "Orange" and "Red". Change the application to close the current list into a new list. Change the application to trim the capacity of the initial array list to the current list size. Change the application to empty the second list. Change the application to test (print out a message) if the second list is empty. Change the application to increase the capacity of...arrow_forwardUse the ArrayList class Add and remove objects from an ArrayList Protect from index errors when removing Practice with input loop Details: This homework is for you to get practice adding and removing objects from an ArrayList. The Voter class was used to create instances of Voters which held their name and a voter identification number as instance variables, and the number of instances created as a static variable. This was the class diagram: The constructor takes a string, passed to the parameter n, which is the name of the voter and which should be assigned to the name instance variable. Every time a new voter is created, the static variable nVoters should be incremented. Also, every time a new voter is created, a new voterID should be constructed by concatenating the string "HI" with the value of nVoters and the length of the name. For example, if the second voter is named "Clark Kent", then the voterID should be "HI210" because 2 is the value of nVoters and 10 is the number...arrow_forward
- This is needed in Java Given an existing ArrayList named friendList, find the first index of a friend named Sasha and store it in a new variable named index.arrow_forwardin this assignment i have to remove vowels from a string using a linked list. the linked list and link code is from a textbook and cannot be changed if the code alters the data structure. for some reason when i implement this code it gives me a logical error where the program only removes all instances of the first vowel in a string instead of moving through all vowels of the string and removing each one with all instances. i would appreciate if you could tell me the problem and solution in words and not in code. The code is in java. output please enter a string.researchhcraeserfalseList (first -->last): researchList (first -->last): researchList (first -->last): rsarch CODE MAIN FUNCTION import java.util.Scanner;/*** Write a description of class test here.** @author (your name)* @version (a version number or a date)*/public class test{// instance variables - replace the example below with your ownpublic static void main(String[] args){Scanner input = new...arrow_forward1. Write a program that would resemble a power utility billing system. There will be an input window that would enter or input the meter number (5 numeric digits) and the present meter reading in kilowatt hours, the maximum would be 9999 kilowatts. A search from a 2d-arraylist would be made in order to get the previous meter reading of a particular meter number. The 2d- arraylist consists of meter number (5 numeric digits) and the previous meter reading in kilowatt hours (4 numeric digits with 9999 as maximum value). Provide at least 5 sample data or 5 rows with meter number and previous meter reading each row for the 2d-arraylist. When an input of meter number and present meter reading is made, search the 2d-arraylist for the equivalent meter number. If a match is found, get the kilowatt hour used (KWH) by subtracting the previous meter reading from the present meter reading. Note that if the present meter reading is less than previous meter reading, add 10000 first to the present...arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education