Analyzing Homework2Class's characterCounter() Method

  • Context: Comp Sci 
  • Thread starter Thread starter prosteve037
  • Start date Start date
  • Tags Tags
    Method
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 6K views
prosteve037
Messages
110
Reaction score
3

Homework Statement


Here's what I have to do in a nutshell:

A description of the required method:

/**
* @param s is the original String (1st parameter)
* @return an int[] of size 27, containing the character counts as indicated above.
*

* The count of the number of 'a' or 'A' characters must be in position 0 of the array,
* the count of the number of 'b' or 'B' characters must be in position 1 of the array,
*
* the count of the number of 'z' or 'Z' characters must be in position 25 of the array, and
* the count of all other characters must be in position 26 of the array.
*
* Some examples:
*
* if s is "", then all entries in the array must be 0.
* if s is "a", then all entries in the array must be 0 except for entry 0, which must be 1.
* if s is "Baaa!", then all entries in the array must be 0 except for:
* entry 0, which must be 3,
* entry 1, which must be 1, and
* entry 26, which must be 1.
*
* The only methods you may call on the String s are charAt(int) and length().
*
* You may use the static toLowerCase method defined in the Character class,
* which maps a char to its lower-case equivalent. For example,
* Character.toLowerCase('a') returns 'a'
* Character.toLowerCase('A') returns 'A'
* Character.toLowerCase('%') returns '%'
*
* You may, if you wish, define private helper method that you call from your
* character counting method.
*
*/

The package that the assignment comes in also provides tests to show if/where our code fails to produce the right responses, based on the input.

Homework Equations



-------------------------

The Attempt at a Solution



Here's the code I've written so far to try and solve this problem:

Code:
public class Homework2Class {
	
	private int _a;
	
	public Homework2Class() {}
	
	public int[] characterCounter(String s) {
		
		int[] characterSetArray = new int[27];
		
		_a = (int)'a';
		
		for (int x = 0; x < characterSetArray.length; x++) {
			characterSetArray[x] = _a;
			_a = _a + 1;
		}
		
		for (int n = 0; n < s.length(); n++) {
			char c = s.charAt(n);
			Character.toLowerCase(c);
			
			int i = (int)c;
			
			int letterIndex = i - characterSetArray[0];
			
			if (letterIndex >= 0 && letterIndex <= 25) {
				characterSetArray[letterIndex]++;
			}
			
			else if (letterIndex > 25) {
				characterSetArray[26]++;
			}
		}
		
		return characterSetArray;
	}
	
}

I ran the JUnit Test for it and all of the tests failed :P

However I did notice that the test results showed that my code returned array entries for each letter of the characterSetArray with the corresponding ASCII value for that letter. Here are the results of one of the tests:

Tests.jpg


What does this mean?
 
Physics news on Phys.org


Well, you have the line
Code:
characterSetArray[x] = _a;
which assigns the character code of "a" (which happens to be 97) to the first element and then increases it by one each time.

Apparantly your second for loop then does nothing, so these values remain.
 


CompuChip said:
Well, you have the line
Code:
characterSetArray[x] = _a;
which assigns the character code of "a" (which happens to be 97) to the first element and then increases it by one each time.

Apparantly your second for loop then does nothing, so these values remain.

Ahh, thank you. I didn't put in the other array as I had intended :P

Now revised:

Code:
package hw2;

public class Homework2Class {
	
	private int _a;
	
	public Homework2Class() {}
	
	public int[] characterCounter(String s) {
		
		int[] returningArray = new int[27];
		
		int[] characterSetArray = new int[27];
		
		_a = (int)'a';
		
		for (int x = 0; x < characterSetArray.length; x++) {
			characterSetArray[x] = _a;
			_a = _a + 1;
		}
		
		for (int n = 0; n < s.length(); n++) {
			char c = s.charAt(n);
			Character.toLowerCase(c);
			
			int i = (int)c;
			
			int letterIndex = i - characterSetArray[0];
			
			if (letterIndex >= 0 && letterIndex <= 25) {
				returningArray[letterIndex]++;
			}
			
			else if (letterIndex < 0 || letterIndex > 25) {
				returningArray[26]++;
			}
		}
		
		return returningArray;
	}
	
}

I'm still getting 4 out of 7 failures though :/

Here are screens of the tests:

Test 2
Test2.jpg


Test 3
Test3.jpg


Test 5
Test5.jpg


Test 7
Tests-1.jpg


I think I understand what's happening in these Test Cases, though I don't understand why; I think the code isn't converting the upper-cased letters to lower-case letters for some reason.

But with the static method Characters.toLowerCase(...); shouldn't this take care of that? What's happening? What am I missing?
 


//char c = s.charAt(n);
// Character.toLowerCase(c);
These one doesn't work. Try to change them all to:
s = s.toLowerCase();
Im pretty sure that this one will work
 


haichau6990 said:
//char c = s.charAt(n);
// Character.toLowerCase(c);
These one doesn't work. Try to change them all to:
s = s.toLowerCase();
Im pretty sure that this one will work

Thanks so much! The tests all green-barred! :]