Analyzing Homework2Class's characterCounter() Method

  • Comp Sci
  • Thread starter prosteve037
  • Start date
  • Tags
    Method
In summary, the task was to create a method that takes in a string and returns an int array of size 27, with each element representing the count of a specific character. The method only allows for the use of the charAt(int) and length() methods, but also allows for the use of the toLowerCase() method from the Character class. The method should handle both upper and lower case characters and return the correct count for each character in the string. After debugging and making some changes to the code, the method passed all 7 JUnit tests provided.
  • #1
prosteve037
110
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
  • #2


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.
 
  • #3


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?
 
  • #4


//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
 
  • #5


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

FAQ: Analyzing Homework2Class's characterCounter() Method

What is the purpose of the characterCounter() method in Homework2Class?

The characterCounter() method in Homework2Class is designed to count the number of characters in a given string. It can be used to analyze text data and gather insights on the length of a string, which can be helpful in various data analysis tasks.

How does the characterCounter() method work?

The characterCounter() method works by taking in a string as its input and using a loop to iterate through each character in the string. It then uses a counter variable to keep track of the number of characters and returns the final count as an integer.

What is the time complexity of the characterCounter() method?

The time complexity of the characterCounter() method is O(n), where n is the length of the input string. This is because the method uses a loop to iterate through each character in the string, resulting in a linear time complexity.

Can the characterCounter() method handle special characters and whitespaces?

Yes, the characterCounter() method is designed to handle all types of characters, including special characters and whitespaces. It counts every character in the input string, regardless of its type or value.

How can the characterCounter() method be improved?

The characterCounter() method can be improved by adding error handling to account for edge cases, such as an empty string or invalid inputs. Additionally, implementing a more efficient algorithm, such as using built-in methods or regular expressions, can also improve its performance.

Similar threads

Replies
5
Views
2K
Replies
4
Views
1K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
3
Views
1K
Replies
2
Views
1K
Replies
2
Views
3K
Replies
1
Views
2K
Back
Top