Vous êtes sur la page 1sur 3

// // // // // // // //

Ishan Ranade 11/12/2013 CSE142 TA: Nadav Ashkenazi Assignment #5 This program creates a graph of the popularity of baby names through time based on a starting year specificed by the user to an end decade specificed by the user

package Chapter6Exercises; import java.util.*; import java.io.*; import java.awt.*; public class Names { //these class constants public static final int public static final int public static final int are used later in the program DECADES = 14; STARTING_YEAR = 1880; HORIZONTAL_WIDTH = 70;

public static void main(String[] args) throws FileNotFoundException { Scanner fileScanner = new Scanner(new File("names.txt")); Scanner inputScanner = new Scanner(System.in); intro(); //this block of code collects information from the user System.out.print("name? "); String name = inputScanner.next(); System.out.print("gender (M or F)? "); char gender = inputScanner.next().toUpperCase().charAt(0); //the next line scans through the text file and finds the desired //line of information String string = returnDesiredLine(fileScanner, name, gender); //if the desired line is not present in the text file, it will return //"nope" and it will say that it wasn't found. Otherwise, if the line //is found, a DrawingPanel will be created and a graph will be drawn if(string.equals("nope")) { System.out.println("name/gender combination not found"); } else { DrawingPanel panel = new DrawingPanel(HORIZONTAL_WIDTH * DECADES + 30, 550); Graphics graphics = panel.getGraphics(); drawGraph(graphics); drawInformation(graphics, string); } } //this method is a basic one that prints out the intro information public static void intro() { 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 " + STARTING_YEAR + "."); System.out.println(); }

//this method scans through the text file and returns the desired line of //information public static String returnDesiredLine(Scanner fileScanner, String name, char gender) { while(fileScanner.hasNextLine()) { String string = fileScanner.nextLine(); Scanner s = new Scanner(string); if(s.next().toLowerCase().equals(name.toLowerCase()) && s.next().charAt(0) == gender) { return string; } } return "nope"; } //this method draws all of the black lines and sets up the drawing panel //for the other information drawn in red public static void drawGraph(Graphics graphics) { graphics.setColor(Color.BLACK); for(int i = 1; i < DECADES; i++) { graphics.drawLine(i * HORIZONTAL_WIDTH, 0, i * HORIZONTAL_WIDTH, 550); } graphics.drawLine(0, 25, HORIZONTAL_WIDTH * DECADES, 25); graphics.drawLine(0, 525, HORIZONTAL_WIDTH * DECADES, 525); for(int i = 1; i <= DECADES; i++) { graphics.drawString(STARTING_YEAR + 10 * (i - 1) + "", HORIZONTAL_WIDTH * (i - 1), 550); } } //this method draws all of the red information by scanning through the //string sent to the method and using that information to draw a graph public static void drawInformation(Graphics graphics, String string) { Scanner stringScanner = new Scanner(string); graphics.setColor(Color.RED); //this block of code first collects the firs two tokens for the name //and gender String name = stringScanner.next(); String gender = stringScanner.next(); //number1 and number2 are just placeholders used to save the numbers //in the line of text as the scanner runs through it //the values x1,y1 and x2,y2 and are placeholder values that save a //point in order to draw the lines later in the code int number1, number2, x1, y1, x2, y2; //number1, x1, and y1 are given initial values number1 = stringScanner.nextInt(); x1 = 0; y1 = (int) (25 + Math.ceil((double) number1 / 2)); if(number1 == 0) { y1 = 525; } for(int i = 1; i <= DECADES - 1; i++) { x2 = i * HORIZONTAL_WIDTH; number2 = stringScanner.nextInt();

y2 = (int) (25 + Math.ceil((double) number2 / 2)); if(number2 == 0) { y2 = 525; } graphics.drawLine(x1, y1, x2, y2); graphics.drawString(name + " " + gender + " " + number1, x1, y1); x1 = x2; y1 = y2; number1 = number2; } graphics.drawString(name + " " + gender + " " + number1, x1, y1); } }

Vous aimerez peut-être aussi