| Free £25 Bet! | Free £50 Bet at VCBet! | Free £25 Bet! |

In association with Sports-Punter Free Bets Odds Comparison BetHelp Limso
We are the Official Forum of FreeBetting.net & FCBet.com
| Sports News | Sports Stats | Live Scores | OddsChecker | Place Bets | Suggest a Site |
| |||||||
| Punters Tools & General Betting Help Forum Everyone has tools that use to help them bet. Match reports, odds comparison services, tipster services, betting calculators, selection software - discuss them here. |
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Pro Punter ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: 23 Oct 2003 Location: Westdorpe Age: 43
Posts: 5,124
| Lesson 5 ( Word document ) Punters Lounge JAVA programming course. Lesson 5 - Learning the language, arrays, arguments and user questions. 5.1 Array's We've seen how to store information in a variable like a numerical value in an integer. Here we will be using more precise numbers so the type double is used. NOTE: i'm using double but might as well use float. It all depends on the type of information. On the one hand the type must be big enough to hold the required values, on the other hand there is no point in wasting computer memory and resources by using types that are too big for the values they hold. In this case double will be used throughout the lessons because it is most easy for me. And yes in most cases it does waste memory space. Read more here: java.sun.com/docs/books/tutorial/java/nutsandbolts/variablesummary.html double Bank = 1000; double odds = 2.5; An array is a way of storing related information in a single variable, like this: double[ ] odds = new double[3]; In this case what we do is take 3 cardboard boxes and tape them toghether in a row using some tape. We name the series of boxes odds and then number the individual boxes 0, 1 and 2 giving 3 boxes in total. We access the contents of the boxes by using the name of the whole and the number of the individual box like this: odds[1] = 2.5; So we can fill the array like this: odds[0] = 2.0; odds[1] = 3.3; odds[2] = 3.6; So to calculate the overround we do this: double overround = 0; overround = ( 1 / odds[0] ) + ( 1 / odds[1] ) + ( 1 / odds[2] ) Two things to note: your array can only contain boxes of the same type. So when you define an array all the individual elements of the array are of the same type like double. Second thing to note is that when you define an array you fix its size. Our array odds[3] is defined as holding 3 double values, ( start counting at 0 ), it cannot be changed to hold more than 3. Arrays are not only used for variables. You can also create an array of objects. We will see more about that when we get to dealing with text. Arrays are not limited to a single row. Imagine a wall of boxes, all nicely piled up in vertical columns 5 high and 10 columns in all. We could number the boxes from bottom to top as box 0 to 4 (remember with array's we start counting at 0 ) And we could number the columns from left to right as columns 0 to 9. So we can find a specific box by counting its position from bottom to top and then from left to right. Bottom left box = 0,0 , top right box = 4,9, a box in the middle is 2,5. We can define the array like this: double[ ][ ] odds = new double[10][3]; In this case we can have 10 columns, each column being 3 boxes high. This could be 10 football matches each having 3 odds, home/away/draw. There really is no limit to how deep array's can go. But keeping it simple is highly advised. Before we continue here is a programming tip that can make life a lot easier. When you put information in an array you have to remember what goes where. Otherwise how will you ever find anything again. In the previous example the problem is you have to remember what goes in 0, 1 and 2 . But we can use a variable for that like this: int home = 0; int draw = 1; int away = 2; double[ ] odds = new double[3]; And we can access the array like this: odds[home] = 2.0; odds[draw] = 3.3; odds[away] = 3.6; So we don't have to remember 0, 1 and 2 but we can use a logical name to find what we want. ROOKIE class: stick to the image of a row or a wall of boxes. That will do for now. We will come back to arrays in the lessons about text. SCOLAR class: this is the chapter on arrays, i suggest you review it completely. java.sun.com/docs/books/tutorial/java/data/arrays.html Assignment 5.1 ( Lesson51.java ) Write a program where you can hold the odds information on 10 football matches. If you have some real life data available use that. Then use a loop and for each match calculate the overround. Hint: the array of odds will have to be of the double type. Assignment 5.2 ( Lesson52.java ) Take the previous program but put the calculation of the overround in a separate object. Assignment 5.3 ( Lesson53.java ) Take the previous assignment and add an array where you can hold the end score of those 10 footballmatches, also add a starting bank of 100. Then calculate the ending bank assuming you had placed a bet on the Under/Over market. Assume you placed an Over bet on each match at odds of 2 to 1 using 10 as a stake. Assignment 5.4 ( Lesson54.java ) Write a program where you can hold the score of 10 football matches in an array including some fictional odds on those matches. If you have some real life data available use that. Then, using a starting bank, calculate the end result as if you had placed bets. So basically the previous assignment but this time with an array holding your bet selection in stead of a simple Over bet. 5.2 Passing arguments What we also need to complete the Java basics is the way of getting data into our program. So far we have had to recompile each time we made a change. We can pass variable data into the program each time we run it. This is where the second line of the programs we've made so far comes into play. public static void main(String[ ] arguments) { This line defines a method called main. Each Java program, being an object in itself, always has at least one method. Otherwise it cannot do anything. In this case the method main is defined in such a way that when started it can be passed information. Inside the declaration of main is the creation of an array called arguments. This is always an array of Strings. To use integers we will need to convert that text to int values. The size of the array is unknown at the time of declaration but will be determined when the program is run. Let's revisit our program from lesson 2. public class Lesson2 { public static void main(String[ ] arguments) {
int Bet = 100; int Odds = 3; int WinLose = 0; if (WinLose == 0) {
else {
System.out.println(Bank); } On the command line in MS-DOS where you start the program you can pass the values to use like this: C JAVA>JAVA Lesson2 1000 100 3 0The values you pass are automatically put into an array called arguments like this. arguments[ 0 ] contains 1000 arguments[ 1 ] contains 100 arguments[ 2 ] contains 3 arguments[ 3 ] contains 0 in the same order as they where added to the program name when the program starts. Now first of all the contents of arguments is Strings, not numbers. To convert these we can use a method in the Integer object like this int number; number = Integer.parseInt(arguments[0]); This converts the text in arguments[0] into a numerical value and stores it in number. Mind you, if your String cannot be converted to a number an error will occur. At this moment i want to keep things simple and basic. So for now you need to take care of giving the right input yourself, no error checking yet. Also in our example you can only use integers so no 2.5 only 2 or 3. We can access the arguments to our program these like this: public class Lesson2 { public static void main(String[ ] arguments) {
int Bet = Integer.parseInt(arguments[ 1 ]); int Odds = Integer.parseInt(arguments[ 2 ]); int WinLose = Integer.parseInt(arguments[ 3 ]); if (WinLose == 0) {
else {
System.out.println(Bank); } This takes the variable data outside the program so we can use the same program with different data without having to change the program itself. This is the command line where you can vary the numbers for Bank etc… each time you run the program. C JAVA>JAVA Lesson2 1000 100 3 0Now you may be thinking what about some nice menu's? You know, where we can enter the information in boxes and stuff. Well that is of course very nice but unfortunately outside the scope of this course. Sorry. Later in the course we will see how to read information from files and also how to write to files. We will also see how to read from internetsites. However passing arguments when starting a program, and reading from files or the internet will be the only ways of getting at data we will cover. Menu's and graphics and interfaces etc. you will have to do on your own. Assignment 5.5 ( Lesson55.java ) Rewrite the above program so it uses the technique described at the end of the array chapter. So you leave the information inside an array but access it using name variables. It starts like this: int[ ] ProgramData = new int[ 4 ]; int Bank = 0; int Bet = 1; int Odds = 2; int WinLose = 3; ProgramData[Bank] = Integer.parseInt(arguments[ 0 ]); and so on… Remember, if you have been using the JCreator editor until now you can use that to write and compile the program. But you have to run this program from an msdos box including only integer parameters like this: C JAVA>JAVA Lesson55 1000 100 3 1C JAVA>JAVA Lesson55 1000 100 3 0C JAVA>JAVA Lesson55 1000 50 2 1C JAVA>JAVA Lesson55 2000 200 4 15.3 Asking the user questions. Another way of getting information into a program is by simply asking the user for input. This is done by using the System object. That object is a representation of your computer system. We have already seen there is a System.out method to write text to the screen and there is also a System.in method to retrieve info from the keyboard. I'm just going to show here how it works without too much explanation. You need to use an inputstream and that subject will be covered in full in lesson 7, when we talk about reading from and writing to files. Also you can only read text. To read numbers you need to convert the text to numbers, we've seen one way to do that by using the Integer.parseInt() method. So for now just showing how it works so you know it exists and you can implement it at a later stage. // first add this line at the very beginning of the program to include the input/output objects. import java.io.*; public class Lesson5 { // add the words : throws IOException , part of the errorhandling system public static void main(String[ ] arguments ) throws IOException { // create a buffer to read the keyboard // the object BufferedReader will be covered in full in Lesson 7 BufferedReader Readkeys = new BufferedReader ( new InputStreamReader (System.in));
// ask a question and read from the keyboard System.out.println("What is your name ?"); Myname = Readkeys.readLine(); System.out.println("Welcome to Java, "+Myname+" , hope you enjoy it."); } } Assignment 5.6 ( Lesson56.java ) Take the program from Lesson2 and re-write it so the user has to input the values based on a question. So you ask for Bank, Stake, Odds, Win or Lose. Remember the user must input integer values only otherwise an error will occur. |
| |
| | #2 (permalink) |
| Pro Punter ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: 23 Oct 2003 Location: Westdorpe Age: 43
Posts: 5,124
| Assignment solutions : Assignment 5.1 Lesson51.java Assignment 5.2 Lesson52.java Assignment 5.3 Lesson53.java Assignment 5.4 Lesson54.java Assignment 5.5 Lesson55.java Assignment 5.6 Lesson56.java Click on the filename to open, or use the right-click and then Save Target As... to save the file on your PC. |
| |
| Free £100 Bet! | Free £100 Bet! |
| Partner Sites |