You are on page 1of 33

Object Oriented

Programming (OOP)

CHAPTER TWO-II

Fundamental Programming
Structures in Java

10/02/22 by: A.F 1


Steps in Program Development
1. Define the problem into three separate components:
• inputs
• outputs
• processing steps to produce required outputs.
2. Outline the solution.
• Decompose the problem to smaller steps.
• Establish a solution outline.
3. Develop the outline into an algorithm.
• The solution outline is now expanded into an algorithm.

10/02/22 by: A.F 2


Steps in Program Development

4. Test the algorithm for correctness.


• Very important in the development of a program, but often
forgotten
• Major logic errors can be detected and corrected at an early
stage.
5.Code the algorithm into a specific programming language.
6.Run the program on the computer.
• This step uses a program compiler and programmer-designed
test data to machine-test the code for
• syntax errors
• logic errors
7.Document and maintain the program.

10/02/22 by: A.F 3


• How to Perform Output?

System.out.println( “Hello world!" );

• How to add input?


Import Declarations:
import java.util.Scanner;
Scanner input = new Scanner( System.in );
number1 = input.nextInt();

10/02/22 by: A.F 4


input in java
int nextInt()
Scans and return the next token of the input as an int. 
byte=nextByte() long=nextLong() short=nextShort()
double nextDouble()
 Scans the next token of the input as a double. 
float nextFloat()
Scans the next token of the input as a float
boolean nextBoolean()
Scans and return the next token of the input into a boolean. 
char next().charAt() scans the next token of the input as char.
String next()
Finds and returns the next complete token from this scanner. 
String nextLine()
Advances this scanner past the current line and returns the
input that was skipped. 

10/02/22 by: A.F 5


Example: Input in java
//This program convert temperature in Fahrenheit to Celsius.

package fahrenheittocelsius;
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a degree in Fahrenheit: ");
double fahrenheit = input.nextDouble();
// Convert Fahrenheit to Celsius
double celsius = 5.0/9* (fahrenheit - 32);
System.out.println("Fahrenheit " + fahrenheit + " is " +
celsius + " in Celsius");
}
} 10/02/22 by: A.F 6
Comments in java
Comments are ignored by the compiler but are useful to other
programmers.
Java programming language supports three kinds of comments:
1. Single line comment // comment text
The compiler ignores everything from // to the end of the line.

2. Multiple line comment /* comment text */

The compiler ignores everything from /* to */

3. Documentation comment /** documentation */


This indicates a documentation comment (doc comment, for short).
The compiler ignores this kind of comment.
 The javadoc tool uses doc comments when preparing automatically
generated documentation.
10/02/22 by: A.F 7
Arithmetic operators in Java
 + (addition)
 – (subtraction)
 * (multiplication)
 / (division)
 Integer division by zero is not allowed where as floating-point division by
zero yields an infinite.
 ++ adds 1 to the current value of the variable .
 -- subtracts 1 from the variable.
 Compound expressions are formed by combining simple expressions using
arithmetic operators

10/02/22 by: A.F 8


Java Operators
 Assignment operator: Used to assign value to a variable.
E.g.
int x=34;

 Logical operators
 && for the logical “and” operator and
 || for the logical “or” operator.

10/02/22 by: A.F 9


Java Operators

Relational operators
• == (equal),

• != (not equal)

• < (less than),

• > (greater than),

• <= (less than or equal), and

• >= (greater than or equal) operators.


10/02/22 by: A.F 10
Java Operators
Bitwise Operators
• When working with any of the integer types, you have operators that can
work directly with the bits that make up the integers.

• The bitwise operators are

• & (“and”)

• | (“or”)

• ^ (“xor”)

• ~ (“not”) e.g. used to get one’s complement of a number. (Read more on


page 1522, Java how to program 7th Ed.)

10/02/22 by: A.F 11


Keywords
• are reserved for use by Java and are always spelled with all
lowercase letters.

10/02/22 by: A.F 12


Primitive vs Reference var
• Primitive Variables
• Are created from primitive data types
• E.g. int age;

• Reference Variables
• Are used to store the address of an object
• Created from Classes
• E.g. Scanner input;
10/02/22 by: A.F 13
Conditional Statements

if Conditions
 if condition
 If…else condition
 if …else if…else condition

10/02/22 by: A.F 14


Conditional Statements

 if condition --- Syntax


if (condition)
{ For example:

statement1 if (yourSales >= target)

statement2 {

... performance = "Satisfactory";

} bonus = 100;
}

10/02/22 by: A.F 15


Conditional Statements
 if … else condition --- Syntax
if (condition)
{
statement1
For example:
statement2
if (yourSales >= target)
... {
} performance = "Satisfactory";
else bonus = 100 + 0.01 * (yourSales - target);
{ }
statementx else
statementy {
... performance = "Unsatisfactory";
bonus = 0;
}
}

10/02/22 by: A.F 16


Example: Conditional Statements in java
//A student pass or fail based on the grade scored out of 100%
package studentpassfail;
import java.util.Scanner;
public class StudentPassFail {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a student grade: ");
int mark = input.nextInt();
if (mark >= 50)
System.out.println(“The student is pass ");
else
System.out.println(“The student is fail ");
}
}

10/02/22 by: A.F 17


Conditional Statements
 if … else if … else condition --- Syntax
if (condition)
{
statement1
...
}
else if
{
statementx
...
}

else
{
statement…
}

10/02/22 by: A.F 18


Example: Conditional Statements in java
//A student pass or fail based on the grade scored out of 100%
import java.util.Scanner;
public class StudentPassFail {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a student mark: ");
int mark = input.nextInt();
if (mark >= 90&& mark<=100)
System.out.println("Grade=A ");
else if (mark >= 80 && mark<90)
System.out.println("Grade=B ");
else if (mark >= 60&& mark<80)
System.out.println("Grade=C ");
else if (mark >= 50&& mark<60)
System.out.println("Grade=D ");
else
System.out.println("Grade=F ");
}
}

10/02/22 by: A.F 19


Conditional Statements
Multiple Selections—The switch
Statement

The if/else construct can be


cumbersome when you have to
• byte,
deal with multiple selections
• short,
with many alternatives. • int
• char
E.g.

To set up menuing system

10/02/22 by: A.F 20


For loop
• The for statement provides a compact way to iterate over a range of
values.
• The general form of the for statement can be expressed as follows:
for (initialization; termination; increment)
{ statement(s) }
E.g.
for(int i=1; i<5; i++)
{
System.out.print (i); }
Output
1234

10/02/22 by: A.F 21


• The three expressions of the for loop are optional; an infinite loop can be
created as follows:
// infinite loop for ( ; ; )
{
// your code goes here
}
• The for statement also has another form designed for iteration through
Collections and arrays
• This form is sometimes referred to as the enhanced for statement, and can be
used to make your loops more compact and easy to read.

10/02/22 by: A.F 22


The while loop
• The while statement continually executes a block of statements while a
particular condition is true.
• Its syntax can be expressed as:
while (expression)
{ statement(s) }
• The while statement evaluates expression, which must return a boolean value.
int count = 1;
while (count < 11)
{
System.out.println("Count is: " + count);
count++;
}

10/02/22 by: A.F 23


Do-while loop
• The Java programming language also provides a do-while statement, which can
be expressed as follows:
do{
statement(s) }
while (expression);
E.g.
count =1
do {
System.out.println("Count is: " + count);
count++; }
while (count <1);

10/02/22 by: A.F 24


Jumping statements

• Java makes it easier to jump out of loops and to


control other areas of program flow with its break
and continue statements.
•The continue statement
• When the continue statement is
executed inside a loop-statement, the
program will skip over the remainder of
the loop-body to the end of the loop

10/02/22 by: A.F 25


The continue statement (cont.)

• Schematically:

10/02/22 by: A.F 26


The break statement

•When the break statement is executed inside a


loop-statement, the loop-statement is terminated
immediately
• The execution of the program will continue with
the statement following the loop-statement

10/02/22 by: A.F 27


The break statement (cont.)
• Schematically:

10/02/22 by: A.F 28


The new operator
• Is used to create an object or instance from a class.

• E.g.
Scanner s=new Scanner(System.in);
DataInputStream str=new DataInputStream
(System.in);

10/02/22 by: A.F 29


Escape Sequences
• Some Java escape sequences:

Escape Sequence Meaning

\b backspace
\t tab
\n newline
\" double quote
\' single quote
\\ backslash

10/02/22 by: A.F 30


Programming Errors

• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result

10/02/22 by: A.F 31


Compilation Errors

public class ShowSyntaxErrors {


public static void main(String[] args) {
i = 30
System.out.println(i+4);
}

10/02/22 by: A.F 32


Runtime Errors

public class ShowRuntimeErrors {


public static void main(String[] args) {
int i = 1 / 0;
}
}

10/02/22 by: A.F 33

You might also like