Chapter 1 Lecture 2

processroguishΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

92 εμφανίσεις

1

Chapter 1

Lecture 2

Topics Covered


Programming Paradigms


Object Oriented Programming



Class and Object



Abstraction



Three Object Oriented Principles


Java Language and Programs


Developing a Java Application


Setup SDK






2

Review of Last Class


What are the different steps of code
execution?


What are the different types of programming
languages? Which are CPU dependent and
which not?


What kind of language does Java use? What
are its advantages?


What is the difference between an interpreter
and a compiler?

3

Programming Paradigms

Two paradigms of programming approach:


“What is happening?”



process oriented approach i.e. code acting on


data .


As program grows larger and more complex, this
approach fails.


“Who is being affected?”


object oriented approach

4

Programming Paradigms


Procedural Language


a programmer specifies a set of instructions to perform a specific task in
the form of procedures or functions
.


C, Fortran etc.


Data centric


Become unmanageable when logic becomes complex


No reuse functionality


Object Oriented Paradigm


Object oriented approach organizes a program around its data (object)
and a set of well defined interfaces to its data.



object oriented paradigm came into consideration to reuse structures
and functions and perform complex task with the help of
abstraction
.


C++, Java


5

Object
-
oriented Programming
(OOP)


Class


tool for encapsulating data and operations
(methods)
into one package


defines a template or model for creating and
manipulating
objects


Objects


data created using the class and its methods


an object is an
instance

of the class


creating an object is
instantiation


6

OOP Advantage: Reuse


Well
-
written classes can be reused in new
applications


Shortens development time because
programmers don't need to write new code


Programs are more robust because the
class code is already tested

7

Basic OOP Principles


Encapsulation


mechanism that binds together code and the data it manipulates
and keeps both safe from the outside world.


Inheritance



process by which one object acquires the property of another
object.


Polymorphism



a feature that allows one interface to be used for a general class
of action.
The specific action is determined by the situation.



8

The Java Language


Created by Sun Microsystems in 1995


Syntax based on C++


Object
-
oriented


Support for Internet applications


Extensive library of prewritten classes


Portability among platforms


Built
-
in networking

9

Java Programs


Applets


Small programs designed to add interactivity
to Web sites


Downloaded with the Web page and launched
by the Internet browser


Servlets


Run by Web server on the server


Typically generate Web content


Applications


Programs that run standalone on a client

10

An Introduction to Programming


Programming Basics


Program Design with Pseudocode


Developing a Java Application

11

Programming Basics


Programming is translating a problem into
ordered steps consisting of operations a
computer can perform:


Input


Calculations


Comparisons of values


Moving data


Output



The order of execution of instructions is
called
flow of control

12

Program Design with
Pseudocode


Pronounced
sue
-
dough
-
code


English
-
like language for specifying the
design of a program


Programmer can concentrate on design of
program without worrying about Java
language rules (
syntax
)


Then convert pseudocode into Java code

13

Four Types of Flow of Control


Sequential Processing


Execute instructions in order


Method Call


Jump to code in method, then return


Selection


Choose code to execute based on data value


Looping or Iteration


Repeat operations for multiple data values

14

Sequential Processing


The pseudocode for calculating the sum of
two numbers would look like this:




read first number


read second number


set total to (first number +


second number)


output total


15

Method Call


Calling the method

executes the method


Methods can take
arguments

(data to
use) and return values


Here is pseudocode for calculating the
square root of an integer:



read an integer


call the square root method,


with integer as argument


output the square root


16

Selection


The pseudocode for determining if a
number is positive or negative is:



read a number


if the number is greater than


or equal to 0



write "Number is positive."


else



write "Number is negative."

17

Looping


The pseudocode for finding the sum of a
set of numbers is:



set total to 0


read a number


while there was a number to


read,



add number to total



read the next number


write total

18

Developing a Java Application

1.
Write the source code


Using an Integrated Development Environment
(IDE)

or text editor


Save in a
.java

file

2.
Compile the source code:


javac ClassName.java


Creates
.class

file

3.
Execute the application:


java ClassName


Run by the Java Virtual Machine

19

A First Application

1 // First program in Java

2 // FirstProgram.java

3

4 public class FirstProgram

5 {

6


public static void main( String [] args )

7 {

8 System.out.println( "Programming is not "

9 + " a spectator sport!" );

10 System.exit( 0 );

11


}

12 }


20


Java is case
-
sensitive. The class name
and the source filename must match
exactly, including capitalization.

21

Program Errors


Compiler errors


Found by the compiler.


Usually caused by incorrect syntax or spelling


Run
-
time errors


Reported by the JVM


Usually caused by incorrect use of prewritten
classes or invalid data


Logic errors


Found by testing the program


Incorrect program design or incorrect execution of
the design

22

Integrated Development Environment


Download & Install Java JDK and NetBeans


http://java.sun.com/j2se/1.5.0/download.jsp

click

23

click

24

select

then click

get
jdk
-
1_5_0_07
-
nb
-
5_0
-
win
-
ml.exe

25


doubleclick
jdk
-
1_5_0_07
-
nb
-
5_0
-
win
-
ml.exe

follow the instruction of the wizard

26

NetBean

27


File
-
>New Project


select

28

Set the
location

Set the file
name

29

Hello.java

1

public class Hello

2

{

3


public static void main(String[] args)

4


{

5


// display a greeting and exit

6



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





System.exit(0)
;

7


}

8

}

// public class
ClassName (same as filename)

// begin of the class

// main method

// case sensitive in Java

// begin of main method

// comments

// method call

// end of main method

// end of the class

// another method call,

// every statement must end with a semicolon