13
Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #1 Introduction to JAVA Programming

Computer Programming, I Laboratory Manual Experiment #1 ...site.iugaza.edu.ps/.../2017/09/Lab-1-Introduction-to-Java-Programming.pdf · Experiment #1: Introduction to JAVA Programming

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Think Twice

Code Once

The Islamic University of Gaza

Engineering Faculty

Department of Computer Engineering

Fall 2017

ECOM 2005

Khaleel I. Shaheen

Computer Programming, I

Laboratory Manual

Experiment #1

Introduction to JAVA Programming

Experiment #1: Introduction to JAVA Programming

2

What is JAVA?

Java is a programming language and computing platform first released by Sun

Microsystems in 1995. Sun Microsystems was purchased by Oracle in 2010. There are lots of

applications and websites that will not work unless you have Java installed, and more are

created every day. Java is fast, secure, and reliable. From laptops to datacenters, game

consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

What does JAVA really is?

Java is a programming language and a

runtime environment. So, when someone says

Java, they could be referring to a Programming

Language or the Runtime Environment. Android is

a runtime environment different from the standard

runtime environment from oracle.

JDK Vs. JRE?

In order to run and create Java applications we need both JDK and JRE. Java Runtime

Environment (JRE) is required to run java applications, and end-users – the ones who are going

to run our apps - normally require only the JRE. Java Development Kit (JDK) provides the tools

required to create java apps. So, developers are normally the ones who install the JDK on their

machines. And in order to develop the apps you need to run them, so, the JDK installation

includes a JRE.

The whole process

At first, we write our java source file, a file that has .java extension. Then we compile

that .java file using tools from JDK to produce .class file.

The JRE provides what we need in order for our java app to run in any host environment

like Windows, macOS, Linux or Android.

JAVA

Programming Language Syntax Data Types

Control Flow Object-Oriented

Runtime Environment Configuration Security

Threading Input/Output

Experiment #1: Introduction to JAVA Programming

3

Java Editions

Java comes in three editions:

1. Java Standard Edition (Java SE), to develop client-side applications. The applications

can run standalone or as applets running from a Web browser.

2. Java Enterprise Edition (Java EE), to develop server-side applications, such as Java

servlets, Java Server Pages (JSP), and Java Server Faces (JSF).

3. Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell

phones.

Java SE is the foundation upon which all other Java technology is based. There are

many versions of Java SE. The latest is Java SE 8, which we will use in this Lab. For Java SE

8, the Java Development Toolkit is called JDK 1.8 (also known as Java 8 or JDK 8).

• Note that Java 9 will be released on 21/09/2017. So, be up-to-date. 😊

Installing Java

1. Go to oracle downloads page.

http://www.oracle.com/technetwork/java/javase/downloads/index.html

2. From the page, choose to download the standard edition as illustrated in the figure

below.

JDK

tools

Java App

(Byte Codes, .class)

JRE Host Environment

(Windows, Mac,

Linux, Android)

Experiment #1: Introduction to JAVA Programming

4

3. Accept license agreement and download the compatible version with your host

machine.

4. Installing java is a straightforward process. You just have to run the installation

wizard and follow it (next >> next >> next ... etc.). You may notice the JRE, the java

runtime environment, installed alongside with JDK.

5. Finally, you will see a screen like this:

Experiment #1: Introduction to JAVA Programming

5

Check Java version

To check the version of Java you have installed, open the command line and print the

following command:

javac -version

After executing the command, you'll get a result similar to this printed on the screen:

javac 1.8.0_77

We can also use this command:

java -version

And you'll get the following result, or similar one:

java version "1.8.0_144"

Java(TM) SE Runtime Environment (build 1.8.0_144-b01)

Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

Experiment #1: Introduction to JAVA Programming

6

Note that javac -version is used to test the JDK version (the developing tools), while

java -version is used to test the JRE version.

Example 1: First Java Program

If you wanted to, you could build your java applications having only installed the JDK.

You could use any text editor, Notepad++, WordPad or any other editor, to write down your

source code. And then you could use command line tools to compile and execute your program.

The following steps show the whole process:

1. Open any text editor of your choice and type in the following code:

public class Main {

public static void main(String[] args) {

System.out.println("Hello, World!");

}

}

2. Save the file as Main.java, then close it.

3. Open the command line and print the following command to compile the source

code to byte codes.

javac Main.java

4. After successful compiling, you will notice Main.class file created. This file is the byte

codes that could be executed using JRE.

5. Now in the command line write

java Main

to run the program.

6. You will notice Hello, World! printed out on the screen, WOW! 😅

Installing IntelliJ IDEA

As may you have noticed, that process is tedious and cumbersome. And we need a more

productive model than that. So, developers around the world use those things called Integrated

Experiment #1: Introduction to JAVA Programming

7

Development Environments (IDE) that allows us to type in our code, compile, debug and format

it, everything in one place.

IntelliJ IDEA Community Edition is the open source version of IntelliJ IDEA, a premier IDE

for Java. In this lab, we are going to use it.

The following steps show you how to install it:

1. Go to the download page and download the latest version of IntelliJ IDEA Community

Edition.

https://www.jetbrains.com/idea/

2. After download is complete, installation process is easy and straightforward. Do it

yourself.

• As a feature of being a student 😊, you can get IntelliJ IDEA Ultimate Edition, and all other JetBrains products, for free

as long as you are a student. Go to this link https://www.jetbrains.com/student/

Creating First IntelliJ IDEA Project

1. When you open the IDE, you will see the following screen.

Experiment #1: Introduction to JAVA Programming

8

2. Click on Create New Project. From the left menu choose Java. Then next, next.

When you launch the IDE for the first time, you may need to assign the Java SDK in

Project SDK field.

3. In the Project name field, enter the name you want, choose the location for your

project to live in, then click finish. After moments, your amazing project will show up.

😲

Experiment #1: Introduction to JAVA Programming

9

The .idea folder and MyApplication.iml file are not parts of the project, they are used by

the IDE for management purposes.

Our source code lives inside src folder which is an abbreviation for source.

4. Right click on src folder, choose new, Java Class and write the name you want for

that class. You will notice a new java file added inside of src folder.

5. Double click on the class name, and write down the code snippet from the previous

section. Click on Run menu from the menu bar and choose Run to run the program.

Class Name Main method

Console Output Window

Experiment #1: Introduction to JAVA Programming

10

Congratulations! You have made a complete Java application. From now on, we will

learn to build useful and complex applications.

Here are some notes on the previous code:

1. Every Java program must have at least one class. In our case we have Main class.

2. Every Java program starts execution from the main method. It is the entry point

where the program begins execution.

3. The statement System.out.println is used to display the string Hello, World! on

the console.

• String is a programming term meaning a sequence of characters, and must be enclosed in double quotation marks ("Hello,

World!").

4. Every statement in Java ends with a semicolon (;).

Example 2: Compute Mathematical Expression

In this example, we are going to write a Java program that computes the value of the

mathematical expression 15 ∗ 3 + 2.9

17 / 3.2 − 5 and prints the result on the screen.

public class Main {

public static void main(String[] args) {

System.out.println((15 * 3 + 2.9) / (17 / 3.2 - 5));

}

}

After executing the program, here is the result

153.28

Process finished with exit code 0

Comments

Comments are text inside of your source code that are ignored by the compiler.

Comments help the people read the code, which is you most of the time, better understand the

intent and functionality of the program. Also, comments are frequently used to hide parts of

the source code without completely deleting them.

Experiment #1: Introduction to JAVA Programming

11

There are three types of comments in Java:

1. Line Comments (//), the compiler ignores everything until the end of the line.

// This is a Line Comment

2. Block Comments (/* … */), the compiler ignores everything between those notations.

They can happen in one or multiple lines.

/*

This is a Block Comment

*/

3. JavaDoc Comments (/** … */), same as block comments, but they are used to

generate documentation using the JavaDoc utility.

/**

This is a JavaDoc Comment

*/

Programming Errors

1. Syntax Errors

Errors that are detected by the compiler. Syntax errors are usually easy to detect

because the compiler tells you where they are and what caused them.

The IDE helps you identify syntax errors by underling the error location with a red

line.

2. Runtime Errors

Errors that cause a program to terminate abnormally. Input mistakes typically cause

runtime errors, for example, when the program is waiting for integer value but the

user enters a string. Runtime errors are not detected by the compiler, so the program

will compile successfully but at the end it will terminate abnormally.

Experiment #1: Introduction to JAVA Programming

12

The IDE may help you detect runtime errors at compile time by coloring the error

location with yellow.

3. Logic Errors

Occur when a program does not perform the way it was intended to. Logic errors

won't abnormally terminate the program, the program continues to run but not as

you wanted it to.

Logic errors are called bugs, and they can be very difficult to find.

Example 3: Read input from user

There are many ways for getting input from the user, the simplest one is using “Scanner”

class. The next program asks the user to enter a number (radius of a circle) and the program

calculates the area of the circle and print the result to the console.

import java.util.Scanner;

public class ComputeArea {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter radius: ");

double radius = input.nextDouble();

double area = radius * radius * 3.14;

System.out.println("The area for circle of radius: "

+ radius + " is : " + area);

}

}

Example 3: IntelliJ IDEA Tips

1. Write psvm in the editor then press tab. This way you can write the main method easily

and quickly.

Experiment #1: Introduction to JAVA Programming

13

2. Write sout in the editor then press tab. This way you can write "System.out.println()"

easily and quickly.

3. Select some code and press ctrl + / at the same time. Used to comment lines.

4. Press ctrl + alt + L at the same time. Used to format your code.

5. Press ctrl + space for auto complete.

Homework

The best way to teach programming is by example, and the only way to learn

programming is by doing. So, please do it yourself and don’t copy paste from others.

1. Write a program that displays Welcome to Java, Welcome to Computer Science,

and Programming is fun on the console.

2. Write a program that displays the result of 9.5 ∗ 8.5 – 5.5 ∗ 2

150.49 – 121.09

3. You can use Cramer’s rule to solve the following 2 * 2 system of linear equation:

Write a program that solves the following equation and displays the value for x and

y:

3.4𝑥 + 50.2𝑦 = 44.5

2.1𝑥 + 0.55𝑦 = 5.9

4. (2.3) Write a program that reads a number in feet, converts it to meters, and displays

the result. One foot is 0.305 meter.

Good Luck

😊