Jump to content

Java Assignment Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
rgs1400

rgs1400

    Newbie

  • Members
  • Pip
  • 5 posts
Hi everyone,

I have to complete the following Java assignment but I'm stuck. I will post the code I currently have and would love some suggestions! I know what I have currently written doesn't meet the requirements for the assignment.

Thanks!

Write a program which reads the string:
"My country tis of thee, Sweet land of liberty, Of thee I sing."
and determines the frequencies of each letter of the alphabet. Using an array of int do the following:

* In the main() method write a loop that reads characters from the above string.
* Use method toUpperCase() to convert all the letters to uppercase.
* If the converted character is a letter, increment the proper element of the array.
* In a separate method, print(int[] bin), print the results.



public class Assignment

{


public static char toUpperCase(char ch)

{

char result;


if (ch >= 'a' && ch <= 'z')


{

result = (char) (ch - 32);

}


else


{

result = ch;

}


return result;

}


public static void main(String[] asd)

{

String s ="My country tis of thee, Sweet land of liberty, Of thee I sing";

int[] bin = new int[26];

for(int j = 0; j < s.length(); j++)

{

char ch = s.charAt(j);

int digit = ch -'0';

bin[digit]++;

}

for(int k = 0; k < 27; k++)

{

System.out.print("# of " + k + "'s = " + bin[k]+"|" );

for(int j = 0; j < bin[k]; j++)

{

System.out.print(k);


System.out.println();

}

}

}


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
First, you should indent and space out all of your code properly so it's readable. Observe below, I will make no change to your code except to add proper spacing:
public class Assignment
{
    public static char toUpperCase(char ch)
    {
        char result;

        if (ch >= 'a' && ch <= 'z')
        {
            result = (char) (ch - 32);
        }
        else
        {
            result = ch;
        }

        return result;
    }

    public static void main(String[] asd)
    {
        String s ="My country tis of thee, Sweet land of liberty, Of thee I sing";
        int[] bin = new int[26];
        for(int j = 0; j < s.length(); j++)
        {
            char ch = s.charAt(j);
            int digit = ch -'0';
            bin[digit]++;
        }

        for(int k = 0; k < 27; k++)
        {
            System.out.print("# of " + k + "'s = " + bin[k]+"|" );

            for(int j = 0; j < bin[k]; j++)
            {
                System.out.print(k);
                System.out.println();
            }
        }
    }
You'll notice that you're missing a bottom bracket, and that's why you're having problems. Well, there are other problems, but that's the compilers problem with your code. :P

Second, this is bad:
    public static char toUpperCase(char ch)
    {
        char result;

        if (ch >= 'a' && ch <= 'z')
        {
            result = (char) (ch - 32);
        }
        else
        {
            result = ch;
        }

        return result;
    }
Bad, bad, bad, bad, bad, bad, bad. Never do this, first off you're assuming ASCII, and since Java pretty much exclusively uses Unicode, that's not a safe assumption. Also, this function is entirely unnecessary, because strings already come with a toUpperCase() method! All you need to do is this:
s = s.toUpperCase();
and you're done. There's nothing else to it, get rid of that function.

Finally, the way you're getting the characters is wrong. While using two separate loops to count and to print is good, the way they're structured isn't very good. Instead, you should have a first loop that counts using a simple mathematical algorithm over each character (and has to ignore characters that are not within the alphabet) and the second that just prints it out. This is how I'd do the first loop:
        for(int iii = 0; iii < s.length(); ++iii)
        {
            int theChar = s.charAt(iii);
            if (theChar < 'A' || theChar > 'Z') continue;
            bin[theChar - 'A']++;
        }
The for loop and the assignment should be pretty self-explanatory. What the if statement is doing is determining if any character that comes from s.charAt() is less than 'A' or greater than 'Z' (Assuming that 'A'++ == 'B', and 'B'++ == 'C', etc. is a safe assumption) and if it is, then to skip it. The third line is executed only if the if statement returned false, which means that the character is within range. What happens is you just take theChar and subtract 'A'. Because of the above assumption, you can also assume that 'A' - 'A' = 0, and that 'B' - 'A' == 1, 'C' - 'A' == 2, etc. This is an easy mathematical way to figure out where to put the letter in the array, and doesn't assume a specific value for 'A'.

The second loop I built like this:
        for(int iii = 0; iii < 26; ++iii)
        {
            System.out.println("# of " + (char) (iii + 'A') + "'s = " + bin[iii]);
        }
Other than the for loop, it's just one line of code. It prints out as a line, the combination of the string "# of ", the value of iii + 'A' cast as a char, the string "'s = ", and the value of bin[iii]. This should result in strings like "# of A's = 1" or whatever value accounts for the number of A's.

This whole thing is much more concise and easier to read, while also making no assumptions about character encoding or what string is being input. For example, you could even input an entire text file into this program and count the number of alphabetical characters and what they are in this program. This is much simpler. :)
Wow I changed my sig!

#3
rgs1400

rgs1400

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks for the help Zeke. Much appreciated.

I do agree that some of my methods were not the simplest, but the assignment requires us to re-use code we had done for earlier assignments, so it makes for a much more complicated thing altogether.