Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-01-2007, 07:50 AM   #1 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
Join Date: Aug 2006
Location: Raleigh, NC
Posts: 1,865
Java/Coding people, please help?

Okay, so I'm starting my first ever programming class this fall quarter and I decided to learn the basics of programming before because I know there'll be geeks in the class I'm going to take and I don't want to be left behind.

Here's what I have thus far:

Applet

public class RootApplet extends java.applet.Applet {
int number;

public void init() {
number = 225;
}

public void paint(java.awt.Graphics g) {
g.drawString("The square root of " +
number +
" is " +
Math.sqrt(number), 5, 50);
}
}


and normal

class NewRoot {
public static void main(String[] arguments) {
int number = 0;
if (arguments.length > 0)
number = Integer.parseInt( arguments[0] );
System.out.println("The square root of "
+ number
+ " is "
+ Math.sqrt(number) );
}
}


I basically used an online tutorial and it went through this. Do the functions (which I'm guessing are class NewRoot and public static void main) always need to come before the commands to the program?

How do you know where to space the brackets and other commands? It seems that spacing is a BIG issue and that's something I've yet to figure out. The tutorial says nothing about the incredible important of spacing and blank lines.

Please help. QQ
__________________
VOCA ME BENEDICTUM !
SANA MEAM ANIMAM !
Kuriin is online now   Reply With Quote
Old 09-01-2007, 11:12 AM   #2 (permalink)
Candiarie
Registered User
 
Candiarie's Avatar
 
Join Date: Jan 2003
Posts: 652
+0 Internets
It's been awhile since I've even looked at Java code but I'm pretty sure this info is accurate.

Java doesn't enforce style at all (only curlys) and some teachers/classes will have different preferences. I was taught using Sun's style and there's some nifty plugins ( Checkstyle - Checkstyle 4.3 ) that will check styles for you.

If I remember right, the class NewRoot is just another class declaration. I don't know shit about applets though, so as far as what you mean by the 'commands to the program' I'm not sure. It seems like if this is your first attempt programming ever this tutorial's throwing a lot at you early on.
Candiarie is offline   Reply With Quote
Old 09-01-2007, 11:40 AM   #3 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
Join Date: Aug 2006
Location: Raleigh, NC
Posts: 1,865
Yeah this is my first time programming at all and I wrote that shit. But, I don't know how to determine how many spaces are needed before a } or next command/line?
__________________
VOCA ME BENEDICTUM !
SANA MEAM ANIMAM !
Kuriin is online now   Reply With Quote
Old 09-01-2007, 12:18 PM   #4 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 477
-16 Internets
Quote:
Originally Posted by Kuriin View Post
Yeah this is my first time programming at all and I wrote that shit. But, I don't know how to determine how many spaces are needed before a } or next command/line?
Ok I'm a Java programmer so I'll answer a few of these questions.

For starters, whitespaces / blanklines are generally used for programming preference. I, personally, don't put a blank space inbetween my zero argument methods. One of my coworkers does. Granted our place doesn't really have set standards. IE: mine looks like

methodCall(); vs. methodCall( );

I will generally insert whitespace between "ideas" or related code.

int myInt = 0;
float myFloat = 0;
double myDouble = 0;

This would be a variable decleration relation. After I declare all my variables then I'll insert a blankspace. It is really a judgement call for when to insert them. Tabbing, however, is mandatory to learn. It makes your code 10x easier to read. As a rule of thumb, anytime you insert a "{" (or one is implied) that equates to tabbing in one. Anytime you insert a "}" (again, or it is implied) you go back one tab. Example of this below.

Code:
//imports public class myClass { public static void myMethod() { int myInt = 0; for(int x=0;x<10;x++) { if(x%2==0) System.out.println("Even"); else System.out.println("Odd"); } } }
Also, if you are taking alot of space to call a method I will generally tab my "(" in my method call. Here is a code snippet from a little game I'm writing in C++ (though tabbing applies to all languages really) where I use this.

Code:
if(currEnemy.getIsOut()==true && currEnemy.getIsDestroyed()==false && x!=size-1) { inputWindow.DrawTriangle( currEnemy.get_X(), currEnemy.get_Y(), currEnemy.get_X(), currEnemy.get_Y()+currEnemy.get_Width(), currEnemy.get_X()+currEnemy.get_Height(), currEnemy.get_Y()+(currEnemy.get_Width()/2), FILLED); }
If you have any other questions feel free to ask/pm. In no time you can start writing some pretty fun stuff. Here is the main game logic! (doesn't include environment or LevelEd source)


>And to the programmers out there: #1 this is a work in progress, #2 its my first C++ program so don't make fun of it!<
Code:
/* WHERE I LAST LEFT OFF-- I have the file handling working, filling a vector with CEnemy classes I need to set up constant for enemy ship size I need to fix the file WRITING (ie: level ed) to get rid of that null space I need to have CEnemy inherit CPlayer's x/y shit I need to write bullet/enemy collision I need to write color changing abilities for player */ #include #include #include #include #include #include "CMUgraphicsLib/auxil.h" #include "CMUgraphicsLib/graphics.h" using namespace std; //Classes class CBullet { public: const static int Bullet_Height=1; const static int Bullet_Width=3; CBullet() { x=-5; y=-5; inFlight = false; } CBullet(int _x, int _y, bool _inFlight) { x=_x; y=_y; inFlight = _inFlight; } int get_X(){return x;} int get_Y(){return y;} void set_X(int _x){x=_x;} void set_Y(int _y){y=_y;} int get_Height(){return Bullet_Height;} int get_Width() {return Bullet_Width;} void set_Flight(bool _inFlight){inFlight = _inFlight;} bool get_Flight(){return inFlight;} private: int x; int y; bool inFlight; }; struct GAME_CONSTANTS { const static int MAXIMUM_ALLOWABLE_BULLETS = 8; const static int PLAYER_HORIZONTAL_SPEED = 10; const static int PLAYER_VERTICAL_SPEED = 10; const static int BULLET_WIDTH = 5; const static int BULLET_SPEED_RATE = 25; }; class CPlayer { public: CPlayer() { x = 0; y = 0; } CPlayer(int _x, int _y) { x = _x; y = _y; } int get_X(){return x;} int get_Y(){return y;} void set_X(int _x){x=_x;} void set_Y(int _y){y=_y;} int get_Height(){return PLAYER_HEIGHT;} int get_Width(){return PLAYER_WIDTH;} int x; int y; private: const static int PLAYER_HEIGHT = 10; const static int PLAYER_WIDTH = 10; }; class CEnemy { public: CEnemy(int _x, int _y, bool _isOut, bool _isDestroyed) { x = _x; y = _y; isOut = _isOut; isDestroyed = _isDestroyed; } CEnemy() { x=0; y=0; isOut=false; isDestroyed=false; } int get_X(){return x;} int get_Y(){return y;} void set_X(int _x){x=_x;} void set_Y(int _y){y=_y;} bool getIsOut(){return isOut;} bool getIsDestroyed(){return isDestroyed;} void setIsOut(bool status){isOut=status;} void setIsDestroyed(bool status){isDestroyed=status;} int get_Height(){return ENEMY_HEIGHT;} int get_Width(){return ENEMY_WIDTH;} private: const static int ENEMY_HEIGHT=10; const static int ENEMY_WIDTH=10; bool isOut; bool isDestroyed; int x; int y; }; //Method dec void BackgroundSetup(window &inputWindow); void ShowScore(window &inputWindow, int score); void WaitNClear(window &inputWindow); void ReDraw(window &inputWindow); void GetPatterns(std::vector &enemies); int stringToInt(string str); CBullet* StartBullets(CBullet aBullets[]); CBullet* DrawAndUpdateBullets(window &inputWindow,CBullet aBullets[]); CBullet* FireBullet(CBullet aBullets[], CPlayer player); void DrawPlayer(window &inputWindow, CPlayer); void DrawEnemies(window &inputWindow, std::vector enemies); void DrawUpdateEnemies(window &inputWindow, std::vector &enemies); window myWindow(700, 400, 5, 5); //Global constants GAME_CONSTANTS GCONS; int main() { std::vector vEnemies; WaitNClear(myWindow); CPlayer player = CPlayer(myWindow.GetWidth()/2,myWindow.GetHeight()/20); CBullet aBullets[GCONS.MAXIMUM_ALLOWABLE_BULLETS]; CBullet* pBullets; GetPatterns(vEnemies); pBullets = StartBullets(aBullets); char cKeyData; keytype ktInput; int counter = 0; do { counter++; for(int x=0;x<=GCONS.MAXIMUM_ALLOWABLE_BULLETS-1;x++) aBullets[x] = *pBullets++; pBullets = 0; ktInput = myWindow.GetKeyPress(cKeyData); if(ktInput == ARROW) { //2 = down //4 = left //6 = right //8 = up switch(cKeyData) { case 2: player.set_Y(player.get_Y()+GCONS.PLAYER_VERTICAL_SPEED); break; case 4: player.set_X(player.get_X()-GCONS.PLAYER_HORIZONTAL_SPEED); break; case 6: player.set_X(player.get_X()+GCONS.PLAYER_HORIZONTAL_SPEED); break; case 8: player.set_Y(player.get_Y()-GCONS.PLAYER_VERTICAL_SPEED); break; } } else if(cKeyData == ' ') //spacebar { pBullets = FireBullet(aBullets,player); for(int x=0;x enemies) { int size = enemies.size(); for(int x=0; x < size; x++) { CEnemy currEnemy = enemies[x]; inputWindow.SetPen(RED); inputWindow.SetBrush(RED); if(currEnemy.getIsOut()==true && currEnemy.getIsDestroyed()==false && x!=size-1) { inputWindow.DrawTriangle( currEnemy.get_X(),currEnemy.get_Y(), currEnemy.get_X(),currEnemy.get_Y()+currEnemy.get_Width(), currEnemy.get_X()+currEnemy.get_Height(),currEnemy.get_Y()+(currEnemy.get_Width()/2),FILLED); } } } void DrawUpdateEnemies(window &inputWindow, std::vector &enemies) { int size = enemies.size(); for(int x=0; x < size; x++) { CEnemy currEnemy = enemies[x]; inputWindow.SetPen(RED); inputWindow.SetBrush(RED); if(currEnemy.getIsOut()==true && currEnemy.getIsDestroyed()==false && x!=size-1) { int newX = enemies[x+1].get_X(); int newY = enemies[x+1].get_Y(); enemies[x].set_X(newX); enemies[x].set_Y(newY); CEnemy currEnemy = enemies[x]; inputWindow.DrawTriangle( currEnemy.get_X(),currEnemy.get_Y(), currEnemy.get_X(),currEnemy.get_Y()+currEnemy.get_Width(), currEnemy.get_X()+currEnemy.get_Height(),currEnemy.get_Y()+(currEnemy.get_Width()/2),FILLED); } else if(currEnemy.getIsOut()==false && currEnemy.getIsDestroyed()==false) { enemies[x].setIsOut(true); break; } } } CBullet* DrawAndUpdateBullets(window &inputWindow,CBullet aBullets[]) { for(int x=0;x<=GCONS.MAXIMUM_ALLOWABLE_BULLETS;x++) { if(aBullets[x].get_Flight()) { inputWindow.SetPen(BLACK); inputWindow.SetBrush(BLACK); aBullets[x].set_X(aBullets[x].get_X()+GCONS.BULLET_SPEED_RATE); inputWindow.DrawRectangle(aBullets[x].get_X(),aBullets[x].get_Y(),aBullets[x].get_X()-aBullets[x].get_Width(),aBullets[x].get_Y()+aBullets[x].get_Height(),FILLED); if(aBullets[x].get_X()>inputWindow.GetWidth()) aBullets[x].set_Flight(false); } } return aBullets; } CBullet* FireBullet(CBullet aBullets[], CPlayer player) { for(int x=0;x<=GCONS.MAXIMUM_ALLOWABLE_BULLETS;x++) { if(!aBullets[x].get_Flight()) { aBullets[x].set_X(player.get_X()+player.get_Width()); aBullets[x].set_Y(player.get_Y()+(player.get_Height()/2)); aBullets[x].set_Flight(true); return aBullets; } } return aBullets; } void WaitNClear(window &inputWindow) { inputWindow.SetPen(BLACK); inputWindow.SetFont(16, BOLD, SWISS); inputWindow.FlushMouseQueue(); inputWindow.SetPen(WHITE, 0); inputWindow.SetBrush(WHITE); inputWindow.DrawRectangle(0, 0, inputWindow.GetWidth(), inputWindow.GetHeight()); } void ReDraw(window &inputwindow) { inputwindow.SetPen(GREEN); inputwindow.SetBrush(GREEN); inputwindow.DrawRectangle(0,700,700,350); } void DrawPlayer(window &inputWindow,CPlayer player) { inputWindow.SetPen(BLUE); inputWindow.SetBrush(BLUE); inputWindow.DrawTriangle( player.get_X(),player.get_Y(), player.get_X(),player.get_Y()+player.get_Width(), player.get_X()+player.get_Height(),player.get_Y()+(player.get_Width()/2),FILLED); } void GetPatterns(std::vector &enemies) { char line[100]; string tempLine; int vectorPosition = -1; int enemyX=0; int enemyY=0; ifstream patternFile("patterns.txt"); if(patternFile.is_open()) { while(patternFile.eof()==0) { patternFile.getline(line,100); for(int x=0;x<100;x++) { //This is a new pattern if(line[x] == '@') { vectorPosition++; //No need to see the rest break; } //Next next chars will be spaces or alpha else if(line[x] == '[') { tempLine=""; } else if(line[x] == '-') { enemyX = stringToInt(tempLine); tempLine = ""; } else if(line[x] == ']') { enemyY = stringToInt(tempLine); enemies.push_back(CEnemy(enemyX,enemyY,false,false)); tempLine = ""; } else if(line[x] != ' ') { tempLine+=line[x]; } } } } } int stringToInt(string str) { int x=0; std::istringstream strStream(str); if(!(strStream>>x)) return -1; else return x; }
Tenks is offline   Reply With Quote
Old 09-01-2007, 12:28 PM   #5 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
Join Date: Aug 2006
Location: Raleigh, NC
Posts: 1,865
Mucho thanks even though a lot of that is still foreign to me. Hopefully I can do that after this upcoming quarter.
__________________
VOCA ME BENEDICTUM !
SANA MEAM ANIMAM !
Kuriin is online now   Reply With Quote
Old 09-01-2007, 01:18 PM   #6 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 477
-16 Internets
I forgot to mention if you program your Java in Eclipse the tabbing is, for the most part, done automatically by Eclipse. You still should learn how to do it manually because sometimes Eclipse fucks.
Tenks is offline   Reply With Quote
Old 09-01-2007, 02:16 PM   #7 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
Join Date: Aug 2006
Location: Raleigh, NC
Posts: 1,865
I like to use Zeus in Windows. Should I just use notepad/text file instead of Zeus?
__________________
VOCA ME BENEDICTUM !
SANA MEAM ANIMAM !
Kuriin is online now   Reply With Quote
Old 09-01-2007, 02:20 PM   #8 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 477
-16 Internets
I have no personal experience with Zeus. I use Eclipse at work and love it. Notepad is a huge pain in the ass, but since nothing is done for you alot of classes like to teach you how to program first in Notepad then go onto other shells.

I'd actually argue Eclipse is better than Visual Studios to program in.
Tenks is offline   Reply With Quote
Old 09-02-2007, 11:50 PM   #9 (permalink)
DickTrickle
Registered User
 
Join Date: Apr 2002
Posts: 303
+1 Internets
I really love Eclipse, more so than Visual Studio. Plus, Eclipse is free
DickTrickle is offline   Reply With Quote
Old 09-03-2007, 06:30 AM   #10 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
Netbeans!
slitz is offline   Reply With Quote
Old 09-03-2007, 11:32 PM   #11 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Tenks, what are your other includes? I can't see anything before:

#include "CMUgraphicsLib/auxil.h"
#include "CMUgraphicsLib/graphics.h"
tikkus is offline   Reply With Quote
Old 09-03-2007, 11:47 PM   #12 (permalink)
Zuuljin
So there's this plane on a treadmill...
 
Zuuljin's Avatar
 
Join Date: Jan 2005
Location: Southern California
Posts: 2,935
+5 Internets
Send a message via AIM to Zuuljin
Eclipse might be a little too good for beginners. Its auto-pilot for alot of stuff. I'd say if your just learning, stick with the text-editor or any basic interface until you know whats going on.

To answer your question (even though its already been answered, but I thought i'd contribute), spaces/tabs/blank lines is all preference and is not enforced in the code at all. The compiler completely ignores it all. The reason for spacing is readability. Generally, the /tab thing is the most important, and everything else is user preference, such as some people like putting the { directly after a method such as
Code:
myMethod() { //stuff }
instead of on the next line as in

Code:
myMethod() { //stuff }
I prefer the latter as it seems to frame the code better, but noone will yell at you for doing it the other way.
Zuuljin is offline   Reply With Quote
Old 09-04-2007, 02:28 AM   #13 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
Here are the official code conventions from SUN btw:
Code Conventions for the Java(TM) Programming Language: Contents

However, this will differ from workplace to workplace which you will notice pretty early. But at least that's the official code convention for java.

Last edited by slitz : 09-04-2007 at 02:33 AM.
slitz is offline   Reply With Quote
Old 09-04-2007, 07:12 PM   #14 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 477
-16 Internets
Quote:
Originally Posted by Zuuljin View Post
Eclipse might be a little too good for beginners. Its auto-pilot for alot of stuff. I'd say if your just learning, stick with the text-editor or any basic interface until you know whats going on.

To answer your question (even though its already been answered, but I thought i'd contribute), spaces/tabs/blank lines is all preference and is not enforced in the code at all. The compiler completely ignores it all. The reason for spacing is readability. Generally, the /tab thing is the most important, and everything else is user preference, such as some people like putting the { directly after a method such as
Code:
myMethod() { //stuff }
instead of on the next line as in

Code:
myMethod() { //stuff }
I prefer the latter as it seems to frame the code better, but noone will yell at you for doing it the other way.
I agree with the above with one restriction and thats try/catch blocks. For some reason I put the curley on the next line except for try/catch. If I just want to print out the exception and excape the method I'll do that in a single line such as:

Code:
try{ MyCode }catch(Exception e){e.printStackTrace();return;}
This comes into play alot when doing database programming where you have to wrap alot of things in try/catch blocks. Just saves alot of space. Because you should really test your data and avoid try/catch via good programming ideas. I don't think I use try/catch outside of File IO and database programming.


Seems like this thread is degenerating into "these are my programming conventions" now though
Tenks is offline   Reply With Quote
Old 09-04-2007, 07:15 PM   #15 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 477
-16 Internets
Quote:
Originally Posted by tikkus View Post
Tenks, what are your other includes? I can't see anything before:

#include "CMUgraphicsLib/auxil.h"
#include "CMUgraphicsLib/graphics.h"
Is the message board fucking with the greater/less signs.

The other includes are (of course enclosed in greater/less signs as per C++):

-iostream
-fstream
-string
-sstream
-vector
Tenks is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 07:03 PM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6