Go to Post It is often less about what you say, and more about how you say it. - IKE [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 05-31-2010, 03:02 PM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Porting to Java: enums, clocks, and multi-files

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
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #2   Spotlight this post!  
Unread 05-31-2010, 05:56 PM
davidthefat davidthefat is offline
Alumni
AKA: David Yoon
FRC #0589 (Falkons)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: California
Posts: 792
davidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud of
Re: Porting to Java: enums, clocks, and multi-files

Quote:
Originally Posted by byteit101 View Post
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
__________________
Do not say what can or cannot be done, but, instead, say what must be done for the task at hand must be accomplished.
Reply With Quote
  #3   Spotlight this post!  
Unread 05-31-2010, 10:34 PM
LukeS LukeS is offline
4272 mentor, 1024 alumnus
AKA: Luke Shumaker
FRC #4272
Team Role: Mentor
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Indianapolis, IN
Posts: 60
LukeS is an unknown quantity at this point
Re: Porting to Java: enums, clocks, and multi-files

enums:

sunspotfrcsdk/lib/WPILibJ/enum.txt:
Code:
//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.
Reply With Quote
  #4   Spotlight this post!  
Unread 06-01-2010, 02:52 PM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Porting to Java: enums, clocks, and multi-files

Quote:
Originally Posted by LukeS View Post
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
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #5   Spotlight this post!  
Unread 06-01-2010, 03:26 PM
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,547
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Porting to Java: enums, clocks, and multi-files

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

Last edited by Joe Ross : 06-01-2010 at 03:28 PM.
Reply With Quote
  #6   Spotlight this post!  
Unread 06-02-2010, 10:00 AM
JesseK's Avatar
JesseK JesseK is offline
Expert Flybot Crasher
FRC #1885 (ILITE)
Team Role: Mentor
 
Join Date: Mar 2007
Rookie Year: 2005
Location: Reston, VA
Posts: 3,608
JesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond repute
Re: Porting to Java: enums, clocks, and multi-files

Quote:
Originally Posted by byteit101 View Post
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
Code:
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
Code:
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.
__________________

Drive Coach, 1885 (2007-present)
CAD Library Updated 5/1/16 - 2016 Curie/Carver Industrial Design Winner
GitHub
Reply With Quote
  #7   Spotlight this post!  
Unread 06-02-2010, 03:06 PM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Porting to Java: enums, clocks, and multi-files

Quote:
Originally Posted by JesseK View Post
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
Code:
 public  boolean CanSend()
{
    return (this.lasttime+(1/this.currentHz)<=GetClock()) && this.IsAnyConnected();
}
public void ResetCounter()
{
    this.lasttime=GetClock();
}
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #8   Spotlight this post!  
Unread 06-02-2010, 06:33 PM
LukeS LukeS is offline
4272 mentor, 1024 alumnus
AKA: Luke Shumaker
FRC #4272
Team Role: Mentor
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Indianapolis, IN
Posts: 60
LukeS is an unknown quantity at this point
Re: Porting to Java: enums, clocks, and multi-files

Quote:
Originally Posted by byteit101 View Post
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.
Reply With Quote
  #9   Spotlight this post!  
Unread 06-02-2010, 06:37 PM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Porting to Java: enums, clocks, and multi-files

Quote:
Originally Posted by LukeS View Post
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.
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #10   Spotlight this post!  
Unread 06-05-2010, 07:55 PM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Porting to Java: enums, clocks, and multi-files

I can't seem to get the jar to compile into a robot sample with the add jar method.
Quote:
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)
Code:
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\wpi\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\wpi\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\wpi\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\wpi\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)
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #11   Spotlight this post!  
Unread 06-06-2010, 07:07 AM
timothyb89 timothyb89 is offline
code monkey
FRC #1977 (Loveland High Robotics)
Team Role: Alumni
 
Join Date: Oct 2008
Rookie Year: 2009
Location: Loveland, Colorado
Posts: 45
timothyb89 will become famous soon enough
Re: Porting to Java: enums, clocks, and multi-files

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.
__________________
FRC-DB: A detailed, web-based scouting database for FRC teams with lots of interesting statistics.

Last edited by timothyb89 : 06-06-2010 at 07:10 AM.
Reply With Quote
  #12   Spotlight this post!  
Unread 06-06-2010, 09:48 AM
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Porting to Java: enums, clocks, and multi-files

Quote:
Originally Posted by timothyb89 View Post
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.
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Labview and Java chsrobotics3283 NI LabVIEW 3 03-13-2010 09:36 PM
**FIRST EMAIL**/Java and Orbit Balls *****JAVA DISCUSSION*** Pjohn1959 Programming 37 08-31-2009 03:55 PM
Real time clocks, out of the question? evan_wilson Programming 23 02-27-2007 09:29 AM
porting camera code to 2005 bot controller dwc309 Programming 6 02-28-2006 07:16 PM
pic: Elgin Clocks Car at FIRST Party dez250 Extra Discussion 17 07-13-2005 05:09 PM


All times are GMT -5. The time now is 08:31 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi