JAVA Beginners part 7

Time to write a simple game. Very first step is to understand the game well, understanding is half the job done.

What is the game?

Store 3 consecutive numbers in a 7 digit array and ask user to guess the locations. When the user gets all the locations correct the game is over, in the end the score and number of attempts is displayed. Seems fun? well it was scary for me initially.

A new process worth trying. You see from this point on its get real. In the sense this is how we need to think in terms of developing a real world business/ practical JAVA application with productive use ( apparently)

prep code <-> test code <-> real code

New Stuff

int x = Integer.parseInt(“3”);
Integer is a inbuilt class in JAVA that has this method parseInt which knows how to convert sting into integer. It will work only if the String passed is like “0” or “1” or “2”

For(Int cell : collection)
Cell is a variable of type int, for every element in the array named collection it will retrieve the value and assign it to the variable. The Colon means for each element. 

Final code of the game. After lot talking, here it is finally. Few modifications in the game, the inputs are hard coded ( meaning you are inputting the values). Game is very simple it will check for the input in the hard coded array to see if it is present, if yes then says hit else it says miss. The kill part is never executed actually. This is more of base for future program that is going to be more complicated. 

In fact the most complicated part is figuring the logic of the game and the flow of implementation ( the methods and objects). Coding is the easiest part. 

t4.jpg

Cognitive Tip

Left Brain activities include
step by step sequences
– logical problem solving
analysis

Right Brain activities include
metaphors
– creative problem solving
pattern matching
visualizing

Well above one was a simple game , Now Full Version.

int num = (int)(Math.random()*5)

Math is a inbuilt class which has the method random which create random numbers. The value is less than 1 , which is why you need to multiply to 5.
5 because we need random numbers between 0 to 5

The Random function returns a double hence we need to type cast into int.

String guess = helper.getUserInput(“Enter the number”);

Calling a custom method to get inputs from command line.
you will see things like this a lot, lets say x++(post) or ++xx(pre)

pre increment
int x = 0 , int z = ++x
value x = 1, z = 1

post increment
int x = 0 , int z = x++
value x = 1, z = 0

Loops.

When you know the exact number of times you want to loop, use

for(int i = 0; i < 10; i++)
{}

when you want to loop based on a exit condition, use

While( true )
{}
Enhanced For, used to iterate through collections like array. Very easy and useful

String[] nameArray = {“Fred”,”Mary”,”Bob”}
for ( String name : nameArray)
{}
String is type of array. colon means for each. { for each element in namearray assign the element to name variable and run the body of loop }
name is the String Variable name that will hold each value of the collection/array nameArray
one at a time. First iteration value stored in name will be Fred and then Mary and then Bob

JAVA comes with lot of pre built classes also called as JAVA API. 

You dont have to re invent all these. Write the code that is custom to your application and use these APIs to ease up the work.

When you trying to think of a new solution or a workaround, When you thinking of a new possibility dont try to think in terms of how you would implement it. 

What do i mean by that? Well , try to think of the most efficient way through an algorithm how a problem can be solved. Once do that , then try to implement that using a code.

I tend to think of a code while i am thinking of logic, what that does is, it create a sense of fear of not knowing how to code and i tend to budge the code in a way to suit my current knowledge of programming. That is kind of low because that Leeds to inefficiency most of the times.

Best approach is to write a good algorithm and then think of ways to code.  

Coding can be done in any language or any number of ways. 

next page 153

More in next part.

References:

Head First Java 2nd Edition

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s