This is my last homework assignment and I just want to know
This is my last homework assignment, and I just want to know what other people would solve this problem by using simple java programming skill. Thanks.
https://courses.cs.washington.edu/courses/cse142/16au/handouts/12.html
Solution
import java.util.*;
import java.awt.*;
import java.io.*;
public class Names
{
public static final int YEAR = 1880;
public static final int DECADE = 14;
public static final int WIDTH = 70;
public static void main(String [] args) throws FileNotFoundException
{
Scanner input = new Scanner(System.in);
File inputFile = new File(\"names.txt\");
if(inputFile.exists())
{
System.out.println(\"File is existed\");
}
else
{
System.out.println(\"File does not exist\");
}
Scanner fileScanner = new Scanner(inputFile);
introduction();
String userInput = introQuery(input);
fileNameScanner(fileScanner, userInput);
}
public static void introduction() {
System.out.println(\"This program allows you to search through the\");
System.out.println(\"data from the Social Security Administration\");
System.out.println(\"to see how popular a particular name has been\");
System.out.println(\"since \" + YEAR +\".\");
}
public static String introQuery(Scanner input)
{
String params = \"\";
System.out.print(\"Name? \");
params += input.next() + \" \";
System.out.print(\"Gender (M or F)? \");
params += input.next();
System.out.println(params);
return params;
}
public static void fileNameScanner(Scanner file, String params)
{
boolean fileContains == false;
System.out.println(\"fileNameScanner ran\");
int rank = 0;
int rank2 = 0;
Scanner split = new Scanner(params).useDelimiter(\" \");
String name = split.next().toUpperCase();
String gender = split.next().toUpperCase();
while (file.hasNextLine() /* && (fileContains == false)*/)
{
String inputLine = file.nextLine().toUpperCase();
if ((inputLine.contains(name) && (inputLine.contains(gender))))
{
String toBeRemoved = name.toUpperCase() + \" \" + gender.toUpperCase();
inputLine = inputLine.replace(toBeRemoved, \" \");
Scanner numberSplit = new Scanner(inputLine);
rank = Integer.parseInt(numberSplit.next());
rank2 = Integer.parseInt(numberSplit.next());
fileContains = true;
Graph(name, gender, rank, rank2);
}
}
if (fileContains == false)
{
System.out.println(\"name/gender combination not found\");
}
}
public static void Graph(String name, String gender, int rank, int rank2)
{
int yearIncrease = 0;
DrawingPanel panel = new DrawingPanel(DECADE * WIDTH, 550);
Graphics g = panel.getGraphics();
g.setColor(Color.BLACK);
for (int i = DECADE * WIDTH; i >= 0; i -= WIDTH)
{
g.drawLine(i, 0, i, 550);
g.drawLine(i, (rank/2) + 25, i + WIDTH, (rank2/2) + 25);
String decadeInterval = String.valueOf(YEAR + yearIncrease);
String intervalLabel = name + \" \" + gender + \" \" + String.valueOf(rank);
String intervalLabel2 = name + \" \" + gender + \" \" + String.valueOf(rank2);
yearIncrease += 10;
g.drawString(decadeInterval, i, 550);
g.drawString(intervalLabel, rank, (rank/2 + 25));
g.drawString(intervalLabel2, rank2, (rank2/2 + 25));
drawLabels(g, rank, gender, name, i);
}
g.drawLine(0, 525, DECADE * WIDTH, 525);
g.drawLine(0, 25, DECADE * WIDTH, 25);
}
public static void drawLabels(Graphics g, int rank1, String gender, String name, int x)
{
g.setColor(Color.RED);
String intervalLabel = name + \" \" + gender + \" \" + String.valueOf(rank1);
g.drawString(intervalLabel, rank1, (rank1/2 + 25));
}
}



