Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you translate this to javascript please. import java.util.Scanner; import java.util.ArrayList; import java.io.*; class NameRecord { private String _name; // The name being considered private

can you translate this to javascript please. import java.util.Scanner; import java.util.ArrayList; import java.io.*; class NameRecord { private String _name; // The name being considered private int[] _rankings;// Its rankings in the 11 censuses public NameRecord() {} public NameRecord(String name, int[] ranks) { _name = name; _rankings = ranks; } public String getName() { return _name; } public int[] getRankings() { return _rankings; } public int getRank(int rank) {// use 0 for 1990, 1 for 1910, .. return _rankings[rank]; } /** Return year of best ranking with 0 for 1900, .. */ public int bestYear() { // returns the year with best rank int best = 0; int value = Integer.MAX_VALUE; for (int k = 0; k < _rankings.length; k++) { int r = _rankings[k]; if (r > 0 && r < value ) { best = k; value = r; } } return best; } /** Returns an array with the NameRecord of each name in the file filename */ public static NameRecord[] getAllNameRecords(String filename) { ArrayList nrs = new ArrayList(); NameRecord[] r = null; Scanner fin = null; try { fin = new Scanner(new File(filename)); while (fin.hasNextLine()) { String line = fin.nextLine(); Scanner scan = new Scanner(line); String name = scan.next(); int[] ranks = new int[11]; for (int k = 0; k < 11; k++) ranks[k] = scan.nextInt(); nrs.add(new NameRecord(name, ranks)); } r = new NameRecord[nrs.size()]; nrs.toArray(r); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fin != null) fin.close(); } return r; } } public class Lab12s09 { private static NameRecord[] records; public static int binary(String name) { int left = 0; int right = records.length-1; while (left <= right) { int middle = (left+right)/2; int c = name.compareToIgnoreCase(records[middle].getName()); if (c < 0) right = middle - 1; else if (c > 0) left = middle + 1; else return middle; } return -1; } /** Display yearly performance for name*/ public static void displayName(String name) { int where = binary(name); if (where < 0) { System.out.println("Sorry, " + name + " is not ranked"); return; } System.out.println(name.toUpperCase()); int[] ranks = records[where].getRankings(); int date = 1900; for (int k = 0; k < 11; k++) { int r = ranks[k]; System.out.print(date + ": "); if (r > 0) { double v = (int)((1000-r)*6.0/100); for (int j = 1; j <= v; j++) System.out.print('x'); } System.out.println(" " + r); date += 10; } } /** Given the string which, it prints all the names where it occurs and for each indicates the census when it did best */ public static void displayNames(String which) { which = which.toUpperCase(); for (NameRecord x: records) { String name = x.getName().toUpperCase(); if (name.indexOf(which) >= 0) { int year = x.bestYear(); System.out.println(name.toUpperCase() + " " + (1900+10*year)); } } } public static void main(String[] args) { String filename = "names-data.txt"; records = NameRecord.getAllNameRecords(filename); Scanner scan = new Scanner(System.in); // Ask for names and show their record while (true) { System.out.print("Enter name to search: "); String name = scan.nextLine().trim(); if (name.length() == 0) break; displayName(name); } // Ask for strings and show all names with that string while (true) { System.out.print("Enter string to search for: "); String s = scan.nextLine().trim(); if (s.length() == 0) break; displayNames(s); } } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How do books become world of wonder?

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago