Results 1 to 11 of 11
- 10-25-2011, 10:47 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
Array Programming Assignment - Digit Counter
The following are the directions for this assignment:
Now, for whatever reason, I'm totally drawing a blank here. I just went back to read the chapter that discusses arrays, and it honestly didn't help me much. Anyway, here are my two classes so far:For this assignment you are to print a table that gives frequencies of the digits 0 through 9 in a typed English text. To keep things simple and concrete, your job will be to print the digit frequencies for multiple lines of text you enter from the keyboard. More specifically, you program should continue entering lines of text until you type two Returns in a row. Here is some sample interaction:
Enter lines of text. Type two Returns in a row to end
[DrJava Input Box]124 people applied for the job 007
[DrJava Input Box]in the new Bond movie. But only 94 or 96 out of the 124 were really possible
[DrJava Input Box]
Digit Frequencies
-----------------------------
0 2
-----------------------------
1 2
-----------------------------
2 2
-----------------------------
3 0
-----------------------------
4 3
-----------------------------
5 0
-----------------------------
6 1
-----------------------------
7 1
-----------------------------
8 0
-----------------------------
9 2
-----------------------------
Tips/Further Qualifications
• You MUST do this assignment with two classes: a driver, which includes main and handles the input, and is called DigitDriver; and a principal processing class, called DigitCounter. Objects in the DigitCounter class are responsible for handling the character processing. DigitCounter must have an array instance variable which acts as a scoreboard, tallying digits when they are encountered in the entered text.
• Remember that if someDigit is a char variable that holds a digit, then (someDigit-'0') gives the numerical position of someDigit among all digits. Thus if someDigit holds a 4, then (someDigit - '0') = 4. Use this fact to drive your array indexing.
• Use the static method isDigit from the Character class to determine when you've come across a digit.
• Your DigitCounter code must include and make use of a method called processString, which is passed a string as a parameter - a typed line of input - and which is responsible for finding and then tallying occurrences of digits in the parameter.
• Be sure to comment your code.
Java Code:import java.util.Scanner; public class DigitDriver{ public static void main(String[] args){ String[] lines = new String[100]; System.out.println("Enter lines of text. Type two Returns in a row to end"); Scanner scan = new Scanner(System.in); int pos=0; String d=" "; while(d.length()>0){ d=scan.nextLine(); lines[pos]=d; pos++; } //DRAW THE TABLE System.out.println("Digit Frequencies"); System.out.println("------------------------"); System.out.println("0 " + count0); System.out.println("------------------------"); System.out.println("1 " + count1); System.out.println("------------------------"); System.out.println("2 " + count2); System.out.println("------------------------"); System.out.println("3 " + count3); System.out.println("------------------------"); System.out.println("4 " + count4); System.out.println("------------------------"); System.out.println("5 " + count5); System.out.println("------------------------"); System.out.println("6 " + count6); System.out.println("------------------------"); System.out.println("7 " + count7); System.out.println("------------------------"); System.out.println("8 " + count8); System.out.println("------------------------"); System.out.println("9 " + count9); System.out.println("------------------------"); } }I feel like its really redundant to have to type out different sections for each individual digit (0-9), so I'm pretty sure there's a much easier way to handle a situation like this, but I'm totally clueless.Java Code:public class DigitCounter{ private int count0; private int count1; private int count2; private int count3; private int count4; private int count5; private int count6; private int count7; private int count8; private int count9; public void processString(){//sorts through each character in each string to determine whether or not there are digits }
As for determining if there are any digits, I plan on using a for-loop to go through each character at each position. If a character.isDigit is true, then I have to determine which digit it is, and then up the counter for that digit. Am I even close to being on the right track? Any tips are totally appreciated! Thanks in advance!
- 10-25-2011, 11:13 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Array Programming Assignment - Digit Counter
You are correct that this is redundant and it appears you already know what a for-loop is for...
- 10-26-2011, 04:05 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
Re: Array Programming Assignment - Digit Counter
Okay, so I think I'm on the right track...
...and...Java Code:import java.util.Scanner; public class DigitDriver{ public static void main(String[] args){ String[] lines = new String[100]; System.out.println("Enter lines of text. Type two Returns in a row to end"); Scanner scan = new Scanner(System.in); int pos=0; String d=" "; while(d.length()>0){ d=scan.nextLine(); lines[pos]=d; pos++; } System.out.println("Digit Frequencies"); for(int i=0; i<=9; i++){ System.out.println("------------------------"); System.out.println(i+ " " + countArray[i]); } } }
I have no idea where to go for the DigitCounter class, though. This is where I'm supposed to do all the "count++" stuff, right?Java Code:public class DigitCounter{ private int countArray[]=new int[9]; public void processString(){ }
- 10-26-2011, 04:38 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Array Programming Assignment - Digit Counter
So your counter class needs needs the text input in order to count the integers. Do you know how to pass from one class to another?
- 10-26-2011, 04:51 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
- 10-26-2011, 05:03 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Array Programming Assignment - Digit Counter
Your assigment states that you need a method called processString. So, while you could pass the entire stringArray to the to class upon creation of DigitCounter object, it is probably not the best way. You want to create that method and, as stated in your assignment, pass input strings to that method. So, create an object of the class DigitCounter and then call the processString() method, passing in your input string.
I would also start naming your variables meaningfully, such as stringArray and myDigitCounter.
- 10-26-2011, 05:50 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
Re: Array Programming Assignment - Digit Counter
Okay, I think I understood most of what you're saying. I changed the driver class so it now looks like this:
Is this what you were talking about?Java Code:import java.util.Scanner; public class DigitDriver{ public static void main(String[] args){ String[] stringArray = new String[100]; System.out.println("Enter lines of text. Type two Returns in a row to end"); Scanner scan = new Scanner(System.in); int pos=0; String d=" "; while(d.length()>0){ d=scan.nextLine(); stringArray[pos]=d; pos++; } DigitCounter myDigitCounter = new DigitCounter(countArray); countArray.processString();//calls processString() method from DigitCounter class System.out.println("Digit Frequencies"); for(int i=0; i<=9; i++){//print out table System.out.println("------------------------"); System.out.println(i+ " " + countArray[i]); } } }
Also, I've tried to keep working on the counter class. Here it is so far, but there's like 5 compiling errors, so I know it still needs work:
Java Code:public class DigitCounter{ private String d; private int pos; public DigitCounter(String d){ this.d = d; this.pos=pos; } private int[] countArray=new int[9]; public void processString(){ for(int j=0; j<=9; j++){ countArray[j]=0; } int digit=0; for(int k=0; k<d.length(); k++){ if(Character.isDigit(d.charAt(k))){ digit=Integer.parseInt(d.charAt(k)); countArray[digit]++; } } } }Last edited by ayelleeeecks; 10-26-2011 at 06:23 PM.
- 10-26-2011, 07:09 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Array Programming Assignment - Digit Counter
In line 16 of DigitDriver , you should be passing stringArray. As far as the DigitDriver class is concerned, countArray does not exist because it is out of scope. Now, all you are passing is a stringArray so remove line 7 from your DigitCounter Constructor. If you don't know what a constructor is, go re-read that section. Remember that d (the value passed into this class) is an array, so use the format d[index], not just d. As far as processing, just think about what you are trying to do. You are looking at an stringArray, one line at a time. Within each line, you are looking at each individual character. Look up nested loops.
- 10-27-2011, 01:13 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
Re: Array Programming Assignment - Digit Counter
How do I know when to use d[index]? Everytime I'm referencing d? Because when I do that and I try to compile it, it says ".class expected"... so unless I'm missing something, I'm not sure what to do. I made the other changes, I think. I'll figure out the processing part when I finish the basics.
Java Code:public class DigitCounter{ private String d; private int pos; public DigitCounter(String[] d){ this.d = d[]; } private int[] countArray=new int[9]; public void processString(){ for(int j=0; j<=9; j++){ countArray[j]=0; } int digit=0; for(int k=0; k<d.length(); k++){ if(Character.isDigit(d.charAt(k))){ digit=Integer.parseInt(d.charAt(k)); countArray[digit]++; } } } }Java Code:import java.util.Scanner; public class DigitDriver{ public static void main(String[] args){ String[] stringArray = new String[100]; System.out.println("Enter lines of text. Type two Returns in a row to end"); Scanner scan = new Scanner(System.in); int pos=0; String d=" "; while(d.length()>0){ d=scan.nextLine(); stringArray[pos]=d; pos++; } DigitCounter myDigitCounter = new DigitCounter(stringArray); countArray.processString();//calls processString() method from DigitCounter class System.out.println("Digit Frequencies"); for(int i=0; i<=9; i++){//print out table System.out.println("------------------------"); System.out.println(i+ " " + countArray[i]); } } }
- 11-01-2011, 07:32 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
Re: Array Programming Assignment - Digit Counter
I'm not trying to bump this thread or anything, but I'm still having problems with my program, so if anyone else is able to give me any hints as to what I'm doing wrong, it would be really appreciated!
- 11-01-2011, 07:45 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Array Programming Assignment - Digit Counter
This thread has taken long enough: there's a nice Character.digit(char c, int radix) method that returns the int value of the character c in the radix, i.e. it returns 4 if the char is '4'. The method returns -1 if c doesn't represent a digit in the given radix. Assume there's an int array named 'freq' with 10 elements, representing the frequency of digits; the following code snippet updates this array:
Given a String s, one can update the array 'freq' for each char in the String as follows:Java Code:public void update(char c) { int index= Character.digit(c, 10); if (index >= 0) freq[index]++; }
I leave it to the OP to print the values in the array 'freq' in a fancy way ...Java Code:for (char c : s) update(s);
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
need help with basic programming assignment (with arrays)
By magictricks1020 in forum New To JavaReplies: 11Last Post: 05-03-2011, 02:19 AM -
assistance w/ java programming assignment
By clemsontigers in forum New To JavaReplies: 4Last Post: 04-07-2011, 08:07 PM -
help..stuck w/ a java programming assignment
By clemsontigers in forum New To JavaReplies: 15Last Post: 02-24-2011, 09:31 PM -
User Enters 4-Digit Integer, Console Returns 1 Digit Per Line
By STANGMMX in forum New To JavaReplies: 0Last Post: 01-23-2011, 12:37 AM -
Really need help with an assignment... counter control loop...
By maxpower1000sa in forum New To JavaReplies: 7Last Post: 02-21-2009, 10:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks