I just need help with this WHOLE program, O boy.
This programming assignment is about pi, you know: 3.14, and all that. You probably know someone who memorized some of its digits back in seventh grade. There are the 3.14 people, just about everyone, then there are the 3.14159, people, and finally there are the elite memorizers, who went to 3.14159265, and beyond. Pi is hard to memorize because its digits are seemingly random, and this assignment takes a first baby step toward assessing the randomness of number's digit pattern.(Click here for an accessible discussion of the nature of the digits of pi.)
I've included a link to a text file that holds the first 100,000 digits of pi: digitsOfPt.txt
Your job for this assignment is to read through that file and then build and display a table that reports the frequency of each of the digits 0 through 9. Your table should look like this:
0 10001
..
8 10271
9 9994
bad symbols: 123
There are some other requirements.
The file is actually slightly corrupted - it contains some symbols besides digits (spaces, for example). Your job as you read through the file is to skip over these, but also report how many non-digits there are in the file. To do this you can use Java's try-catch construction.
Design arrays that most naturally use a non-numeric indexing scheme (here: the digits '0' through '9').
Your solution must use inheritance.
You must have a two class solution, a driver called PiDriver, and a principal class called PiCounter..
SO the file is called digitsOfPt.txt
and i need a PiDriver and a PiCounter
if ANYONE could help with this i would APPRECIATE it a LOT.
ALSO: it is due at 11:45pm EST tonight (July 31)
Re: I just need help with this WHOLE program, O boy.
Post what you have so far with the FULL text of the errors you are getting.
If the program is executing but not giving what you want, show its output and show what you want it to do instead.
To get your logic working, start with a small program with a String of digits and get the count to work with that first.
Good luck on getting it working by tonight.
Re: I just need help with this WHOLE program, O boy.
ya..i got nothing
I have something that i think may work for the PiCounter but I cannot do drivers!
can someone spot check my PiCounter and help with PiDriver?
Code :
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class PiCounter
{
public static void main(String[] args) throws IOException
{
Scanner inFile = new Scanner(new File("digitsOfPt.txt"));
int[] counter = new int[11];
String token = "";
while(inFile.hasNext())
{
token += inFile.next();
}
for(int i = 0; i < token.length(); i++)
{
String character = token.substring(i, i + 1);
if((int)character >= 48 && (int)character <= 57)
{
character = Integer.parseInt(character);
counter[character]++;
}
else
{
counter[10]++;
}
}
}
}
Re: I just need help with this WHOLE program, O boy.
What is the problem with the code you posted?
You can use Arrays.toString(counter) to format an array for debugging output.
What are the numbers in this code:
if((int)character >= 48 && (int)character <= 57)
Are you comparing against the characters '0' and '9'?
The code would be much easier to understand if you used characters vs decimal values.
Re: I just need help with this WHOLE program, O boy.
Re: I just need help with this WHOLE program, O boy.
Two questions.
1) isnt Pi just equal to 22/7? Dont think the question is very relevant to the problem, just wanted to confirm that.
2) are you getting an index out of bounds error? Because you should be. You have set counter to have 11 spots. And in your if statement, you are referencing values based on the variable character. However in your if statement, you are checking if character is between 48 and 57. So basically, you are trying to reference array spots 48 through 57, which are WAY out of bounds. Also, the call (int)(String) cannot be done, which you are trying to do in your if statement. However, you can do Integer.parseInt(String), which you have also used in your if statement. There are two ways to cast ints, and I think you confused them. You can do Integer.parseInt(String), which will convert the String to an int. Or you can do (int)(double) to case the double as an int, by cutting off the decimals.
Re: I just need help with this WHOLE program, O boy.
Quote:
Originally Posted by
aussiemcgr
Two questions.
1) isnt Pi just equal to 22/7? Dont think the question is very relevant to the problem, just wanted to confirm that.
no pi isnt equal to 22/7, that is just the closest fraction resembling pi. 22/7 = 3.14285714
pi =3.14159265...etc