City GUI
Write a Java GUI program which reads data about US cities. The data file is different, but is in
the same format as in Lab #01. The GUI provides a combo box with the cities in the data file. As
cities are selected in the combo box, information about the city is displayed.
Requirements
1. The program must have a graphical user interface similar to the one on the bottom of the
Requirements section. It must have a text field, a button, a combo box, and labels.
2. One or more layout managers must be used. This example was created using a border layout.
The east and west panels each used a grid layout in addition.
3. A message must be displayed in the GUI after the data has been successfully read. A message
should also be displayed if the file is not found. The program should not exit in that case, but
allow the user to enter a different file name.
4. The combo box must be populated by the names of the cities in the data file in alphabetical
order. Note that this means the GUI starts with an empty combo box. Items are added to it as or
after the data file is read.
5. The program must provide an ActionListener for the Read button. It must also provide either
an ActionListener or an ItemListener for the combo box, which displays the state, zip code, and
time zone for that city.
6. The information for each city must be stored in its own object. Another object must be used to
manage the collection of cities. This is similar to Lab #01. For this assignment you may assume
that there will be no more than 100 cities.
7. The time zone should be displayed as the correct abbreviation for that city’s time zone in July.
Here are the abbreviations. If the city observes daylight savings time, the S should be changed to
a D. Note that some cities in the data file do not observe daylight savings time.
Hours relative to GMT -4 -5 -6 -7 -8 -9 -10
Standard Time Abr. AST EST CST MST PST AKST HST
I have attached an executable .jar file to the assignment that you can run to see how this program
should work. Copy it to your computer. Copy the data file into the same directory as the .jar file.
Suggestions
1. Create class diagrams first. This will help you think through and organize your program. There
are many ways to do this. Start with the class diagrams from Lab #01. Both classes will require
some additions.
2. Create the GUI and handle the IO in a separate class which extends JFrame. The main method
can be included in this class.
3. Don’t worry about making your implementation efficient or using wonderful abstract data
types (ADTs). You will be required to do that soon enough.
4. This program will take you several days to complete. Break it down into smaller tasks. This is
the order I used, yours may be different.
*Modify the class diagrams from Lab #01 to include new requirements that you identify now.
You can add additional functionality later as you discover the need.
*Implement those changes in the classes.
* Create a GUI class. Implement a non-functiona.
Python Notes for mca i year students osmania university.docx
City GUIWrite a Java GUI program which reads data about US cities..pdf
1. City GUI
Write a Java GUI program which reads data about US cities. The data file is different, but is in
the same format as in Lab #01. The GUI provides a combo box with the cities in the data file. As
cities are selected in the combo box, information about the city is displayed.
Requirements
1. The program must have a graphical user interface similar to the one on the bottom of the
Requirements section. It must have a text field, a button, a combo box, and labels.
2. One or more layout managers must be used. This example was created using a border layout.
The east and west panels each used a grid layout in addition.
3. A message must be displayed in the GUI after the data has been successfully read. A message
should also be displayed if the file is not found. The program should not exit in that case, but
allow the user to enter a different file name.
4. The combo box must be populated by the names of the cities in the data file in alphabetical
order. Note that this means the GUI starts with an empty combo box. Items are added to it as or
after the data file is read.
5. The program must provide an ActionListener for the Read button. It must also provide either
an ActionListener or an ItemListener for the combo box, which displays the state, zip code, and
time zone for that city.
6. The information for each city must be stored in its own object. Another object must be used to
manage the collection of cities. This is similar to Lab #01. For this assignment you may assume
that there will be no more than 100 cities.
7. The time zone should be displayed as the correct abbreviation for that city’s time zone in July.
Here are the abbreviations. If the city observes daylight savings time, the S should be changed to
a D. Note that some cities in the data file do not observe daylight savings time.
Hours relative to GMT -4 -5 -6 -7 -8 -9 -10
Standard Time Abr. AST EST CST MST PST AKST HST
I have attached an executable .jar file to the assignment that you can run to see how this program
should work. Copy it to your computer. Copy the data file into the same directory as the .jar file.
Suggestions
1. Create class diagrams first. This will help you think through and organize your program. There
are many ways to do this. Start with the class diagrams from Lab #01. Both classes will require
some additions.
2. Create the GUI and handle the IO in a separate class which extends JFrame. The main method
can be included in this class.
3. Don’t worry about making your implementation efficient or using wonderful abstract data
2. types (ADTs). You will be required to do that soon enough.
4. This program will take you several days to complete. Break it down into smaller tasks. This is
the order I used, yours may be different.
*Modify the class diagrams from Lab #01 to include new requirements that you identify now.
You can add additional functionality later as you discover the need.
*Implement those changes in the classes.
* Create a GUI class. Implement a non-functional GUI that just displays a window like that
above.
* Implement an action listener for the Read button. You can use much of the code from the Lab
#01 main method to read the data and create objects.
* Sort the list of cities alphabetically. You can do this as cities are added or after they have all
been added.
* Populate the combo box with the city names.
* Convert the time zone and daylight savings time information into the abbreviation strings. I did
this in a getTimeZone() method added to the City class.
* Implement the combo box selection listener. I used an action listener. It can also be done with
an item listener.
* Our textbook has many good examples, but does not have all of the information you will need.
Simple Google searches for terms like Java adding items to combo box will provide all the
information you need.
* Don’t hesitate to ask your classmates, your instructor, or Java programmers you know for help.
Upload
All of the Java source files required to build your program. You only need to upload the source
files in the Blackboard assignment. I don’t want the executable, byte files, or project files.
Here are the files:
Lab 1 source files:
import java.io.File;
import java.io.*;
import java.util.*;
public class Main
{
/**
* this is the main class
* @return main
* */
public static void main(String[] args)throws IOException
3. {
/**
* this is main() class in which getters are performed and displays info
* @return City()
*/
CityGroup cg=new CityGroup();
Scanner fileScan=new Scanner(new File("CityData1.csv"));
Scanner deLimit=null;
String fileLine=fileScan.nextLine();
while(fileScan.hasNext()==true)
{
fileLine=fileScan.nextLine();
deLimit=new Scanner(fileLine);
deLimit.useDelimiter(",");
while(deLimit.hasNext()==true)
{
int zip=deLimit.nextInt();
String cName=deLimit.next();
String state=deLimit.next();
double lat=deLimit.nextDouble();
double lon=deLimit.nextDouble();
int zone=deLimit.nextInt();
boolean yesDay=false;
String daylightStr=deLimit.next();
if(daylightStr.charAt(0)=='1')
yesDay=true;
City temp=new City(zip,cName,state,lat,lon,zone,yesDay);
cg.addCity(temp);
}
}
System.out.println("Northernmost city: " +cg.findNorthMost());
}
}
public class CityGroup
{
/**
4. * this is main citygroup class
* @return CityGroup
*/
City[] cityArray=new City[100];
int numCities;
public CityGroup()
{
/**
* this sets cities to 0;
* @return numCities
*/
numCities=0;
}
public void addCity(City newCity)
{
/**
* this is returns the cityarray after its added
* @return cityArray[]
*/
cityArray[numCities]=newCity;
numCities++;
}
City findNorthMost()
{
City noa =cityArray[0];
for(int counter=0;counter {
if(cityArray[counter].compareTo(noa)==1)
noa=cityArray[counter];
}
return noa;
}
}
public class City
{
/**
5. * this is main city class
* @return City
*/
int zipcode;
String cityName;
String state;
double latitude;
double longitude;
int timezone;
boolean yesDaylight;
public City (int zip, String cName, String st, double lat, double lon, int zone, boolean daylight)
{
/**
* this is constructor for City
* @return City()
*/
zipcode = zip;
cityName = cName;
state = st;
latitude = lat;
longitude = lon;
timezone = zone;
yesDaylight = daylight;
}
public String toString()
{
/**
* this is the toString class
* @return cityName + "," state;
*/
return cityName + ", " + state;
}
public int compareTo (City otherCity)
{
/**
6. * this is compareTo class to compare
* @return compareTo;
*/
if (otherCity.latitude == latitude)
return 0;
else if (latitude > otherCity.latitude)
return 1;
else
return -1;
}
}
GUI that i have already:
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(new Font("Serif", Font.PLAIN, 48));
paintHorizontallyCenteredText(g2, "City information", 200, 75);
}
protected void paintHorizontallyCenteredText(Graphics2D g2,
String s, float centerX, float baselineY) {
FontRenderContext frc = g2.getFontRenderContext();
Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
float width = (float) bounds.getWidth();
g2.drawString(s, centerX - width / 2, baselineY);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(new Main());
f.setSize(450, 350);
7. f.setVisible(true);
}
}
CityData 2: 3 Cities O O X City Information CityData2.csv J Read Input File Select City State:
ZipCode: Time Zone:
Solution
import java.io.File;
import java.io.*;
import java.util.*;
public static void main(String[] args)throws IOException
{
/**
this is main() class in which the ‘new city’ data is taken and displays info
@return City()
**/
CityGroup cg=new CityGroup();
Scanner fileScan=new Scanner(new File("CityData1.csv"));
Scanner deLimit=null;
String fileLine=fileScan.nextLine();
while(fileScan.hasNext()==true)
{
fileLine=fileScan.nextLine();
deLimit=new Scanner(fileLine);
deLimit.useDelimiter(",");
while(deLimit.hasNext()==true)
{
int zip=deLimit.nextInt();
String cName=deLimit.next();
String state=deLimit.next();
double lat=deLimit.nextDouble();
double lon=deLimit.nextDouble();
int zone=deLimit.nextInt();
boolean yesDay=false;
8. String daylightStr=deLimit.next();
if(daylightStr.charAt(0)=='1')
yesDay=true;
City temp=new City(zip,cName,state,lat,lon,zone,yesDay);
cg.addCity(temp);
}
}
System.out.println("Northernmost city: " +cg.findNorthMost());
}
}
public class CityGroup
{
/**
this is main citygroup class
@return CityGroup
**/
City[] cityArray=new City[100];
int numCities;
public CityGroup()
{
/**
this sets cities to 0;
@return numCities
**/
numCities=0;
}
public void addCity(City newCity)
{
/**
this returns the value for ‘cityarray’ after its added
@return cityArray[]
**/
cityArray[numCities]=newCity;
numCities++;
}
9. City findNorthMost()
{
City noa =cityArray[0];
for(int counter=0;counter {
if(cityArray[counter].compareTo(noa)==1)
noa=cityArray[counter];
}
return noa;
}
}
public class City
{
/**
this is main city class
@return City
**/
int zipcode;
String cityName;
String state;
double latitude;
double longitude;
int timezone;
boolean yesDaylight;
public City (int zip, String cName, String st, double lat, double lon, int zone, boolean daylight)
{
/**
this is constructor for the Class City
@return City()
**/
zipcode = zip;
cityName = cName;
state = st;
latitude = lat;
longitude = lon;
timezone = zone;
yesDaylight = daylight;
10. }
public String toString()
{
/**
this is the toString class
@return cityName + "," state;
**/
return cityName + ", " + state;
}
public int compareTo (City otherCity)
{
/**
this is compareTo class to compare
@return compareTo;
**/
if (otherCity.latitude == latitude)
return 0;
else if (latitude > otherCity.latitude)
return 1;
else
return -1;
}
}
GUI that i have already:
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(new Font("Serif", Font.PLAIN, 48));