In Chapter 6, the Java API is introduced. the Java API is a library of already written code for generic tasks. I had to pull the ArrayList class from the Java API. The ArrayList 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 grown or 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.
Monday, March 9, 2015
Wednesday, March 4, 2015
Age Checker Program
I completed the Age Checker activity using Java. The requirement of the age checker is 18 years or older.
I had to import the JOptionPanel class from the swing package and i imported all classes from the util package so i could get the Date class. All code was done within one class, the Age class, and in the main method.
First, I declared my 3 string variables, day month and year. these will turn into the users day month and year of birth.
Next came the option panels. 3 pop up. the first one asks for the day of birth in 2 digits. Then another pops up asking for the month, again in two digits. Lastly one pops up asking for the year, however, instead if the mandatory two digits like before, the year requires 4. The users input will be stored within the string variables day, month and year.
Now, strings will not work for what this code needs to do, so they need to be converted to integers. The day variable gets turned into d, and is now an integer. The same thing happens from month to m, and year to y.
The users birthday has been gathered, now the current date needs to be gathered to verify if the user is 18. to do this, I created a new GregorianCalendar object. this object can supply alot of information using the imported Date class. I declared a new integer variable cd (stands for current day). cd is equal to calendar.get(Calendar.DAY_OF_MONTH). This snags the day of the month and puts it as an integer stored as cd. this is all done within the imported Date class. Similar code with minor, yet important differences is written for cm (current month), and cy (current year).
Finally the program has all the data it needs. Now it just needs to do a few things with it. there is an if statement with a condition creating a new Date object set up (year,month,day). the variables represent the user's birthday. the .before method asks if this date is before the following date, which is another new Date with variables representing the current date plugged in, except the year is subtracted by 18 becuase the required age is 18. In English, this is saying "Is this date before this other date?" but in java it is comparing the time in milliseconds between them both since January first, 1970. if the first one is less than the second, then the statement will be true. Now if the user is in fact over 18, then an option panel will pop up saying "Access Granted.". What if the user is not old enough? well a very similar line of code is written to solve this, however, instead of .before, .after is used. Also the month needs to have 1 added to it, otherwise The program would say you were not old enough if you turned 18 in the current month! but what about if you just turned 18 today? Well, that's what the final else is for. that else ensures that if you are exactly 18 on the current day, you can still get "Access Granted.".
That is how I designed my Age Checker Program.
Subscribe to:
Comments (Atom)