Results 1 to 12 of 12
  1. #1
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Exclamation Enter a sentence, tells you if each letter is Upper or lower case.

    Hello all!

    I hope you guys can help :)

    So this is what I must have...

    Write a program that prompts user to enter a sentence. The program should display each character on the screen except space character ( ). For each character, there are three possible outputs to display; "Upper case", "Lower case", and "Not a letter". Note that if the character is a space character, then you do not display space character, and you do not display any of the three possible outputs. Also there must not be a blank line. The program also must show total letter count, total lower case count, and total upper case count. For the total letter count, do not include the space characters, or Non-letter characters.

    You must write two classes; LetterCase and LetterCounter.

    LetterCase class must have the following;

    isThisSpace : Takes a char parameter, returns true if the parameter is a space character.

    isThisUpperCase : Takes a char parameter, returns true if the parameter is upper case.

    isThisLowerCase : Takes a char parameter, returns true if the parameter is lower case.

    main : You must create a LetterCounter object in main method. Read in a sentence from the user. To iterate the characters of the sentence, you must use for loop. In each iteration, call the boolean methods above. Then change the status of the counter if necessary. Then display the letter counts.


    LetterCounter class must have the following;

    Default constructor.

    getUpperCount : returns uppercase count (int). Make this method private.

    getLowerCount : returns lowercase count (int). Make this method private.

    increaseUpperCount : increases by one.

    increaseLowerCount : increases by one.

    displayCounts: displays three lines as in the screenshot. Call getUpperCount and getLowerCount methods.



    This is the code I have so far.
    Java Code:
     
    public class LetterCounter {
    int 
    upper;
    int 
    lower;
    public 
    LetterCounter()
    {
    
    }
    
    private 
    int getUpperCount(){
    return 
    upper;}
    
    private 
    int getLowerCount(){
    return 
    lower;}
    
    public 
    void 
    increaseUpperCount(int amount){
    upper=upper+amount;
    }
    public 
    void increasLowerCount(int amount){
    lower=lower+amount;
    }
    
    public 
    int displayCounts(){
    System.out.print("Letter Count: ");
    System.out.println("Lower Count: ");
    return 
    getLowerCount();
    System.out.println("Upper Count: ");
    return 
    getUpperCount();
    
    }
    
     
    }
    Java Code:
    import java.util.Scanner;
    
    public class LetterCase 
    {
    char ch;
    static String sentence;
    
    	public static void main(String[] args) 
    	{
    		System.out.print("Please enter a sentence: "); 
    		Scanner keyboard = new Scanner(System.in); 
    		String sentence = keyboard.next();
    		LetterCounter ch=  LetterCounter;	}
    		public static boolean isThisSpace(char input)
    		{			
    			{ 			
    				return true;	
    			} 			
    		}
    		public void isThisUpperCase(char ch)
    		{
    			if (!(Character.isLowerCase(ch)))
                {
                    for (int i=1; i<sentence.length(); i++)
                    {
                       ch = sentence.charAt(i);
    
                        if (!Character.isUpperCase(ch))
                           {  
                               sentence = "";
                           }
                         
    
                    } 
                   
    
    						
    
                    
    
    
    } System.out.print(sentence);}}
    Questions I have: I'm lost! Can you please push me in the right direction?



    Oh ya, and attached is the sample output.
    Attached Thumbnails Attached Thumbnails Enter a sentence, tells you if each letter is Upper or lower case.-image-3.png  

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    What's your question? What does your code do?

    And is that really how you format your code? Yikes! That is hard to read!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    My question is how do I get the program to display like the sample output. I am lost when trying to figure out how to make the uppercase and lowercase methods. If you could push me in the right direction that would help so much! I'm guessing I need to use the charAt() but I don't know where.

    Also I think something like this is right maybe???

    Java Code:
     for (int i = 0; i < sentence.length(); i++) {
     char thisChar = sentence.charAt(i);
     if (thisChar >= 65 && thisChar <= 90) {
     upper += thisChar;
     } else if (thisChar >= 97 && thisChar <= 122) {
     lower += thisChar;
     } else {
     other += thisChar;
     }
    And no that is not my normal formatting, it must have just copied weird from Eclipse.

    I really do appreciate any help! Sorry for being such a noob. :(

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    Don't throw in those magic numbers like 97 and 90 etc. Have a look at the Character class and its many utility functions; if you're not allowed to use them use 'A' etc. instead ('A' is the value 65 in a more readable format).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    I think you were closer when you were using the isLowerCase() method of the Character class.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    Also, what is supposed to be in the default method?
    And for can you help me split the sentence to have it be able to print vertically down instead of on one line?
    Last edited by pence; 06-18-2013 at 08:35 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    Quote Originally Posted by pence View Post
    Also, what is supposed to be in the default method?
    It's 'default constructor', not 'default method'; a better term would be 'no-args constructor' (no-args == no arguments); it's a constructor X() { ... } for a class X.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    Okay so this is where I am at now... I need help calling the other methods in the MAIN.

    Java Code:
    import java.util.Scanner;
    
    public class LetterCase 
    {
     char ch;
     static String sentence;
    
    	public static void main(String[] args) 
    	{
    		System.out.print("Please enter a sentence: "); 
    		Scanner keyboard = new Scanner(System.in); 
    		String sentence = keyboard.nextLine();			
    		for (int i = 0; i < sentence.length(); i++) 
    		{
    			if (sentence.charAt(i)!=' ')
    		    System.out.println(sentence.charAt(i)+" : ");
    		}
    	}
    		public boolean isThisSpace(char input)
    		{	for (int i = 0; i < sentence.length(); i++) 
    			if (sentence.charAt(i)!=' ')
    			{ 			
    				return true;	
    			}
    		return false; 			
    		}
    	
    		public void isThisLowerCase(char ch)
    		{
    			if (!(Character.isLowerCase(ch)))
                {
                    for (int i=1; i<sentence.length(); i++)
                    {
                       ch = sentence.charAt(i);
    
                        if (!Character.isUpperCase(ch))
                           {  
                               sentence = "";
                           }
                    }
                }
                         
    
                    } 
                    public void isThisUpperCase(char ch)
            		{
            			if (!(Character.isUpperCase(ch)))
                        {
                            for (int i=1; i<sentence.length(); i++)
                            {
                               ch = sentence.charAt(i);
    
                                if (!Character.isUpperCase(ch))
                                   {  
                                       sentence = "";
                                   }
                                 
                           }  
    
    }}

    Java Code:
    public class LetterCounter 
    {
    	int upperNum;
    	int lowerNum;
    	public LetterCounter()
    	{
    		upperNum=0;
    		lowerNum=0;		
    	}
    	
    	private int getUpperCount(){
    		return upperNum;}
    	
    	private int getLowerCount(){
    		return lowerNum;}
    	
    	public void increaseUpperCount(int amount){
    		for (int i=0; i<LetterCase.sentence.length(); i++)  
    		    {     
    		        for (char c='A'; c<='Z'; c++)  
    		        {    
    		            if (LetterCase.sentence.charAt(i) == c)  
    		            {  
    		            upperNum ++;  
    		            }  
    		        }  
    		   }
    
    	}
    	public void increasLowerCount(int amount){
    		for (int i=0; i<LetterCase.sentence.length(); i++)  
    			    {     
    			        for (char c='a'; c<='z'; c++)  
    			        {    
    			            if (LetterCase.sentence.charAt(i) == c)  
    			            {  
    			            lowerNum ++;  
    			            }  
    			        }  
    			    }
    		}
    
    	public void displayCounts(){
    		System.out.print("Letter Count: "+getLowerCount()+getUpperCount());
    		System.out.println("Lower Count: "+getLowerCount());
    		System.out.println("Upper Count: "+getUpperCount());	
    	}
    }
    Somebody please HELP! I will love you forever!

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    can we remove this topic,

    I figured out the answer and don't want this to be found via google.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    Quote Originally Posted by pence View Post
    can we remove this topic,

    I figured out the answer and don't want this to be found via google.
    You shouldn't have posted your question here to start with; once posted, your post belongs to this forum; why do you want this thread removed? It could help other people. You'd be a good sports if you post your solution here as well (just to help other people).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Enter a sentence, tells you if each letter is Upper or lower case.

    The professor probably told the students he is on this forum as well :)

Similar Threads

  1. enter a string sentence
    By amorosa19 in forum New To Java
    Replies: 13
    Last Post: 09-06-2013, 10:34 AM
  2. Replies: 5
    Last Post: 02-16-2012, 01:35 AM
  3. Making string insensitive to upper or lower case
    By ahmedaa16 in forum New To Java
    Replies: 0
    Last Post: 10-21-2011, 12:59 PM
  4. How to convert a String to upper case
    By Valeriano in forum New To Java
    Replies: 16
    Last Post: 03-01-2010, 12:39 PM

Posting Permissions

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