22
Fundamentals of OOP IT 2 Instructor: Paul Kisambira

Fundamentals of oop lecture 2

  • Upload
    miiro30

  • View
    1.390

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Fundamentals of oop lecture 2

Fundamentals of OOPIT 2

Instructor: Paul Kisambira

Page 2: Fundamentals of oop lecture 2

Some History

• Developed and maintained by Sun Microsystems– Originally called Oak– Aimed at producing an operating environment for

networked devices and embedded systems– …but has been much more successful

• Design objectives for the language– Simple, object-oriented, – Distributed, multi-threaded, and platform neutral– Robust, secure, scaleable

Page 3: Fundamentals of oop lecture 2

The Virtual Machine

• Java is both compiled and interpreted– Source code is compiled into Java bytecode– Which is then interpreted by the Java Virtual Machine (JVM)– Therefore bytecode is machine code for the JVM

• Java bytecode can run on any JVM, on any platform– …including mobile phones and other hand-held devices

• Networking and distribution are core features– In other languages these are additional APIs– Makes Java very good for building networked applications, server side

components, etc.

Page 4: Fundamentals of oop lecture 2

Features of the JVM• Security

– Java offers very fine control over what an application is allowed to do– E.g. Read/write files, open sockets to remote machines, discover

information about the users environment, etc– Used in Java Applets to create a “sandbox”. Stops a rogue applet

attacking your machine.– Makes Java very safe, an important feature in distributed systems

• Class Loading– Loading of bytecode into the virtual machine for execution– Code can be read from a local disk, over a network, or the Internet– Allows downloading of applications and applets on the fly– …and even ‘mobile code’

Page 5: Fundamentals of oop lecture 2

Versions of Java• Java Language vs Java Platform

– Current version of the language is 1.4.1– Core language plus additional APIs is called the Java 2 platform– Three versions of the Java 2 Platform, targetted at different uses

• Java 2 Micro Edition (J2ME)– Very small Java environment for smart cards, pages, phones, and

set-top boxes– Subset of the standard Java libraries aimed at limited size and

processing power• Java 2 Standard Edition (J2SE)

– The basic platform, which this course will cover• Java 2 Enterprise Edition (J2EE)

– For business applications, web services, mission-critical systems– Transaction processing, databases, distribution, replication

Page 6: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 6

Some Salient Characteristics of Java

• Java is platform independent: the same program can run on any correctly implemented Java system

• Java is object-oriented:– Structured in terms of classes, which group data

with operations on that data– Can construct new classes by extending existing

ones

Page 7: Fundamentals of oop lecture 2

• Java designed as– A core language plus– A rich collection of commonly available packages

• Java can be embedded in Web pages

Page 8: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 8

Java Processing and Execution

• Begin with Java source code in text files: Model.java

• A Java source code compiler produces Java byte code– Outputs one file per class: Model.class– May be standalone or part of an IDE

• A Java Virtual Machine loads and executes class files– May compile them to native code (e.g., x86) internally

Page 9: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 9

Compiling and Executing a Java Program

Page 10: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 10

Classes and Objects• The class is the unit of programming• A Java program is a collection of classes

– Each class definition (usually) in its own .java file– The file name must match the class name

• A class describes objects (instances)– Describes their common characteristics: is a blueprint– Thus all the instances have these same characteristics

• These characteristics are:– Data fields for each object– Methods (operations) that do work on the objects

Page 11: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 11

Grouping Classes: The Java API

• API = Application Programming Interface• Java = small core + extensive collection of packages• A package consists of some related Java classes:

– Swing: a GUI (graphical user interface) package– AWT: Application Window Toolkit (more GUI)– util: utility data structures

• The import statement tells the compiler to make available classes and methods of another package

• A main method indicates where to begin executing a class (if it is designed to be run as a program)

Page 12: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 12

A Little Example of import and main

import java.lang.*; // all classes from javax.swingpublic class HelloWorld { /* starts a class*/

public static void main (String[] args) {

// starts a main method System.out.println(“HelloWorld”); }}• public = can be seen from any package• static = not “part of” an object

Page 13: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 13

Processing and Running HelloWorld

• javac HelloWorld.java– Produces HelloWorld.class (byte code)

• java HelloWorld– Starts the JVM and runs the main method

Page 14: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 14

Primitive Data TypesData type Range of values

byte -128 .. 127 (8 bits)

short -32,768 .. 32,767 (16 bits)

int -2,147,483,648 .. 2,147,483,647 (32 bits)

long -9,223,372,036,854,775,808 .. ... (64 bits)

float +/-10-38 to +/-10+38 and 0, about 6 digits precision

double +/-10-308 to +/-10+308 and 0, about 15 digits precision

char Unicode characters (generally 16 bits per char)

boolean True or false

Page 15: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 15

Defining Your Own Classes

• The modifier private limits access to just this class

• Only class members with public visibility can be accessed outside of the class* (* but see protected)

• Constructors initialize the data fields of an instance

Page 16: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 16

The Person Class// we have omitted javadoc to save spacepublic class Person {private String givenName;private String familyName;private String IDNumber;private int birthYear;

private static final int VOTE_AGE = 18; private static final int SENIOR_AGE = 65;...

Page 17: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 17

The Person Class (2)// constructors: fill in new objectspublic Person(String first, String family,

String ID, int birth) {this.givenName = first;this.familyName = family;this.IDNumber = ID;this.birthYear = birth;

}public Person (String ID) {this.IDNumber = ID;

}

Page 18: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 18

The Person Class (3)

// modifier and accessor for givenName

public void setGivenName (String given) {this.givenName = given;

}

public String getGivenName () {return this.givenName;

}

Page 19: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 19

The Person Class (4)

// more interesting methods ...public int age (int inYear) {return inYear – birthYear;

}public boolean canVote (int inYear) {int theAge = age(inYear);return theAge >= VOTE_AGE;

}

Page 20: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 20

The Person Class (5)

// “printing” a Personpublic String toString () {return “Given name: “ + givenName + “\n”

+ “Family name: “ + familyName + “\n”

+ “ID number: “ + IDNumber + “\n”+ “Year of birth: “ + birthYear +

“\n”;}

Page 21: Fundamentals of oop lecture 2

Appendix A: Introduction to Java 21

The Person Class (6)

// same Person?

public boolean equals (Person per) {

return (per == null) ? false :

this.IDNumber.equals(per.IDNumber);

}

Page 22: Fundamentals of oop lecture 2

End for now Be sure to try out different versions with

different scenariosNOTE:Programming is a skill, you practice you will pass

this course and you will be good at it