8
\$\begingroup\$

Below is some code I have written today to test out some bits I have learnt. It isn't much nor is it spectacular. Please critique and let me know what I could/should have done differently or anything I could do to improve it so far.

package easy8;

import java.util.Scanner;

public class song99bottles {

public static void main(String[] args) {

// Declare a reference variable of type song99bottles - new object  
song99bottles go = new song99bottles();
// calls the method "queston" for object "go".
go.question();

}

public void question(){

    song99bottles start = new song99bottles();

    String answer;

    System.out.println("Would you like to hear a nursery rhyme?\nPlease enter yes or no:");
    Scanner input = new Scanner(System.in);
    answer = input.next();
    if(answer.equalsIgnoreCase("yes")){
        start.lyrics();
    } else{
        System.out.println("Bye");
    }

}



public void lyrics() {

    int peeps  = 10;
    String intro = "There were ";
    String intro2 = " in the bed and the little one said rollover";
    String fall = "\nSo they all rolled over and 1 fell out.";
    String end = "And the little said 'I'm lonely'.";

    while (peeps > 0){
        System.out.println( intro + peeps + intro2 + fall);
        peeps--;
        if (peeps == 1){
            System.out.println(end);
            return;
                        }
    }


}

}
\$\endgroup\$
2

6 Answers 6

15
\$\begingroup\$
  1. Class names should be PascalCase (song99bottles -> Song99Bottles)
  2. You should indent your code or you will end up with a long code and you don't understand the various { } pairs (and an horrible code!)

    public class song99bottles {
    public static void main(String[] args) {
    // Declare a reference variable of type song99bottles - new object  
    song99bottles go = new song99bottles();
    // calls the method "queston" for object "go".
    go.question();   
    }
    // [...]
    

    if you want to follow Code conventions, you code should look like this

    public class Song99Bottles {
    
        public static void main(String[] args) {
            // Declare a reference variable of type song99bottles - new object
            Song99Bottles go = new Song99Bottles();
            // calls the method "queston" for object "go".
            go.question();
        }
    
        // [...]
    

    Personally i follow the new-line style, but you will see with time you will get "your style".

    i hope you understand the concept. Read wikipedia page.

  3. You should give to your variables a more descriptive name, you should understand the scope of the variable by just reading the name. (song99bottles go = new song99bottles(); go? What is? a countdown? I could call it song (example))


About your code:

  1. You create two song99bottles objects, why? One inside main and one inside question(). You should move question code inside main because song99bottles class should just play the song, the main will take care if the user want or not to play the song. Then in your main, just call go.lyrics();

  2. About lyrics() code, see Roger answer.

\$\endgroup\$
8
  • \$\begingroup\$ Java Code Style Conventions require that the opening brace for a compound statement goes at the end of the line, not on a new line. Your should be example code is wrong.... \$\endgroup\$
    – rolfl
    Commented Mar 25, 2014 at 21:54
  • 1
    \$\begingroup\$ I will edit with that. But i should admit i always follow the "new line" style, i thought the code more clear. \$\endgroup\$ Commented Mar 25, 2014 at 21:57
  • \$\begingroup\$ I understand, and I have worked with both. Two comments though... if you are producing 'example code' it should follow conventions.... and, with the { on the next line it looks like C#. Also, by the way, empty-line before method declarations ;-). Your answer is good, it already has my +1. \$\endgroup\$
    – rolfl
    Commented Mar 25, 2014 at 22:02
  • \$\begingroup\$ About empty-line, yes.. i think i deleted the line by error... Fixed thanks! \$\endgroup\$ Commented Mar 25, 2014 at 22:08
  • \$\begingroup\$ Might as well update my answer too... \$\endgroup\$ Commented Mar 25, 2014 at 22:08
10
\$\begingroup\$

Taking Roger's answer as a starting point, you can also only use one string in the lyrics() function, because you always use intro2 before fall. If you use one string, you save an allocation and concatenation.

String intro = "There were ";
String fall  = " in the bed and the little one said rollover"
               + System.lineSeparator()
               + "So they all rolled over and 1 fell out.";
String end   = "And the little said 'I'm lonely'.";

for(int peeps = 10; peeps > 0; peeps--){
    System.out.println(intro + peeps + fall);
}
System.out.println(end);
\$\endgroup\$
3
  • \$\begingroup\$ ... I don't even know what to say... Thanks. \$\endgroup\$ Commented Mar 25, 2014 at 21:55
  • \$\begingroup\$ You might want to get the line break sequence from the System object instead of hard coding a newline in the middle of the 'fall' string. \$\endgroup\$
    – atk
    Commented Mar 26, 2014 at 1:56
  • \$\begingroup\$ @atk Like this? It's kinda verbose, but it might save a cross platform headache or two. \$\endgroup\$ Commented Mar 26, 2014 at 2:46
3
\$\begingroup\$

There are a number of improvements you can make, but removing the declaration for peeps in lyrics() and changing the while loop to a for loop would be a good start:

for(int peeps=10;peeps>0;peeps--) {
    System.out.println( intro + peeps + intro2 + fall);            
}
System.out.println(end);
\$\endgroup\$
3
\$\begingroup\$

Suggestion to code

  1. Do not create Song99bottles twice, from question() you can call lyrics()
  2. Do not + strings, it creates an unbelievable overhead of strings in the stack (memory). Use a string formatter String.format("format %d", integer)
  3. Do not use comments unless the really matter. Comments are a sign of bad names and too few funtions, see this http://ptgmedia.pearsoncmg.com/images/9780132350884/samplepages/0132350882_Sample.pdf
\$\endgroup\$
0
1
\$\begingroup\$

In addition to the other good advice I see here, I would rename your lyrics method to imply exactly what it does. For example printLyrics().

\$\endgroup\$
0
\$\begingroup\$

The only thing I can think to add to others' is that the comments in the main method are superfluous.

// Declare a reference variable of type song99bottles - new object  
song99bottles go = new song99bottles();

It's obvious you're declaring a reference here. You're not adding much by saying it.

// calls the method "queston" for object "go".
go.question();

This is worse. This is like saying x = 10; // Sets x to 10. If you're set on leaving some comments it'd be more beneficial to leave some in the other methods with more logic involved like the lyrics method.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.