Upload
charity-stanton
View
13
Download
0
Tags:
Embed Size (px)
DESCRIPTION
S E 3 60 Advances in Software Development. Asst . Prof.Dr . Senem Kumova Metin. Introduction. Instructor : Dr. Senem Kumova Metin Email: senem. kumova @ ieu.edu.tr Room: 3 12 Course website: http://homes.ieu.edu.tr/ skumova You have to c heck website regularly for - PowerPoint PPT Presentation
Citation preview
Introduction
Instructor : Dr. Senem Kumova MetinEmail: [email protected]: 312Course website: http://homes.ieu.edu.tr/skumova
You have to check website regularly for announcements, homeworks, course slides and projects
Assignments Labs and Project
• Starting next lecture, there will be 1 hour lab time. Bring your laptops!!!
• In the second part you will do a course project, in Java.
– Start thinking about your project now!!!– I leave the topic up to you.– Up to 2 people per group.
Classification of High level Programming languages
• Programming paradigm : Alternative approaches to the programming process
– Imperative (Procedural) (Fortran, Algol, Pascal, Basic, C)
– Functional (Lisp, ML)– Declarative(Logic) (Prolog,GPSS)– Object-Oriented (C++, Java, Scala, Smalltalk)
Programming Languages 1/2
• Imperative: The language provides statements, such as assignment statements , which explicitly change the state of the memory of the computer. X:=X+1
• Functional: In this paradigm we express computations as the evaluation of mathematical functions.
(defun factorial (n) (if (<= n 1)
1 (* n (factorial (- n 1)))))
The program can then be called as(factorial 10)
Programming Languages 2/2• Logic (Declarative): In this paradigm we express computation in
exclusively in terms of mathematical logic
brother(X,Y) /* X is the brother of Y */ :- /* if there are two people F and M for which*/ father(F,X), /* F is the father of X */ father(F,Y), /* and F is the father of Y */ mother(M,X), /* and M is the mother of X */ mother(M,Y), /* and M is the mother of Y */ male(X). /* and X is male */
uncle (X,Y)
:- father(F,Y), brother(X,F).
• Object-Oriented: In this paradigm we associate behavior with data-structures called " objects " which belong to classes which are usually structured into a hierarchy
What exactly is an object?
• An OBJECT is an identity which includes both Attributes and Behaviors
EXAMPLE -- > PERSON OBJECTAttributes : Eye Color, Age, Height …Behaviors : Walking, Talking, Breathing
…
The Object Model • A OO-software system is a set of cooperating
objects • Objects have state and processing ability • Objects exchange messages
Class and Object• A class is a collection of
objects that have common properties, operations and behaviors.
• A class is a data type, objects are instances of that data type.
Object Oriented Programming• A programming paradigm that uses abstraction (in the form of
classes and objects) to create models based on the real world environment.
• An object-oriented application uses a collection of objects, which
communicate by passing messages to request services.
• Objects are capable of passing messages, receiving messages, and processing data.
• The aim of object-oriented programming is to try to increase the flexibility and maintainability of programs. Because programs created using an OO language are modular, they can be easier to develop, and simpler to understand after development.
Java
• Main topic of the course is Object Oriented Concepts
• The implementation will be done in Java• You will learn Java, and use it in the project• I will use Eclipse as the Java IDE
– Free IDE, highly popular– Easy to use– The resource links are given in course web page
What is Java
• Java is a high level programming language
• Uses a virtual machine– Means your code is compiled for the VM andworks in any architecture or OS (byte code– Performance penalty due to VM instead of nativeapplications
• You can write applications or applets using Java Applet is a Java program that works on web pages.
Java: Write Once, Run Anywhere• Consequence of Java’s history:
platform-independence
Mac user running Safari
Windows user running Internet Explorer
Web page stored on Unix server
Click on link to Applet
Byte code is downloaded
Virtual machine translates byte code to
native Mac code and the Applet is run
Byte code (part of web page)
From slides of James Tam
Java: Write Once, Run Anywhere• Consequence of Java’s history:
platform-independent
Mac user running Safari
Windows user running Internet Explorer
Web page stored on Unix server
Click on link to AppletByte code is downloaded
Virtual machine translates byte code to
native Windows code and the Applet is runFrom slides of James Tam
What is Java
• If you are a C/C++ programmer, Java is easier to code– No pointers– Has a garbage collection mechanism– Built in security
• Java is object oriented
A short history for Java The invention of the microprocessor revolutionized computers
(from huge computers to smaller ones )
The next logical step for microprocessors was to have them run in intelligent consumer devices like cable TV switchboxes..
Sun Microsystems funded an internal research project “Green” to investigate this opportunity (~1991)
– Result: A programming language called “Oak” JAVA
Java versus Java ScriptJava (this is what you need to know for this course)
– A complete programming language developed by Sun– Can be used to develop either web based or stand-alone
software– Many pre-created code libraries available– For more complex and powerful programs
Java Script (not covered in this course)– A small language that’s mostly used for web-based applications
(run through a web browser like Internet Explorer, Firefox, Safari, Chrome)
– Good for programming simple special effects for your web page e.g., roll-overs
– e.g.,
A High Level View Of Translating/Executing Java Programs
Java compiler (javac)
Java program
Filename.java
Java bytecode (generic binary)
Filename.class
Stage 1: Compilation
From slides of James Tam
A High Level View Of Translating/Executing Java Programs
Java interpreter (java)
Java bytecode (generic binary)
Filename.class
Machine language instruction (UNIX)
Machine language instruction (Windows)
Machine language instruction (Apple)
Stage 2: Interpreting and executing the byte code
From slides of James Tam
Java Instructions
• Each Java instruction must be followed by a semi-colon!
int num = 123;System.out.println(“Hello!");
Java OutputSystem.out.print(<string or variable name one> + <string or variable name two>..);
ORSystem.out.println(<string or variable name one> + <string or variable name two>..);
public class OutputExample1{ public static void main (String [] args) { int num = 123; // More on this shortly
System.out.println(“Hello!"); // prints to new line System.out.print(num); // prints to current line System.out.println("num="+num);}
}
Output : Some Escape Sequences For Formatting
Escape sequence Description
\t Horizontal tab
\r Carriage return
\n New line
\” Double quote
\\ Backslash
Variables• Variables must be declared before they can be
used.• Variable declaration:
– Creates a variable in memory.– Specify the name of the variable as well as the type
of information that it will store.– E.g. int num;
• Using variables– Only after a variable has been declared can it be
used.
Declaring Variables: Syntax
• Format:<type of information> <name of variable>;
• Example:char myFirstInitial;
• Variables can be initialized (set to a starting value) as they’re declared:char myFirstInitial = ‘j’;
int age = 30;
Some Built-In Types Of Variables In JavaType Description
byte 8 bit signed integer
short 16 bit signed integer
int 32 bit signed integer
long 64 bit signed integer
float 32 bit signed real number
double 64 bit signed real number
char 16 bit Unicode character (ASCII and beyond)
boolean 1 bit true or false value
String A sequence of characters between double quotes ("")
Location Of Variable Declarationspublic class <name of class>
{
public static void main (String[] args)
{
// Local variable declarations occur here
<< Program statements >>
: :
}
}
Java Constants
Reminder: constants are like variables in that they have a name and store a certain type of information but unlike variables they CANNOT change.
Format: final <constant type> <CONSTANT NAME> = <value>;
Example: final int SIZE = 100;
Location Of Constant Declarationspublic class <name of class>{ public static void main (String[] args) {
// Local constant declarations occur here (more later)// Local variable declarations
< Program statements >> : :
} }
Java Keywords
abstract boolean break byte case catch char
class const continue default do double else
extends final finally float for goto if
implements import instanceof int interface long native
new package private protected public return short
static super switch synchronized this throw throws
transient try void volatile while
Common Java Operators / Operator Precedence
Precedence level
Operator Description Associativity
1 expression++
expression--
Post-increment
Post-decrement
Right to left
2 ++expression
--expression
+
-
!
~
(type)
Pre-increment
Pre-decrement
Unary plus
Unary minus
Logical negation
Bitwise complement
Cast
Right to left
Common Java Operators / Operator Precedence
Precedence level
Operator Description Associativity
3 *
/
%
Multiplication
Division
Remainder/modulus
Left to right
4 +
-
Addition or String concatenation
Subtraction
Left to right
5 <<
>>
Left bitwise shift
Right bitwise shift
Left to right
Common Java Operators / Operator Precedence
Precedence level
Operator Description Associativity
6 <
<=
>
>=
Less than
Less than, equal to
Greater than
Greater than, equal to
Left to right
7 = =
!=
Equal to
Not equal to
Left to right
8 & Bitwise AND Left to right
9 ^ Bitwise exclusive OR Left to right
Common Java Operators / Operator Precedence
Precedence level
Operator Description Associativity
10 | Bitwise OR Left to right
11 && Logical AND Left to right
12 || Logical OR Left to right
Common Java Operators / Operator Precedence
Precedence level
Operator Description Associativity
13 =
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
Assignment
Add, assignment
Subtract, assignment
Multiply, assignment
Division, assignment
Remainder, assignment
Bitwise AND, assignment
Bitwise XOR, assignment
Bitwise OR, assignment
Left shift, assignment
Right shift, assignment
Right to left
Post/Pre Operators
public class Order1{ public static void main (String [] args) { int num = 5; System.out.println(num); // 5 num++; System.out.println(num); // 6 ++num; System.out.println(num); //7 System.out.println(++num); //8 System.out.println(num++); //8 }}
Accessing Pre-Created Java Libraries
• It’s accomplished by placing an ‘import’ of the appropriate library at the top of your program.
• Syntax:import <Full library name>;
• Example:import java.util.Scanner;
Getting Text Input
• You can use the pre-written methods (functions) in the Scanner class.
• General structure:import java.util.Scanner;
main (String [] args){ Scanner <name of scanner> = new Scanner (System.in); <variable> = <name of scanner> .<method> (); }
Creating a scanner object (something that can scan user input)
Using the capability of the scanner object (actually getting user input)
Getting Text Input (2)import java.util.Scanner;
public class MyInput{ public static void main (String [] args) { String str1; int num1; Scanner in = new Scanner (System.in); System.out.print ("Type in an integer: "); num1 = in.nextInt (); System.out.print ("Type in a line: "); in.nextLine (); str1 = in.nextLine (); System.out.println ("num1:" +num1 +"\t str1:" + str1); }}
Useful Methods Of Class Scanner• nextInt ()• nextLong ()• nextFloat ()• nextDouble ()• nextLine () • next()
Decision Making: If
Format:if (Boolean Expression) Body
Example:if (x != y) System.out.println("X and Y are not equal");
if ((x > 0) && (y > 0)) { System.out.println("X and Y are positive"); }
• Indenting the body of the branch is an important stylistic requirement of Java
• What distinguishes the body is either:
1.A semi colon (single statement branch)
2.Braces (a body that consists of multiple statements)
Decision Making: If, Else
Format:if (Boolean expression) Body of ifelse
Body of else
Example:if (x < 0) System.out.println("X is negative");else System.out.println("X is non-negative");
Example Program: If-Elseimport java.util.Scanner;
public class BranchingExample1{ public static void main (String [] args) { Scanner in = new Scanner(System.in); final int WINNING_NUMBER = 131313; int playerNumber = -1;
System.out.print("Enter ticket number: "); playerNumber = in.nextInt(); if (playerNumber == WINNING_NUMBER) System.out.println("You're a winner!"); else System.out.println("Try again."); }}
If, Else-If
Format:if (Boolean expression) Body of ifelse if (Boolean expression)
Body of first else-if : : :else if (Boolean expression)
Body of last else-ifelse Body of else
If, Else-If (2)
import java.util.Scanner;
public class BranchingExample2{ public static void main (String [] args) { Scanner in = new Scanner(System.in); int gpa = -1; System.out.print("Enter letter grade: "); gpa = in.nextInt();
If, Else-If (3) if (gpa == 4) System.out.println("A"); else if (gpa == 3) System.out.println("B"); else if (gpa == 2) System.out.println("C"); else if (gpa == 1) System.out.println("D"); else if (gpa == 0) System.out.println("F"); else System.out.println("Invalid letter grade"); }}
Alternative To Multiple Else-If’s: Switch
Format (character-based switch):switch (character variable name){
case '<character value>': Body break;
case '<character value>': Body break;
: default: Body
}
1 The type of variable in the brackets can be a byte, char, short, int or long
Alternative To Multiple Else-If’s: Switch (2)
Format (integer based switch):switch (integer variable name){
case <integer value>: Body break;
case <integer value>: Body break;
: default: Body
}
1 The type of variable in the brackets can be a byte, char, short, int or long
While Loops
Format:while (Boolean expression) Body
Example:int i = 1;while (i <= 10){
// Some statements ; i = i + 1;
}
For Loops
Format:for (initialization; Boolean expression; update control)
Body
Example:for (i = 1; i <= 10; i++)
{
// some statements
i = i + 1;
}
Post-Test Loop: Do-While
• Recall: Post-test loops evaluate the Boolean expression after the body of the loop has executed.
• This means that post test loops will execute one or more times.
• Pre-test loops generally execute zero or more times.
Do-While LoopsFormat:
do
Body
while (Boolean expression);
Example: char ch = 'A';
do
{
System.out.println(ch);
ch++;
}
while (ch <= ‘D');
Output :ABCD
Arrays
• Arrays are a collection of elements of same typeint[] myInt = new int[3];myInt[0]=1;myInt[1]=2;myInt[2]=3;
Arrays (2)
Iterating an array :
int[] myInt = new int[10];for (int i=0; i<10; i++){ myInt[i]=i;
System.out.println(myInt[i]); }