I don't understand java at all

Discussion in 'Mac Programming' started by TuffLuffJimmy, Jan 19, 2010.

  1. TuffLuffJimmy macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #1
    Class? Method? Parameter? I can't get my feeble noggin around these things at all.

    I'm taking a java class at school and we've been given an assignment to work on. I don't really have time to talk to the teacher as she's booked solid. I knew I should have made an appointment earlier!

    So I know how much you guys hate to be bothered with a newb's homework, but I thought I'd give it a shot. I'm not looking for anyone to write my code for me as that would be counterproductive since I will be having a midterm and final in the class.

    So I'll post the instructions my teacher gave and then post the minuscule bit of code I've done (and I'm sure it's wrong as Eclipse gives me errors that I can't seem to fix, try as I might).

    As you can see I'm stuck on step 1.... but maybe after a little help from you guys I'll be able to knock the rest of the code out without anyone holding my hand.
    Code:
    public class Project1 {
        public static void main(String[] args) {
            public static void myUserName()
            {
                System.out.println("myLastNamej")
            }
        }
    //note that myLastNamej is just a place holder for my actual last name
    }
    
    I don't want to start with the other steps until I've perfected the first.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    You've put one method definition inside another. Some languages allow that sort of thing. Java is not one of them. Pull myUserName out of main, then invoke it in main like:
    Code:
    myUserName();
    -Lee

    Class - the building block of object oriented programming. This defines a set of methods and variables, some to be used at the class level (static is used for class methods and variables in java) and some that only apply to an instance of the class. The class is a cake recipe an instance/object is one specific cake made from the recipe.

    Method - A set of code that is run as a unit, normally acting on a class or methods variables. This normally performs some specific task like getting, setting, displaying, etc. a variable. It could also do something more complex like launch a missle or fax your aunt Wilma.

    Parameter - a piece or data you pass to a method. For a method setIcingColor you might need to pass the color, either as a string or a special icingColor type. A method may have many parameters (but only one return value).

    Edit:
    my suggestion above covers 2.

    For 3. you just need to define another method like myUserName and print the things requested.
    For 4. you just need to define a method that returns String, return a String literal, and assign the result to a String back in main.
    For 5. you'll need to define a seperate class, or instantiate an instance of the existing class in it's own main(). This is not a problem, just a little awkward.
    For 6. you need to setup a method that takes one parameter. Between the () put something like "int x". then either add 1-10 in one long line or do it in a loop if you've covered loops. This will always be 55, but I don't think you are supposed to use 55. Then multiply the result by x, or whatever you name your parameter. Return the result.
    7. is pretty straight-forward after the rest. Parameter like 6., then take the modulus by a small number (I won't tell you which) and compare to 0 to determine if the parameter is even or odd. Return true or false. In main, you can put this call right in an if or assign the result to a boolean and test that.
     
  3. TuffLuffJimmy thread starter macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #3
    Thanks very much, Lee! You're a great help!
    See I thought that everything needed to be contained within the main method. All of my teacher's examples (from what I recall) and the few bits of example code we've been given are inside of main, but I think that's because no other methods are declared throughout the examples. Which seems a little unfair to me as we're being tested on something a bit more advanced than we've done, but she is the teacher and it's not up to me.


    That bit of code is working now! Thanks sir!
    This is what I have now:
    Code:
    public class Project1
    {
        public static void myUserName()
        {
            System.out.println("myLastNamej");
        }
        public static void mathConstants()
        {
            System.out.println("PI = " + Math.PI);
            System.out.println("e = " + Math.E);
        }
        public static void myUserName2()
        {
        }
        public static void main(String[] args)
        {
            myUserName();
            mathConstants();
        }
    }
    I think I'm catching onto this thing!

    My issue I'm running into now is the third method (step 4). She wants it to return my user name, not print it out. I'm not really sure what the difference is, other than the command of course. I'm not familiar with a return command (or maybe I'm just too tired and glazing over my notes).
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    See my replies above, they were in an edit. To return something, replace void in the method signature (the line you define static, public, method name, etc.) with the type you want to return. Then use the keyword return followed by the value/expression you want to return. In main, declare a String and assign myUserName2() to it. Then display the String you declared. Alternately, you could use + to concatenate "myUserName2 returned: " and the call to the method, using no temporary value to store the result, but that may not have been covered.

    -Lee
     
  5. TuffLuffJimmy thread starter macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #5
    Code:
    public static String myUserName2(string "myLastNamej")
        {
        }
    So I replaced void with String, but I'm getting an error and I'm still not exactly sure what goes in the brackets on the next lines.

    Sorry I'm being such a bother, you're a huge help, Lee!
     
  6. TuffLuffJimmy thread starter macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #6
    Thanks again for all your help, Lee. I got it all finalized and got 100% (she has a grading script).

    Code:
    public class Project1
    {    
        public static void main(String[] args) {
        
        
            myUserName();
            mathConstants();
            System.out.println(myUserName2());
        }
        public static void myUserName()
            {
            System.out.println("myLastNamej");
            }
        public static void mathConstants()
            {
            System.out.println("PI = " + Math.PI);
            System.out.println("e = " + Math.E);
        }
        public static String myUserName2() {
        return "myLastNamej";
        }
        public int first10(int a) {
            int b = (1+2+3+4+5+6+7+8+9+10)*a;
            return b;
        }
        public boolean isEven(int c) {
            if (c%2==0) {
                return true;
                
            }
            else {
                return false;
            }
        }
            }
    
    
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    I am glad you got it working and got full credit for the assignment. Now you should reflect on why the assignment was such a mystery to you.

    Do you have a text book that is supposed to serve as a supplement to the lectures the teacher is providing? Have you been pointed to online java documentation? i.e. http://java.sun.com/docs/books/tutorial/java/index.html

    Since you referred to your instructor as your teacher and not a professor, said school rather than college/university, etc. I'm guessing this is for a high school course. If that's the case, i can only imagine that there is some sort of text book. Even if there aren't explicit assignments to read from it, i would imagine it lays a lot of this out for you, especially the terminology.

    You already stated that you should have made an appt with your teacher earlier, which is also very true. I would make a standing appointment with her every week, twice a week, etc. even if you don't think you'll need it. Worst case you have no problems at the time and discuss some concepts with her to make sure you understand what's going on.

    The moral is that you shouldn't find yourself totally out of your bearings when you get an assignment, and you should have a way out of that state if you find yourself there. I and others here are certainly glad to help, and you shouldn't hesitate to post again if you are stuck, but you should have resources other than message boards on the internet if you are enrolled in a class where you should be learning this stuff.

    Good luck.

    -Lee
     
  8. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #8
    May I suggest Head First Java as a way to wrap your head around all these concepts? :)
     
  9. dasmb macrumors regular

    Joined:
    Jul 12, 2007
    #9
    Get Head First Java. It's obvious you need a lot of basics, and that book will help you. The Head First series is weird at first -- they use comics and "humor" to break up the monotony of text and explain every concept from multiple angles, which gets repetitive. You are supposed to read until you feel you understand fully, then skip over the redundant parts until you're confused again. In essence, it's training wheels for difficult concepts and it's highly effective. Just know that you'll never read it again once you're done with it.

    If you were a procedural programmer already, I'd suggest Bruce Eckels' Thinking in Java. Java is an Object Oriented language, and the best programmers are the ones who learn Objects as well as syntax. That's what Eckels' book tries to do -- align the concepts of OO Design with the basics of language syntax. For example, it trains you to use design patterns as if they were elementary syntax (which in some other languages they are -- see delegates and properties in C#). However, if you're not already pretty good at basics like looping, conditionals and data structures, TiJ isn't your best choice.

    Object Orientation is not a gimmick. If you are thinking about programming as a career, OO concepts are the building blocks upon which modern design is built. You need to understand OO to work with modern frameworks, understand event driven systems, understand Aspect Oriented Programming or Composite Oriented Programming, understand ObjC Messages and Protocols, etc.
     
  10. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #10
    I've found "The Java Programming Language, Fourth Edition" and the "Java Pocket Guide" indispensable for learning the ins and outs of Java.
     
  11. TuffLuffJimmy thread starter macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #11
    Nah, Lee I actually do go to college, I just talk like I'm still in high school. Anyway the truth is I'm taking some hard classes this term I figured I could ignore my Java class and focus on the harder things like discrete math (a class which I had to learn LaTeX for), since I picked up HTML and CSS so quickly I figured java wouldn't be an issue.

    So over the past two days I've been trying to cram everything we learned in my Java labs and lectures, but it was proving to be impossible. Another mistake I made was buying the online version of the textbook Big Java, 3rd Edition. I thought that I would be able to get by since I read these forums so much, but reading a book online really is no fun.

    So thanks for all the advice everyone, I'm headed down to my library now to check out some of those recommended books and on my way back I'll swing by my professor's office and ask about scheduling regular appointments.
     
  12. jtara macrumors 65816

    Joined:
    Mar 23, 2009
    #12
    I'm shocked that a class would dive-in like this without first giving an introduction to object-oriented programming and terminology. Did you skip a prerequisite class?

    Understanding terminology is SO important, because so many terms in programming are "overloaded" on everyday terminology, and means something entirely different within that context.
     
  13. TuffLuffJimmy thread starter macrumors G3

    TuffLuffJimmy

    Joined:
    Apr 6, 2007
    Location:
    Portland, OR
    #13
    Kind of. I didn't skip any prerequisites, but I am in higher math classes than I most and math was the only prerequisite for this class. So in the program I'm in I shouldn't be in Java yet, as I skipped over another more basic computing class.
     
  14. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
    Apologies for making assumptions. It's easy to try to read into the language someone uses.

    You haven't mentioned what you're studying, but if it's CS the Java course your taking is hopefully intended to give you a basis for programming that will carry through the rest of your studies. It definitely shouldn't be neglected. As you've probably found out by now, HTML and CSS are modeling/display languages. This is pretty different from a programming language. Off the top of my head, postscript springs to mind as an actual Turing-complete display language, but most of the rest aren't both display and programming languages.

    It's too bad that you got yourself into that situation. Hopefully you have time to catch up now and can be more fastidious about keeping up with this class in spite of your heavy schedule. In terms of a digital copy... online documentation is great. You can jump around to what you need fast, copy and paste, etc. In terms of actually having a reference on a language/concept to refer to and learn from, dead trees really take the cake. You can scribble in the margins, you can read it away from a machine so you can focus on the concepts instead of trying to jump in and program something every time something is introduced, etc. If it's financially feasible, print each chapter as it's being covered so you do have a dead tree version to refer to. If not... well, it's tough. Maybe find someone that dropped the class and offer to buy their book for $1 more than the book store would pay them to buy it back?
    [/QUOTE]

    Sounds like a good plan. Again, good luck.

    -Lee
     

Share This Page