Java Programming Robert Chatley William Lee

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Written by: Dr. JJ Shepherd
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Lab#1 (14/3/1431h) Introduction To java programming cs425
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Principles of Object-Oriented Software Development The language Java.
Outline Java program structure Basic program elements
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Introduction to Java.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Developing User Interfaces (DUI) Chris North cs3724: HCI.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
Java and C++, The Difference An introduction Unit - 00.
CSCI 273: Processing An Introduction. Programming Languages –An abstract "human understandable" language for telling the computer what to do –The abstract.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java Spring PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Lecture :2 1.  DEFENTION : Java is a programming language expressly designed for use in the distributed environment of the Internet. It was designed.
POS 406 Java Technology And Beginning Java Code
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
CSCI Processing CSCI Introduction to Algorithm Design An Introduction.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 11: Handling Errors; File Input/Output.
Chapter 2: Java Fundamentals
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
CS Software Studio Java Lab 1 Meng-Ting Wang PLLAB, Computer Science Department, National Tsing-Hua University.
Introduction IS Outline  Goals of the course  Course organization  Java command line  Object-oriented programming  File I/O.
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
introductory lecture on java programming
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Java Spring PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
JAVA MULTIPLE CHOICE QUESTION.
Internet and Java Foundations, Programming and Practice
Interface.
Java Programming Language
Introduction to Java Originally developed by James Gosling at Sun Microsystems In 1991 named “OAK” In 1995, this language was renamed as “Java”. (which.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Introduction to Algorithm Design
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Presentation transcript:

Java Programming Robert Chatley William Lee

Previous Experience

Course Outline Day 1 - Introduction Java VM and Memory Model Java Object-Orientation Key language features Day 2 - The standard library Collections and ADTs Using containers to store data Generics (new Java 1.5 features)

Course Outline 11 – 12 LEC 12 – 1 LEC 1 – 2 2 – 3 LAB 3 – 4 LAB 4 – 5 LEC

Materials

Compiling C++ Programs #include main( int argc, char *argv[]) { // do something }

The Java Virtual Machine class Hello { public static void main(String[] args) { System.out.println( “Hello World” ); } Hello.java … Method Hello() 0 aload_0 1 invokespecial #1 4 return Method void main(java.lang.String[]) 0 getstatic #2 3 ldc #3 5 invokevirtual #4 8 return Hello.class javac

The Java Virtual Machine class Hello { public static void main() { // do something } Mac JVMLinux JVMWin JVM Hello.class

Platform Independence C++ compiles to native code for a specific architecture (Linux, Windows…) Java compiles to Java bytecode Same bytecode runs on virtual machine for any platform Only VM is platform specific Good for downloadable code Applets etc

Memory Management (C++) int main( int argc, char *[] argv ) { Dog d(“rover”); Cat c(“fluffy”); d.bark(); Dog *dp = new Dog(“sirius”); dp->bark(); delete dp; } C++ has two ways of creating objects On the stack (“automatic”) On the heap (with new ) Need to delete

Memory Management (C++) int main( int argc, char *[] argv ) { for( int i=0; i bark(); } }

Memory Management (Java) public static void main( String[] args ) { Dog d = new Dog(“rover”); Cat c = new Cat(“fluffy”); d.bark(); Dog dp = new Dog(“sirius”); dp.bark(); } All Java objects are created on the heap No stars or arrows

Memory Management (Java) public static void main( String[] args ){ for( int i=0; i<100 ; i++ ) { Dog dp = new Dog(“sirius”); dp.bark(); } } Java’s heap is garbage collected

Memory Management (Java) public static void main( String[] args ){ for( int i=0; i<100 ; i++ ) { Dog dp = new Dog(“sirius”); dp.bark(); } }

A First Program class Hello { public static void main( String[] args ) { System.out.println(“Our first Java program”); } } All programs must have at least one class At least one class must have a main method with this signature A standard library function to print text on the console

Making it Run Demo Text Editor and Command Line Tools

Classpath The Classpath tells Java where to look for classes Can be an environment variable or flag to java(c) setenv CLASSPATH /homes/rbc/classes java A java –classpath /homes/rbc/classes A Often include current dir (.) but can cause problems/confusion

More Classes Programs comprise sets of classes Classes can have Fields Methods Constructors

A Dog Class class Dog { String name; int age; Dog ( String p_name ) { name = p_name; } void bark() { System.out.println(“Woof! Woof!”); } } Field declarations A constructor – NB no destructor as Java has garbage collection Methods are defined inside the class

animals Some other Animals Dog Abstract class Group classes in a package CatFishBird Animal

animals Some other Animals Animal DogCatFishBird package animals; abstract class Animal { String name; int age; public abstract void makeNoise(); } abstract class abstract method package statement

animals Some other Animals Animal DogCatFishBird package animals; class Dog extends Animal { Dog ( String p_name ) { name = p_name; } public void makeNoise() { bark(); } void bark() { System.out.println(“Woof! Woof!”); } } implementing abstract method inheritance – Dog extends Animal

Using our Animals import animals.*; class PetShop { public static void main( String[] args ) { Animal pet = new Dog(“rover”); pet.makeNoise(); pet = new Bird(); pet.makeNoise(); } } Import types defined in animals package Creating an object with the new keyword

Access Modifiers package animals; public class Dog extends Animal { public Dog ( String p_name ) { name = p_name; } public void makeNoise() { bark(); } private void bark() { System.out.println(“Woof! Woof!”); } } Classes and methods accessed from outside package must be public Methods called only in this class should be private

Making it Run Demo

Java has Single Inheritance An Elephant is an AnimalA RingMaster is a Human What if we want them both to be Performers?

Interfaces ElephantRingMaster HumanAnimalPerformer Inheritance - RingMaster extends Human Interface implementation

Performer Interface public interface Performer { public void perform(); } Interfaces define sets of methods Interfaces define named types No body for the method given

Implementing the Interface public class Elephant extends Animal implements Performer { public void makeNoise() { trumpet(); } public void perform() { walk(); trumpet(); } … } Implement method from Performer Declare class as implementing interface

Using Performers import animals.*; class Circus { public static void main( String[] args ) { Performer p = new RingMaster(); p.perform(); p = new Elephant(); p.perform(); } } Anything that implements Performer may be assigned to p Can only call methods from Performer interface on p

Exceptions Sometimes a method may fail Exceptional behaviour Catch and handle this special case Methods can throw an Exception to signal that something is wrong

Documentation for FileReader FileReader public FileReader(File file) throws FileNotFoundExceptionFileFileNotFoundException Creates a new FileReader, given the File to read from. Parameters: file - the File to read from Throws: FileNotFoundException FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.

Try and Catch class Test { … public void readFile() { File myFile = new File( “data.txt” ); try { FileReader fr = new FileReader( myFile ); } catch ( FileNotFoundException fnfe ) { System.out.println( “File not found.” ); } } }

Things to do in Catch class Test { … } catch ( FileNotFoundException fnfe ) { System.err.println( “File not found.” ); System.err.println( fnfe.getMessage() ); fnfe.printStackTrace(); } } }

Alternatively, Throw class Test { … public void readFile() throws FileNotFoundException { File myFile = new File( filename ); FileReader fr = new FileReader( myFile ); } }

Throwing your own exceptions class Test { … public void unlockDoor() throws AlreadyOpenException { if ( open ) { throw new AlreadyOpenException(); } } class AlreadyOpenException extends Exception {}

…and Finally public void readFile( filename ) { File myFile = new File( filename ); FileReader fr = new FileReader( myFile ); try { int i = fr.read(); } catch ( IOException ioe ) { System.err.println( “Error reading file” ); } finally { try { if ( fr != null ) fr.close(); } catch ( IOException ioe ) { System.err.println( “Error closing stream” ); } } }

Today’s lab : 2-4pm Rm 219 JeffreyMarkus Helpers Writing a program using fundamental Java concepts Using the tools from the Java Development Kit