Java Introductory Presentation

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

15 Αυγ 2012 (πριν από 5 χρόνια και 26 μέρες)

313 εμφανίσεις

CSE 1340

Class 3

Class 03 objectives


State the difference between Machine Language vs. High
Level Languages


Discuss some characteristics of the Java language


Write an algorithm


Differentiate between the compilation and execution
processes of a program


Understand the purpose of using a compiler


Gain an elementary understanding of object
-
oriented
programming



Evolution of computer languages


Machine language


Characterized by binary ones and zeroes


Low
-
level assembly languages


Characterized by mnemonic codes


High
-
level languages


English
-
like


Very
-
high level languages


Results instead of procedural oriented


Natural languages


Conversational

What Is Java?


High
-
level language


Object
-
oriented


Data and operations are packaged into
a single unit called an object


Basic syntax derived from C, C++,
and Smalltalk


Designed by a team from Sun
Microsystems led by James Gosling in
the early 1990’s

What Is Java?


Parsimonious


Compatible with older versions


Robust


Strongly typed and incorruptible data


Secure


Protection against misuse of code


Portable


Platform
-
independent

What Is the Java SDK?


The Java Software Development Kit (SDK)
is a programming package to develop
Java applications


The Java Runtime Environment (JRE)
provides the tools to deploy Java
applications



Features of the J2SE


The Java Compiler


Converts code into bytecode


The Java Virtual Machine


Contains an interpreter to execute the
bytecode


The Java API


The standard set of packages available in Java


The Java Applet Viewer


Mini browser to display Java applets

Any Java Compiler that is compatible
with the

Java 2 Platform 6.0

(J2SE 6.0)

Please see your Blackboard
homepage for link to free download
of Sun Microsystems J2SE 6.0


If you use J2SE 6.0, it is a
command line interface. In other
words, there is no User Interface
to work with. Programs have to be
entered into a text editor, saved
and then compiled through
commands at a command prompt.
Instructions for doing so are in a
link on your Blackboard
homepage.



Your textbook also comes with a CD
that includes Textpad that make
coding a little easier. See textbook
for installation.

See also the link on your
Blackboard homepage for
instructions on using the NetBeans
platform as another option.

Editor

Compiler

Class
Loader

Byte Code
Verifier

.

.

.

Interpreter

.

.

.

.

.

.

Editor

Disk

Program1.java

Compiler

Disk

Program1.java

Program1.class

Class

Loader

.

.

.

Primary

Memory

Program1.class

Disk

Program1.class

Program1.html

Java Virtual Machine


JVM

Java Source code

.java

Java byte
-
code

.class

Environment





Java VM

javac

java

Byte Code

Verifier

.

.

.

Primary

Memory

Program1.class

JIT Compiler


JIT
-

takes byte
-
codes and change it
to machine code.



JVM

Running

Applet or Application

J.I.T.

Compiler

.class file

machine code

Interpreter

.

.

.

Primary

Memory

Program1.class

Editor

Compiler

Class
Loader

Byte Code
Verifier

.

.

.

Interpreter

.

.

.

.

.

.

Java Program Types


Console and Windowed applications


Applets


Servlets


Web Services


JavaBeans

Console Applications


Stand
-
alone programs using a
command
-
line interface

Windowed Applications


Stand
-
alone programs using a
graphical user interface (GUI)

Applets


Client
-
side programs executed as
part of a displayed Web page

Servlets


Server
-
side programs hosted and run
on a Web server


Used in conjunction with Java Server
Pages (JSP) to provide sophisticated
server
-
side logic


Enable connections to server
databases through Java Database
Connectivity (JDBC)

Servlets



Web Services


Services receive information
requests over the Web and return the
requested data

JavaBeans


Reusable software components

Sample Problem



A programmer needs an algorithm to
determine an employee’s weekly wages.
How would the calculations be done by
hand?




One Employee’s Wages



In one week an employee works 52 hours
at the hourly pay rate of $24.75. Assume a
40.0 hour normal work week and an
overtime pay rate factor of 1.5


What are the employee’s wages?

40 x $ 24.75 = $ 990.00

12 x 1.5 x $ 24.75

=

$ 445.50

___________

$ 1435.50

If hours are more than 40.0, then



wages = (40.0 * payRate) + (hours
-

40.0) * 1.5
*payRate





otherwise,




wages = hours * payRate


Weekly Wages, in General



RECALL EXAMPLE




( 40 x $ 24.75 ) + (
12 x 1.5 x $ 24.75 ) =

$1435.50

An Algorithm is . . .


a step
-
by
-
step procedure for solving a
problem in a finite amount of time.

Algorithm to Determine an
Employee’s Weekly Wages



1. Display to the screen “how many hours did you
work?”


2. Input hours


3. Display to the screen “what is your rate of pay?”


4. Input rate


5. Calculate this week’s regular wages


6. Calculate this week’s overtime wages (if any)


(Fill in details of how to do steps 5 and 6)



7. Add the regular wages to overtime wages (if any)


to determine total wages for the week




Translating your algorithm into a
programming language is called
CODING


The “code” or “program” for this algorithm
is on the next screen









// A payroll program


public class Payroll{


public static void main(String [] args)


{ double hours, rate, regularPay, overtimePay,grossPay;


Scanner keyboard = new Scanner(System.in);


System.out.print( "How many hours did you work?“) ;


hours = keyboard.nextInt();


System.out.print( "how much were you paid per hour?“);


rate = keyboard,nextInt();


if (hours <= 40)


{ regularPay = hours * rate;


overtimePay = 0.0; }


else


{ regularPay = 40 * rate;


overtimePay = (hours
-

40 ) * (rate * 1.5 ); }


grossPay = regularPay + overtimePay;


System.out.println(“Your gross pay is” + grossPay);


} }

Introduction to Object
-
oriented
Programming and Design

Key concepts in beginning to
understand object
-
oriented
programming


A specific approach to programming


Data and operations on the data are
packaged together into a single unit
called an object.


Models real
-
world problems


OBJECT ORIENTED
PROGRAMMING

What is an object?

A noun

A person

A bank account

A rectangle

A grade book

What is an object’s data members?

Person

A name

An address

A phone number

A social security number

What is an object’s behavior?

Person

Eats

Drinks

Walks

Exercises

In order to eat, does the person
object need anything?

Food

Utensils

Rather than describing every single person,
in object
-
oriented programming, the
programmer describes a general type of an
object and then creates many of the objects
of that same type.

The person class

class Person

{ // data members


name;


address;


phonenumber;


socialno;


hand;


mouth;


// methods (behaviors)


void eat(good food, fork utensils)


{ hand = utensils;


mouth = food; }


Create many objects of type Person:

Person Judy;

Person John;

Person Ann;

Creating a new class from an
existing class

class Instructor extends Person

{ // Instructor gets to use all of Person data members


// and methods


// Instructor’s data members


textbooks;


computer;


// Instructor’s methods


void teach(….. )


{ ……. };

// other stuff }

Test your understanding on an
airline reservation system


What would some of the objects be?


What data would the object contain?


What operations would be performed
on that data?

Conclusion of Class 3