View unanswered posts | View active topics
It is currently Sat Jul 05, 2025 9:43 pm
Author |
Message |
big_D
What's a life?
Joined: Thu Apr 23, 2009 8:25 pm Posts: 10691 Location: Bramsche
|
Java is a better introduction to OOP programming than C++, while it forces good programming practices. C++ doesn't force programmers to use OOP, which in a teaching environment is bad.
Also, the Java libraries are the same on all platforms, so it doesn't matter what hardware the different students have. If they are learning C++, then they need to learn the graphic libraries for the platform(s) they have, they vary from platform to platform and version to version.
As Java and its development GUI are free, it makes a good choice for learning OOP programming - it is a pure language, the tools are free and it is cross platform.
_________________ "Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari
Executive Producer No Agenda Show 246
|
Wed Nov 11, 2009 10:40 am |
|
 |
kalisclark
Occasionally has a life
Joined: Mon May 04, 2009 10:16 am Posts: 130 Location: In between a rock and a hard place
|
Thanks guys!!! useful info! I like it! I will post any major java problems I have here so that others in my position can benefit from this post.
Thanks again.
Kal
_________________ [color=#BF0000][b]Arctic Cooling Freezer 7 Pro 620W Corsair HX Series Modular SLi PSU Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz asus maximus formula, iX38, S 775 ASUS GEFORCE EN8800 GT 512MB GDDR3
|
Sat Nov 21, 2009 2:09 pm |
|
 |
kalisclark
Occasionally has a life
Joined: Mon May 04, 2009 10:16 am Posts: 130 Location: In between a rock and a hard place
|

Ok Java Geniuses!
I need to make the following java code feasable for 2 players and was hoping to get some help, I also need to make it so that if you press say "Q" it will close the programme...I know this is done with a systemexit <0> Im just not sure how to implement it.
Any help would be greatly appreciated....Thank you.
public class ThrowDart { /* This program is used to play a game of darts. You type in the number you want to hit It will show you the number that you have hit or if you have missed. The total will be shown at the end as well as how many goes you have had. You have the options to play again or terminate if entering 0. */ public static void main(String[] args) {
int dartThrow = 0; // 1st Dart to be thrown. int dartThrow2 = 0; // 2nd Dart to be thrown. int dartThrow3 = 0; // 3rd Dart to be thrown. int modifier = 0; int score = 501; //Starting score. int turn = 0; // How many turns you have had. int total; // Total number of darts thrown. Neighbour[]neighbour = new Neighbour[20]; //Assigning neighbours for each dart.
neighbour[0] = new Neighbour(20, 18); neighbour[1] = new Neighbour(15, 17); neighbour[2] = new Neighbour(17, 19); neighbour[3] = new Neighbour(18, 13); neighbour[4] = new Neighbour(12, 20); neighbour[5] = new Neighbour(13, 10); neighbour[6] = new Neighbour(19, 16); neighbour[7] = new Neighbour(11, 16); neighbour[8] = new Neighbour(14, 12); neighbour[9] = new Neighbour(6, 15); neighbour[10] = new Neighbour(14, 8); neighbour[11] = new Neighbour(9, 5); neighbour[12] = new Neighbour(4, 6); neighbour[13] = new Neighbour(11, 9); neighbour[14] = new Neighbour(10, 2); neighbour[15] = new Neighbour(8, 7); neighbour[16] = new Neighbour(3, 2); neighbour[17] = new Neighbour(1, 4); neighbour[18] = new Neighbour(7, 3); neighbour[19] = new Neighbour(5, 1);
TextIO.putln("Welcome! Press <Enter> if you would like to play a game of darts!"); TextIO.getln(); TextIO.putln(); TextIO.putln(); TextIO.putln(); //First Dart TextIO.put("Type in the number where you would like to throw your first dart: " ); dartThrow = TextIO.getlnInt(); dartThrow=getThrow(dartThrow, neighbour[dartThrow-1].getLeft(),neighbour[dartThrow-1].getRight()) ;
modifier = (int)(Math.random()*3) + 1; //Picks from 1 of the Cases. TextIO.putln("Your first dart has hit " + dartThrow); switch(modifier) {
case 1: dartThrow = dartThrow * 1; TextIO.putln("You have hit a Single"); break;
case 2: dartThrow = dartThrow * 2; TextIO.putln("You have hit a Double"); break;
case 3: dartThrow = dartThrow * 3; TextIO.putln("You have hit a Triple"); break; } TextIO.putln("Your first dart scored " + dartThrow); //Score after S/D/T TextIO.putln(); score = score - dartThrow; //Takes away from score for first dart. TextIO.putln(); TextIO.putln("You still have " + score + " points remaining "); //Points still remaining. TextIO.putln(); //Second Dart TextIO.put("Type in the number where you would like to throw your Second dart: " ); dartThrow2 = TextIO.getlnInt(); dartThrow2=getThrow(dartThrow2, neighbour[dartThrow2-1].getLeft(),neighbour[dartThrow2-1].getRight()) ;
if (dartThrow2 >= 0 && dartThrow2 <= 20 || dartThrow2 == 25 || dartThrow2 == 50) // input is OK; jump out of loop TextIO.putln("You may only enter 1-20, 25 or 50 "); modifier = (int)(Math.random()*3) + 1; //Picks from 1 of the Cases. TextIO.putln("Your second dart has hit " + dartThrow2); switch(modifier) {
case 1: dartThrow2 = dartThrow2 * 1; TextIO.putln("You have hit a Single"); break;
case 2: dartThrow2 = dartThrow2 * 2; TextIO.putln("You have hit a Double"); break;
case 3: dartThrow2 = dartThrow2 * 3; TextIO.putln("You have hit a Triple"); break; } TextIO.putln("Your second dart scored " + dartThrow2); TextIO.putln(); score = score - dartThrow2; //Takes away from score for second dart. TextIO.putln(); TextIO.putln("You still have " + score + " points remaining "); TextIO.putln(); //Third Dart TextIO.put("Type in the number where you would like to throw your Third dart: " ); dartThrow3 = TextIO.getlnInt(); dartThrow3=getThrow(dartThrow3, neighbour[dartThrow3-1].getLeft(),neighbour[dartThrow3-1].getRight()) ; if (dartThrow3 >= 0 && dartThrow3 <= 20 || dartThrow3 == 25 || dartThrow3 == 50) // input is OK; jump out of loop TextIO.putln("You may only enter 1-20, 25 or 50 "); modifier = (int)(Math.random()*3) + 1; //Picks from 1 of the Cases. TextIO.putln("Your third" + " dart has hit " + dartThrow3); switch(modifier) {
case 1: dartThrow3 = dartThrow3 * 1; TextIO.putln("You have hit a Single"); break;
case 2: dartThrow3 = dartThrow3 * 2; TextIO.putln("You have hit a Double"); break;
case 3: dartThrow3 = dartThrow3 * 3; TextIO.putln("You have hit a Triple"); break; } TextIO.putln("Your third dart scored " + dartThrow3); TextIO.putln(); { total = dartThrow + dartThrow2 + dartThrow3; // Adds total of all three darts. TextIO.putln("Your total score is " + total); score = score - total; //Takes away from score. TextIO.putln(); TextIO.putln("You still have " + score + " points remaining "); TextIO.putln(); turn++; TextIO.putln(); TextIO.putln(); } // end class. } //end main.
public static int getThrow(int dartThrow, int left, int right) {
// if (dartThrow >= 0 && dartThrow <= 20 || dartThrow == 25 || dartThrow == 50) // input is OK; jump out of loop // break; // TextIO.putln("You may only enter that values 1-20, 25 or 50, please enter again! "); int neighbour = (int)(3*Math.random())+ 1;
switch (neighbour) { case 1: dartThrow = left; break; case 2: dartThrow = right; break; case 3: dartThrow = dartThrow; break; } return dartThrow;
} }
_________________ [color=#BF0000][b]Arctic Cooling Freezer 7 Pro 620W Corsair HX Series Modular SLi PSU Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz asus maximus formula, iX38, S 775 ASUS GEFORCE EN8800 GT 512MB GDDR3
|
Sat Nov 28, 2009 1:27 am |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|

You are using a switch incorrectly. A switch should always have a default case for code safety (and also have it throw an error or message or something so you know)
When you do your input check for a 'q', simple as really, do a try catch on the input for the number to throw at, then if it throws an exception (not up on the java exceptions but it should be something like try{stuff()}catch{error message}) or use a break if available to quit the loop you are in.
You need to seed your random variable too, otherwise due to the way it works you will get the same random result every time, defeating the point of it being random
instead of 3 seperate darts why not have darts[3]? Then you can use a while statement such as while(dart[0] != 0 && dart[1]!=0 && dart[2]!=0) { do the darts throwing }
And by the looks of it your game will end after the first throw, you need to separate your game into 3 areas
Initialisation of the game, this is where you seed your random number (from the system time or something), initialise the neighbours etc The game, this is a loop that goes through input, processing and output (one loop per full turn) of throwing the darts and calculating the score The end, cleanup if needed, ending the program
Your code is also commented poorly, you have declared the end of the class before the end of the main function, which is incorrect from the braces your main is inside the class. You should have ALL functions that will be used more than once outside the main game
I would introduce a new variable, a byte for the dart number, then you can adjust part of the code so each dart throw is based on the same code, but the variable changes for dart number (with an array) and if/elseif/else for the dart throw (and reset at the end of the turn.
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Sat Nov 28, 2009 1:57 am |
|
 |
big_D
What's a life?
Joined: Thu Apr 23, 2009 8:25 pm Posts: 10691 Location: Bramsche
|
What Fin said. Especially the loop for the dart throwing - in fact, you should have a dart class and be calling the throw method of the dart class. It looks like you have created a class and then used one method to create a procedural chunk of code, completely defeating the point of using an OOP...  I take my point back, about Java forcing people to learn to write OOP code 
_________________ "Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari
Executive Producer No Agenda Show 246
|
Sat Nov 28, 2009 8:09 am |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|

OK, what the two guys above me are syaing is this.
ATM you have one big long file with all your code in it. This is very much like procedural programming and not what Java is for.
You should have a class called Dart(). Dart will have a method in it called Throw which will have an input number of the number you wish to hit and will return an int (i.e. the number you actually hit).
Then you have a class called Main(). This will "play" the game.
Main will go something like...
Dart dart = new Dart;
int throw; //You don't actually need this as an array as you will never access the individual parts. int turnTotal; int score = 501; int total = 0; int turn = 0;
/*All the dart maths like which number has been hit will be in the Dart.Throw() method.
i.e. to get the score you would do...*/
turn++; turnTotal = 0;
for (int i = 1 ; i <= 3 ; i++) { /*Get input for which number the player would like to hit here*/ throw = dart.Throw(number); turnTotal = turnTotal + throw; total++; }
/*Now you have 3 throws with the values for them and the total for the turn. Now you can calculate the scores etc...*/
score = score - turnTotal;
/*println "Grats you scored turnTotal with that turn and have a score of score remaining. You have had turn goes in total."*/
I hope that makes sense.
Once you have this set up and looping until the player gets down to 0 then it's fairly easy to take the variables like score, turnTotal, total, turn, etc... out to another class called Player().
Once you have this then your main will literally just go...
Player player[] = new Player[2]; player[1] = new Player("Oliver"); player[2] = new Player("Kal");
/*loop forever*/ for (int i = 1 ; i <= 2 ; i++) { player[i].play();
if (player[i].getScore() == 0) { /*leave loop (I think break is right?).*/ } }
now each Player stores their own scores and has their own dart etc...
You will prob have a method called Player.play() or something. It will loop 3 times and get input for desired number to throw three darts and then return the total for that turn and the remaining score for that player.
When one player gets to 0 it will win.
I just read through your code and noticed your STATIC method.
Static methods are only used by people who don't know how to use methods properly.
They are good in some areas but should generally not be used if you can avoid it.
Once you have your OOP sorted and a Dart class you can stick the PUBLIC method in there.
Last edited by Fogmeister on Sat Nov 28, 2009 12:06 pm, edited 2 times in total.
|
Sat Nov 28, 2009 11:01 am |
|
 |
kalisclark
Occasionally has a life
Joined: Mon May 04, 2009 10:16 am Posts: 130 Location: In between a rock and a hard place
|
WOW GUYS!!!
thank you so much, although I will need to put all you have just said into practice, the information you have provided is priceless and I am very greatful, it seems I have quite a bit of work to do lol....I will let you know how I get on and post the final programme when im finished.
Thanks
Kal
_________________ [color=#BF0000][b]Arctic Cooling Freezer 7 Pro 620W Corsair HX Series Modular SLi PSU Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz asus maximus formula, iX38, S 775 ASUS GEFORCE EN8800 GT 512MB GDDR3
|
Sat Nov 28, 2009 11:32 am |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
Don't worry. Almost everyone makes the mistake you made when first starting. On the computing course I was on for 2 years there were people at the end of the course who still didn't get the whole OOP thing and would only program in single procedural programs. Also, get into the practice of putting each class into its own file. i.e. for the 1 player game you will have a file called main.java and a file called dart.java. That way it's easier once you start creating programs with more and more classes. It keeps your hole program organised.
|
Sat Nov 28, 2009 11:43 am |
|
 |
kalisclark
Occasionally has a life
Joined: Mon May 04, 2009 10:16 am Posts: 130 Location: In between a rock and a hard place
|
Will do, thanks for the tip Fogmiester....gotta admit Im still a bit hazy on Java but im sure with practice it will come to me lol...I hope.
Kal
_________________ [color=#BF0000][b]Arctic Cooling Freezer 7 Pro 620W Corsair HX Series Modular SLi PSU Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz asus maximus formula, iX38, S 775 ASUS GEFORCE EN8800 GT 512MB GDDR3
|
Sat Nov 28, 2009 2:22 pm |
|
 |
big_D
What's a life?
Joined: Thu Apr 23, 2009 8:25 pm Posts: 10691 Location: Bramsche
|
A general rule of thumb is, if you are writing a procedure, method, property etc. if the code is longer than 2 screens (rule of thumb), then it is too long and you need to look at breaking the functionality up.
Obviously there are exceptions - if you are initialising a class that has lots of properties that need initialising or you need to move all the properties into a string for writing a SQL statement to the database, that CAN take a lot of lines of code.
But generally each method or property should be short and concise.
_________________ "Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari
Executive Producer No Agenda Show 246
|
Sat Nov 28, 2009 4:01 pm |
|
 |
kalisclark
Occasionally has a life
Joined: Mon May 04, 2009 10:16 am Posts: 130 Location: In between a rock and a hard place
|
Thanks big D, that is really helpful, I was thinking that it looked far too long.
Much appreciated.
Kal
_________________ [color=#BF0000][b]Arctic Cooling Freezer 7 Pro 620W Corsair HX Series Modular SLi PSU Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz asus maximus formula, iX38, S 775 ASUS GEFORCE EN8800 GT 512MB GDDR3
|
Sun Nov 29, 2009 4:38 pm |
|
 |
forquare1
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 6:36 pm Posts: 5156 Location: /dev/tty0
|
This is very rule of thumb, I know one of my lecturers says this, and two of "his" screens are 48 lines (two normal terminal screens). Plus no line should be more than 80 characters (same reason) As Dave said, there's no problem with going over that limit, but it's a good rule of thumb.
|
Sun Nov 29, 2009 9:16 pm |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|
On my compiler writing module if ANY of the following was wrong you lost a mark (the whole assignment was negatively marked, half for the tests, half for code quality) No comment for headers (can be grouped), constants, statics etc 2 space indentation in a function blank line after a function poor naming for functions (could do it how we wanted, hungarian etc as long as it was clear and consistent) etc.
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Mon Nov 30, 2009 12:03 am |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
One of my pet hates at work is having to edit/add to badly coded programming. I normally have to deal with everything that your compiler would class as a fail. In fact, I've completely rewritten (refactored) code before actually applying the edits I need to make just to make it easier to see what I'm doing. I've seen code before where all of around 20 - 30 variables were all prefixed with "w" and most of them (i.e. start of hire date) were strings as they were converted into them for printing / display. Either that or having several different conditions that all run the same code but the repeated 50+ lines of codes were all duplicated around 5 or 6 times making the file about 10 times longer than it needed to be. Each time you had to edit part of the code you had to make 5 identical edits. Soooo annoying.
|
Mon Nov 30, 2009 12:23 am |
|
 |
kalisclark
Occasionally has a life
Joined: Mon May 04, 2009 10:16 am Posts: 130 Location: In between a rock and a hard place
|

Hi guys,
ok, I took what you said above into account but now im stuck. Im trying to get the darts to work and it doesnt seem to be happening, I was hoping you could possibly tell me if im at least flowing in the right direction lol.
public class ThrowDart { /* This program is used to play a game of darts. You type in the number you want to hit It will show you the number that you have hit or if you have missed. The total will be shown at the end as well as how many goes you have had. You have the options to play again or terminate if entering 0. */ public static void main(String[] args) {
int dartThrow; //You don't actually need this as an array as you will never access the individual parts. int turnTotal; int score = 501; int total = 0; int turn = 0;
turnTotal = 0;
Neighbour[]neighbour = new Neighbour[20]; //Assigning neighbours for each dart.
neighbour[0] = new Neighbour(20, 18); neighbour[1] = new Neighbour(15, 17); neighbour[2] = new Neighbour(17, 19); neighbour[3] = new Neighbour(18, 13); neighbour[4] = new Neighbour(12, 20); neighbour[5] = new Neighbour(13, 10); neighbour[6] = new Neighbour(19, 16); neighbour[7] = new Neighbour(11, 16); neighbour[8] = new Neighbour(14, 12); neighbour[9] = new Neighbour(6, 15); neighbour[10] = new Neighbour(14, 8); neighbour[11] = new Neighbour(9, 5); neighbour[12] = new Neighbour(4, 6); neighbour[13] = new Neighbour(11, 9); neighbour[14] = new Neighbour(10, 2); neighbour[15] = new Neighbour(8, 7); neighbour[16] = new Neighbour(3, 2); neighbour[17] = new Neighbour(1, 4); neighbour[18] = new Neighbour(7, 3); neighbour[19] = new Neighbour(5, 1);
TextIO.putln("Welcome! Press <Enter> if you would like to play a game of darts!"); TextIO.getln(); TextIO.putln(); //First Dart int i = 0; Dart[]dart = new Dart[i]; TextIO.putln("Please enter your first dart: " ); dartThrow = TextIO.getlnInt(); for (i = 1 ; i <= 3 ; i++) dartThrow=getThrow(dartThrow, neighbour[dartThrow-1].getLeft(),neighbour[dartThrow-1].getRight()) ; //Second Dart
Dart[]dart1 = new Dart[i]; TextIO.putln("Please enter your second dart: " ); dartThrow = TextIO.getlnInt(); for (i = 1 ; i <= 3 ; i++) dartThrow=getThrow(dartThrow, neighbour[dartThrow-1].getLeft(),neighbour[dartThrow-1].getRight()) ; //Third Dart Dart[]dart2 = new Dart[i]; TextIO.putln("Please enter your third dart: " ); dartThrow = TextIO.getlnInt(); for (i = 1 ; i <= 3 ; i++)
dartThrow=getThrow(dartThrow, neighbour[dartThrow-1].getLeft(),neighbour[dartThrow-1].getRight()) ; int modifier = (int)(Math.random()*3) + 1; //Picks from 1 of the Cases.
switch(modifier) {
case 1: dartThrow = dartThrow * 1; TextIO.putln("You have hit a Single"); break;
case 2: dartThrow = dartThrow * 2; TextIO.putln("You have hit a Double"); break;
case 3: dartThrow = dartThrow * 3; TextIO.putln("You have hit a Triple"); break; } // end class } // end main
public static int getThrow(int dartThrow, int left, int right) {
int neighbour = (int)(3*Math.random())+ 1;
switch (neighbour) { case 1: dartThrow = left; break; case 2: dartThrow = right; break; case 3: dartThrow = dartThrow; break; } return dartThrow;
} }
THE NEXT CLASS IS:
/* Neighbour Class is used to change the value of the initial value entered by the use. This will be the neighbour array or original array which is equivalent to the number either side of the dart board or the actual number entered */ public class Neighbour {
private int left; //Stores the left dart number private int right; //Stores the right dart number
public Neighbour(int left, int right) //Declares these values within method neighbour.
{ this.left = left; this.right = right; }
public int getLeft(){
return this.left;
} public int getRight() { return this.right;
}
} //end class Neighbour
THE NEXT CLASS IS:
public class Dart {
private int dart; private int dart1; private int dart2; public Dart() { this.dart = dart; this.dart1 = dart1; this.dart2 = dart2; }
public int getDart() { return this.dart; } public int getDart1() { return this.dart1; } public int getDart2() { return this.dart2; }
}
I hope someone can help me with this dilema.
Many thanks
Kal
_________________ [color=#BF0000][b]Arctic Cooling Freezer 7 Pro 620W Corsair HX Series Modular SLi PSU Intel Core 2 Quad-Core Q6600 G0 SLACR, 95W, S775, 2.40 GHz asus maximus formula, iX38, S 775 ASUS GEFORCE EN8800 GT 512MB GDDR3
|
Wed Dec 02, 2009 5:23 pm |
|
|
Who is online |
Users browsing this forum: No registered users and 4 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum
|
|