Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 31 32 [33] 34 35 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95983 times)

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #480 on: December 03, 2010, 04:29:44 am »

Eh, I've done some C# stuff before, and the syntax looks similar, I'm also learning Java in class, some googling helps too, plus help from Dad, but yeah, I'm making this stuff up as I go on, hoping that it'll work. :-P
I don't think using it as an array is the issue, as it throws the same error for "public RGB black = new RGB (0, 0, 0)"
Code: [Select]
main.java:48: cannot find symbol
symbol  : constructor RGB(int,int,int)
location: class RGB
    public RGB black = new RGB (0, 0, 0);
                       ^
Making RGB.red, green or blue static doesn't help either. Here's the whole file.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #481 on: December 03, 2010, 04:51:37 am »

Coming from C, I'd say it can't implicitly convert 0 (int) to 0 (byte).

Which is utter nonsense, but you can try
Code: [Select]
    public RGB (int r, int g, int b) {
        red = (byte)r;
        green = (byte)g;
        blue = (byte)b;
    }
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #482 on: December 03, 2010, 04:54:12 am »

what? C cant convert from int to unsigned char?

this is madness!

unrelated: what kind of info do I generally need to include in a readme file?
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #483 on: December 03, 2010, 05:02:20 am »

I'm pretty sure gcc casts ints to chars. Anyway, that did it, thanks.
God, I was looking for a multiplatform language, and now I've got a headache.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #484 on: December 03, 2010, 05:55:48 am »

what? C cant convert from int to unsigned char?
C does that, but a lot of other implicit casts that I would find logical, it doesn't. That's why I said it, and apparently, gcc is more advanced than java in that respect ;)

In a readme you put input, output, license info and caveats, as far as I'm concerned, but I'm no authority on the matter.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #485 on: December 03, 2010, 07:41:32 am »

gcc is more advanced than java in that respect ;)
Indeed. It took me while to understand that Javac will never ever cast for you.
Anyway, more constructor issues:
Code: [Select]
class Canvas {
    private int hght;
    private int widt;
    private RGB[][] data;

    public Canvas (int w, int h) {
    hght = h;
    widt = w;
    data = new RGB[w][h];
    }
  ...
    public RGB getPixel (int x, int y) {
    return data[x][y];
    }
}
Creating a canvas with "Canvas canv = new Canvas (8,8);" and then trying to run "canv.getPixel(4,4)" gives a NullPointerException, which I believe is thrown when you try to use something as an object when it isn't one.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #486 on: December 03, 2010, 08:06:05 am »

Is
Code: [Select]
data[x][y] defined? You might want to fill the array with new instances, most languages don't do that automatically for you, especially arrays with objects.
Loop over widt, hght in the constructor and make a
Code: [Select]
data[x][y] = new RGB(0,0,0);
« Last Edit: December 03, 2010, 08:27:37 am by Siquo »
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #487 on: December 03, 2010, 08:13:05 am »

a lot of times you may not be able to do data[][] anyways right.

you may prefer to do data[], and reference it by data[x+y*w]
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #488 on: December 03, 2010, 06:48:07 pm »

You must explicitly initialize each element of an array in Java (not including primitives). So you have to iterate over the array and do
Code: [Select]
data[x][y] = RGB(0,0,0);
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #489 on: December 03, 2010, 07:05:53 pm »

a lot of times you may not be able to do data[][] anyways right.
Java can work with 2-dimensional arrays pretty well actually, no need to do complex arithmetic


Also, NEVER make something static unless you know you want to (No when fixing bugs you don't want to do it). Making something static means the compiler will only use 1 variable for all objects. In other words, if you've got a class with a static variable and make 2 instances of that class, changing said variable in one instance will change it in the other as well. If you are absolutely dead sure you don't want the variable to change after you create the instance, use Final instead. This also adds a nice sanity check, because if making a variable of a class static solves your problem, that means you failed to properly create an instance of that class somewhere.


Furthermore in your code, in your canvas class, you start out with
    private int hght;
    private int widt;
    private RGB[] data = new RGB(0, 0, 0)[hght*widt];

Problem here is that you're using hght and widt (why not use height and width to avoid mistyping them?), but you haven't initialized them yet. This will probably mean they are both 0, as the Java compiler is pretty nice to you. But it might just as well result in something weird. What you actually want to do is just define the RGB array, but not assign it until in the constructor, after hght and widt have been set.


Lastly, if you've got to order the point array like you asked several posts back, consider using Array.sort(). When using this on an array of custom classes, you may want to consider implementing a comperator interface, but I'm not too sure on how that works.
« Last Edit: December 03, 2010, 07:14:33 pm by Virex »
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #490 on: December 03, 2010, 11:18:29 pm »

Comparator interfaces are relatively simple. They're simply an interface (like any other interface) that a class will implement. Look through the method summary here: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Comparator.html. Just implement the two methods, then pass an instance of the comparator as an argument to Array.sort(Object[] a, Comparator c).
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #491 on: December 04, 2010, 06:06:23 am »

Furthermore in your code, in your canvas class, you start out with
    private int hght;
    private int widt;
    private RGB[] data = new RGB(0, 0, 0)[hght*widt];
The code has changed quite a bit since then, Canvas looks like this now:
Code: [Select]
class Canvas {
    private int hght;
    private int widt;
    private RGB[][] data;

    public Canvas (int w, int h) {
        hght = h;
        widt = w;
        data = new RGB[w][h];
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
            data[x][yj] = new RGB(0, 0, 0);
            }
        }
    }

    public int getHght () {
        return hght;
    }
    public int getWidt () {
        return widt;
    }
    public RGB getPixel (int x, int y) {
        return data[x][y];
    }
}
Adding that for loop with
Code: [Select]
data[x][y] = new RGB(0, 0, 0); doesn't seem to help. Here's the error, it's the same NullPointerException.
Code: [Select]
ron@Tux ~/code/java/displacment $ java -jar draw.jar
0,0    <-- point 0,0 of the array is fine.
Exception in thread "main" java.lang.NullPointerException
    at Bitmap.<init>(main.java:72)
    at Displacment.main(main.java:140)
Here's the whole file again, I'm pretty sure I can shrink that bitmap class.
e, Whoops, got it, I forgot to initialize one of my arrays.
« Last Edit: December 04, 2010, 06:15:34 am by ILikePie »
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #492 on: December 04, 2010, 08:40:04 am »

Since a struct is just a class that has public elements by default, where would structs be used?
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #493 on: December 04, 2010, 10:11:49 am »

Since a short is just an integer with smaller range, where would shorts be used?

Java's I/O capabilities have failed me (It keeps writing bytes backwards, i.e. instead of writing 196,662 as 36|00|03|00 (like C does), it writes 00|30|00|36), y'all know of a multiplatform language that works with classes and whatnot. How's Python? Does it have types similar to C (eg, char, short, int, and long 1, 2, 4, and 8 bits in length)? And how well does it run on Windows?
« Last Edit: December 04, 2010, 10:18:31 am by ILikePie »
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #494 on: December 04, 2010, 10:27:54 am »

Python shares variable types with C/C++ IIRC.

Shorts are used in situations where longs, doubles, ints etc. would be too long; if you have  a number that'll only be whole and only goes up to 100, why use a double? It's good practice to make variables as small as possible to save on memory space.

Dammit, brain.
Logged
Pages: 1 ... 31 32 [33] 34 35 ... 78