HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

And the little one said "Roll over"

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thesaidrollonelittleandover

Problem

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;
                        }
    }

}

}

Solution


  • Class names should be PascalCase (song99bottles -> Song99Bottles)



-
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.

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

-
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();

-
About lyrics() code, see Roger answer.

Code Snippets

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 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();
    }

    // [...]

Context

StackExchange Code Review Q#45346, answer score: 15

Revisions (0)

No revisions yet.