In Chapter 6, the Java API is introduced, the Java API is a library of already written code for generic tasks. What I had to do in this chapter is to pull the ArrayList class from the java API. What the ArrayList is, is an object that one can use methods on, which cannot be done with regular arrays. In fact, ArrayList objects are more powerful and flexible than regular arrays, the only case where a regular Array might be more useful is in a list of primitive values. Since ArrayLists are objects, the use regular Java language, unlike regular arrays that have specialized components. There is also a way to restrict the type of things that are allowed into the ArrayList. ArrayLists do not need an assigned size, things can be added to them and removed and the size will grow and shrink whenever needed. Regular Arrays do not do that, if one exceeds the size of the array, a runtime error pops up.
Within boolean expressions, I leanred new concpets that can be applied, like the 'and' and 'or' operators. within a boolean expression next to an if statement, while loop, etc. && can be used inbetween two statements to say "Hey if both of these are true, do this" or "while bnoth of these are true, do this". || works similarly, except EITHER statement can be true for the whole expression to be considered true. Also, x != 1 means x does not equal 1, and that little thing can be put wherever an expression needs to be true when something does not equal something else. But what about if the code requires the .equals() operator rather than an =? well then a ! gets plopped in front of the entire thing and it means not equal.
In order to use any class from the Java API, one can just use it as if it were their own class he or she made and compiled. The only exception is the FULL package must be imported to the package bein written. For example, to use an ArrayList, one can type "import Java.util.*". "import" is the import function, "Java" is the language, "util" is the package, and "*" is where the class goes, however using the * imports all classes within the package. any code that is imported that begins with "Javax" rather than "Java" originally began as an extension, but then began to be included in the Java API. other than that, there is no difference.
Tuesday, March 10, 2015
HeadFirst Java Chapter 5
When I read this chapter I found it to be extremely useful and full of useful information. This chapter was mostly about bigger, stronger methods, and the beginnings of creating a good old game of battleship. This chapter mostly focused on creating a very simple 2D grid version with three ships. But instead of doing that I made one row board with one ship that will be expanded into the full game in Chapter 6.
One really key method to writing code it to first write a prep-code, then you should test the code, then do the real code. The prep-code is just what you want to happen and not how to do it, then test code is where you start to test certain parts of the code without implementing methods, then the real code is the full blown code where everything will work.
A few new things were explained in this chapter. The Integer.parseInt() method was explained. the "Integer" is a class that ships with Java. "parseInt()" converts whatever is in the parenthesis into an integer if, and only if, it is a digit. For example, a string "3" would be changed into an integer 3. For loops were explained. They are formatted like so; for (variable : array). They mean for every variable in the array repeat. The post-increment operator, or ++ after a variable, was explained and it just adds 1 to whatever variable it is in front of. There is also a post-decrement operator which is a variable with -- after it. it subtracts 1 rather than adds 1. "Pre" versions of both exist and mean add or subtract 1 then use the new value of the variable, if they are in an expression of some kind. Break statements were explained. They just immediately end loops by saying "break". How to make a random number was explained. "int randomNum" declares an integer that is called randomNum. Then "= (int) (Math.random() * 5) follows it. The "(int)" forces the random number to be an integer. "Math" is a class that comes with Java, and "random()" is a method within that class. it generates a random number from zero to just less than 1 (so 0.999999...). The "* 5" increases the range of the random number from anywhere between 0 and just below 5 (or 4.9999999...). Since the variable must be an integer, any of those decimal nines will be cut off, leaving a number between 0 and 4. How to get user input was also explained. "String guess = helper.getUserInput("enter a number");" is how to do so. "String guess" declares the string variable guess. "helper" is an instance of a class made to help specifically with the battleship game. "getUserInput" is a method of the "helper" class that takes what the user has typed when the user pressed return, and gives it back as a string. "("enter a number")" is what is going to be displayed right before the method starts looking for user input. There is also another kind of for loop. "for(int i = 0; i < 100; i++) {}". "int i = 0" declares a new integer variable. "i < 100" is the boolean test. this specific for loop will run 100 times. "i++" adds 1 to "i" every time the loop is run. When the amount of times a loop is wanted to run, a for loop should be used rather than a while loop.

One really key method to writing code it to first write a prep-code, then you should test the code, then do the real code. The prep-code is just what you want to happen and not how to do it, then test code is where you start to test certain parts of the code without implementing methods, then the real code is the full blown code where everything will work.
A few new things were explained in this chapter. The Integer.parseInt() method was explained. the "Integer" is a class that ships with Java. "parseInt()" converts whatever is in the parenthesis into an integer if, and only if, it is a digit. For example, a string "3" would be changed into an integer 3. For loops were explained. They are formatted like so; for (variable : array). They mean for every variable in the array repeat. The post-increment operator, or ++ after a variable, was explained and it just adds 1 to whatever variable it is in front of. There is also a post-decrement operator which is a variable with -- after it. it subtracts 1 rather than adds 1. "Pre" versions of both exist and mean add or subtract 1 then use the new value of the variable, if they are in an expression of some kind. Break statements were explained. They just immediately end loops by saying "break". How to make a random number was explained. "int randomNum" declares an integer that is called randomNum. Then "= (int) (Math.random() * 5) follows it. The "(int)" forces the random number to be an integer. "Math" is a class that comes with Java, and "random()" is a method within that class. it generates a random number from zero to just less than 1 (so 0.999999...). The "* 5" increases the range of the random number from anywhere between 0 and just below 5 (or 4.9999999...). Since the variable must be an integer, any of those decimal nines will be cut off, leaving a number between 0 and 4. How to get user input was also explained. "String guess = helper.getUserInput("enter a number");" is how to do so. "String guess" declares the string variable guess. "helper" is an instance of a class made to help specifically with the battleship game. "getUserInput" is a method of the "helper" class that takes what the user has typed when the user pressed return, and gives it back as a string. "("enter a number")" is what is going to be displayed right before the method starts looking for user input. There is also another kind of for loop. "for(int i = 0; i < 100; i++) {}". "int i = 0" declares a new integer variable. "i < 100" is the boolean test. this specific for loop will run 100 times. "i++" adds 1 to "i" every time the loop is run. When the amount of times a loop is wanted to run, a for loop should be used rather than a while loop.
This is the JVM section of the textbook. It was easy to complete because all that was needed was to run through the program and add variables, the writing on the page explains what I did to figure out the output.
Game.class This is the main class
GameHelper.class
Simpledotcom.class
This is the result when a game is played. The player won without missing.
Friday, March 6, 2015
Age Verifier
What these pieces of codes do is call the current day,month,year and uses the JOptionPane to collect the information from the user. Then it converts the month value to a string to present your birthdate, and compares the date of the current date to your provided birth date by checking a few of the following things:
- If (currentYear - year <= 17) you are younger than 18 and cannot gain access to the website.
- If (year >= 17) and (month <= currentMonth) and (day <= currentDay), you are above 18 and can gain access to the website
- But if (year >= 17) and (month <= currentMonth) and (day > currentDay), you are younger than 18 and cannot gain access to the website.
The current date is callled by using the Date and DateFormat utilities, then each integer is parsed using a delimiter, "l". These integers are then made into the values of currentYear, currentMonth and currentDay, which are compared to the user input values, year,month, and day. Excepting a tiny portion of the population that was born in the 12 hours between the current day and the next day, anyone younger than 18 cannot convert the boolean value "access" to "true", unless they lie.
Subscribe to:
Comments (Atom)