Page 2 of 2 FirstFirst 1 2
Results 21 to 28 of 28

Thread: Java Help.     submit to reddit submit to twitter

  1. #21
    netz
    Guest

    Wholeheartedly agree with both of you, Correction and Kaylia. Regrettably, I've seen similar things done. I wasn't suggesting that learning things like Swing or AWT was a genuinely good idea, but it's not quite impossible to do either of those without resorting to Kaylia's instruction strategy first. Perhaps the instructor picked that route just so that students could easily see a result to the work they've invested? I don't know.

    Quote Originally Posted by Kaylia View Post
    None of the thing I mentioned require more than "if/else", while loop and basic variable type. I'm not asking to understand every search algorithm by the time you end your first class, but you should at least be able to program a basic one on your own.
    Yeah, you can get away with arrays and linearly searching them. No big deal there. Just need to explain how to create them, the if syntax, and how to do a loop. You will get to this in the introductory course. I see what you mean now.

    Basic implementation of a stack, though... you can explain the principles, but this one is honestly more challenging. The prerequisite for this in Java is an understanding of object-oriented design, despite not taking much more than an array and an index.

    Then, after you teach them out to write the stack, you can tell them to forget it and just import java.util.Deque and ArrayDeque instead. Whee, generics.

    Speaking of enumerations (earlier in the thread) and generics and Collections that I indirectly referenced just now, here's a completely useless (to the original poster) solution to the problem presented earlier of containing months in an object. It just shows some basic usage of a couple things I just mentioned; maybe someone can find some value from it. This is explained more fully in Effective Java, 2nd edition, item #30 (enums instead of int constants).

    Spoiler: show

    Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.TreeMap;
    
    /**
     * An enumeration representing the months of the year
     * 
     * Sample usage:
     * 	Month m = Month.MAY;
     *  m = Month.fromNumber(5);
     *  m = Month.fromString("December");
     *
     */
    public enum Month {
    	JANUARY(1), FEBRUARY(2), MARCH(3), APRIL(4), MAY(5), JUNE(6),
    	JULY(7), AUGUST(8), SEPTEMBER(9), OCTOBER(10), NOVEMBER(11), DECEMBER(12);
    	
    	private int monthNumber;
    	
    	Month(int monthNumber) { this.monthNumber = monthNumber; }
    	
    	private static final Map<String, Month> sToE = new HashMap<String, Month>();
    	private static final Map<Integer, Month> iToE = new TreeMap<Integer, Month>();
    	static {
    		for (Month m : values()) {
    			sToE.put(m.toString(), m);
    			iToE.put(m.getNumber(), m);
    		}
    	}
    	
    	/**
    	 * Gets the number of a month for the current month selected by the enum
    	 * @return An integer corresponding to the number of the month
    	 */
    	public int getNumber() {
    		return this.monthNumber;
    	}
    		
    	/**
    	 * Returns the correct member of the enumeration for a given month as a String
    	 * @param theMonth A string representing a month of the year
    	 * @return The month in Enum form
    	 */
    	public static Month fromString(String theMonth) {
    		return sToE.get(theMonth.toUpperCase());
    	}
    	
    	/**
    	 * Returns the correct member of the enumeration for a given month as an integer
    	 * @param theMonth An integer representing a month of the year
    	 * @return The month in Enum form
    	 */
    	public static Month fromNumber(int theMonth) {
    		return iToE.get(theMonth);
    	}
    }

  2. #22
    Banned.

    Join Date
    Jul 2005
    Posts
    17,471
    BG Level
    9
    FFXI Server
    Ifrit
    WoW Realm
    Area 52

    Yeah, stack might be 2nd level material, but my point was simply to let the student get familiarized with object programming, and logical tool. Like you said, I'm sure it's doable, but it's pretty damn confusing to start with everything at the same time.

    Imo, the first programming classes should be something like this:
    Hello world --> read console input --> write a simple class/use method -> write another program using additional stuff (table, case, this. ...etc) --> searching/ordering/recursive program

    After that, there is many direction you can take I guess. I personally wouldnt bother with interface at all, and leave this to another class

  3. #23
    Banned.

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

    In situations like the OP is asking, don't use if, use a switch.

    switch (month) {
    case 1: System.out.println("January"); break;
    case 2: System.out.println("February"); break;
    case 3: System.out.println("March"); break;
    case 4: System.out.println("April"); break;
    case 5: System.out.println("May"); break;
    case 6: System.out.println("June"); break;
    case 7: System.out.println("July"); break;
    case 8: System.out.println("August"); break;
    case 9: System.out.println("September"); break;
    case 10: System.out.println("October"); break;
    case 11: System.out.println("November"); break;
    case 12: System.out.println("December"); break;
    default: System.out.println("Invalid month.");break;
    }

  4. #24
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,218
    BG Level
    8

    Quote Originally Posted by Senoska View Post
    In situations like the OP is asking, don't use if, use a switch.
    He's outputting the digit from the name so the switch is backwards but the idea is correct. It would be more correct to use a switch here though. I'd naturally go for a hash table though, like Correction mentioned.

  5. #25
    netz
    Guest

    Quote Originally Posted by Maguspk View Post
    He's outputting the digit from the name so the switch is backwards but the idea is correct. It would be more correct to use a switch here though. I'd naturally go for a hash table though, like Correction mentioned.
    The hash table just might be a little suspect in the intro to programming course, of course.

  6. #26
    Banned.

    Join Date
    Nov 2009
    Posts
    1
    BG Level
    0

    Quote Originally Posted by Darkslitter View Post
    if(stringVariable.equals("January"))
    spacingintVariable = 1;
    else if(stringVariable.equals("February"))
    spacingintVariable = 2;

    etc etc...

    This great video, thank you for sharing them. I will return to post the video on this forum and you'll find them great.

  7. #27
    Banned.

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

    Quote Originally Posted by cilake View Post
    This great video, thank you for sharing them. I will return to post the video on this forum and you'll find them great.
    Uh.... What?

  8. #28
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    How funny that a bot chose a topic about programming.

Page 2 of 2 FirstFirst 1 2

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 jinkazama in forum General Discussion
    Replies: 28
    Last Post: 2010-01-30, 23:07
  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