New to Java with a Static Method Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Codeseeker99
    New Member
    • Jan 2008
    • 5

    New to Java with a Static Method Question

    I am new to java and I am trying to learn how to to reduce lines of code that are redundant in a simple file I made. I am trying to figure out how to reduce this code down to 13 methods not including the main and reduce any extra lines without using array's



    [CODE=java]public class Song {
    public static void main(String[] args) {
    One();
    Two();
    Three();
    Four();
    Five();
    Six();
    Seven();
    }
    public static void One() {
    System.out.prin tln("On the 1st day of \"Xmas\", my true love sent to me");
    OneVerse();
    }
    public static void Two() {
    System.out.prin tln("");
    System.out.prin tln("On the 2nd day of \"Xmas\", my true love sent to me");
    TwoVerse();
    OneVerse();
    }
    public static void Three() {
    System.out.prin tln("");
    System.out.prin tln("On the 3rd day of \"Xmas\", my true love sent to me");
    ThreeVerse();
    TwoVerse();
    OneVerse();
    }
    public static void Four() {
    System.out.prin tln("");
    System.out.prin tln("On the 4th day of \"Xmas\", my true love sent to me");
    FourVerse();
    ThreeVerse();
    TwoVerse();
    OneVerse();
    }
    public static void Five() {
    System.out.prin tln("");
    System.out.prin tln("On the 5th day of \"Xmas\", my true love sent to me");
    FiveVerse();
    FourVerse();
    ThreeVerse();
    TwoVerse();
    OneVerse();
    }
    public static void Six() {
    System.out.prin tln("");
    System.out.prin tln("On the 6th day of \"Xmas\", my true love sent to me");
    SixVerse();
    FiveVerse();
    FourVerse();
    ThreeVerse();
    TwoVerse();
    OneVerse();
    }
    public static void Seven() {
    System.out.prin tln("On the 7th day of \"Xmas\", my true love sent to me");
    SevenVerse();
    SixVerse();
    FiveVerse();
    FourVerse();
    ThreeVerse();
    TwoVerse();
    OneVerse();
    }
    public static void OneVerse() {
    System.out.prin tln("a partridge in a pear tree.");
    }
    public static void TwoVerse() {
    System.out.prin tln("two turtle doves, and");
    }
    public static void ThreeVerse() {
    System.out.prin tln("three French hens,");
    }
    public static void FourVerse() {
    System.out.prin tln("four calling birds,");
    }
    public static void FiveVerse() {
    System.out.prin tln("five golden rings,");
    }
    public static void SixVerse() {
    System.out.prin tln("six geese a-laying,");
    }
    public static void SevenVerse() {
    System.out.prin tln("Seven swans a-swimming,");
    }
    }[/CODE]
    Last edited by Ganon11; Jan 14 '08, 01:45 PM. Reason: Please use the [CODE] tags provided.
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Hi,
    this is the .Net forums, I will move the thread to the java forum. Please look at where you post your thread to get more responses.

    Regarding this question, Can you store the strings into an array. and make a method which will print the first n strings in that array. this way you can reduce it much lesser than 13 methods.

    -Shashi

    Comment

    • Codeseeker99
      New Member
      • Jan 2008
      • 5

      #3
      Originally posted by Shashi Sadasivan
      Hi,
      this is the .Net forums, I will move the thread to the java forum. Please look at where you post your thread to get more responses.

      Regarding this question, Can you store the strings into an array. and make a method which will print the first n strings in that array. this way you can reduce it much lesser than 13 methods.

      -Shashi
      Thanks for moving it. I haven't learned array's yet but was told I could use call methods and get it down to 13 methods not including the main just not sure what to change.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        If you set up a static array of strings with the song verses in it, and use an integer parameter for which verse you need in a sing() function that calls itself recursively, you can do this in 2 functions, main included.

        If by 13 methods, your instructor means 13 calls, this might cover it, I'm not sure. You'd have to track the recursive calls.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          You could also do this using a tricksy switch statement. You would pass an integer dayNum to the method, and use this number to print the whole list of gifts. You'll need to decide what order you make your switch statements, what you have to do before the switch statement (if anything), and any special cases you'll have to take care of.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by Ganon11
            You could also do this using a tricksy switch statement. You would pass an integer dayNum to the method, and use this number to print the whole list of gifts. You'll need to decide what order you make your switch statements, what you have to do before the switch statement (if anything), and any special cases you'll have to take care of.
            "The Twelve Days of Christmas" is the standard example of that hack. Outside of that, it's rarely used.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Originally posted by BigDaddyLH
              "The Twelve Days of Christmas" is the standard example of that hack. Outside of that, it's rarely used.
              Yeah, I can't think of very many other practical applications of omitting the break; statement.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Ganon11
                Yeah, I can't think of very many other practical applications of omitting the break; statement.
                Not in Java but in C it was/is a very practical way of manual loop unrolling.
                See Duff's device for a nice example.

                kind regards,

                Jos

                Comment

                Working...