JAVA PROGRAMMING-AN INTRODUCTION, HISTORY, AND THE FUNDAMENTALS FOR CREATING YOUR FIRST PROGRAM CHAPTER 5

CHAPTER 5


How to Start Writing Programs with JAVA Step by
Step



Because of its flexibility, JAVA is one of the most widely used programming languages  in use by software  developers  to date. As previously  stated in this book, JAVA is the core programming language that is used in the development of the apps for Android, and JAVA is really widely used in the back-end side of web development as well. If you happen to be brand new to the programming world and are looking to or thinking about making a career or hobby in this field, the following step by step beginner’s guide will give you a good idea what you may expect when learning to program and write your own code. Since it is easy to use and so widely  used by all sorts of programs  and programmers,  JAVA programming is an excellent option for the interested first time code writer, even if you do not necessarily have your sights set on a full time career in this field. The following step by step guide will supply you with a really solid foundation in   the   beginning   basics   of   programming,   Object   Oriented   Programming concepts, or OOP, a better understanding of computer science, and even get you on the right path to succeed in software engineering. This information will also successfully put you ahead of the game in the instance that a career in computer science is what you are really interested in.

1.    How to install JAVA Development Kit, or JDK.
Open your browser and search for JAVA JDK

Look for an option from Oracle.com and click

Click on the JAVA JDK download

Click to accept license

Choose your operating system

Minimize everything else on your computer

Once it is downloaded, be ready to run the executable file

Click next and check status

JAVA JDK will start installing

Click next and wait for JAVA to finish installing

Once installed, you can go to your C folder and you should see two files, JDK and JRE


2.    How to install and set up Eclipse Integrated Development Environment, or
IDE
Type www.eclipse.org in your browser

Opt to download eclipse IDE for JAVA developers

Choose your operating system

You will then be directed to choose your PC’s nearest location, so click the location that is nearest to you

You will then be prompted to save the zip file, save zip file

Open the zip file once it is finished downloading

Minimize everything else on your computer

The zip file contains a folder named eclipse, extract to C folder

Once the extraction is complete, you should be able to see eclipse in your C folder

Open up Eclipse

The very first time that you openup Eclipse, a question will appear that will ask you where you want your projects to be saved - choose a location or leave it at C folder default

Click OK and eclipse should open shortly

In order to start a brand new project, go to file, then click on new, then click on JAVA project

Name your project

Capitalize the first letter of each of the words in your project name

Click next

Click finish

Your “My Project” folder is created

3.    How to create your first JAVA code in Eclipse IDE Start a new project within Eclipse

You will see that eclipse will also create a subdirectory for the project you create, this will contain your source file

Close all projects except for the project you will be working on

Right click on the source folder of the project you want to work on and you will be able to add packages to your folder (whenever you add a package it will add a folder to your source folder)

Access the package you just created and you will be able to create a new class

When naming your class, make sure the first letter of each word of the name you choose is capitalized

You will be asked which method stop you would like to create, check public static void mean

Press finish

You will want to include a comment section in your project; in order to do this you will first start with a forward slash, followed by an asterisk, then press enter

This comment section is not executed by your program, it is only info you are documenting for your program

Another way of creating comments is by first starting with two forward slashes and posting your comment after; this will not be executed either

Whatever you write in the main class will be executed

JAVA has built-in classes to help you program; the most important of these is called System – make sure the S is capitalized

When you type System then a period eclipse will automatically know what to do; it should look like this: System.

A popup menu will then appear, showing you the available methods of a label in this system class

Select the method called out.println

Once that is selected,  you will then be able to click on it and start leaving a comment

When you are ready to run your program, you will then right click on your project and run it as a JAVA application – or – you can click the “run my class” option, which will open the save and launch popup

Whenever you scroll over a built-in method of class in eclipse editor it will give you a help popup which shows you what that particular code can be used for


4.    Programming with variables and Types in JAVA
When declaring a variable in JAVA you must first select the data type For example, let’s say you want to store an integer in a variable Declare the data type as “int” (for integer)
Name your variable, say you name your variable x

Assign value to your variable, such as 10

Your line of code should now look like this: int x = 10;

You can also directly use your variable in the System.out.println line

Instead of typing a message in this line, you can put x as the value;
your line should then look like this: System.out.println(x);
When this program is run, the value assigned to x, which is 10, will be printed where the x is in the line

Basic data types include:

1.  a byte – which is a number and 1 byte
2.  a short – which is a number and 2 bytes
3.  an int – which is a number and 4 bytes
4.  a long – which is a number and 8 bytes
5.  a float – which is a float number and 4 bytes
6.   a double – which is a float number and 8 bytes
7.  a char – which is a character and 2 bytes

8.  a boolean – which is a true or false and 1 byte
Byte, short, int, and long can all store a number, the only difference is their size – as indicated

The range of all of the variables increases with byte size

Short example: short my_variable=10;

When using the float variable, you need to include the word float into your code: float my_decimal=(float)4.5;

When using the double variable, it is not necessary to differentiate and should look like this: double my_double=11.52;

Use  char   if  you  want   to  store   any  one  single   character:   char my_char=’A’;

Boolean can store true or false: boolean is_true=false;

When  printed  out,  only  the  values  of  all  of  these  variables  would appear, so in order they would show up as 10, 4.5, 11.52, A, and false


5.    What it means to get user input using JAVA
There  is a class in JAVA called  Scanner  which enables  you receive input and looks like this: Scanner scan=new Scanner(System.in);

In order to be able to give output in the same line, it should look like this: Scanner scan=new Scanner(System.out);

If you want to gather input from a user, your first line of code should be: System.out.println(“Enter some number”);

You then want to name your variable and continue coding: Int user_input_number = scan.
When you reach the part in the second line at scan., eclipse will then
prompt you to make a choice, here you want to choose next int, so the completed second line will look like this: int user_input_number = scan.next int();

Now if you want to print this value, that line of code should look like this: System.out.println(“The entered value is”);

If you want to print your output without breaking the line, first omit the ln after print in the line code, then copy your variable, which in this case is user_input_number, then paste it in the parentheses and run the

program,    and    the    very    last    line    will    appear    this    way: System.out.print(user_input_number);

When you’ve run this program, you will notice that it asks the user to enter a value. Let’s say you enter the value of 1,000

Once you enter the value, the program will confirm the entered value

Double  works  the  same  way,  but  you  have  to  make  sure  you  are consistent in changing the entire line of code, for example:

Scanner scan1 = new Scanner (will appear as “System.in”); System.out.println (Enter a decimal value here);
double user_input_double = scan1.nextdouble(); System.out.println(“The entered value is”); System.out.print(user_input_double);
If you want to take text input, you need to define the variable as string

Take the previous code and just change it scan.next line, because you are expecting a line from the user

The entire program should then look like this:


First line: “Scanner scan1 = new Scanner (System.in);" Second line: “System.out.println(“Enter  some string”);” Third line: “string user_input_string = scan1.nextLine();” Fourth line: “System.out.println(“The entered string is”);” Fifth line: “System.out.print(user_input_string);”


When you run this program you will be prompted to enter some string, enter a phrase of your choosing


6.    Getting to know the math and arithmetic operators in JAVA First, declare two variables, x and y in this instance

Then assign an answer int x, y, answer;
Assign value to x and y, 20 and 30 respectively

Assign value to the answer, 50

If you want to print out your answer, the program should look like this:

int x, y, answer;
x = 20;
y = 30;
answer = x + y; System.out.println(“Answer = “ + answer);
When  an  addition  or plus  symbol  is used  in a print  function,  it is known as a concatenation or concatenation operator

The  correct  answer  should  be  printed  out  in  eclipse  without  you actually having to do the math, as long as the code is correct

In order to perform  a subtraction,  all you need to do is replace  the addition symbol with the subtraction symbol in the fourth line of code, or the answer line: answer = x – y;

When performing multiplication, x cannot be used as the multiplication symbol because x is often a variable; instead, an asterisk is used

All you have to do to perform  multiplication  is replace  the symbol again: answer = x * y;

Division  is a little bit trickier  – if you divide a lower number by a higher number and have declared your variable as integer, the answer will always be zero because integers cannot be decimals

In the same light, when your variable has been declared as integer and you  are  dividing  a  higher  number  that  is  not  divisible  by  a  lower number without using decimals, you will not get an exactly mathematically correct answer either – if you try to divide seventy by thirty, your answer will be two because that is how many times thirty can go into seventy while having a whole number as an answer

In order to get accurate answers in division, you must declare your variables as double

A forward slash symbol is used in place of the other symbols when using division

One more operator in JAVA is called the modulus operator and it gives you the remainder of a division


7.    Getting to know the increment operator and the assignment operator
An increment operator is used when you would like to increase the

value of a variable       and somewhat resembles algebra int x = 10;
x = x + 1; System.out.println(x);
Another way of writing this code to achieve the same goal would be to replace the + 1 with two addition symbols, this is called post increment operation

int x = 10; x++; System.out.println(x);
The same answer of eleven has been reached both times in print

If you use x++ in the print line it looks like this:

int x = 10; System.out.println(x++);
In post increment operation, the value of x will only be changed after this plus  operation

Now you are going to increase the value of x by one which should look like this:

int x = 10; System.out.println(x++); System.out.println(x);
Pre increment operation places the plus symbols in front of the x and
increases its value before the operation is performed

So if we add ++x in place of x++ in the previous code, this is what you would see:

int x = 10; System.out.println(++x); System.out.println(x);
The answer to this is actually:

11

11

This is because the pre increment operator, where x had a value of 11 and the post increment operator, where x also had a value of 11 were compiled

Now let’s say you want to add five to this value, you can do it by writing out: x = x + 5

Another way of writing this is: x+ = 5

If you would like to multiply the value of 5 and the value of 10 in the equation, you would only need to replace the addition symbol in x+ =5 with an asterisk – that shortcut works for multiplication as well – and it would look like this: x* = 5

The term ‘equal to’ in JAVA is referred to as assignment operator

Thus, x* = 5 and x = x*5 is an assignment operator and this operator works for addition, subtraction, multiplication and division


8.    How to incorporate the if else or conditional statements and their relational operators
A conditional statement is a statement which evaluates whether a condition  is true or false,  and based  upon this condition  executes  a certain code

A double equal sign == symbolizes relational operator

An if statement looks like this:

int x = 10;
if (x == 10) {
System.out.println(“yes x == 10”);

This statement is basically saying if x is equal to 10, then I want to run this code. Here, the program answers that yes, x is in fact equal to 10, which is true.
That  means  the  statement  is  followed  and  the  condition  will  be executed

Now try to see if x is equal to 20 by only changing the statement by placing the number 20 in the if line

No answer will come up in eclipse because this statement is false and the condition cannot be executed

An if else statement  can execute when an if statement  does not and looks like this:

int x = 10;

if (x == 20) { System.out.println(“yes x == 10”);
}

else  {

System.out.println(“no x != 10”);

Take note of the exclamation point that has taken the place of one of the  equal  signs  after  the  else  line  –  this  is  called  a  non-equality operator

Now when you run the program again eclipse will give you the answer that x is in fact not equal to 10

In JAVA, comparison operators include:

1.   ==   is equal to
2.   !=    is not equal to
       is greater than
3.   <      is less than
4.   >=   is greater than or equal to
5.   <=   is less than or equal to


9.    How to utilize the logical operators in JAVA
If you want  to evaluate  more  than  one  condition  using  a simple  if statement or an if else statement you will need to use a logical operator

There  are  two  basic  kinds  of  logical  operators  –  the  and  operator, which is symbolized by double ampersands && and the second type is called an or operator which is symbolized by double pipe symbols ||

The and operator and the or operator can both be used to define two conditions simultaneously in both if and if else statements

The and operator checks to see if all conditions are simultaneously true while the or operator checks to see if one or more things is true

The symbol placed between subjects tells which you are using, as in line four of the following two codes:

int subject1 = 40;
int subject2 = 60;
// && ->AND   || ->OR
if ((subject1 >= 35) && (subject2 >= 35)) { System.out.println(“the condition is true”);

This condition is true because both 40 and 60 are greater than 35.
Now try to incorporate an else statement with the if statement, nut first change the value of subject1 to 20

if else
int subject1 = 20;
int subject2 = 60;
// && ->AND   || ->OR
if ((subject1 >= 35) && (subject2 >= 35)) { System.out.println(“the condition is true”);
} else {
System.out.println(“the condition is false”); This condition is false.
Now try running the same program but replace the and operator with the or operator:

int subject1 = 40;
int subject2 = 60;
// && ->AND    || ->OR
if ((subject1 >= 35) || (subject2 >= 35)) { System.out.println(“the condition is true”);
} else {
System.out.println(“the condition is false”); This condition is true.

10.  Working with the switch statement in JAVA
Whenever you have to check multiple in JAVA, you will want to utilize

the conditions switch statement                    

You can also switch statements in place of if else statements

The breaks in a switch statement act to break up the responses you will  

receive from the program when you run it

Part of a switch statement includes the use of ‘default’ which works like the else in an if else statement

int = 90;

// byte, int, short, or char. switch (score)
{
case 90 :
System.out.println(“Very good”);
break;
case 60 : System.out.println(“Good”); break;
case 30 : System.out.println(“OK”); break;
default :
System.out.println(“Grades  are not defined”);
break;
}
With the value of the integer identified as 90, the case 90 statement is true.
When  there  is no break  between  case  statements,  the  program  will default to the next lowest in succession when utilizing this type of code


11.   Working with the while statements in JAVA, also known as while loops
While loops are the most basic loops in JAVA

A loop is a piece of code or a statement which executes some block of code again and again until some condition is met

If you want to execute some code again and again without having to rewrite it over and over, this is what loop is for:

int a = 0;
while (a <=10)
{
System.out.println(a);
a++;

}
12.   Proceeding to the do while statements in JAVA, also known as the do while loops
The basic dissimilarity  from a while loop to a do while loop is that even though  loops first evaluate  the condition  and then execute  the code, the do while loops execute the code first and then evaluate the condition


13.   How to learn to implement arrays in your JAVA program
An array is similar to a variable but it can store more than one value at a time – the only condition being that even though you can store more than one value in an array, it has to be the same type of values

For example, you would be able to store 10 integers in an array, but if you wanted to store 5 integers and 5 doubles, you would not be able to do it
This is one way of declaring an array: int [] myintarray = {4, 2,1,5,3}; Another way is: int myinarray2[] = {4,2,1,5,3} This way is fine but it
is not the preferred way of declaring an array

There are three more ways of declaring arrays:

1.   int[] myIntArray = new int[3];
2.   int[] myIntArray = {1,2,3};
3.   int[] myIntArray = new int []{1,2,3};

14.   How to use the JAVA string in your program
A string is a sequence of characters and can also be known as an array of characters

In order declare a string in JAVA, you just use the keyword ‘string’ You then name your string whatever you would like
Then you place an equal sign after that

Then in double quotes, place whatever string you want to assign and print it just the same as all the others:

String mystring = “Hello World”; System.out.println(mystring)

15.  Introduction to using methods in your JAVA programming

The terms method and function can be used somewhat interchangeably, but the preferred term in JAVA is method

A method is a piece of code which executes some logic and you can wrap this method with a name and you can recall this method as many times as you would like whenever you want to use it

You can name your method anything you wish

This is the most basic kind of method:


public static void myFirstMethod() {
}
The term ‘public static’ is known as the pacifier

If you want to print a message using method, this is what the syntax or code would look like:

public static void myFirstMethod() { System.out.println(“Hello Youtube”);
}


CONCLUSION

Thank you again for taking the time out of your schedule to download JAVA Programming: An Introduction, History, and the Fundamentals For Creating Your First Program!

Since you have completed reading this book, you should now have a good understanding  of JAVA  programming  and also possess  the skills  to complete some basic JAVA code!

If you have enjoyed this book as much as I have enjoyed writing it, I ask that you please take the time to leave me a review on Amazon. I appreciate  your positive feedback!


Post a Comment

Previous Post Next Post