You can use the JOptionPane
class.
The first argument is the prompt. The second is the default value. Note that it returns a String. When executed, the code above displays the following window.String input = JOptionPane.showInputDialog("Enter n", "0");
Look at the
getScanner
method from the ClassScores classwork assignment.
Use repeated division by
10 or use the Math.log10
method.
Use repeated division by 10
again, followed by the modulo operator to pick out just the rightmost
digit or use the Math.pow
method.
You can check to see if two strings are equal using the
.equals
method. For example,
if some String token
is equal to (*, then
token.equals("(*")
will evaluate to
true
.
nthDigit
.
nthDigit(n,num)
finds the n
th
highest order digit of num
, i.e., the
n
th digit from the left. We take the
leftmost digit to be the 0
th.
nthDigit
should evaluate to 0
for digits
beyond the "end" of the number. For example:
When computing
nthDigit(0,678)
⇒ 6
nthDigit(1,678)
⇒ 7
nthDigit(2,678)
⇒ 8
nthDigit(3,678)
⇒ 0
nthDigit(0,0)
⇒ 0
nthDigit(3,18023)
⇒ 2
nthDigitTally
, using nthDigit
. nthDigitTally(n, nums)
returns a tally of frequencies of 0–9 as the
n
th digits of all the numbers in
nums
.
Here's a sample test case. These are enrollments in Research Triangle Park colleges and universities in Fall 2000 (thanks to the "Research Triangle Regional Partnership" website: http://www.researchtriangle.org/data/enrollment.html).
Institution | Enrollment |
---|---|
Duke University | 12176 |
North Carolina Central University | 5476 |
Louisburg College (Junior College) | 543 |
Campbell University | 3490 |
University of North Carolina at Chapel Hill | 24892 |
North Carolina State University | 28619 |
Meredith College | 2595 |
Peace College | 603 |
Shaw University | 2527 |
St. Augustine's College | 1465 |
Southeastern Baptist Theological Seminary | 1858 |
enrollments
contains the enrollment
numbers from that table. Then:
nthDigitTally(0, enrollments)
⇒
[0,3,4,1,0,2,1,0,0,0]
readNumbers
that reads
whitespace-separated integers from a Scanner
and returns a list of the numbers suitable as input to
nthDigitTally
. Here's the university enrollment data
from above:
12176 5476 543 3490 24892 28619 2595 603 2527 1465 1858From this,
readNumbers
should produce the list
[12176, 5476, 543, 3490, 24892, 28619, 2595, 603, 2527, 1465,
1858]
.
main
method to prompt the user for the number
n
and to choose a file for the data set. The program should tally the
n
th digits of the numbers in the data set and
print out a table of the results. For example, given that n
=0
and the following file: