Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TuffLuffJimmy

macrumors G3
Original poster
Apr 6, 2007
9,035
167
Portland, OR
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.
  1. Method 1:
    Write a public static method named myUserName. This method does not return anything. This method accepts no parameters. This method simply prints out your engr username (all lowercase letters). For example, mine prints out
    wallacch
  2. Before going on to the next method, you should write a main method and use it to test your myUserName method. Never turn in code that you haven't tested!!!
    So, do you remember how to call a static method?
    (Your main method can be inside the Project1 class OR it can be in a new class all by itself.) As you complete the other 4 methods of this assignment, add more code to this main method to thoroughly test each of the 5 methods of this assignment.
  3. Method 2:
    Write a public static method named mathConstants. This method does not return anything. This method accepts no parameters. This method simply has two print statements. The first one prints out:
    PI = 3.141592653...
    The second print statement prints out:
    e = 2.7182818284...

    I didn't show you all the digits because I didn't want you to type in the digits. I want you to use the constants in the Math class.
    Math.PI
    Math.E
    Just concatenate the String "PI = " with Math.PI (use the + operator).
    Do the same with "e = " and Math.E

    If you do this correctly, the print statements will show 15 digits to the right of the decimal point.
  4. Method 3:
    Write a public static method named myUserName2. This method returns a String. This method accepts no parameters. The only thing this method does is to return a String that is your engr username in all lowercase. Do not print it, return it. For example, my method returns
    "wallacch"
  5. Method 4:
    Write a public non-static method named first10. This method returns an int. Do not use print statements in this method. This method accepts an int parameter. This method adds up the first 10 ints [1 - 10] and then multiplies the sum by the int parameter and returns that number.
    For example, if the int parameter is 3, then this method returns (1+2+...+10) * 3.
    If the int parameter is 42, then this method returns (1+2+...+10) * 42.
  6. Method 5:
    Write a public non-static method named isEven. This method returns a boolean. This method accepts an int parameter. This method returns true if the parameter is even. This method returns false if the parameter is odd. (Hint: use the % operator.)
    This method does NOT print anything, it returns a boolean. Test this method by calling it from a main method. Print out what this method returns in main, not in isEven.

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.
 
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.
 
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).
 
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
 
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!
 
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;
        }
    }
        }
 
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
 
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.
 
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.
 
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.
 
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?
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.
 
Nah, Lee I actually do go to college, I just talk like I'm still in high school.
Apologies for making assumptions. It's easy to try to read into the language someone uses.

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

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

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.

Sounds like a good plan. Again, good luck.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.