Question

Help with java . can someone make it compile with pictures please! For this project, you...

Help with java . can someone make it compile with pictures please!

For this project, you will design and implement a program that analyzes baby name popularities in data provided by the Social Security Administration.

Every 10 years, the data gives the 1,000 most popular boy and girl names for kids born in the United States. The data can be boiled down to a single text file as shown below. On each line we have the name, followed by the rank of that name in 1900, 1910, 1920, ... 2000 (11 numbers). A rank of 1 was the most popular name that year, while a rank of 997 was not very popular. A 0 means the name did not appear in the top 1,000 that year at all. The elements on each line are separated from each other by a single space. The lines are in alphabetical order, although we will not depend on that.

Sam 58 69 99 131 168 236 278 380 467 408 466

Samantha 0 0 0 0 0 0 272 107 26 5 7

Samara 0 0 0 0 0 0 0 0 0 0 886

Samir 0 0 0 0 0 0 0 0 920 0 798

Sammie 537 545 351 325 333 396 565 772 930 0 0

Sammy 0 887 544 299 202 262 321 395 575 639 755

Samson 0 0 0 0 0 0 0 0 0 0 915

Samuel 31 41 46 60 61 71 83 61 52 35 28

Sandi 0 0 0 0 704 864 621 695 0 0 0

Sandra 0 942 606 50 6 12 11 39 94 168 257

The complete file is here : https://owd.tcnj.edu/~papamicd/name_data.txt

Classes Required

  • NameRecord—encapsulates the data for one name: the name and its rank over the years. This is essentially the data of one line from the file shown above. Use an int array to store the int rank numbers. The NameRecord constants START=1900 and DECADES=11 define the start year and the number of decades in the data.

Methods:

  • Constructor—takes a String line as in the file above and sets up the NameRecord object.
  • String getName()—returns the name.
  • int getRank(int decade)—returns the rank of the name in the given decade. Use the convention that decade=0 is 1900, decade=1 is 1910, and so on.
  • int bestYear()—returns the year where the name was most popular, using the earliest year in the event of a tie. Looking at the data above Samir's best year is 2000, while Sandra's best year is 1940. Returns the actual year, for example 1920, so the caller does not need to adjust for START. It is safe to assume that no rank in the data is ever larger than 1100 and every name has at least one year with a non-zero rank.
  • void plot()—uses the StdDraw class to plot the popularity of the name over the 11 decades in a random color. Available colors are BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, and YELLOW. You can scale the coordinate system appropriately, assuming that no rank is ever larger than 1100 and remembering that a rank of 1 should be on top of the image, while a rank of 1000 should be on the bottom. The StdDraw class can be downloaded. Documentation can be found online as well.
  • NameSurfer—the driver. The main method should read all of the data from the file and store it in an array of NameRecord objects. It should then offer the following menu to the user:

1 – Find the best year for a name

2 – Find the best rank for a name

3 – Plot the popularity of a name

4 – Clear the plot

5 – Quit

Enter your selection.

If 1, 2, or 3 is entered, the program should prompt the user for a name, search for that name in the array, print (or plot) the desired information, and display the menu again. If the name is not found in the array (search should ignore the case), print an error message and display the menu again.

If 4 is entered, clear the plot by calling StdDraw.clear();

The program quits only when the plot window is closed.

NOTE: Remember to keep your objects encapsulated.

Formatting Requirements

  • Follow indentation rules as discussed in class.
  • Use descriptive variable names.
  • Comment your code: Your name, name of the class and assignment at the beginning of program, and description of program functionality at the beginning of program. Every method should have a comment with a short description on top. Every instance variable should be followed by a comment that describes it on the same line.

Assessment

Evaluation of project success will be determined by the following criteria:

  • NameRecord class has the correct instance variables and constants
  • NameRecord constructor is implemented correctly
  • getName is implemented correctly
  • getRank is implemented correctly
  • bestYear is implemented correctly
  • plot is implemented correctly
  • driver correctly reads data from name_data.txt and stores it in array
  • driver prints user menu and reads user’s option
  • search for name is performed correctly
  • option 1 is handled correctly
  • option 2 is handled correctly
  • option 3 is handled correctly
  • option 4 is handled correctly
  • option 5 is handled correctly
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

/*

   Name:
   Class Name:
   Assignment:
   Project Name: Name Surfer
   Project Description: This is a simple project that uses the data provided by the Social Security Administration and analyses baby name popularities

*/

package project;

import java.util.*;
import java.lang.*;
import java.io.*;
import project.StdDraw;
import java.awt.Font;

class NameRecord{

   int START = 1900; // defines the start year
   int DECADES = 11; // defines the number of decades in data
   int[] ranks = new int[11]; // stores the rank of that name for the years 1900,1910,..,2000
   String name; // stores the name

   //Constructor to initialise the NameRecord object with the name nad its rank over the decades using individual lines from the file
   NameRecord(String line){
       String[] words = line.split(" ");// used to store all the words of a line , line is of the format - [ NAME RANK1 RANK2 ... RANK11 ]      
       name = words[0];
       for(int i=1;i<12;i++){
           ranks[i-1] = Integer.parseInt(words[i]);
       }
   }

   //Simple function which returns the name of the NameRecord object
   String getName(){
       return name;
   }
  
   //Simple function to get the rank of a particular name in a decade(0-1900,1-1910,..,10-2000)
   int getRank(int decade){
       return ranks[decade];
   }

   //Simple function to return the year when the name was most popular
   int bestYear(){  
       int bestFoundRank = 1111; //stores the best rank found so far while traversing the ranks array
       int bestYr = START; // stores the best year found so far while traversing the ranks array
       for(int i=0;i<11;i++){
           if(ranks[i]<bestFoundRank && ranks[i]>0){
               bestFoundRank = ranks[i];
               bestYr = START + i*10;
           }
       }
       return bestYr;
   }

}

class NameSurfer{

   public static void main(String[] args){

       Vector<String> lines = new Vector<String>();// used to store all the lines in the name_data.txt
       BufferedReader reader; // used to read all the lines from the name_data.txt file
      

       try{
           reader = new BufferedReader(new FileReader("/home/171070030/name_data.txt"));
           String line = reader.readLine(); //read the first line of the file

           //until lines are there read the lines and store them into the vector declared above
           while(line!= null){
               lines.add(line);
               line = reader.readLine();
           }

           //close the reader
           reader.close();

           int numberOfLines = lines.size(); //stores the number of lines
           Vector<NameRecord> records = new Vector<NameRecord>();//using the lines we create individual records and store them
          
           for(int i=0;i<numberOfLines;i++){
               NameRecord newRecord = new NameRecord(lines.elementAt(i));  
               records.add(newRecord);
           }

           boolean validEntry = true; //used to terminate when 5 is selected as an option
           int selection; //stores the option selected by the user
           Scanner sc = new Scanner(System.in); //used for user input

          

           while(validEntry){
              

               //Displaying the menu
               System.out.println("1 - Find the best year for a name");
               System.out.println("2 - Find the best rank for a name");
               System.out.println("3 - Plot the popularity of a name");
               System.out.println("4 - Clear the plot");
               System.out.println("5 - Quit");
               System.out.println("Enter your selection");
              
              
               //obtain user's choice
               selection = sc.nextInt();
               String name; //used if options 1 or 2 or 3 is selected
              
               if(selection == 1){
  
                   //Find the best year for the name
              
                   boolean nameFound = false;
                   String temp = sc.nextLine();
                   System.out.print("Enter the name: ");
                   name = sc.nextLine();

                   for(int i=0;!nameFound && i<numberOfLines;i++){
                       if(records.elementAt(i).getName().equals(name)){
                           System.out.println(records.elementAt(i).bestYear());
                           nameFound = true;                          
                       }  
                   }

                   if(!nameFound){
                       System.out.println("Name not found");
                   }

               }else if(selection == 2){
  
                   //Find the best rank for a name
                  

                   boolean nameFound = false;
                   String temp = sc.nextLine();
                   System.out.print("Enter the name: ");                  
                   name = sc.nextLine();

                   int indexOfBestRank;

                   for(int i=0;!nameFound && i<numberOfLines;i++){
                       if(records.elementAt(i).getName().equals(name)){
                           indexOfBestRank = (records.elementAt(i).bestYear()-1900)/10;
                           System.out.println(records.elementAt(i).ranks[indexOfBestRank]);
                           nameFound = true;  
                       }  
                   }
                  
                   if(!nameFound){
                       System.out.println("Name not found");
                   }      

               }else if(selection == 3){

                   //Plot the popularity of the name  

                   boolean nameFound = false;
                   System.out.print("Enter the name: ");
                   String tmp = sc.nextLine();
                   name = sc.nextLine();                  

                   //Setting up the canvas
                   StdDraw.setCanvasSize(800,800);                  
                   StdDraw.setXscale(0,120);
                   StdDraw.setYscale(0,1000);
                   StdDraw.setPenColor(StdDraw.BLACK);

                   for(int i=0;!nameFound && i<numberOfLines;i++){
                      
                       if(records.elementAt(i).getName().equals(name)){
                          
                           //Displaying the X and Y axis
                           StdDraw.line(2,10,119,10);
                           StdDraw.line(3,3,3,999);  

                           StdDraw.setPenColor(StdDraw.RED);
                           nameFound = true;
                           int[] Ranks = records.elementAt(i).ranks;

                           //If any rank is 0 then replace it by 1111
                           for(int j=0;j<11;j++){
                               if(Ranks[j]==0)
                                   Ranks[j] = 1111;
                           }

                           //Drawing the first line by using the first two ranks i.e. of 1900 and 1910
                           StdDraw.line(10,7500*(1.0/Ranks[0]),20,7500*(1.0/Ranks[1]));


                           //Drawing the lines further using consecutive pair of points
                           for(int j=1;j<=9;j++){
                               StdDraw.line((j+1)*10,7500*(1.0/Ranks[j]),(j+2)*10,7500*(1.0/Ranks[j+1]));
                           }


                           //Displaying the points along with the co-ordinates                      
                           StdDraw.setPenColor(StdDraw.BLUE);
                           StdDraw.setPenRadius(0.008);
                           Font font = new Font("Arial", Font.ITALIC, 10);
                            StdDraw.setFont(font);
                           for(int j=0;j<11;j++){
                               StdDraw.point((j+1)*10,7500*(1.0/Ranks[j]));
                               StdDraw.textLeft((j+1)*10,7500*(1.0/Ranks[j]),"("+String.valueOf(1900+(j)*10)+","+String.valueOf(Ranks[j])+")");
                           }
                       }

                   }
                  
                   if(!nameFound){
                       System.out.println("Name not found");
                   }

               }else if(selection == 4){

                       //Clear the plot              

                       System.out.println("clearing the plot...");  
                       StdDraw.clear();
               }else{

                      //Exits the program provided the plot is exited
      
                   System.out.println("Exiting the program...");
                   validEntry = false;
               }       
           }
       }catch(IOException e){
           e.printStackTrace();      
       }
      
   }              
}


SCREENSHOTS:

OUTPUT:

In the above project ,I have created a project directory in which I have two files namely NameSurfer.java and the other one is StdDraw.java. Both of these java files have as their first lines - 'package project' so that I could use StdDraw in NameSurfer class.

Add a comment
Know the answer?
Add Answer to:
Help with java . can someone make it compile with pictures please! For this project, you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with java . For this project, you will design and implement a program that analyzes...

    Help with java . For this project, you will design and implement a program that analyzes baby name popularities in data provided by the Social Security Administration. Every 10 years, the data gives the 1,000 most popular boy and girl names for kids born in the United States. The data can be boiled down to a single text file as shown below. On each line we have the name, followed by the rank of that name in 1900, 1910, 1920,...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will...

    Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will need to name . class file A4MA5331550 also when done put both files in a zipped folder named A4MA5331550 and email the folder to ensaye@gmail.com here is the program: import java.util.Scanner; /** * 09/17/2017 * Dakota Mammedaty * MA5331550 * Bubble sorted */ public class MA5331550 { public static void main(String[] args) throws IOException { Sort st =new Sort(); st.getData(); System.out.println("=================Sorting Algorithms================="); System.out.println("1. Bubble...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • I've done all questions except question 6. how do I do this? please help. code needs...

    I've done all questions except question 6. how do I do this? please help. code needs to be java 1. You need to be able to open and read the text files that have been provided for the project. Write a Java program that reads the driver.txt file and displays each data value for each line from the file to the screen in the following format: Licence Number: Licence Class: First Name: Last Name: Address: Suburb: Postcode: Demerit Points: Licence...

  • Please, please help with C program. This is a longer program, so take your time. But...

    Please, please help with C program. This is a longer program, so take your time. But please make sure it meets all the requirements and runs. Here is the assignment: I appreciate any help given. Cannot use goto or system(3) function unless directed to. In this exercise you are to create a database of dogs in a file, using open, close(), read, write(), and Iseek(). Do NOT use the standard library fopen() ... calls! Be sure to follow the directions!...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

  • Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...

    Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT