Chapter 5 : Writing a Program

Hi everyone, in this article, we will discuss how to write the program. What we do before writing coding. Ok, let’s discuss this.

When we create the program (writing coding) we follow this order.

  • Figure out the general flow of the program.

This step gives us to clear idea about our program. Most programmers first do this flow chart for easy understanding.

  • Figuring out what kind of objects we’ll need to do the work.
  • Developing Class

When we develop the class, we follow this order.

  • Figure out what the class is supposed to do.
  • List the instance variables and methods.
  • Write prep code for the methods.
  • Write test code for the methods.
  • Implement the class.
  • Test the methods.
  • Debug and reimplement as needed.

There are three things we need to do when we write for each class.

  • Prep Code:- A form of pseudocode, to help you focus on the logic without stressing about syntax.
  • Test code:- A class or method that will test the real code and validate that it’s doing the right thing.
  • Real code:- The actual implementation of the class. This is where we write real Java code.

Note:-

Most prep code includes three parts: instance variable declarations, method declarations, and method logic. Prepcode should describe what to do, not how to do it. Implementation comes later. Use the prep code to help design the test code. Write test code before you implement the methods.

Ok, now see a clear image of some methods

Converting a String to an int :- Use Integer.parseInt() to get the int value of a String. Integer.parseInt() works only if the String represents a digit (“0”,”1”,”2”, etc.)

The enhanced For loop :- That makes it easier to iterate over all the elements in an array or other kinds of collections. depending on the programming language they’ve used in the past, some people refer to the enhanced for as the “for each” or the “for in” loop, because that’s how it reads: “for EACH thing IN the collection…”

The post-increment operator :- Use the pre/post increment operator to add 1 to a variable (x++;). Use the pre/post decrement to subtract 1 from a variable (x — ;)

Break statement :- Use break to leave a loop early (i.e. even if the boolean test condition is still true).

Make a random number

Getting user input using the GameHelper class

Note:- Integer.parseInt() works only on Strings

Regular For Loop

Initialization:- Use this part to declare and initialize a variable to use within the loop body. You’ll most often use this variable as a counter. You can initialize more than one variable here, but we’ll get to that later in the book.

boolean test:- This is where the conditional test goes. Whatever’s in there, it must resolve to a boolean value (you know, true or false). You can have a test, like (x >= 4), or you can even invoke a method that returns a boolean.

Iteration expression:- In this part, put one or more things you want to happen with each trip through the loop. Keep in mind that this stuff happens at the end of each loop.

Pre and Post Increment

The short cut for adding 1 from a variable.

X++; Is the same as X = X +1 ; , they both are meaning add 1 to the current value of x “ or “increment x by 1” .

The placement of the operator (either before or after the variable) can affect the result. Example

int x= 0;

Method 1:

Int z = ++x; // output x=1 , z = 1

Method 2:

int y = x++; // Output x=1 , y=0

That means method 1 first increment x by 1, and then use this new value of x. but Method 2 is Y gets the value of x and then x is incremented.

Pre and Post Decrement

The shortcut for subtracting 1 from a variable.

X — ; Is the same as X = X — 1 ; , they both are meaning subtract 1 to the current value of x “ or “decrement x by 1”.

Difference between for and while

A while loop has only the boolean test; it doesn’t have a built-in initialization or iteration expression.

A while loop is good when you don’t know how many times to loop and just want to keep going while some condition is true.

Note:

When you assign a value from one primitive data type to another, this is known as type casting.

For example, converting a smaller type to larger type size.

long y = 42;

int x = y; //this code won’t compile

Int x = (int) y; //putting in the cast

Reference

Head First Java(Book), 2nd Edition

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store