ITCS122
Introduction to Programming Techniques
Introduction to Java environment
Programming languages
a programming language :
A language to be used in writing instructions for the computer
History of Programming
languages
Source: Why there are so many programming languages? (scienceabc.com)
Programming Languages
Machine Languages
• Any computer can directly understand only its own machine language, defined by its
hardware design.
• Generally consist of strings of numbers (ultimately reduced to 1s and 0s) that instruct
computers to perform their most elementary operations one at a time.
• Machine dependent—a particular ma-chine language can be used on only one type of
computer.
Assembly Languages and Assemblers
• English-like abbreviations that represent elementary operations formed the basis of
assembly languages.
• Translator programs called assemblers convert early assembly-language programs to
machine language.
Programming Languages
High-Level Languages and Compilers
• High-level languages
• Single statements accomplish substantial tasks.
• Compilers convert high-level language programs into machine language.
• Allow you to write instructions that look almost like everyday English and contain commonly used
mathematical notations.
• A payroll program written in a high-level language might contain a single statement such as
• grossPay = basePay + overTimePay
Interpreters
• Compiling a high-level language program into machine language can take
considerable computer time.
• Interpreter programs, developer to execute high-level language programs directly,
avoid the delay or compilation, although they run slower than compiled programs.
What is Java?
-Java is a high level programming language and
computing platform.
-In 1991, Sun Microsystems funded an internal
corporate research project led by James Gosling,
which resulted in a C++-based object-oriented
programming language that was called Java.
-First released by Sun Microsystems in 1995.
-Currently owned by Oracle.
Why Java?
- General-purpose programming language
Website development, Cloud App development, Mobile App
development , Embedded software, ……
-Write once, run everywhere
you can write programs that will run on a great variety of computer systems
and computer-controlled devices
-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!
Why Java?
• Java has become the most widely used general-purpose programming
language with more than 10 million developers.
• For many organizations, the preferred language for meeting their
enterprise programming needs is Java.
• There are billions of personal computers in use and an even larger
number of mobile devices with computers at their core.
• According to Oracle’s 2016 JavaOne conference keynote presentation,
Java runs on 15 billion devices, including two billion vehicles and 350
million medical devices.
What is Java Platform?
The Java platform consists of the Java application programming
interfaces (APIs) and the Java virtual machine (JVM).
Java APIs are libraries of compiled code that you can use in your
programs. They let you add ready-made and customizable
functionality to save you programming time.
Java Virtual Machine
JVM (Java Virtual Machine) is an engine that provides runtime
environment to drive the Java Code or applications. It converts
Java bytecode into machines language. JVM is a part of Java
Runtime Environment (JRE).
In other programming languages, the compiler produces machine
code for a particular system. However, Java compiler produces
code for a Virtual Machine known as Java Virtual Machine.
Programming in Java
Source: Head First Java, 2nd Edition
Programming in Java
Source: Head First Java, 2nd Edition
Java Compiler
A program has to be converted to a form the Java VM can
understand, so any computer with a JVM can interpret and run the
program. Compiling a Java program means taking the
programmer-readable text in your program file (also called source
code) and converting it to bytecodes, which are platform-
independent instructions for the JVM.
Java IDE
Java IDE (Integrated Development Environment) is a software
application that enables users to write and debug Java programs
more easily. Most IDEs have features such as syntax highlighting
and code completion that helps users to code more easily.
Usually, Java IDEs include a code editor, a compiler, a debugger,
and an interpreter that the developer may access via a single
graphical user interface. Java IDEs also provide language-specific
elements such as Maven, Ant building tools, Junit, and TestNG for
testing.
Java IDE examples
(http://www.eclipse.org)
(http://www.netbeans.org)
(http://www.jetbrains.com)
(https://jcreator.en.softonic.com)
Code Structure in Java
public class Grade {
Public static void main(String[] args){
//statements
}
void print{
//statements
}
Source: Head First Java, 2nd Edition
}
Note
• - Each Java program must have one class with the main method
• - JVM will start by executing the main method
Example
Note
Java programming language is case-sensitive
uppercase and lowercase letters are distinct
Quiz
Rearrange the code snippets to make a complete Java program:
Class Declaration
Access Modifier:
A keyword that specifies Class name (identifier)
the accessibility or scope
It can be one of the below:
Public: accessible everywhere
Private: only within class
Protected: packages and
inheritance
Keyword
Each class declaration that begins with the access modifier public must be stored in a file that has the
same name as the class and ends with the .java filename extension.
Every class declaration contains keyword class followed immediately by the class’s name.
Java Identifier
-A series of characters consisting of letters, digits, underscores (_)
and dollar signs ($) that does not begin with a digit and does
not contain spaces.
-It is a name given to a package, class, interface, method, or variable. It
allows a programmer to refer to the item from other places in the
program.
-To make the most out of the identifiers, choose a meaningful name
and use CamelCase.
-CamelCase: Class names begin with an uppercase letter, and method
and variable names begin with a lowercase letter.
Java Identifiers
Valid Identifiers: Invalid Identifiers:
$value 7button
_value input field
m_inputField1 class
button7 First%Name
Value
value
Methods
A Java method is a collection of statements that are grouped
together to perform an operation. When a method terminates, the
values of its local variables are lost.
Method name
Example: Access (identifier) Parameters list (if needed)
Modifier
Return type:
Can be any data type: double, int, String, etc. or it can be void.
void: means that this method will perform a task but will not return any information.
Main Method
The main() is the starting point for JVM to start execution of a Java
program. Without the main() method, JVM will not execute the
program. The syntax of the main() method is:
Performing output in Java
System.out.println( );
displays (or prints) a line of text in the command window, then it positions the
output cursor at the beginning of the next line in the command window.
System.out.print( );
print does not position the output cursor at the beginning of the next line.
Thus, the next character the program displays will appear immediately after the
last character that print displays.
System.out.printf( );
(f means “formatted”) which displays formatted data
Performing output in Java
Example:
System.out.println( “Welcome to” );
System.out.println( “Java Programming!” );
Example:
System.out.print( “Welcome to” );
System.out.print( “Java Programming!” );
Performing output in Java
Example:
System.out.printf("%s %s%n", "Welcome to", "Java Programming");
System.out.printf("%s%n%s%n", "Welcome to", "Java Programming “);
System.out.printf ("Welcome to%n%s%n", "Java Programming “);
Method printf’s first argument is a format string that may consist of fixed text and format
specifiers.
Displaying multiple line of text
with a single statement
• A single statement can display multiple lines by using newline
characters (\n)
System.out.println("Welcome\nto\nJava\nProgramming!");
Escape Sequence
• The backslash (\) is an escape character, which has special meaning
to System.out’s print and println methods. When a backslash appears
in a string, Java combines it with the next character to form an escape
sequence.
Escape Sequence
Comments
• To document the program and improve its readability.
• Java compiler ignores comments
• Three types of comments:
// end of line comment
/* Traditional comment
that could be split over multiple lines */
/** Javadoc comments */
Source: https://www.learncomputerscienceonline.com/instruction-cycle/
Source: https://www.learncomputerscienceonline.com/instruction-cycle/
Variables
Source: https://sites.google.com/a/iharrow.org.uk/compsci/2-2-programming/2-2-1-programming-concepts
Source: http://www.bitsofbytes.co/variables--data-types.html
Variable
• It is a location in the computer’s memory where a value can be
stored for use later in a program.
• The information being stored is called the value of the variable.
• All Java variables must be declared with a name and a type
before they can be used.
• Declaration statements end with a semicolon ;
• A variable’s name enables the program to access the variable’s
value in memory.
• A variable name can be any valid identifier
Variable
• A variable’s type specifies what kind of information is stored at
that location in memory.
• A variable’s type specifies the amount of memory allocated for
a given variable and how the value associated with that
variable should be encoded into 1's and 0's in memory are
specified by its type.
Primitive Data Types
Variable Declaration
DataType variableName ; Declaration Statement
Examples:
Identifier - Apply CamelCase; variable names begin
int x; with a lowercase letter
double y;
char n;
float z;
Boolean isGood;
Note: Primitive-types names are keywork and must appear in all
lowercase letters!
Assigning Values to Variables
variableName = expression ; Assignment Statement
Assignment Operator
Examples:
x = 5;
y = x / 2;
n = ‘J’;
z= 32.5f;
isGood = true;
Variables Initialization
DataType variableName = expression ;
Initialization Statement
Examples:
int x = 5;
double y = x/2;
char n = ‘J’;
float z= 32.5f;
boolean isGood = true;
Getting input in Java
-Input / data can come from different sources, such as:
- user at the keyboard
- file on disk
-The pre-defined Scanner class from java.util package will be used for
input Import java.util.Scanner;
Scanner input = new Scanner (System.in);
Scanner Class
public final class Scanner extends Object implements
Iterator<String>
{
nextInt() { ….. }
nextDouble() { ….. }
nextFloat() { ….. }
nextLine() { ….. }
nextBoolean() { ….. }
next() { ….. }
}
Getting input in Java - continue
-For every value to be read, we shall declare a variable of the proper
data type int noStudent;
double salary;
-Assign values to the variables
noStudent = input.nextInt();
salary = input.nextFloat();
Getting input in Java - continue
-Before an input statement, we put an output statement as a prompt
instructing the user on what to do and what to enter!
System.out.prnitln(“Enter the number of students in the class”);
noStudent = input.nextInt();
System.out.prnitln(“Enter your salary”);
salary = input.nextFloat();
Displaying the value read from
the user
System.out.prnitf(“The number of student is: %d%n”, noStudent);
System.out.prnitf(“The salary is:%f%n“, salary);
Exercise:
• Solve the following Computing Problem :
Read two numbers from the user, compute and display their summation.
Algorithm Pseudocode
-Read number 1 Input:
-Read number 2 Number1 , number2
-Add number1 and number 2 to get the summation
Output:
-Display the summation
Summation
Process:
Summation = number1 + number2
Flow chart
Start
Number 1,
Number 2
summation = Number 1 + Number 2
summation
Stop
Solution- Adding Two Numbers
// File Name: Addition.java
import java.util.Scanner; // program uses
class Scanner
public class Addition {
public static void main(String[] args) {
// create a Scanner to obtain input from
the command window
Scanner input = new Scanner(System.in);
System.out.print("Enter first integer:
"); // prompt
Solution- Adding Two Numbers
System.out.print("Enter second integer: "); //
prompt
int number2 = input.nextInt();
// read second number
int sum = number1 + number2; // add
numbers, then store total in sum
System.out.printf("Sum is %d%n", sum); //
display sum Console
} // end method main
} // end class Addition