2-Introduction To Java - Version 1.0 - 2016-12-01
2-Introduction To Java - Version 1.0 - 2016-12-01
Contents
• Introduction to Java
• Object-Oriented Programming in Java
• Specification and Testing
1
1/16/2017
Contents
• Introduction to Java
– Hello World!
– The type system
– Quick ‘n’ dirty I/O
– Collections
– Methods common to all Objects
• Object-Oriented Programming in Java
• Specification and Testing
Contents
• Introduction to Java
– Hello World!
– The type system
– Quick ‘n’ dirty I/O
– Collections
– Methods common to all Objects
• Object-Oriented Programming in Java
• Specification and Testing
2
1/16/2017
3
1/16/2017
4
1/16/2017
5
1/16/2017
Contents
• Introduction to Java
– Hello World!
– The type system
– Quick ‘n’ dirty I/O
– Collections
– Methods common to all Objects
• Object-Oriented Programming in Java
• Specification and Testing
6
1/16/2017
7
1/16/2017
Implementation inheritance
• A class:
– Inherits visible fields and methods from its
superclasses
– Can override methods to change their behavior
• Overriding method implementation must obey
contract(s) of its superclass(es)
– Ensures subclass can be used anywhere
superclass can
– Liskov Substitution Principle (LSP)
8
1/16/2017
9
1/16/2017
Interface types
Enum types
10
1/16/2017
Enum types
package Myjavaenum;
Boxed primitives
11
1/16/2017
12
1/16/2017
Contents
• Introduction to Java
– Hello World!
– The type system
– Quick ‘n’ dirty I/O
– Collections
– Methods common to all Objects
• Object-Oriented Programming in Java
• Specification and Testing
Output
• Unformatted
System.out.println("Hello World");
System.out.println("Radius: " + r);
System.out.println(r * Math.cos(theta));
System.out.println();
System.out.print("*");
• Formatted
System.out.printf("%d * %d = %d%n", a, b, a * b);
13
1/16/2017
class Echo {
public static void main(String[] args) {
for (String arg : args) {
System.out.print(arg + " ");
}
}
}
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various
class InputParse{
public static void main(String[] args) {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
….
}
}
14
1/16/2017
Scanner input
Scanner input
15
1/16/2017
Contents
• Introduction to Java
– Hello World!
– The type system
– Quick ‘n’ dirty I/O
– Collections
– Methods common to all Objects
• Object-Oriented Programming in Java
• Specification and Testing
A Queue is a collection
Lists may contain for holding elements
duplicate prior to processing
A Set is elements
a Collection that
cannot contain A deque is a double-ended-queue, it is a
duplicate elements. linear collection of elements that supports
the insertion and removal of elements at
both end points
16
1/16/2017
17
1/16/2017
Set example
18
1/16/2017
Map Example
Contents
• Introduction to Java
– Hello World!
– The type system
– Quick ‘n’ dirty I/O
– Collections
– Methods common to all Objects
• Object-Oriented Programming in Java
• Specification and Testing
19
1/16/2017
Object implementations
20
1/16/2017
Overriding toString
21
1/16/2017
Overriding equals
Overriding hashCode
22
1/16/2017
Contents
• Introduction to Java
– Hello World!
– The type system
– Quick ‘n’ dirty I/O
– Collections
– Methods common to all Objects
• Object-Oriented Programming in Java
• Specification and Testing
23
1/16/2017
24