Results 1 to 11 of 11
  1. #1
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Exclamation Array Programming Assignment - Digit Counter

    The following are the directions for this assignment:
    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.
    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:

    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("------------------------");
      }
    }
    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
        
    }
    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.

    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!

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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...

  3. #3
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Array Programming Assignment - Digit Counter

    Okay, so I think I'm on the right track...
    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]);
        }
      }
    }
    ...and...

    Java Code:
    public class DigitCounter{
      private int countArray[]=new int[9];  
      public void processString(){
        
    }
    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?

  4. #4
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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?

  5. #5
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Array Programming Assignment - Digit Counter

    Quote Originally Posted by joeyvitoro View Post
    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?
    Gah, wouldn't it be something like
    Java Code:
    DigitCounter c = new DigitCounter(d);
    and I would put that in my driver class so it knows to pull information from the counter class?

  6. #6
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  7. #7
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default 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:

    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]);
        }
      }
    }
    Is this what you were talking about?


    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.

  8. #8
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  9. #9
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default 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]);
        }
      }
    }

  10. #10
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default 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. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default 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:

    Java Code:
    public void update(char c) {
       int index= Character.digit(c, 10);
       if (index >= 0)
          freq[index]++;
    }
    Given a String s, one can update the array 'freq' for each char in the String as follows:

    Java Code:
    for (char c : s)
       update(s);
    I leave it to the OP to print the values in the array 'freq' in a fancy way ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. help..stuck w/ a java programming assignment
    By clemsontigers in forum New To Java
    Replies: 16
    Last Post: 02-19-2014, 04:16 AM
  2. need help with basic programming assignment (with arrays)
    By magictricks1020 in forum New To Java
    Replies: 11
    Last Post: 05-03-2011, 02:19 AM
  3. assistance w/ java programming assignment
    By clemsontigers in forum New To Java
    Replies: 4
    Last Post: 04-07-2011, 08:07 PM
  4. Replies: 0
    Last Post: 01-23-2011, 12:37 AM
  5. Replies: 7
    Last Post: 02-21-2009, 10:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •