Log in

View Full Version : Porting to Java: enums, clocks, and multi-files


byteit101
31-05-2010, 15:02
I was porting some code to Java (a new ZomB interface), and have come across only a few small snags:
1. How do you do enum's? I am getting "enums are not supported in source 1.3 use src 5 to enable" and my switch statement is not working either
2. What is the equivalent of the C++ GetClock() which returns seconds since boot. Currently i am using Timer.getUsClock() / 1000000f but it is kinda bulky
3. Can you turn off the error that classes need to be in their own files? I am hoping I can have a single file for a bunch of public classes

davidthefat
31-05-2010, 17:56
I was porting some code to Java (a new ZomB interface), and have come across only a few small snags:
1. How do you do enum's? I am getting "enums are not supported in source 1.3 use src 5 to enable" and my switch statement is not working either
2. What is the equivalent of the C++ GetClock() which returns seconds since boot. Currently i am using Timer.getUsClock() / 1000000f but it is kinda bulky
3. Can you turn off the error that classes need to be in their own files? I am hoping I can have a single file for a bunch of public classes

I really don't see the point in having all the classes in one file... It just nasty to look at... and try consts

LukeS
31-05-2010, 22:34
enums:

sunspotfrcsdk/lib/WPILibJ/enum.txt:
//Going to use this pattern for enumeration classes, since they are otherwise unsupported

//The typesafe enum pattern
public class Suit {
public final int value;

protected static final int CLUBS_VAL = 0;
protected static final int DIAMONDS_VAL = 1;
protected static final int HEARTS_VAL = 2;
protected static final int SPADES_VAL = 3;

public static final Suit CLUBS = new Suit(CLUBS_VAL);
public static final Suit DIAMONDS = new Suit(DIAMONDS_VAL);
public static final Suit HEARTS = new Suit(HEARTS_VAL);
public static final Suit SPADES = new Suit(SPADES_VAL);

private Suit(int value){
this.value = value;
}
}

WPILIB uses this method throughout, and I used it in our bot.

Classes in one file:
Don't. Trust me, I know where you're coming from, I was there once. You're trying to program in some other language. Wrap your mind around Java, and then, this will seem stupid.

However, you may be interested to know that you can define sub-classes, which are all in the same file.

byteit101
01-06-2010, 14:52
enums:

sunspotfrcsdk/lib/WPILibJ/enum.txt:
...
WPILIB uses this method throughout, and I used it in our bot.

Classes in one file:
Don't. Trust me, I know where you're coming from, I was there once. You're trying to program in some other language. Wrap your mind around Java, and then, this will seem stupid.

However, you may be interested to know that you can define sub-classes, which are all in the same file.

I saw the WPILib enums, but was hoping there was something else.

Ok, how would one make a jar/zip/dll/whateverstaticlibrarysarecalledinjava to house multiple classes in netbeans then? I'm going for the fewest files possible

Joe Ross
01-06-2010, 15:26
Java 1.4 and earlier does not support enums, unfortunately. See the following presentation for more things that are "missing" http://www.usfirst.org/uploadedFiles/Robotics_Programs/FIRST_Place/Workshops_and_Conferences/2010_Assets/JavaForFRCKickoff.pdf

JesseK
02-06-2010, 10:00
I was porting some code to Java (a new ZomB interface), and have come across only a few small snags:
1. How do you do enum's? I am getting "enums are not supported in source 1.3 use src 5 to enable" and my switch statement is not working either
2. What is the equivalent of the C++ GetClock() which returns seconds since boot. Currently i am using Timer.getUsClock() / 1000000f but it is kinda bulky
3. Can you turn off the error that classes need to be in their own files? I am hoping I can have a single file for a bunch of public classes


For #1, use (and repeat for each enum type

public static final <Insert Type Here> eVar1 = new <Insert Type Here>(ctor vars...){
<override/implement any methods here>
};


A simple String/Enum Constant would have to have 2 vars per 'enum', as Luke pointed out

public static final int sVar1 = 1;
public static final String sVar1_str = "Foo";


2.) I'd recommend using System.currentTimeMillis() (or System.nanoTime() if it's supported in 1.3) as soon as the main() method is called and storing it in a globally accessible variable.

3.) Everyone else has pretty much hit is spot on.

byteit101
02-06-2010, 15:06
2.) I'd recommend using System.currentTimeMillis() (or System.nanoTime() if it's supported in 1.3) as soon as the main() method is called and storing it in a globally accessible variable.
.

is this a reference? or would I still have to multiply? the code i'm working on is a timing function
public boolean CanSend()
{
return (this.lasttime+(1/this.currentHz)<=GetClock()) && this.IsAnyConnected();
}
public void ResetCounter()
{
this.lasttime=GetClock();
}

LukeS
02-06-2010, 18:33
Ok, how would one make a jar/zip/dll/whateverstaticlibrarysarecalledinjava to house multiple classes in netbeans then? I'm going for the fewest files possible
JAR - Java Archive. Really it's just a .zip file with a different file extension. It just contains all your compiled classes (same as .a libraries do your compiled objects on *nix). In concept, although, you also need a manifest file describing it's contents. Google `make java jar' for a list of tutorials, I can't personally recommend any of them.

Once you've got you're JAR file made, to `link' your program against it (in NetBeans, anyway) right-click on your project, select properties. From that window select the ``Java Sources Classpath'' category. You should see where the WPILIBJ JAR is already added. Click ``Add JAR/Folder...'', and find your jar.

byteit101
02-06-2010, 18:37
JAR - Java Archive. Really it's just a .zip file with a different file extension. It just contains all your compiled classes (same as .a libraries do your compiled objects on *nix). In concept, although, you also need a manifest file describing it's contents. Google `make java jar' for a list of tutorials, I can't personally recommend any of them.

Once you've got you're JAR file made, to `link' your program against it (in NetBeans, anyway) right-click on your project, select properties. From that window select the ``Java Sources Classpath'' category. You should see where the WPILIBJ JAR is already added. Click ``Add JAR/Folder...'', and find your jar.

Thanks! I was pretty sure that I needed a JAR, but due to my lack of experience with java, was not 100% sure.

byteit101
05-06-2010, 19:55
I can't seem to get the jar to compile into a robot sample with the add jar method.right-click on your project, select properties. From that window select the ``Java Sources Classpath'' category. You should see where the WPILIBJ JAR is already added. Click ``Add JAR/Folder...'', and find your jar.
I get this compile error (but no IDE errors)
init:
No to.jar.file specified.
Using "suite\RobotTemplate_1.0.0.jar"
Deleting directory C:\Documents and Settings\*\My Documents\NetBeansProjects\RobotTemplate\build
clean:
Created dir: C:\Documents and Settings\*\My Documents\NetBeansProjects\RobotTemplate\build
Compiling 1 source file to C:\Documents and Settings\*\My Documents\NetBeansProjects\RobotTemplate\build
C:\Documents and Settings\*\My Documents\NetBeansProjects\RobotTemplate\src\edu\w pi\first\wpilibj\templates\RobotTemplate.java:11: package org.thecatattack.System451.Communication.Dashboard does not exist
import org.thecatattack.System451.Communication.Dashboard .*;
C:\Documents and Settings\*\My Documents\NetBeansProjects\RobotTemplate\src\edu\w pi\first\wpilibj\templates\RobotTemplate.java:23: cannot find symbol
symbol : class ZomBDashboard
location: class edu.wpi.first.wpilibj.templates.RobotTemplate
ZomBDashboard zomB = ZomBDashboard.getInstance(ZomBModes.TCP);
^
C:\Documents and Settings\*\My Documents\NetBeansProjects\RobotTemplate\src\edu\w pi\first\wpilibj\templates\RobotTemplate.java:23: cannot find symbol
symbol : variable ZomBModes
location: class edu.wpi.first.wpilibj.templates.RobotTemplate
ZomBDashboard zomB = ZomBDashboard.getInstance(ZomBModes.TCP);
^
C:\Documents and Settings\*\My Documents\NetBeansProjects\RobotTemplate\src\edu\w pi\first\wpilibj\templates\RobotTemplate.java:23: cannot find symbol
symbol : variable ZomBDashboard
location: class edu.wpi.first.wpilibj.templates.RobotTemplate
ZomBDashboard zomB = ZomBDashboard.getInstance(ZomBModes.TCP);
^
4 errors
C:\Documents and Settings\*\sunspotfrcsdk\ant\compile.xml:48: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)

timothyb89
06-06-2010, 07:07
Looks like the jarfile containing the ZomB classes isn't on your compilation classpath. From the looks of things, simply adding the file via NetBeans like explained above doesn't do anything to the build process (as noted by the dialog). So while the editor's validation may have passed, compilation still failed.

To be honest, I have no idea what the suggested method for adding libraries is for FRC dev. I assume it involves modifying the build file, but so much of that is included from elsewhere it'd be a pain to find the proper place.

Your best/easiest bet, as far as I know, is to copy the source files you need directly into the source for your project and compile the whole thing at once. Assuming the library (ZomB in this case) is simple enough, it won't be pretty, but it should work.
But... if there's a better way, I'd be happy to hear it!

Edit: after looking at the ZomB guide, it looks like copying the ZomB source files is the recommended thing to do.

byteit101
06-06-2010, 09:48
Looks like the jarfile containing the ZomB classes isn't on your compilation classpath. From the looks of things, simply adding the file via NetBeans like explained above doesn't do anything to the build process (as noted by the dialog). So while the editor's validation may have passed, compilation still failed.

To be honest, I have no idea what the suggested method for adding libraries is for FRC dev. I assume it involves modifying the build file, but so much of that is included from elsewhere it'd be a pain to find the proper place.

Your best/easiest bet, as far as I know, is to copy the source files you need directly into the source for your project and compile the whole thing at once. Assuming the library (ZomB in this case) is simple enough, it won't be pretty, but it should work.
But... if there's a better way, I'd be happy to hear it!

Edit: after looking at the ZomB guide, it looks like copying the ZomB source files is the recommended thing to do.

Ah, darn.
About your edit, I am the ZomB creator, and am making more advanced bindings that are 8 file monsters, and copying the source was the way to do it for the old 1 file bindings, although it appears it also will be what you need to do now as well.