+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 29

Thread: Java Help     submit to reddit submit to twitter

  1. #1
    E. Body
    Join Date
    Apr 2007
    Posts
    2,135
    BG Level
    7
    FFXI Server
    Valefor
    WoW Realm
    Skullcrusher

    Java Help

    So basically I am in an intro to java programming class and out of the 8 classes my teacher has missed 4 classes and still expects us to know how to code our current project. Needless to say we're all running around mindlessly trying to figure out wtf to do.

    Objectives:
    Gain more experience with interactive programming (prompts and replies)
    Gaining experience in writing a Java program from scratch
    To gain experience with Strings
    To gain experience with validation of inputs
    To gain experience with loops and conditional expressions.

    Functionality: This is quite similar to a program in your textbook with a few enhancements I added…

    Design and implement an application that prints the verses of the song, The Twelve Days of Christmas,” in which each verse adds one line. The familiar first two verses of the song are:
    On the 1st day of Christmas my true love gave to me
    A partridge in a pear tree
    On the 2nd day of Christmas my true love gave to me
    Two turtle doves, and
    A partridge in a pear tree

    You will need to code these twelve Strings into your program as String objects, since at this time we have not yet covered file input / output. (However, if you want a significant bonus, say 30 points, you may try to create a file and read these in one line at a time. You would likely need to put them into an array of Strings…)

    Use a switch statement in a loop to control which lines get printed. Hint: Order the cases carefully and avoid the break statement. Use a separate switch statement to put the appropriate suffix on the day number (1st, 2nd, 3rd, etc.). The final verse of the song involves all 12 days as follows:

    On the 12th day of Christmas, my true love gave to me
    Twelve drummers drumming,
    Eleven pipers piping,
    Ten lords a leaping,
    Nine ladies dancing,
    Eight maids a milking,
    Seven swans a swimming,
    Six geese a laying,
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    Now, in addition to these requirements, you are to accept an input using a Scanner object. This input is used to input your name and your classification (numeric) (1 = Freshman, 2 = Sophomore, …, 5 Graduate Student). You are to prompt the user for a name and a classification. You are to print out the following:

    This output is produced by Joe Schmuck (please use your name),who is a junior at UNF.

    Note, JoeSchmuck is a String input and requires no conversion. ‘3’ is also a character that is inputted, but your Scanner object will take care of this for you.

    Once this line is printed on the screen, you are to skip two lines (two blank lines). Then you are to prompt the user (me) for a number between 1 and 12. If the number the user submits does not lie between 1 and 12, you are to prompt the user to try again, such as providing the message, “Try Again. Enter a number between 1 and 12 inclusive and press Enter” and allow the misguided person (probably me again!) to re-enter another number. (This will require a loop). If the user fails to submit a number between 1 and 12 with three tries (total, including the first attempt and two repeats) then you are to display a message, “Get a Life! Program Terminated” and terminate your program. (Be certain to test this option. I will.)

    Now, once a proper integer is submitted to your program, this number is to be treated by your program as the starting verse of the Christmas song to be printed. Using this number, say 8, print that verse and remaining verses down to and including the first verse.

    The starting verse is the high verse number. Given an 11, you will display verse 11 down to and including verse 1.

    Your specific output should conform to these specifications:
    When a proper number is entered (1 through 12) you are to skip two lines (means have one blank line) and print a ‘header’ line that says:

    Given an input of (enter the number), the following verses are now sung: (skip to next line)
    <follow this text with the appropriate number of verses – one verse per line>
    Again, verses displayed are to proceed downward from the number inputted.



    I honestly have no clue how to attack this project, so far we've only covered println and print methods, and touched on scanners minorly. Any help would be amazing. Dont necessarily want any code written for me just need help trying to figure out where to begin.

  2. #2
    Sponsored by Orton
    Join Date
    Sep 2007
    Posts
    4,518
    BG Level
    7
    FFXI Server
    Valefor

    Rule #1 for any coding project: Google is your friend.

    I'm not even joking. Nearly anything I think of "how do I do X with Y code?" someone has already done it.

    I would go into more detail, but I have to run out of here soon. Only other advice I can give is to think first in "pseudo-code". That is, think about which functions and variables you need, where you will need them, etc. Flow charts and jotting down quick notes will help in any kind of coding project.

    edit: and if you don't happen to know what function you need, that's another "Google is your friend" case. Syntax is always the easy part, knowing how to put the puzzle together correctly is the hard part.

  3. #3

    i don't remember java's syntax and im not doing your homework for you, but this should get you started

    Code:
    int myNumber = getInput();
    
    for (i=myNumber; i>0; i--) {
     switch(myNumber)
      1: print "and a partridge in a pear tree";
      2: print "2 turtle doves";
    ...
    }

  4. #4

    You teacher is a dumbass first off for missing so many classes and secondly for assiging you this project (if what you say about your current knowledge with Java is true.) Follow Quannum's code if you need help because it looks like he's on the right track. Use a while loop instead of a for loop if you want a more visually-easy way to recognize your loops. Comment everything if you need to so you dont fuck up and know where your stuff is.

    Just a recommendation: http://netbeans.org/

    You can download the Netbeans IDE. It should help out alot with your coding (especially for beginners [like myself.])

    PS: The ability to make getters, setters and constructors with a couple of mouse clicks in Netbeans is so awesome.

  5. #5
    Banned.

    Join Date
    Jul 2005
    Posts
    5,836
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    What exactly is he asking you to do with the loop here? I'm sorta confused what he wants. I'd be happy to help though.. Hit me up on aim if you want to talk.

  6. #6
    Banned.

    Join Date
    Jul 2005
    Posts
    5,836
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Also, why would you avoid the break statement? That's just going to make you have a lot of if-ifelse checks and its going to be a piece of shit code. What a stupid teacher & assignment.

  7. #7

    Quote Originally Posted by Senoska View Post
    Also, why would you avoid the break statement? That's just going to make you have a lot of if-ifelse checks and its going to be a piece of shit code. What a stupid teacher & assignment.
    He probably just wants the students to do some repetition or some bullshit like that.

  8. #8
    Radsourceful

    Join Date
    Jul 2007
    Posts
    1,964
    BG Level
    6
    FFXI Server
    Bismarck

    The junk he wants in the switch statement is to avoid a for loop like quannum posted. You need one loop to count up through the verses but the switch can handle the verses themselves.
    Code:
    get input n
    for i = n:12
    print on the nth day of christmas etc etc
    switch (n){
    case 12: Twelve drummers drumming,
    case 11:
    .
    .
    .
    case 1: patridge; break;
    }
    Agreed though, not really a useful thing.

  9. #9

    Code:
    public class MyStupidProject {
       private static String[] m_songLines = {"nothing",
          "a partridge in a pear tree",
          "two turtle doves",
          ...,
          "twelve drummers drumming"};
    
       public static void main() {
          String daySuffix;
    
          for(int day = 1; day <= 12; day++) {
             if(day == 1) {
                daySuffix = "st";
             } else if(day == 2) {
                daySufix = "nd";
             } else if(day == 3) {
                daySufix = "rd";
             }else {
                daySufix = "th";
             }
    
             System.out.println("On the " + day + daySuffix + " day of Christmas my true love gave to me...");
    
             for(int i = day; i > 0; i--) {
                if(i == 1 && day > 1) {
                   System.out.print("and ");
                }
                System.out.println(m_songLines[i]);
             }
          }
       }
    }
    That should get you started. You might have to format the text better, I dunno. Periods and commas to make the song look good?

    I understand why he thinks you'd need a switch but an inner for loop is better, if only for readability.

  10. #10
    Banned.

    Join Date
    Jul 2005
    Posts
    5,836
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Finished the first part. I still have no idea why or HOW he wants to "Avoid the break statement," so do what you will with this:
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication1;
    
    /**
     *
     * @author Seno
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            String[] arrLyrics;
                arrLyrics = new String[13];
                arrLyrics[0] = "On the % day of Christmas, my true love gave to me";
                arrLyrics[1] = "A partridge in a pear tree.";
                arrLyrics[2] = "Two turtle doves, and";
                arrLyrics[3] = "Three French hens,";
                arrLyrics[4] = "Four calling birds,";
                arrLyrics[5] = "Five golden rings,";
                arrLyrics[6] = "Six geese a laying,";
                arrLyrics[7] = "Seven swans a swimming,";
                arrLyrics[8] = "Eight maids a milking,";
                arrLyrics[9] = "Nine ladies dancing,";
                arrLyrics[10] = "Ten lords a leaping,";
                arrLyrics[11] = "Eleven pipers piping,";
                arrLyrics[12] = "Twelve drummers drumming,";
    
            int loopAmount = 13;
    
            for (int i=1; i<loopAmount; i++){
                String day;
                String dailyLyric;
                switch(i){
                        case 1:
                            day = "st";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 2:
                            day = "nd";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 3:
                            day = "rd";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 4:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 5:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 6:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 7:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 8:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 9:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 10:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 11:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            System.out.println(arrLyrics[i]);
                            break;
                       case 12:
                            day = "th";
                            dailyLyric = arrLyrics[0].replace("%", i + day);
                            System.out.println(dailyLyric);
                            for (int x = 12; x>0; x--){
                                System.out.println(arrLyrics[x]);
                            }
                            break;
                }
    
            }
                    
        }
    
    }

  11. #11

    Avoiding the break would be this:
    Code:
    String[] songLines = { "nothing", "a partridge in a pear tree", ..., "twelve drummers drumming" };
    
    for(int day = 1; day <= 12; day++) {
       System.out.println("On the " + day + " day of Christmas my true love gave to me...");
    
       switch(day) {
          case 12:
             System.out.println(songLines[12]);
             //FALL-THROUGH//
          case 11:
             System.out.println(songLines[11]);
             //FALL-THROUGH//
          case 10:
             System.out.println(songLines[10]);
             //FALL-THROUGH//
          ....
          case 1:
             System.out.println(songLines[1]);
             break;
       }
    }
    If you don't use a break then the switch falls-through to the next case automatically.

  12. #12
    Banned.

    Join Date
    Jul 2005
    Posts
    5,836
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    That defeats the purpose of the switch since its going to fall through to the next line. May as well just remove the switch entirely.

    Edit: I see where you're going for but it wont work with this assignment since it doesn't want every verse to fall through to the next, does it? I dont know this christmas song but I looked up the lyrics and it didn't repeat every lyric over again. If thats what he wanted, that'd work fine but it doesn't seem to be the case.

  13. #13

    Quote Originally Posted by Senoska View Post
    That defeats the purpose of the switch since its going to fall through to the next line. May as well just remove the switch entirely.

    Edit: I see where you're going for but it wont work with this assignment since it doesn't want every verse to fall through to the next, does it? I dont know this christmas song but I looked up the lyrics and it didn't repeat every lyric over again. If thats what he wanted, that'd work fine but it doesn't seem to be the case.
    Switch fall-through is useful in some instances... but with this song it's simpler and easier to read when using an inner for-loop.

    Yes, the song repeats on each phrase.

    On the 1st day of Christmas my true love gave to me...
    a partridge in a pear tree

    On the 2nd day of Christmas my true love gave to me...
    two turtle doves,
    and a partridge in a pear tree

    On the 3rd day of Christmas my true love gave to me...
    3 french hens,
    two turtle doves,
    and a partridge in a pear tree

    ...

    On the 12th day of Christmas my true love gave to me...
    twelve drummers drumming,
    ...
    three french hens,
    two turtle doves,
    and a partridge in a pear tree

  14. #14
    Banned.

    Join Date
    Jul 2005
    Posts
    5,836
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    I stand corrected then. Easy switch though.

  15. #15
    I Am, Who I Am.
    Join Date
    Nov 2005
    Posts
    15,997
    BG Level
    9
    FFXIV Character
    Trixi Sephyuyx
    FFXIV Server
    Excalibur
    FFXI Server
    Ragnarok

    I wouldnt worry about trying to learn Java, according to Apple, Flash is an inferior bloated pos, and will soon be history, and Java is right next to that.

    You should spend your time learning LotusScript.

  16. #16
    Banned.

    Join Date
    Jul 2005
    Posts
    5,836
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Code:
    package javaapplication2;
    
    public class Main {
    
        public static void main(String[] args) {
            String[] arrLyrics;
                arrLyrics = new String[13];
                arrLyrics[0] = "On the % day of Christmas, my true love gave to me";
                arrLyrics[1] = "A partridge in a pear tree.";
                arrLyrics[2] = "Two turtle doves, and";
                arrLyrics[3] = "Three French hens,";
                arrLyrics[4] = "Four calling birds,";
                arrLyrics[5] = "Five golden rings,";
                arrLyrics[6] = "Six geese a laying,";
                arrLyrics[7] = "Seven swans a swimming,";
                arrLyrics[8] = "Eight maids a milking,";
                arrLyrics[9] = "Nine ladies dancing,";
                arrLyrics[10] = "Ten lords a leaping,";
                arrLyrics[11] = "Eleven pipers piping,";
                arrLyrics[12] = "Twelve drummers drumming,";
    
            int loopAmount = 13;
    
            for (int i=1; i<loopAmount; i++){
                String day;
                String dailyLyric;
    
                if(i==1){
                    day = "st";
                } else if(i==2){
                    day = "nd";
                } else if(i==3){
                    day = "ed";
                } else {
                    day = "th";
                }
    
               dailyLyric = arrLyrics[0].replace("%", i + day);
               System.out.println(dailyLyric);
    
    
                switch(i){
                       case 12:
                            System.out.println(arrLyrics[12]);
                       case 11:
                            System.out.println(arrLyrics[11]);
                       case 10:
                            System.out.println(arrLyrics[10]);
                       case 9:
                            System.out.println(arrLyrics[9]);
                       case 8:
                            System.out.println(arrLyrics[8]);
                       case 7:
                            System.out.println(arrLyrics[7]);
                       case 6:
                            System.out.println(arrLyrics[6]);
                       case 5:
                            System.out.println(arrLyrics[5]);
                       case 4:
                            System.out.println(arrLyrics[4]);
                       case 3:
                            System.out.println(arrLyrics[3]);
                       case 2:
                            System.out.println(arrLyrics[2]);
                        case 1:
                            System.out.println(arrLyrics[1]);
                            System.out.println();
                            break;
                }
    
            }
    
        }
    
    }
    Output:
    Spoiler: show


    On the 1st day of Christmas, my true love gave to me
    A partridge in a pear tree.

    On the 2nd day of Christmas, my true love gave to me
    Two turtle doves, and
    A partridge in a pear tree.

    On the 3ed day of Christmas, my true love gave to me
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 4th day of Christmas, my true love gave to me
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 5th day of Christmas, my true love gave to me
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 6th day of Christmas, my true love gave to me
    Six geese a laying,
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 7th day of Christmas, my true love gave to me
    Seven swans a swimming,
    Six geese a laying,
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 8th day of Christmas, my true love gave to me
    Eight maids a milking,
    Seven swans a swimming,
    Six geese a laying,
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 9th day of Christmas, my true love gave to me
    Nine ladies dancing,
    Eight maids a milking,
    Seven swans a swimming,
    Six geese a laying,
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 10th day of Christmas, my true love gave to me
    Ten lords a leaping,
    Nine ladies dancing,
    Eight maids a milking,
    Seven swans a swimming,
    Six geese a laying,
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 11th day of Christmas, my true love gave to me
    Eleven pipers piping,
    Ten lords a leaping,
    Nine ladies dancing,
    Eight maids a milking,
    Seven swans a swimming,
    Six geese a laying,
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.

    On the 12th day of Christmas, my true love gave to me
    Twelve drummers drumming,
    Eleven pipers piping,
    Ten lords a leaping,
    Nine ladies dancing,
    Eight maids a milking,
    Seven swans a swimming,
    Six geese a laying,
    Five golden rings,
    Four calling birds,
    Three French hens,
    Two turtle doves, and
    A partridge in a pear tree.


    Edit: You could probably remove that String.replace... Don't know why I didn't.. I don't think it'll matter any but its not really necessary.

    Edit 2: My Java course requires me use jGrasp. I have no idea why our professor thought that using something other than the editor officially backed by Sun was a good idea. Fuck jGrasp.

  17. #17
    blax n gunz
    Join Date
    May 2005
    Posts
    11,161
    BG Level
    9

    Quote Originally Posted by Senoska View Post
    My Java course requires me use jGrasp. I have no idea why our professor thought that using something other than the editor officially backed by Sun was a good idea. Fuck jGrasp.
    Because the real world uses editors other than the ones officially blessed by the vendor? I don't really know why some professors require certain dev environments but knowing how to use anything is a valuable skill.

  18. #18

    vim, all day

  19. #19
    Banned.

    Join Date
    Jul 2005
    Posts
    5,836
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Quote Originally Posted by Correction View Post
    Because the real world uses editors other than the ones officially blessed by the vendor? I don't really know why some professors require certain dev environments but knowing how to use anything is a valuable skill.
    Java is Java. One having almost no correction compared to one that corrects as you go, etc. jGrasp is fucking AWFUL. Nothing about it is good. Its debugging is horrible, just everything about it. Formatting is almost exclusively hand done, etc.

    I mean, I could use notepad to do Java but I'd rather use something that lends a hand here and there.

  20. #20
    blax n gunz
    Join Date
    May 2005
    Posts
    11,161
    BG Level
    9

    Just saying, some dev environments will force apps on you. I've worked at a few. 'Use Eclipse!' 'Use MSDEV!' 'Use emacs!' and so on. Spreading the Sun gospel never got someone a job.

    p.s. fuck vi

Similar Threads

  1. Need help fixing Java Code
    By Minardi in forum General Discussion
    Replies: 18
    Last Post: 2010-03-15, 23:56
  2. Java Help.
    By Zilla in forum General Discussion
    Replies: 27
    Last Post: 2009-11-16, 18:41
  3. Java Programming Help
    By bori in forum General Discussion
    Replies: 9
    Last Post: 2008-09-19, 09:00
  4. Help with Java Programming Project. Get GF off my back plz
    By Azkarin in forum General Discussion
    Replies: 18
    Last Post: 2007-10-10, 10:59
  5. Java HELP :(
    By Maguspk in forum General Discussion
    Replies: 47
    Last Post: 2006-10-24, 10:36
  6. Need java help...
    By Sekkite in forum General Discussion
    Replies: 15
    Last Post: 2005-10-16, 15:58