Must score > 70% to pass course (see syllabus). Absolutely NO ARRAYS are to be u
ID: 3692483 • Letter: M
Question
Must score > 70% to pass course (see syllabus). Absolutely NO ARRAYS are to be used in this application. You will write a menu-driven Java application to do the activities shown in the attached example output. All of the activities involve reading the file, names.xt, which gives boys and girls names and their popularity rankings for 11 decades beginning in 1900. A popularity of 0 indicates a ranking that was 1000 or more. There are three menu items the user may choose 1. Displaying a histogram of a name's popularity. Note that the lower the rank the longer the histogram line should be. Names whose ranks are 0 should show an empty histogram line. No histogram line should wrap on the console display, but do not allow huge ranges of ranks to have the same number of stars. See the example output. Comparing the data via a histogram for two names during one specific decade. The user should be prompted for the two names and the decade. See the example output. Search for the two names (usually one male, one female - program does need to know which is male) whose rank is given by the user. The user should be prompted for the rank and the decade in which to search. See the example output 2. 3. Another menu choice should offer the user the opportunity to quit the application. NOTE: Always aim to make your output look like the example output. The user should NOT be able to crash the program by entering data. Examples: entering a letter when the program expects a number, entering a decade that does not exist, entering a name that is not in the file, entering a rank that is less than 1 or more than 999... None of these or any other erroneous inputs should be capable of crashing your application: your application must defend against user attempts to crash the application. You must force the user to enter names that ARE in the ile for menu item #2. Ignore the case of user input. Always process the file one entire line at a time, i.e., read the file using nextLine ) only must ass kenize strings containing the and c You will be expected to organize the code using static methods to implies that you will have separate methods for such tasks as 1) getting string data from the user, 2) getting integer data from the user, 3) getting the data from the file, 4) validating user input, 5) searching the file for:a name, and any other separate tasks that you might identify. Any tasks that are repeated should be organized as a separate static method, and then called by whatever section of code needs to use that task. No recursion may be used, either directly (A calls itself) or indirectly (A calls B calls A). Each method should have one returm and System.exit must not be used. lexity. ThisExplanation / Answer
import java.io.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Name{
private String givenName;
private int[] ranks = new int[11];
//parametrised constructor
public Name(String name, int[] popularityRanks)
{
givenName = name;
for (int i = 0; i < 11; i++)
{
ranks[i] = popularityRanks[i];
}
}
public String getName()
{
return givenName;
}
public int getPop(int decade){
if (decade >= 1 && decade <= 11){
return ranks[decade];
}
else
{
return -1;
}
}
public String getHistoLine(int decade){
String histoLine = ranks[decade] + ":";
return histoLine;
}
public String getHistogram()
{
String histogram = "";
for (int i = 0; i < 11; i++)
{
histogram += ranks[i] + "* " + this.getHistoLine(i) + " ";
}
return histogram;
}
}
//NameApp.java
public class NameApp
{
public static void main(String[] args)
{
List<Name> list = new ArrayList<Name>();
loadFile();
System.out.println(list.get(0).getPop());
}
private static void loadFile()
{
Scanner inputStream = null;
String fileName = "names.txt";
try
{
inputStream = new Scanner (new File(fileName));
}
catch (FileNotFoundException e)
{
System.out.println("Error opening file named: " + fileName);
System.out.println("Exiting...");
}
while (inputStream.hasNext())
{
list.add(new Name(givenName, ranks);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.