Is Java harder than Finnish?

I’m taking a computer class where we are learning Java programming, and am about ready to tear my hair out. Right now we’re working on classes, and I make the class, I make the main-method, but I can’t get them to connect or something!

Does the file name have to be called the same as the name of the class I’m using? For example, right now I’m working on this code:

import java.io.*;
import javagently.*;

public class Uppg8_CS {
public static void main(String[] args) throws IOException {
System.out.print (”Type a number (99 exits the program): ”);
BufferedReader in = Text.open(System.in);
new Number(Text.readInt(in));
Number.add(6);
} // end main
} // end Uppg8_CS

class Number {

private int num = 0;

public Number(int x) {
this.num = x;
} // Number

public void add(int y) {
this.num = this.num + y;
} // add

public void divide(int z) {
this.num = this.num / z;
} // divide

public int value() {
return this.num;
} // value

} // end Number

Eventually I’m supposed to make the program so that it does different things depending on whether you feed in an odd or even number, and so it avoids certain errors that can occur. Right now I’m just trying to make it work in general (and obviously can’t even manage that).

What happens now, is when it comes to the main-method and the line ”Number.add(6)” (that line is just for testing purposes) then it says that it ”cannot make a static reference to the non-static method add(int) from the type Number”. In other words, main is static, Number isn’t, and I can’t change any of that. I have to use the Number class as it was given to me. So I just don’t know how to reference the Number class from the main-method. It seems I’m able to create a new Number, however, so all isn’t lost. I guess.

Help? I will give you a cookie😉

Also, very sorry about the lack of indentation. It’s stupid ol’ wordpress acting up, it always takes away what it considers ”unneccessary” blank lines, spaces, tabs, etc.

~ av christa89 på november 16, 2008 - 20:01.

2 svar to “Is Java harder than Finnish?”

  1. I know this is late, but I couldn’t get on until now.

    As the compiler said, you’re trying to access a non-static method in a static way. Quite obviously you must make the add method into a static method probably by using the static keyword. This throws up an error too, because apparently you can’t use the this object inside a static method.
    That’s about all I know. I don’t do Java. You should be doing C/C++ instead.

  2. Dude. I CAN’T make the add method static. That would mean that my object Number would have to be the same every time, which it isn’t. You no help.

    You should be learning Spanish instead of Finnish since it’s easier. I don’t care what language you actually NEED, just learn the easiest. Yah.

Kommentera

Fyll i dina uppgifter nedan eller klicka på en ikon för att logga in:

WordPress.com Logo

Du kommenterar med ditt WordPress.com-konto. Logga ut / Ändra )

Twitter-bild

Du kommenterar med ditt Twitter-konto. Logga ut / Ändra )

Facebook-foto

Du kommenterar med ditt Facebook-konto. Logga ut / Ändra )

Google+ photo

Du kommenterar med ditt Google+-konto. Logga ut / Ändra )

Ansluter till %s

 
%d bloggare gillar detta: