Monday, March 2, 2015

Head First Java: Chapter 5

Battleship Game:

        In this chapter we were required to build a video game. This allowed us to test and apply our knowledge in a real application. The game that had to be build was battleship, and the in this chapter I learned the steps and process of building this game. Instead of following the book step by step I implemented a few of my own methods and ideas (explained below).

One thing I did differently was instead of using the Game Helper class from the book I used the Scanner class to get and use User input. Another thing I did differently was instead of the Math class to get a random location I used the Random Class to generate a random number for the location of the dot com. 



Other learning points:

Casting primitives : This allows you to convert between different primitive types. For example this allows you to convert 7.14 which is a double to 7 which is an int.

Another learning point was converting a string to an integer using Interger.parseInt("Numerical String value)

Saturday, February 14, 2015

Head First Java: Chapter 4



  • · Variables come in two flavors: primitive and  reference.
  • · Variables must always be declared with a name  and a type.
  • · A primitive variable value is the bits representing the value (5, ‘a’, true, 3.1416, etc.).
  • · A reference variable value is the bits representing a way to get to an object on the heap.
  •   A reference variable is like a remote control.
  • · Using the dot operator (.) on a reference variable is like pressing a button on the remote control to access a method or instance variable.
  • A reference variable has a value of null when it is not referencing any object.
  • An array is always an object, even if the array  is declared to hold primitives. There is no such thing as a primitive array, only an array that   holds primitives.






·          Methods
o   void speak(){}
o   Call method with (.)
o   Methods have ( ) when called
o   Ex. person1.speak( );
 

·          Getters and Return Values
o   Getters
§  Ex. public int getAge( ){
Return age;
§   Must return correct type

·         Method Parameters
o   Inside ( ) are parameters
o   Call a method
o   (int value) or ( String name)
o   Person1.name = “Jael”
                       
                       

·         Setters and "this"
o   Ex. void setAge (int age){
this.age; }


This is the people class that shows the instance variable name being declared private and I use my getters and setters in my greeting method.

·       Encapsulation: Public, Private, Protected
o   Public = can access anywhere
o   Private = access in  same class only
o   Protected = can be accessed in sub-classes and packages
o   No modifier = same package only 
This is the people class that shows the instance variable name being declared private and I use my getters and setters in my greeting method.

This is the programs main method and here I  instantiate a people array and use my setter and getters to produce a greeting statement with user input.