INTRODUCTION TOCOMPUTER SCIENCE
CSC 1302
LECTURE 6
Department of Maths and Computer-
Science
Faculty of Natural and Applied Science
BY
UMAR DANJUMA MAIWADA
OBJECTIVES
 Programing tools
 Computer programming
 Programming language levels
 Introduction to JAVA language
 Syntax of JAVA
2
PSEUDOCODE
 Pseudocode is an abbreviated version of
actual computer code (hence, pseudocode).
The geometric symbols used in flowcharts
are replaced by English-like statements that
outline the process.
 Pseudocode allows the programmer to
focus on the steps required to solve a
problem rather than on how to use the
computer language.
 Pseudocode has several advantages. It is
compact and probably will not extend for
many pages as flowcharts commonly do.
3
PROGRAMMING TOOLS
 A programming tool may be any software program
or utility that aids software developers or
programmers in creating, editing, debugging,
maintaining and/or performing any programming or
development-specific task.
 Programming tools were initially designed to
support or complement programming languages by
providing the functionality and features these
languages did not have.
 Compilers, linkers, assemblers, disassemblers,
load testers, performance analysts, GUI
development tools and code editors are also all
forms of programming tools.
4
COMPUTER PROGRAMMING
 Programming is all about designing and coding algorithm
for solving problems.
 Computers are not very intelligent, the instruction they
receive must be extraordinary special.
 We write computer instructions in programming
languages, which are more constrained and exact than
human languages are.
 It must follow a series of algorithm.
 As a programmer your job is to decompose a task into
individual, ordered steps of input, calculating, comparing,
rearranging and outputting.
 The order in which the individual statements, instructions
or function calls of an imperative or a declarative
program are executed or evaluated by the computer is
5
THERE ARE FOUR DIFFERENT WAYS THAT THE
FLOW OF CONTROL CAN PROGRESS THROUGH A
PROGRAM.
 Sequential execution
 Method call
 Selection
 Looping
6
PROGRAMMING LANGUAGE LEVELS
1. Machine languages
2. Assembly languages
3. High-level languages
 The following section of an assembly-language
program also adds overtime pay to base pay and
stores the result in gross pay, but somewhat more
clearly than its machine-language equivalent.
 LOAD BASEPAY
 ADD OVERPAY
 STORE GROSSPAY 7
Machine Instruction Machine Operation
00000000 Stop Program
00000001 Turn bulb fully on
00000010 Turn bulb fully off
00000100 Dim bulb by 10%
00001000 Brighten bulb by 10%
00010000 If bulb is fully on, skip over next instruction
00100000 If bulb is fully off, skip over next instruction
01000000 Go to start of program (address 0)
INTRODUCTION TO JAVA LANGUAGE
 Java was developed by a team lead by James gosling
at microsystem. Originally called Oak.
 It was designed in 1991 for used in embedded
consumer appliance.
 In 1995, renamed Java. It was designed for
developing internet applications
 Java is a full-featured, general purpose programming
language that is capable of developing robust
mission-critical applications. Today, it is used not only
for web programming, but also for developing
standalone applications across platforms on server,
desktops, and mobile devices. 9
BENEFITS OF USING JAVA
 Syntax identical to that of C++, except that java
eliminates some of the C++’s more complex features
 Object orientation
 Internet-related features, such as applets, which are
run by the browser, and servlets, which are run by the
web server.
 An extensive library of classes that can be reused
readily, including swing classes for providing graphical
user interface and java database connectivity (JDBC)
for communication with a database.
 Portability among every platform that supports a java
virtual machine
 Built-in networking
 Open source availability of the java development kits
10
SYNTAX OF JAVA PROGRAM
 /*
 This is a simple syntax of Java program.
 Call this file classname.java.
 */
 class classname {
 // A Java program begins with a call to main().
 public static void main(String args[]) {
 statement1;
 statement2;
 …
 …
 Statementn;
 }
 }
11
THREE STEPS
 Enter the program.
 Compile the program.
 Run the program
12
EXAMPLE 1
1. Public class Hello
2. {
3. /**
4. * My first program in java
5. */
6. public static void main(String [] args) {
7. // prints the string “Hello World” on screen
8. System.out.println(“Hello World”);
9. }
10. } 13
ANALYZING MY FIRST PROGRAM IN JAVA
1. Public class Hello
2. {
3. /**
4. * My first program in java
5. */
• Indicates the name of the class which is Hello
• In java all codes should be placed inside a class
declaration
• The class uses an access specifier public, which
indicates that
our class is accessible to other class in other package
(collection of class). Package and access specifier
will be
14
1. PUBLIC CLASS HELLO
2. {
3. /**
4. * MY FIRST PROGRAM IN JAVA
5. */
• THE SECOND LINE CONTAINS A CURLY BRACE {
WHICH INDICATE THE START OF A
BLOCK.
• IN THIS CODE WE PLACED THE CURLY BRACE AT
THE NEXT LINE AFTER THE CLASS
DECLARATION, HOWEVER, WE CAN ALSO PLACED
THIS NEXT TO THE FIRST LINE OF
OUR CODE, SO WE COULD ACTUALLY WRITE OUR
CODE AS
PUBLIC CLASS HELLO{
15
1. PUBLIC CLASS HELLO
2. {
3. /**
4. * MY FIRST PROGRAM IN JAVA
5. */
• THE NEXT THREE LINES INDICATES A JAVA
COMMENT
• A COMMENT
- SOMETHING USED TO DOCUMENT A PART OF A
CODE
- IT IS NOT PART OF THE PROGRAM ITSELF, BUT
ONLY FOR
DOCUMENTATION PURPOSE
- IT IS A GOOD PROGRAMING PRACTICE TO ADD A
COMMENT TO YOUR CODE 16
1. PUBLIC CLASS HELLO
2. {
3. /**
4. * MY FIRST PROGRAM IN JAVA
5. */
6. PUBLIC STATIC VOID MAIN(STRING [] ARGS) {
• INDICATES THE NAME OF THE METHOD IN HELLO
CLASS WHICH IS THE
MAIN METHOD
• ALL PROGRAMS EXCEPT APPLET (TO BE DISCUSS
LATER IN THIS COURSE)
WRITTEN IN JAVA STARTS WITH THE MAIN METHOD
• BE CAREFUL TO FOLLOW THE SYNTAX 17
1. PUBLIC CLASS HELLO
2. {
3. /**
4. * MY FIRST PROGRAM IN JAVA
5. */
6. PUBLIC STATIC VOID MAIN(STRING [] ARGS)
{
7. // PRINTS THE STRING “HELLO WORLD”
ON SCREEN
• THIS IS ALSO A COMMENT IN JAVA
18
1. PUBLIC CLASS HELLO
2. {
3. /**
4. * MY FIRST PROGRAM IN JAVA
5. */
6. PUBLIC STATIC VOID MAIN(STRING [] ARGS)
{
7. // PRINTS THE STRING “HELLO WORLD” ON
SCREEN
8. SYSTEM.OUT.PRINTLN(“HELLO WORLD”);
• THIS COMMAND SYSTEM.OUT.PRINTLN(),
PRINTS THE TEXT ENCLOSED
BY QUOTATION ON THE SCREEN 19
1. PUBLIC CLASS HELLO
2. {
3. /**
4. * MY FIRST PROGRAM IN JAVA
5. */
6. PUBLIC STATIC VOID MAIN(STRING [] ARGS) {
7. // PRINTS THE STRING “HELLO WORLD” ON
SCREEN
8. SYSTEM.OUT.PRINTLN(“HELLO WORLD”);
9. }
10. }
• THE LAST TWO LINES CONTAINING THE CURLY
BRACES ARE USED TO CLOSE THE
MAIN METHOD AND THE CLASS RESPECTIVELY 20
THE MAIN METHOD
main method can be written in any of the following forms:
public static void main(String [] args)
or
public static void main(String args [])
 the public keyword is an access specifier.
 The keyword static allow main() to be called without
having to
instantiate a particular instance of the class
+ This is necessary since main() is called by the java
interpreter before any object is made
 void means main() does not return any value.
 String args[] declares a parameter named args, which
is an array of
instance of the class String.
+ args receive any command line arguments presents
when the program executed
21
JAVA KEYWORDS
 They are predefined identifiers reserved by java
for a specific purpose.
 They can not be used as a names of variables,
classes, methods, etc.
22
JAVA KEYWORDS
 double, int, super, abstract
 Boolean, switch, else, interface
 Break, extends, long, synchronized
 Case, finally, null, throw
 Byte, false, native, this
 Catch, float, package, transient
 Byvalue, final, threadsafe, new
 For, private, true, class
 If, public, void, char
 Class, protected, goto, try
 While, continue, implement, break
 Else, case, string, return
 Static, short, import, default 23
PRIMITIVE DATA TYPE
 • The Java programming language defines eight
primitive data
types.
 – boolean (for logical)
 – char (for textual)
 – byte
 – short
 – int
 – long (integral)
 – double
 – float (floating point).
24
VARIABLE
 A variable is a named memory location that can be
assigned a value.
 As you learned earlier, variables are declared using
this form of statement,
type var-name;
 A variable is an item of data used to store the state
of objects.
 A variable has a:
– data type
 The data type indicates the type of value that the
variable can hold.
– name
 The variable name must follow rules for identifiers.
Declaration and Initializing variable
 Declare a variable as follows:
25
HOW TO INITIALIZE VARIABLE
 . One way to give a variable a value is through an
assignment statement, as you have already seen.
 To do this, follow the variable’s name with an equal sign
and the value being assigned.
 The general form of initialization is shown here:
type varname = value;
 int count = 10; // give count an initial value of 10
 char ch = 'X'; // initialize ch with the letter X
 float f = 1.2F; // f is initialized with 1.2
 When declaring two or more variables of the same type
using a comma-separated list, you can give one or more of
those variables an initial value. For example:
 int a, b = 8, c = 19, d; // b and c have initializations
 In this case, only b and c are initialized.
 Int a = 3, b = 8, c = 19; //a, b, c all have initializations
 In this case a, b, c are initialized.
26
DECLARING AND INITIALIZING VARIABLES
SAMPLE PROGRAM
1. public class VariableSamples {
2. public static void main( String[] args ){
3. //declare a data type with variable name
4. // result and boolean data type
5. boolean result;
6.
7. //declare a data type with variable name
8. // option and char data type
9. char option;
10. option = 'C'; //assign 'C' to option
11.
12. //declare a data type with variable name
13. //grade, double data type and initialized
14. //to 0.0
15. double grade = 0.0;
16. }
17. }
27
EXAMPLE 2
28
 The following program creates two
variables called x and y.
 /*
 This demonstrates a variable.
 Call this file Example2.java.
 */
 class Example2 {
 public static void main(String args[]) {
 int x; // this declares a variable
 int y; // this declares another variable
 x = 1024; // this assigns 1024 to x
 System.out.println("x contains " + x);
 y = x / 2;
 System.out.print("y contains x / 2: ");
 System.out.print(y);
 }
 }
 When you run this program, you will
see the following output:
 x contains 1024
 y contains x / 2: 512
DECLARING AND INITIALIZING VARIABLES:
CODING GUIDELINES
1. It always good to initialize your variables as you declare
them.
2. Use descriptive names for your variables. Like for
example, if
you want to have a variable that contains a grade for a
student, name it as, grade and not just some random
letters you choose.
3. Declare one variable per line of code. For example, the
variable declarations,
double exam=0;
double quiz=10;
double grade = 0;
is preferred over the declaration,
double exam=0, quiz=10, grade=0;
29
OUTPUTTING VARIABLE DATA
• In order to output the value of a certain variable, we can use the following
commands:
System.out.println()
System.out.print()
Sample program
1. public class OutputVariable {
2. public static void main( String[] args ){
3. int value = 10;
4. char x;
5. x =
6.
7. System.out.println( value );
8. System.out.println(
9. }
10. }
The program will output the following text on screen:
10
The value of x=A
30
OPERATORS
• Different types of operators:
– arithmetic operators
– relational operators
– logical operators
– conditional operators
• These operators follow a certain kind of precedence
so that the compiler will
know which operator to evaluate first in case multiple
operators are used in
one statement.
31
ARITHMETIC OPERATORS
operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- decrement
32
EXAMPLE 3
33
The following program demonstrates the
modulus operator.
// Demonstrate the % operator.
class ModDemo {
public static void main(String args[]) {
int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
irem = 10 % 3;
dresult = 10.0 / 3.0;
drem = 10.0 % 3.0;
System.out.println("Result and remainder of 10 /
3 = " + iresult + " " + irem);
System.out.println("Result and remainder of
10.0 / 3.0 = " + dresult + " " + drem);
}
}
The output from the program is shown
here are:
Result and remainder of 10 / 3 = 3 1
Result and remainder of 10.0 / 3.0 =
3.3333333333333335 1.0
As you can see, the% yields a
remainder of 1 for both integer and
floating-point operations.
RELATIONAL OPERATORS
• Relational operators compare two values and
determines the relationship
between those values.
• The output of evaluation are the boolean values true
or false.
34
CONTROL STATEMENTS
 Java Control statements control the order of
execution in a java program, based on
data values and conditional logic.
 There are three main categories of control flow
statements;
· Selection statements: if, if-else and switch.
· Loop statements: while, do-while and for.
· Transfer statements: break, continue, return, try-
catch-finally and assert.
35
SELECTION STATEMENTS
 The If Statement
 The if statement executes a block of code only if
the specified expression is true. If the value is false,
then the if block is skipped and execution continues with
the rest of the program. You can either have a single
statement or a block of code within an if statement.
 if (<conditional expression>)
<statement action>
36
37
public class IfStatementDemo {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
if (a < b)
System.out.println("b > a");
}
}
The program will print b > a
THE IF-ELSE STATEMENT
 The if/else statement is an extension of the if
statement. If the statements in the if statement fails,
the statements in the else block are executed. You
can either have a single statement or a block
of code within if-else blocks.
 The if-else statement has the following syntax:
if (<conditional expression>)
<statement action>
else
<statement action>
38
39
public class IfElseStatementDemo {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b) {
System.out.println("a > b");
} else {
System.out.println("b > a");
}
}
}
Output displays b > a
SWITCH CASE STATEMENT
 The switch case statement, also called a case statement is a
multi-way branch with several choices. A switch is easier to
implement than a series of if/else statements. The switch
statement begins with a keyword, followed by an expression
that equates to a no long integral value. Following the
controlling expression is a code block that contains zero or
more labelled cases. Each label must equate to an
integer constant and each must be unique. When the switch
statement executes, it compares the value of the controlling
expression to the values of each case label.
 switch (<non-long integral expression>) {
case label1: <statement1>
case label2: <statement2>
…
case labeln: <statementn>
default: <statement>
} // end switch
40
41
public class SwitchCaseStatementDemo {
public static void main(String[] args) {
int a = 10, b = 20, c = 30;
int status = -1;
if (a > b && a > c) {
status = 1;
} else if (b > c) {
status = 2;
} else {
status = 3;
}
switch (status) {
case 1:
System.out.println("a is the greatest");
break;
case 2:
System.out.println("b is the greatest");
break;
case 3:
System.out.println("c is the greatest");
break;
default:
System.out.println("Cannot be determined");
}
}
}
Output displayed is: c is the greatest
ITERATION STATEMENTS
While Statement
 The while statement is a looping construct control
statement that executes a block of code while a
condition is true. You can either have
a single statement or a block of code within the
while loop. The loop will never be executed if the
testing expression evaluates to false.
 The syntax of the while loop is
while (<loop condition>)
<statements>
42
43
public class WhileLoopDemo {
public static void main(String[] args) {
int count = 1;
System.out.println("Printing Numbers from 1 to
10"); while (count <= 10) {
System.out.println(count++); } }}
Output
Printing Numbers from 1 to 10
1
2
3
4
5
6
7
8
9
10
DO-WHILE LOOP STATEMENT
 The do-while loop is similar to the while loop,
except that the test is performed at the end of the
loop instead of at the beginning.
This ensures that the loop will be executed at least
once. A do-while loop begins with the keyword do,
followed by the statements that make up the body
of the loop.
 The syntax of the do-while loop is
do
<loop body>
while (<loop condition>);
44
45
public class DoWhileLoopDemo {
public static void main(String[] args) {
int count = 1;
System.out.println("Printing Numbers from 1 to 10");
do {
System.out.println(count++);
}
while (count <= 10);
}}
Output
Printing Numbers from 1 to 10
1
2
3
4
5
6
7
8
9
10
FOR LOOPS
 The for loop is a looping construct which can
execute a set of instructions a specified number of
times. It’s a counter controlled loop.
 The syntax of the loop is as follows:
for (<initialization>; <loop condition>;
<increment expression>)
<loop body>
46
47
public class ForLoopDemo {
public static void main(String[] args) {
System.out.println("Printing Numbers from 1 to
10"); for (int count = 1; count <= 10; count++) {
System.out.println(count); }
}}
Output
Printing Numbers from 1 to 10
1
2
3
4
5
6
7
8
9
10
TRANSFER STATEMENTS
Continue Statement
 A continue statement stops the iteration of a loop
(while, do or for) and causes execution to
resume at the topof the nearest enclosing loop. You
use a continue statement when you do not want to
execute the remaining statements in the loop, but
you do not want to exit the loop itself.
 The syntax of the continue statement is
continue; // the unlabeled form
continue <label>; // the labeled form
48
49
public class ContinueExample {
public static void main(String[] args) {
System.out.println("Odd Numbers");
for (int i = 1; i <= 10; ++i) {
if (i % 2 == 0)
continue;
// Rest of loop body skipped when i is even
System.out.println(i + "t"); } }}
Output
Odd Numbers
1
3
5
7
9
BREAK STATEMENT
 The break statement transfers control out of
the enclosing loop ( for, while, do or
switch statement). You use a break statement when
you want to jump immediately to
the statement following the enclosing control
structure. You can also provide a loop with a label,
and then use the label in your break statement.
 The Syntax for break statement is as shown below;
break; // the unlabeled form
break <label>; // the labeled form
50
51
public class BreakExample {
public static void main(String[] args) {
System.out.println("Numbers 1 - 10");
for (int i = 1;; ++i) {
if (i == 11)
break;
// Rest of loop body skipped when i is even
System.out.println(i + "t");
} }}
Output
Numbers 1 – 10
1
2
3
4
5
6
7
8
9
10
QUESTIONS???
THANK YOU FOR YOUR ATTENTION
52

More Related Content

PPTX
Templates and Exception Handling in C++
PDF
Programming in Java: Getting Started
PPT
Preprocessors
PPT
Ppt chapter07
PPT
Pptchapter04
PPS
Interface
PPTX
Preprocessor
PPT
Savitch Ch 04
Templates and Exception Handling in C++
Programming in Java: Getting Started
Preprocessors
Ppt chapter07
Pptchapter04
Interface
Preprocessor
Savitch Ch 04

What's hot (19)

PPT
Basics1
PPT
Java interface
PDF
Chapter 13.1.11
PPTX
Interface java
PDF
Module 05 Preprocessor and Macros in C
PPT
Preprocessors
PDF
Programming by imitation
DOC
Basic construction of c
PDF
Ooabap notes with_programs
PPT
Java interfaces
PPTX
Chapter 2.1
PPTX
Interfaces in JAVA !! why??
PDF
SULTHAN's - C Programming Language notes
PPTX
Chapter 2.4
PDF
Preprocessor directives
PDF
Java Programming Assignment
PPT
Big Java Chapter 1
PDF
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
PPTX
Preprocessor directives in c language
Basics1
Java interface
Chapter 13.1.11
Interface java
Module 05 Preprocessor and Macros in C
Preprocessors
Programming by imitation
Basic construction of c
Ooabap notes with_programs
Java interfaces
Chapter 2.1
Interfaces in JAVA !! why??
SULTHAN's - C Programming Language notes
Chapter 2.4
Preprocessor directives
Java Programming Assignment
Big Java Chapter 1
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Preprocessor directives in c language
Ad

Similar to lecture 6 (20)

PPT
Basics of java 1
PPT
a basic java programming and data type.ppt
DOCX
Unit of competency
PDF
Java lab1 manual
PPT
JAVA_BASICS_Data_abstraction_encapsulation.ppt
PDF
java intro.pptx.pdf
PPT
Introduction
PPT
01slide
PPT
01slide
PDF
Java lab-manual
PPT
Java for Mainframers
DOCX
Cis 355 i lab 1 of 6
DOCX
Cis 355 ilab 1 of 6
PPTX
objectorientedprogrammingCHAPTER 2 (OOP).pptx
PDF
(Ebook pdf) java programming language basics
PPT
Jacarashed-1746968053-300050282-Java.ppt
DOCX
Cis 355 i lab 1 of 6
PDF
Java programming language basics
PPT
Java Concepts with object oriented programming
Basics of java 1
a basic java programming and data type.ppt
Unit of competency
Java lab1 manual
JAVA_BASICS_Data_abstraction_encapsulation.ppt
java intro.pptx.pdf
Introduction
01slide
01slide
Java lab-manual
Java for Mainframers
Cis 355 i lab 1 of 6
Cis 355 ilab 1 of 6
objectorientedprogrammingCHAPTER 2 (OOP).pptx
(Ebook pdf) java programming language basics
Jacarashed-1746968053-300050282-Java.ppt
Cis 355 i lab 1 of 6
Java programming language basics
Java Concepts with object oriented programming
Ad

More from umardanjumamaiwada (20)

PPTX
Handover delay improvement reduction penang.pptx
PPTX
Dynamic-User-Equipment-Tracking-in-5G-Wireless-Systems.pptx
PPTX
Seminar Information Protection & Computer Security (Cryptography).pptx
PPTX
Oop using JAVA
PPTX
Computer hardware
DOC
2javascript web programming with JAVA script
DOC
1 web programming
PPTX
0 csc 3311 slide internet programming
PPTX
0 lecture 6 wp wireless protocol
PPTX
0 lecture 5 wp wireless protocol
PPTX
0 lecture 4 wp wireless protocol
PPTX
0 lecture 3 wp wireless protocol
PPTX
0 lecture 2 wp wireless protocol
PPTX
0 lecture 1 wp wireless protocol
PPTX
lecture 5
PPTX
lecture 4
PPTX
lecture 3
PPTX
lecture 2
PPTX
lecture 1
Handover delay improvement reduction penang.pptx
Dynamic-User-Equipment-Tracking-in-5G-Wireless-Systems.pptx
Seminar Information Protection & Computer Security (Cryptography).pptx
Oop using JAVA
Computer hardware
2javascript web programming with JAVA script
1 web programming
0 csc 3311 slide internet programming
0 lecture 6 wp wireless protocol
0 lecture 5 wp wireless protocol
0 lecture 4 wp wireless protocol
0 lecture 3 wp wireless protocol
0 lecture 2 wp wireless protocol
0 lecture 1 wp wireless protocol
lecture 5
lecture 4
lecture 3
lecture 2
lecture 1

Recently uploaded (20)

PPTX
BODY FLUIDS AND CIRCULATION class 11 .pptx
PDF
Packaging materials of fruits and vegetables
PPT
veterinary parasitology ````````````.ppt
PPT
1. INTRODUCTION TO EPIDEMIOLOGY.pptx for community medicine
PDF
Assessment of environmental effects of quarrying in Kitengela subcountyof Kaj...
PDF
Science Form five needed shit SCIENEce so
PDF
CHAPTER 3 Cell Structures and Their Functions Lecture Outline.pdf
PDF
BET Eukaryotic signal Transduction BET Eukaryotic signal Transduction.pdf
PPTX
PMR- PPT.pptx for students and doctors tt
PPTX
GREEN FIELDS SCHOOL PPT ON HOLIDAY HOMEWORK
PDF
Is Earendel a Star Cluster?: Metal-poor Globular Cluster Progenitors at z ∼ 6
PPT
Presentation of a Romanian Institutee 2.
PPTX
Presentation1 INTRODUCTION TO ENZYMES.pptx
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PDF
S2 SOIL BY TR. OKION.pdf based on the new lower secondary curriculum
PDF
Looking into the jet cone of the neutrino-associated very high-energy blazar ...
PPT
Heredity-grade-9 Heredity-grade-9. Heredity-grade-9.
PPT
THE CELL THEORY AND ITS FUNDAMENTALS AND USE
PPT
LEC Synthetic Biology and its application.ppt
PPTX
Introcution to Microbes Burton's Biology for the Health
BODY FLUIDS AND CIRCULATION class 11 .pptx
Packaging materials of fruits and vegetables
veterinary parasitology ````````````.ppt
1. INTRODUCTION TO EPIDEMIOLOGY.pptx for community medicine
Assessment of environmental effects of quarrying in Kitengela subcountyof Kaj...
Science Form five needed shit SCIENEce so
CHAPTER 3 Cell Structures and Their Functions Lecture Outline.pdf
BET Eukaryotic signal Transduction BET Eukaryotic signal Transduction.pdf
PMR- PPT.pptx for students and doctors tt
GREEN FIELDS SCHOOL PPT ON HOLIDAY HOMEWORK
Is Earendel a Star Cluster?: Metal-poor Globular Cluster Progenitors at z ∼ 6
Presentation of a Romanian Institutee 2.
Presentation1 INTRODUCTION TO ENZYMES.pptx
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
S2 SOIL BY TR. OKION.pdf based on the new lower secondary curriculum
Looking into the jet cone of the neutrino-associated very high-energy blazar ...
Heredity-grade-9 Heredity-grade-9. Heredity-grade-9.
THE CELL THEORY AND ITS FUNDAMENTALS AND USE
LEC Synthetic Biology and its application.ppt
Introcution to Microbes Burton's Biology for the Health

lecture 6

  • 1. INTRODUCTION TOCOMPUTER SCIENCE CSC 1302 LECTURE 6 Department of Maths and Computer- Science Faculty of Natural and Applied Science BY UMAR DANJUMA MAIWADA
  • 2. OBJECTIVES  Programing tools  Computer programming  Programming language levels  Introduction to JAVA language  Syntax of JAVA 2
  • 3. PSEUDOCODE  Pseudocode is an abbreviated version of actual computer code (hence, pseudocode). The geometric symbols used in flowcharts are replaced by English-like statements that outline the process.  Pseudocode allows the programmer to focus on the steps required to solve a problem rather than on how to use the computer language.  Pseudocode has several advantages. It is compact and probably will not extend for many pages as flowcharts commonly do. 3
  • 4. PROGRAMMING TOOLS  A programming tool may be any software program or utility that aids software developers or programmers in creating, editing, debugging, maintaining and/or performing any programming or development-specific task.  Programming tools were initially designed to support or complement programming languages by providing the functionality and features these languages did not have.  Compilers, linkers, assemblers, disassemblers, load testers, performance analysts, GUI development tools and code editors are also all forms of programming tools. 4
  • 5. COMPUTER PROGRAMMING  Programming is all about designing and coding algorithm for solving problems.  Computers are not very intelligent, the instruction they receive must be extraordinary special.  We write computer instructions in programming languages, which are more constrained and exact than human languages are.  It must follow a series of algorithm.  As a programmer your job is to decompose a task into individual, ordered steps of input, calculating, comparing, rearranging and outputting.  The order in which the individual statements, instructions or function calls of an imperative or a declarative program are executed or evaluated by the computer is 5
  • 6. THERE ARE FOUR DIFFERENT WAYS THAT THE FLOW OF CONTROL CAN PROGRESS THROUGH A PROGRAM.  Sequential execution  Method call  Selection  Looping 6
  • 7. PROGRAMMING LANGUAGE LEVELS 1. Machine languages 2. Assembly languages 3. High-level languages  The following section of an assembly-language program also adds overtime pay to base pay and stores the result in gross pay, but somewhat more clearly than its machine-language equivalent.  LOAD BASEPAY  ADD OVERPAY  STORE GROSSPAY 7
  • 8. Machine Instruction Machine Operation 00000000 Stop Program 00000001 Turn bulb fully on 00000010 Turn bulb fully off 00000100 Dim bulb by 10% 00001000 Brighten bulb by 10% 00010000 If bulb is fully on, skip over next instruction 00100000 If bulb is fully off, skip over next instruction 01000000 Go to start of program (address 0)
  • 9. INTRODUCTION TO JAVA LANGUAGE  Java was developed by a team lead by James gosling at microsystem. Originally called Oak.  It was designed in 1991 for used in embedded consumer appliance.  In 1995, renamed Java. It was designed for developing internet applications  Java is a full-featured, general purpose programming language that is capable of developing robust mission-critical applications. Today, it is used not only for web programming, but also for developing standalone applications across platforms on server, desktops, and mobile devices. 9
  • 10. BENEFITS OF USING JAVA  Syntax identical to that of C++, except that java eliminates some of the C++’s more complex features  Object orientation  Internet-related features, such as applets, which are run by the browser, and servlets, which are run by the web server.  An extensive library of classes that can be reused readily, including swing classes for providing graphical user interface and java database connectivity (JDBC) for communication with a database.  Portability among every platform that supports a java virtual machine  Built-in networking  Open source availability of the java development kits 10
  • 11. SYNTAX OF JAVA PROGRAM  /*  This is a simple syntax of Java program.  Call this file classname.java.  */  class classname {  // A Java program begins with a call to main().  public static void main(String args[]) {  statement1;  statement2;  …  …  Statementn;  }  } 11
  • 12. THREE STEPS  Enter the program.  Compile the program.  Run the program 12
  • 13. EXAMPLE 1 1. Public class Hello 2. { 3. /** 4. * My first program in java 5. */ 6. public static void main(String [] args) { 7. // prints the string “Hello World” on screen 8. System.out.println(“Hello World”); 9. } 10. } 13
  • 14. ANALYZING MY FIRST PROGRAM IN JAVA 1. Public class Hello 2. { 3. /** 4. * My first program in java 5. */ • Indicates the name of the class which is Hello • In java all codes should be placed inside a class declaration • The class uses an access specifier public, which indicates that our class is accessible to other class in other package (collection of class). Package and access specifier will be 14
  • 15. 1. PUBLIC CLASS HELLO 2. { 3. /** 4. * MY FIRST PROGRAM IN JAVA 5. */ • THE SECOND LINE CONTAINS A CURLY BRACE { WHICH INDICATE THE START OF A BLOCK. • IN THIS CODE WE PLACED THE CURLY BRACE AT THE NEXT LINE AFTER THE CLASS DECLARATION, HOWEVER, WE CAN ALSO PLACED THIS NEXT TO THE FIRST LINE OF OUR CODE, SO WE COULD ACTUALLY WRITE OUR CODE AS PUBLIC CLASS HELLO{ 15
  • 16. 1. PUBLIC CLASS HELLO 2. { 3. /** 4. * MY FIRST PROGRAM IN JAVA 5. */ • THE NEXT THREE LINES INDICATES A JAVA COMMENT • A COMMENT - SOMETHING USED TO DOCUMENT A PART OF A CODE - IT IS NOT PART OF THE PROGRAM ITSELF, BUT ONLY FOR DOCUMENTATION PURPOSE - IT IS A GOOD PROGRAMING PRACTICE TO ADD A COMMENT TO YOUR CODE 16
  • 17. 1. PUBLIC CLASS HELLO 2. { 3. /** 4. * MY FIRST PROGRAM IN JAVA 5. */ 6. PUBLIC STATIC VOID MAIN(STRING [] ARGS) { • INDICATES THE NAME OF THE METHOD IN HELLO CLASS WHICH IS THE MAIN METHOD • ALL PROGRAMS EXCEPT APPLET (TO BE DISCUSS LATER IN THIS COURSE) WRITTEN IN JAVA STARTS WITH THE MAIN METHOD • BE CAREFUL TO FOLLOW THE SYNTAX 17
  • 18. 1. PUBLIC CLASS HELLO 2. { 3. /** 4. * MY FIRST PROGRAM IN JAVA 5. */ 6. PUBLIC STATIC VOID MAIN(STRING [] ARGS) { 7. // PRINTS THE STRING “HELLO WORLD” ON SCREEN • THIS IS ALSO A COMMENT IN JAVA 18
  • 19. 1. PUBLIC CLASS HELLO 2. { 3. /** 4. * MY FIRST PROGRAM IN JAVA 5. */ 6. PUBLIC STATIC VOID MAIN(STRING [] ARGS) { 7. // PRINTS THE STRING “HELLO WORLD” ON SCREEN 8. SYSTEM.OUT.PRINTLN(“HELLO WORLD”); • THIS COMMAND SYSTEM.OUT.PRINTLN(), PRINTS THE TEXT ENCLOSED BY QUOTATION ON THE SCREEN 19
  • 20. 1. PUBLIC CLASS HELLO 2. { 3. /** 4. * MY FIRST PROGRAM IN JAVA 5. */ 6. PUBLIC STATIC VOID MAIN(STRING [] ARGS) { 7. // PRINTS THE STRING “HELLO WORLD” ON SCREEN 8. SYSTEM.OUT.PRINTLN(“HELLO WORLD”); 9. } 10. } • THE LAST TWO LINES CONTAINING THE CURLY BRACES ARE USED TO CLOSE THE MAIN METHOD AND THE CLASS RESPECTIVELY 20
  • 21. THE MAIN METHOD main method can be written in any of the following forms: public static void main(String [] args) or public static void main(String args [])  the public keyword is an access specifier.  The keyword static allow main() to be called without having to instantiate a particular instance of the class + This is necessary since main() is called by the java interpreter before any object is made  void means main() does not return any value.  String args[] declares a parameter named args, which is an array of instance of the class String. + args receive any command line arguments presents when the program executed 21
  • 22. JAVA KEYWORDS  They are predefined identifiers reserved by java for a specific purpose.  They can not be used as a names of variables, classes, methods, etc. 22
  • 23. JAVA KEYWORDS  double, int, super, abstract  Boolean, switch, else, interface  Break, extends, long, synchronized  Case, finally, null, throw  Byte, false, native, this  Catch, float, package, transient  Byvalue, final, threadsafe, new  For, private, true, class  If, public, void, char  Class, protected, goto, try  While, continue, implement, break  Else, case, string, return  Static, short, import, default 23
  • 24. PRIMITIVE DATA TYPE  • The Java programming language defines eight primitive data types.  – boolean (for logical)  – char (for textual)  – byte  – short  – int  – long (integral)  – double  – float (floating point). 24
  • 25. VARIABLE  A variable is a named memory location that can be assigned a value.  As you learned earlier, variables are declared using this form of statement, type var-name;  A variable is an item of data used to store the state of objects.  A variable has a: – data type  The data type indicates the type of value that the variable can hold. – name  The variable name must follow rules for identifiers. Declaration and Initializing variable  Declare a variable as follows: 25
  • 26. HOW TO INITIALIZE VARIABLE  . One way to give a variable a value is through an assignment statement, as you have already seen.  To do this, follow the variable’s name with an equal sign and the value being assigned.  The general form of initialization is shown here: type varname = value;  int count = 10; // give count an initial value of 10  char ch = 'X'; // initialize ch with the letter X  float f = 1.2F; // f is initialized with 1.2  When declaring two or more variables of the same type using a comma-separated list, you can give one or more of those variables an initial value. For example:  int a, b = 8, c = 19, d; // b and c have initializations  In this case, only b and c are initialized.  Int a = 3, b = 8, c = 19; //a, b, c all have initializations  In this case a, b, c are initialized. 26
  • 27. DECLARING AND INITIALIZING VARIABLES SAMPLE PROGRAM 1. public class VariableSamples { 2. public static void main( String[] args ){ 3. //declare a data type with variable name 4. // result and boolean data type 5. boolean result; 6. 7. //declare a data type with variable name 8. // option and char data type 9. char option; 10. option = 'C'; //assign 'C' to option 11. 12. //declare a data type with variable name 13. //grade, double data type and initialized 14. //to 0.0 15. double grade = 0.0; 16. } 17. } 27
  • 28. EXAMPLE 2 28  The following program creates two variables called x and y.  /*  This demonstrates a variable.  Call this file Example2.java.  */  class Example2 {  public static void main(String args[]) {  int x; // this declares a variable  int y; // this declares another variable  x = 1024; // this assigns 1024 to x  System.out.println("x contains " + x);  y = x / 2;  System.out.print("y contains x / 2: ");  System.out.print(y);  }  }  When you run this program, you will see the following output:  x contains 1024  y contains x / 2: 512
  • 29. DECLARING AND INITIALIZING VARIABLES: CODING GUIDELINES 1. It always good to initialize your variables as you declare them. 2. Use descriptive names for your variables. Like for example, if you want to have a variable that contains a grade for a student, name it as, grade and not just some random letters you choose. 3. Declare one variable per line of code. For example, the variable declarations, double exam=0; double quiz=10; double grade = 0; is preferred over the declaration, double exam=0, quiz=10, grade=0; 29
  • 30. OUTPUTTING VARIABLE DATA • In order to output the value of a certain variable, we can use the following commands: System.out.println() System.out.print() Sample program 1. public class OutputVariable { 2. public static void main( String[] args ){ 3. int value = 10; 4. char x; 5. x = 6. 7. System.out.println( value ); 8. System.out.println( 9. } 10. } The program will output the following text on screen: 10 The value of x=A 30
  • 31. OPERATORS • Different types of operators: – arithmetic operators – relational operators – logical operators – conditional operators • These operators follow a certain kind of precedence so that the compiler will know which operator to evaluate first in case multiple operators are used in one statement. 31
  • 32. ARITHMETIC OPERATORS operator Meaning + Addition - Subtraction * Multiplication / Division % Modulus ++ Increment -- decrement 32
  • 33. EXAMPLE 3 33 The following program demonstrates the modulus operator. // Demonstrate the % operator. class ModDemo { public static void main(String args[]) { int iresult, irem; double dresult, drem; iresult = 10 / 3; irem = 10 % 3; dresult = 10.0 / 3.0; drem = 10.0 % 3.0; System.out.println("Result and remainder of 10 / 3 = " + iresult + " " + irem); System.out.println("Result and remainder of 10.0 / 3.0 = " + dresult + " " + drem); } } The output from the program is shown here are: Result and remainder of 10 / 3 = 3 1 Result and remainder of 10.0 / 3.0 = 3.3333333333333335 1.0 As you can see, the% yields a remainder of 1 for both integer and floating-point operations.
  • 34. RELATIONAL OPERATORS • Relational operators compare two values and determines the relationship between those values. • The output of evaluation are the boolean values true or false. 34
  • 35. CONTROL STATEMENTS  Java Control statements control the order of execution in a java program, based on data values and conditional logic.  There are three main categories of control flow statements; · Selection statements: if, if-else and switch. · Loop statements: while, do-while and for. · Transfer statements: break, continue, return, try- catch-finally and assert. 35
  • 36. SELECTION STATEMENTS  The If Statement  The if statement executes a block of code only if the specified expression is true. If the value is false, then the if block is skipped and execution continues with the rest of the program. You can either have a single statement or a block of code within an if statement.  if (<conditional expression>) <statement action> 36
  • 37. 37 public class IfStatementDemo { public static void main(String[] args) { int a = 10, b = 20; if (a > b) System.out.println("a > b"); if (a < b) System.out.println("b > a"); } } The program will print b > a
  • 38. THE IF-ELSE STATEMENT  The if/else statement is an extension of the if statement. If the statements in the if statement fails, the statements in the else block are executed. You can either have a single statement or a block of code within if-else blocks.  The if-else statement has the following syntax: if (<conditional expression>) <statement action> else <statement action> 38
  • 39. 39 public class IfElseStatementDemo { public static void main(String[] args) { int a = 10, b = 20; if (a > b) { System.out.println("a > b"); } else { System.out.println("b > a"); } } } Output displays b > a
  • 40. SWITCH CASE STATEMENT  The switch case statement, also called a case statement is a multi-way branch with several choices. A switch is easier to implement than a series of if/else statements. The switch statement begins with a keyword, followed by an expression that equates to a no long integral value. Following the controlling expression is a code block that contains zero or more labelled cases. Each label must equate to an integer constant and each must be unique. When the switch statement executes, it compares the value of the controlling expression to the values of each case label.  switch (<non-long integral expression>) { case label1: <statement1> case label2: <statement2> … case labeln: <statementn> default: <statement> } // end switch 40
  • 41. 41 public class SwitchCaseStatementDemo { public static void main(String[] args) { int a = 10, b = 20, c = 30; int status = -1; if (a > b && a > c) { status = 1; } else if (b > c) { status = 2; } else { status = 3; } switch (status) { case 1: System.out.println("a is the greatest"); break; case 2: System.out.println("b is the greatest"); break; case 3: System.out.println("c is the greatest"); break; default: System.out.println("Cannot be determined"); } } } Output displayed is: c is the greatest
  • 42. ITERATION STATEMENTS While Statement  The while statement is a looping construct control statement that executes a block of code while a condition is true. You can either have a single statement or a block of code within the while loop. The loop will never be executed if the testing expression evaluates to false.  The syntax of the while loop is while (<loop condition>) <statements> 42
  • 43. 43 public class WhileLoopDemo { public static void main(String[] args) { int count = 1; System.out.println("Printing Numbers from 1 to 10"); while (count <= 10) { System.out.println(count++); } }} Output Printing Numbers from 1 to 10 1 2 3 4 5 6 7 8 9 10
  • 44. DO-WHILE LOOP STATEMENT  The do-while loop is similar to the while loop, except that the test is performed at the end of the loop instead of at the beginning. This ensures that the loop will be executed at least once. A do-while loop begins with the keyword do, followed by the statements that make up the body of the loop.  The syntax of the do-while loop is do <loop body> while (<loop condition>); 44
  • 45. 45 public class DoWhileLoopDemo { public static void main(String[] args) { int count = 1; System.out.println("Printing Numbers from 1 to 10"); do { System.out.println(count++); } while (count <= 10); }} Output Printing Numbers from 1 to 10 1 2 3 4 5 6 7 8 9 10
  • 46. FOR LOOPS  The for loop is a looping construct which can execute a set of instructions a specified number of times. It’s a counter controlled loop.  The syntax of the loop is as follows: for (<initialization>; <loop condition>; <increment expression>) <loop body> 46
  • 47. 47 public class ForLoopDemo { public static void main(String[] args) { System.out.println("Printing Numbers from 1 to 10"); for (int count = 1; count <= 10; count++) { System.out.println(count); } }} Output Printing Numbers from 1 to 10 1 2 3 4 5 6 7 8 9 10
  • 48. TRANSFER STATEMENTS Continue Statement  A continue statement stops the iteration of a loop (while, do or for) and causes execution to resume at the topof the nearest enclosing loop. You use a continue statement when you do not want to execute the remaining statements in the loop, but you do not want to exit the loop itself.  The syntax of the continue statement is continue; // the unlabeled form continue <label>; // the labeled form 48
  • 49. 49 public class ContinueExample { public static void main(String[] args) { System.out.println("Odd Numbers"); for (int i = 1; i <= 10; ++i) { if (i % 2 == 0) continue; // Rest of loop body skipped when i is even System.out.println(i + "t"); } }} Output Odd Numbers 1 3 5 7 9
  • 50. BREAK STATEMENT  The break statement transfers control out of the enclosing loop ( for, while, do or switch statement). You use a break statement when you want to jump immediately to the statement following the enclosing control structure. You can also provide a loop with a label, and then use the label in your break statement.  The Syntax for break statement is as shown below; break; // the unlabeled form break <label>; // the labeled form 50
  • 51. 51 public class BreakExample { public static void main(String[] args) { System.out.println("Numbers 1 - 10"); for (int i = 1;; ++i) { if (i == 11) break; // Rest of loop body skipped when i is even System.out.println(i + "t"); } }} Output Numbers 1 – 10 1 2 3 4 5 6 7 8 9 10
  • 52. QUESTIONS??? THANK YOU FOR YOUR ATTENTION 52

Editor's Notes

  • #6: Scalability of handover framework to handle increased handovers without compromising latency performance Flexibility to support various 4G deployments