Results 1 to 9 of 9
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default Would you mind examining one of my programs and letting me know what you think.

    The point of this post is for me to grow and possibly look at things in a new perspective. I know on a small scale stuff doesn't seem to matter as much, but it is important to me that I catch bad habits now. This code basically counts to 1000 and displays it as text.

    I wanted to know if you can grade me on design and efficiency and help me see a different perspective if you have one.

    Do you think the way I approached this is reasonably efficient?

    I haven't messed too much with creating my own methods. So I was wondering, if I was to take it more on that approach, what methods would you have made?


    Go ahead and rip me apart, I am ready to grow.


    Java Code:
    public class SumOfLetters1000 {
    
        public static void main(String[] args) {
    
            String countString;
            String display = "";
            int ones, teens, tens, hundreds;
    
            for (int count = 1; count <= 1000; count++) {
                display = "";
                hundreds = 0;
                tens = 0;
                teens = -1;
                ones = 0;
                countString = Integer.toString(count);
                ones = Integer.parseInt(countString.substring(countString.length()-1, countString.length()));
    
                if (count == 1000) {
                    display += "One Thousand";
                    System.out.println(display);
                    break;
                }
                
                if (count > 99) {
                    hundreds = Integer.parseInt(countString.substring(countString.length()-3, countString.length()-2));
                }
                
                if (count > 19) {
                    tens = Integer.parseInt(countString.substring(countString.length()-2, countString.length()-1));
                }
                
                if (count > 9) {
                    if (Integer.parseInt(countString.substring(countString.length()-2, countString.length()-1)) == 1) {
                        teens = ones;
                        ones = 0;
                    }
                }
    
    
    
                switch (hundreds) {
                    case 1:
                        display += "One Hundred ";
                        if (count != 100) {
                            display += "and ";
                        }
                        break;
                    case 2:
                        display += "Two Hundred ";
                        if (count != 200) {
                            display += "and ";
                        }
                        break;
                    case 3:
                        display += "Three Hundred ";
                        if (count != 300) {
                            display += "and ";
                        }
                        break;
                    case 4:
                        display += "Four Hundred ";
                        if (count != 400) {
                            display += "and ";
                        }
                        break;
                    case 5:
                        display += "Five Hundred ";
                        if (count != 500) {
                            display += "and ";
                        }
                        break;
                    case 6:
                        display += "Six Hundred ";
                        if (count != 600) {
                            display += "and ";
                        }
                        break;
                    case 7:
                        display += "Seven Hundred ";
                        if (count != 700) {
                            display += "and ";
                        }
                        break;
                    case 8:
                        display += "Eight Hundred ";
                        if (count != 800) {
                            display += "and ";
                        }
                        break;
                    case 9:
                        display += "Nine Hundred ";
                        if (count != 900) {
                            display += "and ";
                        }
                        break;
    
                }
    
                switch (tens) {
    
                    case 2:
                        display += "twenty ";
                        break;
                    case 3:
                        display += "thirty ";
                        break;
                    case 4:
                        display += "forty ";
                        break;
                    case 5:
                        display += "fifty ";
                        break;
                    case 6:
                        display += "sixty ";
                        break;
                    case 7:
                        display += "seventy ";
                        break;
                    case 8:
                        display += "eighty ";
                        break;
                    case 9:
                        display += "ninety ";
                        break;
    
                }
                switch (teens) {
                    case 0:
                        display += "ten";
                        break;
                    case 1:
                        display += "eleven";
                        break;
                    case 2:
                        display += "twelve";
                        break;
                    case 3:
                        display += "thirteen";
                        break;
                    case 4:
                        display += "fourteen";
                        break;
                    case 5:
                        display += "fifteen";
                        break;
                    case 6:
                        display += "sixteen";
                        break;
                    case 7:
                        display += "seventeen";
                        break;
                    case 8:
                        display += "eighteen";
                        break;
                    case 9:
                        display += "nineteen";
                        break;
                }
    
                switch (ones) {
                    case 1:
                        display += "one";
                        break;
                    case 2:
                        display += "two";
                        break;
                    case 3:
                        display += "three";
                        break;
                    case 4:
                        display += "four";
                        break;
                    case 5:
                        display += "five";
                        break;
                    case 6:
                        display += "six";
                        break;
                    case 7:
                        display += "seven";
                        break;
                    case 8:
                        display += "eight";
                        break;
                    case 9:
                        display += "nine";
                        break;
    
                }
                System.out.println(display);
            }
    
        }
    }
    Last edited by AcousticBruce; 12-20-2010 at 03:15 PM.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    My solution used the same method for hundreds and for ones.

    Java Code:
    public class Problem17 {
    	public static void main(String[] args) {
    		int sum = 11; // "one thousand" = 11 letters
    		for(int i = 1; i < 1000; i++) {
    			if(i % 100 == 0) {
    				sum += (numberOfLetters1to19(i / 100)) + 7; // "hundred" = 7 letters
    			}
    			else if(i < 20) {
    				sum += numberOfLetters1to19(i);
    			}
    			else if(i < 100) {
    				sum += numberOfLetters20to90(i);
    				sum += numberOfLetters1to19(i % 10);
    			}
    			else {
    				sum += numberOfLetters1to19(i / 100) + 10; // "hundred and" = 10 letters
    				if(i % 100 < 20) {
    					sum += numberOfLetters1to19(i % 100);
    				}
    				else {
    					sum += numberOfLetters20to90(i % 100);
    					sum += numberOfLetters1to19(i % 10);
    				}
    			}
    		}
    		System.out.println(sum);
    	}
    	public static int numberOfLetters1to19(int i) {
    		switch(i) {
    			case 0: return 0; 
    			case 1: return 3;
    			case 2: return 3;
    			case 3: return 5;
    			case 4: return 4;
    			case 5: return 4;
    			case 6: return 3;
    			case 7: return 5;
    			case 8: return 5;
    			case 9: return 4;
    			case 10: return 3;
    			case 11: return 6;
    			case 12: return 6;
    			case 13: return 8;
    			case 14: return 8;
    			case 15: return 7;
    			case 16: return 7;
    			case 17: return 9;
    			case 18: return 8;
    			case 19: return 8;
    			default: return -1;
    		}
    	}
    	public static int numberOfLetters20to90(int i) {
    		switch(i / 10) {
    			case 2: return 6;
    			case 3: return 6;
    			case 4: return 5;
    			case 5: return 5;
    			case 6: return 5;
    			case 7: return 7;
    			case 8: return 6;
    			case 9: return 6;
    			default: return -1;
    		}
    	}
    }
    Edit: Assuming you're doing this for Project Euler #17, why display the result as text when the problem asks for the letter count?
    Last edited by Iron Lion; 12-20-2010 at 07:43 PM.

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

    Default

    Here's my solution for you to study an alternative solution. It can process numbers up to a billion.

    Java Code:
    public class Words {
    	
    	private static final String[] simple= { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
    					"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
    					"eightteen", "nineteen" };
    	
    	private static final String[] tens= { null, null, "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    	
    	public static String say(int number) { return say(number, true); }
    
    	private static String say(int number, boolean sayZero) {
    		
    		if (number < 0)				return "minus "+say(-number, sayZero);
    		
    		if (number == 0)			return sayZero?simple[number]:"";
    
    		if (number < 20) 			return simple[number];
    		
    		if (number < 100)			return tens[number/10]+say(number%10, false);
    	
    		if (number < 1000) 			return say(number/100, false)+"hundred"+say(number%100, false);
    
    		if (number < 1000000) 		return say(number/1000, false)+"thousand "+say(number%1000, false);
    
    		if (number < 1000000000)	return say(number/1000000, false)+"million "+say(number%1000000, false);
    		
    		return say(number/1000000000, false)+"billion "+say(number%1000000000, false);
    	}
    	
    	public static void main(String[] args) {
    		
    		System.out.println(say(1234));
    		System.out.println(say(1234567894));
    		System.out.println(say(1000));
    		System.out.println(say(-321));
    		System.out.println(say(-Integer.MAX_VALUE));
    	}
    }
    kind regards,

    Jos
    Last edited by JosAH; 12-20-2010 at 07:43 PM.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    Quote Originally Posted by Iron Lion View Post
    My solution used the same method for hundreds and for ones.

    Java Code:
    ...
    Edit: Assuming you're doing this for Project Euler #17, why display the result as text when the problem asks for the letter count?
    It was for Project Euler but this is in the form of displaying the text. I take Project Euler as a place to learn who to program by challenging myself. So a lot of times I do a bit more then it asks manly because I want to see how the program responds. So its a learning experience more than fun, but I find it incredibly fun also.



    Quote Originally Posted by JosAH View Post
    Here's my solution for you to study an alternative solution. It can process numbers up to a billion.

    Java Code:
    ...
    kind regards,

    Jos
    This is nice Jos. And I will study it also. Thank you both for sharing your code.

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Here's my solution for you to study an alternative solution. It can process numbers up to a billion.

    Java Code:
    public class Words {
    	...
    kind regards,

    Jos

    It actually returned this

    onethousand twohundredthirtyfour
    onebillion twohundredthirtyfourmillion fivehundredsixtyseventhousand eighthundredninetyfour
    onethousand
    minus threehundredtwentyone
    minus twobillion onehundredfourtysevenmillion fourhundredeightythreethousand sixhundredfourtyseven

    Process finished with exit code 0

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    It actually returned this
    Is anything wrong with it? (note that I'm not a native English speaker so teach me).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Is anything wrong with it?...
    Jos
    That depends on what you were trying to accomplish. Basically I copied the code and when I ran it it didn't display all the numbers in text.

    It seemed to only pic a couple numbers to display and it says "minus" in areas.

    What was your code supposed to do? I will have more time to examine it tonight. It seems to be very efficient compared to mine. You also have some new code that I am unfamiliar with. So I will learn tonight.

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    That depends on what you were trying to accomplish. Basically I copied the code and when I ran it it didn't display all the numbers in text.

    It seemed to only pic a couple numbers to display and it says "minus" in areas.

    What was your code supposed to do? I will have more time to examine it tonight. It seems to be very efficient compared to mine. You also have some new code that I am unfamiliar with. So I will learn tonight.
    Well, it has to display five numbers (which it did) and two of them are negative so that is also correct. Please elaborate if you find any errors.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Well, it has to display five numbers (which it did) and two of them are negative so that is also correct. Please elaborate if you find any errors.

    kind regards,

    Jos
    I didn't mean to say there are errors. I just do not understand the code YET. I was simply asking what it was supposed to do. Now I know. It displays 5 numbers. I cant wait to check it out tonight to see what I can do with it.

    thanks

Similar Threads

  1. Writing a programs
    By smray7 in forum New To Java
    Replies: 7
    Last Post: 04-28-2011, 08:49 AM
  2. some error make me mind
    By andyzju in forum New To Java
    Replies: 0
    Last Post: 11-20-2010, 04:22 AM
  3. Replies: 8
    Last Post: 11-05-2010, 02:52 PM
  4. test automation programs
    By Ravanelly in forum Advanced Java
    Replies: 8
    Last Post: 01-27-2010, 02:49 AM
  5. I need a simple programs
    By mikau in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 04:37 AM

Posting Permissions

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