Analyzing Homework2Class's characterCounter() Method

  • Context: Comp Sci 
  • Thread starter Thread starter prosteve037
  • Start date Start date
  • Tags Tags
    Method
Click For Summary

Discussion Overview

The discussion revolves around the implementation of the characterCounter() method in a Java class, which is intended to count occurrences of each letter in a given string and return the counts in an integer array. The scope includes coding challenges, debugging, and understanding method functionality.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes the initial implementation of characterCounter() and notes that it fails all tests, returning ASCII values instead of counts.
  • Another participant points out that the line assigning character codes to the array is incorrect and suggests that the second loop does not function as intended.
  • A revised version of the code is presented, but the participant still encounters multiple test failures and suspects issues with character case conversion.
  • Some participants suggest changing the method of character case conversion from using Character.toLowerCase() to converting the entire string to lower case at once.
  • After making the suggested change, the participant reports that all tests pass successfully.

Areas of Agreement / Disagreement

Participants generally agree on the need to modify the character case conversion method, but there are differing views on the initial implementation and the specific reasons for the test failures.

Contextual Notes

Limitations include unresolved issues regarding the handling of character cases and the initial logic in the character counting loop. The discussion reflects ongoing debugging and refinement of the code without reaching a final consensus on the initial approach.

Who May Find This Useful

Readers interested in Java programming, debugging techniques, and character manipulation in strings may find this discussion relevant.

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! :]
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K