Question
Program description: Your program is to introduce and then prompt the user for a name and sex combination to display (remember that names like Leslie
Program description:
Your program is to introduce and then prompt the user for a name and sex combination to display (remember that names like Leslie can be given to both boys and girls). Then it will read through the data file, searching for that combination. If it finds it, it should graph the data for that combination. If not, it should generate a short message indicating that the combination is not found. Look at the sample log of execution at the end of this write-up. You are to reproduce this format exactly. Notice that we print a message if the combination is not found, but we dont print anything when it is found. Instead, we graph the data.
How do I fix my program?
import java.io.*; import java.util.*; import java.awt.*;
public class Names { public static final String file = "names2.txt"; public static final int decades = 14; public static final int horizontalWidth = 70; public static final int year = 1920; public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File(file)); giveIntro(); String data = find(input); if (data.length() > 0) { DrawingPanel panel = new DrawingPanel(decades * horizontalWidth, 550); Graphics g = panel.getGraphics(); drawChart(g); chartGraph(data, g); } else { System.out.println("name/sex combination not found"); } } // This method introduces the program to its user. public static void giveIntro() { 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 + "."); System.out.println(); } // This method allows its user to search for a specific name with sex and return the // line when the name with sex is contained in the input. public static String find(Scanner input) { Scanner console = new Scanner(System.in); System.out.print("name? "); String name = console.nextLine().toLowerCase(); System.out.print("sex (M or F)? "); String sex = console.next(); String searchString = name.toUpperCase() + " " + sex.toUpperCase(); while (input.hasNextLine()) { String nameYear = input.next() + " " + input.next(); String line = input.nextLine(); if(nameYear.toUpperCase().contains(searchString)) { return nameYear + line; } } return " "; } // This method sets the DrawingPanel including lines that separate different decades. public static void drawChart(Graphics g) { g.drawLine(0, 25, decades * horizontalWidth, 25); g.drawLine(0, 525, decades * horizontalWidth, 525); for (int i = 0; i 0) { y = 25 + (rank * (1 / 2)); } else { y = 525; } g.drawString(infoResult + " " + String.valueOf(rank), x, y); g.drawLine(x - horizontalWidth, explanation, x, y); x += horizontalWidth; } } }
What the graph should look like:
My graph:
Come to windows AF F33 1970 2010 Come to windows AF F33 1970 2010Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started