Java(ch3)

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

2 Δεκ 2013 (πριν από 3 χρόνια και 7 μήνες)

90 εμφανίσεις

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
1


Why Java?


It is object
-
oriented


provides many ready to use classes


platform independent


modern features

GUI

exceptions

multiple threads

networking


suitable for multiple courses

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
2


State (3.2.1)


Instance Variables

avoid public

generally use private


Class Variables

values associated with the entire class

static int count


Symbolic Constants

final int SIZE = 100;

make them static?

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
3


Behavior (3.2.2)


method signatures


overloaded methods have unique signature
but identical name


Instance Methods

invoked by an object

generally public


Class Methods

invoked using class name

public static int getCount( )

answer = Math.sqrt(16.0);

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
4


Main methods


the first method called by the Java Runtime
System


specific signature

public static void main(String args[])


Command Line Arguments

System.out.println(args[I])


Sample execution

java program arg1 arg2


Sample result

arg1

arg2

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
5


Identity (3.2.3)


Objects are instantiated and associated
with a reference


Memory is allocated


Constructor is invoked


Reference is returned


Scenarios

multiple references

no references

garbage collection


Parameters

passed by value

copies are made

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
6


Inheritance (3.3)


most state and behavior is available to the
subclass


not inherited

private members

constructors


overloading vs. overriding


keyword final

prevents a class from being extended

prevents a method from being overridden

prevents a variable from being changed

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
7


abstract (3.3.2)


abstract methods must be overridden and
contain no implementation


abstract classes contain one or more
methods and cannot be instantiated


interfaces

allows specification form of inheritance

all methods are abstract

no instance variables

constants are OK

a class implements an interface

can implement multiple interfaces

similar to an abstract class?

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
8


Polymorphism (3.3.4)


Polymorphic references are resolved at
run
-
time, not during compilation

called “late binding”


References can point to classes and
subclasses

Mammal m = new Dog( );


Example with mammals

Dog, Pig, Cat, Tiger

speak method

story: working for toy company


Sample Code

Mammals ark[SIZE];

for (index=0; index<ark.length; index++)

ark[index].speak( );

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
9


Class Object 3.3.5


Every class is a subclass of Object


Several methods are provided


It is useful to override them

String toString( )

boolean equals(Object)

Object clone( )

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
10


Compiling 3.4


Source code is converted to machine code
for some languages such as C


Instead, Java is interpreted


Source code (java files) is compiled into
bytecode (class files)


JVM runs the program on multiple
platforms

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
11


Exceptions (10.1
-
10.3)


handles rare and unexpected events

we must
detect

and
recover

some must be handled (checked)

others are optional (unchecked)


Each exception has a name

Arithmetic Exception

Index Out of Bounds

Null Pointer

File Not Found

Illegal Argument

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
12


Throwing Exceptions


A simple way to handle error messages


Sample Code

if (value < 0)

throw new IllegalArgumentException (“value must
be positive”);


method terminates


It is often more appropriate to ‘catch’ the
exception and handle it appropriately

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
13


Catching Exceptions


Sample Code

try{

// possibility of exception

x = x / y;

} catch (ArithmeticException e){

System.out.println (“error: “ + e.toString());

e.printStackTrace();

}


multiple catch clauses can be provided but
only one will be executed


Flow of control continues after last catch
clause

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
14


What can go wrong?


European Space Agency


Ariane rocket in 1996


embedded software written in Ada


exception not caught


rocket self destructed

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
15


Object Serialization (not in book)


Java provides support to save and load
objects from external files


Information is stored in binary


For each class you must:

import java.io.*;

implement Serializable


No additional methods must be written!

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
16


Saving to a file


This sample code is often embedded
in a try/catch block


Sample Code

SomeObject obj;

FileOutputStream outFile = new FileOutputStream
(“filename”);

ObjectOutputStream objectOut = new
ObjectOutputStream (outFile);

objectOut.writeObject (obj);

outFile.flush();

outFile.close();

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
17


Loading from a file


This sample code is often embedded in a
try/catch block


Sample Code

SomeObject obj;

FileInputStream inFile = new FileInStream (“filename”);

ObjectInputStream objectIn = new ObjectInputStream (inFile);

obj = (SomeObject) objectIn.readObject ();

inFile.close();

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
18


Javadoc Documentation


generates API documentation in HTML


parses ‘special’ comments within source
code

/**

*/


javadoc *.java
-
author


pulldown menu in BlueJ does this
automatically

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
19


Doc comments


Should precede

class declarations

variable declarations

method headers


The first sentence should be a concise
summary optionally followed by additional
information


Followed by zero or more special tags

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
20


Special Tags


@author
text


@version

text


@param
parameter
-
name description


@return description

Scott Grissom, copyright 2004

Ch 3: Java Features Slide
21


Sample

Refer to our online Style Guide

/**

Calculate the sqare root.

@author Scott Grissom

@param value to find square root

@return square root of value

*/

double squareRoot (double value){