Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this Java assignment, I need to write the code. I started to write a Java file but I am stuck and I need your

In this Java assignment, I need to write the code. I started to write a Java file but I am stuck and I need your help. Here are the descriptions of the assignment.

The code above is a basic implementation of a data file that contains information about the states of the United States. The data file consists of records, each representing a state, with four fields: name, capital, population density, and total population.

The code uses a class "State" to store information about each state, and an ArrayList to store instances of the State class. The ArrayList is populated with data from a text file using the method "readData".

The main method implements four options for accessing and displaying the data: List all data:

Displays all the states and their respective information stored in the ArrayList.

List the data for a selected state (by name): Accepts user input for the state name and displays the information for that particular state.

Take a quiz on the state capitals: Quizzes the user on the state capitals.

Display the distribution of states by total population: Displays the distribution of states based on their total population.

Here's my code

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

// Class to represent a single record in the data file

class State {

private String name;

private String capital;

private double populationDensity;

private int totalPopulation;

csharp

Copy code

public State(String name, String capital, double populationDensity, int totalPopulation) {

this.name = name;

this.capital = capital;

this.populationDensity = populationDensity;

this.totalPopulation = totalPopulation;

}

public String getName() {

return name;

}

public String getCapital() {

return capital;

}

public double getPopulationDensity() {

return populationDensity;

}

public int getTotalPopulation() {

return totalPopulation;

}

}

// Main class that contains the main method

public class Main {

public static void main(String[] args) {

ArrayList states = new ArrayList();

scss

Copy code

// Read the data file and store the records

try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {

String line;

while ((line = reader.readLine()) != null) {

String[] parts = line.split("\t");

String name = parts[0];

String capital = parts[1];

double populationDensity = Double.parseDouble(parts[2]);

int totalPopulation = Integer.parseInt(parts[3]);

states.add(new State(name, capital, populationDensity, totalPopulation));

}

} catch (IOException e) {

System.out.println("Error reading data file: " + e.getMessage());

}

// Display a menu of options to the user

Scanner input = new Scanner(System.in);

while (true) {

System.out.println("Select an option:");

System.out.println("1. List all data");

System.out.println("2. List the data for a selected state");

System.out.println("3. Take a quiz on the state capitals");

System.out.println("4. Display the distribution of states by total population");

System.out.println("0. Exit");

int option = input.nextInt();

if (option == 0) {

break;

} else if (option == 1) {

listAllData(states);

} else if (option == 2) {

listSelectedState(states, input);

} else if (option == 3) {

takeQuiz(states);

} else if (option == 4) {

displayDistribution(states);

} else {

System.out.println("Invalid option");

}

}

}

// Method to implement option 1: list all data

private static void listAllData(ArrayList states) {

for (State state : states) {

System.out.println("Name: " + state.getName());

System.out.println("Capital: " + state.getCapital());

System.out.println("Population density: " + state.getPopulationDensity());

System.out.println("Total population: " + state.getTotalPopulation());

break;

case 'Q': //Quit

break;

case '?': //Display Menu

printMenu();

break;

default:

System.out.print(" Unknown action");

break;

}

}

} while (input1 != 'Q' || line.length() != 1); // continue do while till user inputs 'Q'

}

// The method printMenu displays the menu to a user

public static void printMenu()

{

System.out.print("Choice\t\tAction " + //It will display the messages below about the movie information

"------\t\t------ " +

"A\t\tAdd State " +

"D\t\tDisplay Information " +

"Q\t\tQuit " +

"?\t\tDisplay Help ");

}

}

image text in transcribedimage text in transcribed

The first part of your assignment is to select a subject for a data file, which will be a simple version of what is called a "database." A data file (sometimes called a "flat file") typically contains information describing a group of related items, like the states of the United States, or the chemical elements, or Clint Eastwood movies. Each item (state, element, or movie) is described by a line of the file, called a "record." Each record includes a set of entries, called "fields", describing properties or characteristics of the data item. All records in a data file must contain the same set of fields. For example, the fields of a state record might include the state name, the state capital, the population density (per square mile), and the total population, and a few typical records might look like this: Typically, one of the fields, called the "key" field, is unique for each record, like the state name, or the chemical element name. No two records may have the same entry for the key field so that it may be used to request selected records from the data file. For example, if someone wanted to see all the information on the state of Pennsylvania, that would be contained in the Pennsylvania record. The first part of your assignment is to turn in a description of your data file. Describe the general topic, as well as the fields of each record and their data types. Do NOT list the actual data, just provide a general description. Identify what each record represents, the four fields, and the specifics of each of the four user options. An example file description is on the last page. Turn this in by email, as soon as possible, so that you can receive a response with comments. If you do not turn in a file by the beginning of class on the due date (with no grace period), your program will not be accepted. When the program is complete, turn in two files by email: the program file (.java) and your data file called (.txt). Turn these in to: simms@mesacc.edu Sample Data File Description Title: States of the United States: Fields: name, capital, population density, and total population. options: (1) list all data (2) list the data for a selected state (by name) (3) take a quiz on the state capitals. (4) display the distribution of states by total population

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions

Question

Are there any changes you would recommend in the selection process?

Answered: 1 week ago