Содержание

Слайд 2

Appendix A: Introduction to Java

Topics of the Review

Essentials of object-oriented programming, in

Appendix A: Introduction to Java Topics of the Review Essentials of object-oriented
Java
Java primitive data types, control structures, and arrays
Using some predefined classes:
Math
JOptionPane, I/O streams
String, StringBuffer, StringBuilder
StringTokenizer
Writing and documenting your own Java classes

Слайд 3

Appendix A: Introduction to Java

Some Salient Characteristics of Java

Java is platform independent:

Appendix A: Introduction to Java Some Salient Characteristics of Java Java is
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
Java designed as
A core language plus
A rich collection of commonly available packages
Java can be embedded in Web pages

Слайд 4

Appendix A: Introduction to Java

Java Processing and Execution

Begin with Java source code

Appendix A: Introduction to Java Java Processing and Execution Begin with Java
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

Слайд 5

Appendix A: Introduction to Java

Compiling and Executing a Java Program

Appendix A: Introduction to Java Compiling and Executing a Java Program

Слайд 6

Appendix A: Introduction to Java

Classes and Objects

The class is the unit of

Appendix A: Introduction to Java Classes and Objects The class is the
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

Слайд 7

Appendix A: Introduction to Java

Grouping Classes: The Java API

API = Application Programming

Appendix A: Introduction to Java Grouping Classes: The Java API API =
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 (important to CS 187!)
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)

Слайд 8

Appendix A: Introduction to Java

A Little Example of import and main

import javax.swing.*;

Appendix A: Introduction to Java A Little Example of import and main
// all classes from javax.swing
public class HelloWorld { // starts a class
public static void main (String[] args) {
// starts a main method
// in: array of String; out: none (void)
}
}
public = can be seen from any package
static = not “part of” an object

Слайд 9

Appendix A: Introduction to Java

Processing and Running HelloWorld

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

Appendix A: Introduction to Java Processing and Running HelloWorld javac HelloWorld.java Produces
HelloWorld
Starts the JVM and runs the main method

Слайд 10

Appendix A: Introduction to Java

References and Primitive Data Types

Java distinguishes two kinds

Appendix A: Introduction to Java References and Primitive Data Types Java distinguishes
of entities
Primitive types
Objects
Primitive-type data is stored in primitive-type variables
Reference variables store the address of an object
No notion of “object (physically) in the stack”
No notion of “object (physically) within an object”

Слайд 11

Appendix A: Introduction to Java

Primitive Data Types

Represent numbers, characters, boolean values
Integers: byte,

Appendix A: Introduction to Java Primitive Data Types Represent numbers, characters, boolean
short, int, and long
Real numbers: float and double
Characters: char

Слайд 12

Appendix A: Introduction to Java

Primitive Data Types

Appendix A: Introduction to Java Primitive Data Types

Слайд 13

Appendix A: Introduction to Java

Primitive Data Types (continued)

Appendix A: Introduction to Java Primitive Data Types (continued)

Слайд 14

Appendix A: Introduction to Java

Operators

subscript [ ], call ( ), member access

Appendix A: Introduction to Java Operators subscript [ ], call ( ),
.
pre/post-increment ++ --, boolean complement !, bitwise complement ~, unary + -, type cast (type), object creation new
* / %
binary + - (+ also concatenates strings)
signed shift << >>, unsigned shift >>>
comparison < <= > >=, class test instanceof
equality comparison == !=
bitwise and &
bitwise or |

Слайд 15

Appendix A: Introduction to Java

Operators

logical (sequential) and &&
logical (sequential) or ||
conditional cond

Appendix A: Introduction to Java Operators logical (sequential) and && logical (sequential)
? true-expr : false-expr
assignment =, compound assignment += -= *= /= <<= >>= >>>= &= |=

Слайд 16

Appendix A: Introduction to Java

Type Compatibility and Conversion

Widening conversion:
In operations on mixed-type

Appendix A: Introduction to Java Type Compatibility and Conversion Widening conversion: In
operands, the numeric type of the smaller range is converted to the numeric type of the larger range
In an assignment, a numeric type of smaller range can be assigned to a numeric type of larger range
byte to short to int to long
int kind to float to double

Слайд 17

Appendix A: Introduction to Java

Declaring and Setting Variables

int square;
square = n *

Appendix A: Introduction to Java Declaring and Setting Variables int square; square
n;
double cube = n * (double)square;
Can generally declare local variables where they are initialized
All variables get a safe initial value anyway (zero/null)

Слайд 18

Appendix A: Introduction to Java

Referencing and Creating Objects

You can declare reference variables
They

Appendix A: Introduction to Java Referencing and Creating Objects You can declare
reference objects of specified types
Two reference variables can reference the same object
The new operator creates an instance of a class
A constructor executes when a new object is created
Example: String greeting = ″hello″;

Слайд 19

Appendix A: Introduction to Java

Java Control Statements

A group of statements executed in

Appendix A: Introduction to Java Java Control Statements A group of statements
order is written
{ stmt1; stmt2; ...; stmtN; }
The statements execute in the order 1, 2, ..., N
Control statements alter this sequential flow of execution

Слайд 20

Appendix A: Introduction to Java

Java Control Statements (continued)

Appendix A: Introduction to Java Java Control Statements (continued)

Слайд 21

Appendix A: Introduction to Java

Java Control Statements (continued)

Appendix A: Introduction to Java Java Control Statements (continued)

Слайд 22

Appendix A: Introduction to Java

Methods

A Java method defines a group of statements

Appendix A: Introduction to Java Methods A Java method defines a group
as performing a particular operation
static indicates a static or class method
A method that is not static is an instance method
All method arguments are call-by-value
Primitive type: value is passed to the method
Method may modify local copy but will not affect caller’s value
Object reference: address of object is passed
Change to reference variable does not affect caller
But operations can affect the object, visible to caller

Слайд 23

Appendix A: Introduction to Java

The Class Math

Appendix A: Introduction to Java The Class Math

Слайд 24

Appendix A: Introduction to Java

Escape Sequences

An escape sequence is a sequence of

Appendix A: Introduction to Java Escape Sequences An escape sequence is a
two characters beginning with the character \
A way to represents special characters/symbols

Слайд 25

Appendix A: Introduction to Java

The String Class

The String class defines a data

Appendix A: Introduction to Java The String Class The String class defines
type that is used to store a sequence of characters
You cannot modify a String object
If you attempt to do so, Java will create a new object that contains the modified character sequence

Слайд 26

Appendix A: Introduction to Java

Comparing Objects

You can’t use the relational or equality

Appendix A: Introduction to Java Comparing Objects You can’t use the relational
operators to compare the values stored in strings (or other objects)
(You will compare the pointers, not the objects!)

Слайд 27

Appendix A: Introduction to Java

The StringBuffer Class

Stores character sequences
Unlike a String object,

Appendix A: Introduction to Java The StringBuffer Class Stores character sequences Unlike
you can change the contents of a StringBuffer object

Слайд 28

Appendix A: Introduction to Java

StringTokenizer Class

We often need to process individual pieces,

Appendix A: Introduction to Java StringTokenizer Class We often need to process
or tokens, of a String

Слайд 29

Appendix A: Introduction to Java

Wrapper Classes for Primitive Types

Sometimes we need to

Appendix A: Introduction to Java Wrapper Classes for Primitive Types Sometimes we
process primitive-type data as objects
Java provides a set of classes called wrapper classes whose objects contain primitive-type values: Float, Double, Integer, Boolean, Character, etc.

Слайд 30

Appendix A: Introduction to Java

Defining Your Own Classes

Unified Modeling Language (UML) is

Appendix A: Introduction to Java Defining Your Own Classes Unified Modeling Language
a standard diagram notation for describing a class

Class name

Field values

Class name

Field signatures: type and name

Method signatures: name, argument types, result type

Слайд 31

Appendix A: Introduction to Java

Defining Your Own Classes (continued)

The modifier private limits

Appendix A: Introduction to Java Defining Your Own Classes (continued) The modifier
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

Слайд 32

Appendix A: Introduction to Java

The Person Class

// we have omitted javadoc to

Appendix A: Introduction to Java The Person Class // we have omitted
save space
public 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;
...

Слайд 33

Appendix A: Introduction to Java

The Person Class (2)

// constructors: fill in new

Appendix A: Introduction to Java The Person Class (2) // constructors: fill
objects
public 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;
}

Слайд 34

Appendix A: Introduction to Java

The Person Class (3)

// modifier and accessor for

Appendix A: Introduction to Java The Person Class (3) // modifier and
givenName
public void setGivenName (String given) {
this.givenName = given;
}
public String getGivenName () {
return this.givenName;
}

Слайд 35

Appendix A: Introduction to Java

The Person Class (4)

// more interesting methods ...
public

Appendix A: Introduction to Java The Person Class (4) // more interesting
int age (int inYear) {
return inYear – birthYear;
}
public boolean canVote (int inYear) {
int theAge = age(inYear);
return theAge >= VOTE_AGE;
}

Слайд 36

Appendix A: Introduction to Java

The Person Class (5)

// “printing” a Person
public String

Appendix A: Introduction to Java The Person Class (5) // “printing” a
toString () {
return “Given name: “ + givenName + “\n”
+ “Family name: “ + familyName + “\n”
+ “ID number: “ + IDNumber + “\n”
+ “Year of birth: “ + birthYear + “\n”;
}

Слайд 37

Appendix A: Introduction to Java

The Person Class (6)

// same Person?
public boolean equals

Appendix A: Introduction to Java The Person Class (6) // same Person?
(Person per) {
return (per == null) ? false :
this.IDNumber.equals(per.IDNumber);
}

Слайд 38

Appendix A: Introduction to Java

Arrays

In Java, an array is also an object
The

Appendix A: Introduction to Java Arrays In Java, an array is also
elements are indexes and are referenced using the form arrayvar[subscript]

Слайд 39

Appendix A: Introduction to Java

Array Example

float grades[] = new float[numStudents];
... grades[student] =

Appendix A: Introduction to Java Array Example float grades[] = new float[numStudents];
something; ...
float total = 0.0;
for (int i = 0; i < grades.length; ++i) {
total += grades[i];
}
System.out.printf(“Average = %6.2f%n”,
total / numStudents);

Слайд 40

Appendix A: Introduction to Java

Array Example Variations

// possibly more efficient
for (int i

Appendix A: Introduction to Java Array Example Variations // possibly more efficient
= grades.length; --i >= 0; ) {
total += grades[i];
}
// uses Java 5.0 “for each” looping
for (float grade : grades) {
total += grade;
}

Слайд 41

Appendix A: Introduction to Java

Input/Output using Class JOptionPane

Java 1.2 and higher provide

Appendix A: Introduction to Java Input/Output using Class JOptionPane Java 1.2 and
class JOptionPane, which facilitates display
Dialog windows for input
Message windows for output

Слайд 42

Appendix A: Introduction to Java

Input/Output using Class JOptionPane (continued)

Appendix A: Introduction to Java Input/Output using Class JOptionPane (continued)

Слайд 43

Appendix A: Introduction to Java

Converting Numeric Strings to Numbers

A dialog window always

Appendix A: Introduction to Java Converting Numeric Strings to Numbers A dialog
returns a reference to a String
Therefore, a conversion is required, using static methods of class String:

Слайд 44

Appendix A: Introduction to Java

Input/Output using Streams

An InputStream is a sequence of

Appendix A: Introduction to Java Input/Output using Streams An InputStream is a
characters representing program input data
An OutputStream is a sequence of characters representing program output
The console keyboard stream is System.in
The console window is associated with System.out

Слайд 45

Appendix A: Introduction to Java

Opening and Using Files: Reading Input

import java.io.*;
public static

Appendix A: Introduction to Java Opening and Using Files: Reading Input import
void main (String[] args) {
// open an input stream (**exceptions!)
BufferedReader rdr =
new BufferedReader(
new FileReader(args[0]));
// read a line of input
String line = rdr.readLine();
// see if at end of file
if (line == null) { ... }

Слайд 46

Appendix A: Introduction to Java

Opening and Using Files: Reading Input (2)

//

Appendix A: Introduction to Java Opening and Using Files: Reading Input (2)
using input with StringTokenizer
StringTokenizer sTok =
new StringTokenizer (line);
while (sTok.hasMoreElements()) {
String token = sTok.nextToken();
...;
}
// when done, always close a stream/reader
rdr.close();

Слайд 47

Appendix A: Introduction to Java

Alternate Ways to Split a String

Use the split

Appendix A: Introduction to Java Alternate Ways to Split a String Use
method of String:
String[] = s.split(“\\s”);
// see class Pattern in java.util.regex
Use a StreamTokenizer (in java.io)

Слайд 48

Appendix A: Introduction to Java

Opening and Using Files: Writing Output

// open a

Appendix A: Introduction to Java Opening and Using Files: Writing Output //
print stream (**exceptions!)
PrintStream ps = new PrintStream(args[0]);
// ways to write output
ps.print(“Hello”); // a string
ps.print(i+3); // an integer
ps.println(“ and goodbye.”); // with NL
ps.printf(“%2d %12d%n”, i, 1<ps.format(“%2d %12d%n”, i, 1<// closing output streams is very important!
ps.close();

Слайд 49

Appendix A: Introduction to Java

Summary of the Review

A Java program is a

Appendix A: Introduction to Java Summary of the Review A Java program
collection of classes
The JVM approach enables a Java program written on one machine to execute on any other machine that has a JVM
Java defines a set of primitive data types that are used to represent numbers, characters, and boolean data
The control structures of Java are similar to those found in other languages
The Java String and StringBuffer classes are used to reference objects that store character strings
Имя файла: Intro.pptx
Количество просмотров: 17
Количество скачиваний: 0