r/javahelp • • Feb 24 '15

College Project

Alright so im in an intro java class, and we got partnered on a project and my 2 partners are completely clueless on how to do java coding, i have a very small minute experience. I got assigned the part of making a custom package, class, and method to show to the class. How would i go about this? My teacher says that if i can find one online and explain it I could do that but im clueless what to do as just 4 weeks ago i was learning how to get CMD to say hello world.

1 Upvotes

5 comments sorted by

3

u/darkpool2005 Intermediate Brewer Feb 24 '15

Might be in for a rough ride, but since you've claimed to get "Hello World" in command, I want you to tell me each part of this code snippet:

package reddit.is.fun;

public class HelloWorld {
    private int myNumber = 100;

    public static void main(String[] args) {
        System.out.println("Hello World!  My number is " + myNumber);
    }
}

Once you understand each part of this snippet, we can move forward

3

u/Balthalzarzo Feb 24 '15

the package is reddit is fun, the class is helloworld.

the private int is your number and it equals 100. the command will print hello world it will say my number is + the number.

We've done harder stuff than that so far. including using the joption pane and the scanner for dialogs and stuff. were at 10-15 lines of code problems and stuff. Were doing alot of debugging. I may explain stuff stupidly but i can usually understand whats going on in the code so far.

2

u/darkpool2005 Intermediate Brewer Feb 24 '15

Good to know :)

I got assigned the part of making a custom package, class, and method to show to the class

With that, you already know how to do 1/3rd of your task. I'll leave you to figure out which part that is.

Moving on, have you been taught how to create different classes and methods, beyond the normal HelloWorld class?

2

u/Balthalzarzo Feb 24 '15 edited Feb 24 '15

We might have, but so far nothing extensive, we are going to learn more of that soon. im really just confused on what i should make and what it should do

1

u/darkpool2005 Intermediate Brewer Feb 24 '15

Well, it doesn't need to be complex at all. Usually, a method that adds two ints (or doubles, doesn't matter) together is proof enough.

An example of a method:

public int add(int a, int b){
    // Run your code here and return a result
}

Pretty straight forward! You'll need to implement the logic and return the correct value. You also need to place this into it's own class, but lets say the main & this method are together in the same class:

public class CustomClass {
    public static void main(String[] args){
        System.out.println("2 + 3 equals " + add(2,3));
    }
    public static int add(int a, int b){
        //Put your logic here and return the correct value
    }
}

When you've implemented the add method, you've completed that portion of your assignment. Next would be moving it into it's own class, which I'll go over when you've got the above working (or at least show me was the guts of the add method are!)