Chapter 3 Java
Chapter 3 Java
Interfaces
A semicolon immediately
follows each method header
Interfaces
An interface cannot be instantiated
Methods in an interface have public visibility by
default
A class formally implements an interface by:
stating so in the class header
providing implementations for each abstract method in the
interface
// etc.
}
Interfaces
A class that implements an interface can
implement other methods as well
In addition to (or instead of) abstract
methods, an interface can contain
constants
When a class implements an interface, it
gains access to all its constants
Packages are containers for classes that are used to keep the
class name space compartmentalized.
For example, a package allows you to create a class named
List, which you can store in your own package without
concern that it will collide with some other class named List
stored elsewhere.
Packages are stored in a hierarchical manner and are
explicitly imported into new class definitions.
Through the use of the interface keyword, Java allows you to
fully abstract the interface from its implementation.
Using interface, you can specify a set of methods that can be
implemented by one or more classes.
The interface, itself, does not actually define any
implementation.
A class can implement more than one interface. By contrast, a
class can only inherit a single superclass (abstract or
otherwise).
Java provides a mechanism for partitioning the class name
space into more manageable chunks. This mechanism is the
package.
The package is both a naming and a visibility control
mechanism.
You can define classes inside a package that are not
accessible by code outside that package.
You can also define class members that are only exposed to
other members of the same package.
Defining a Package
To create a package is quite easy: simply include a package command as the
first statement in a Java source file.
Any classes declared with in that file will be long to the specified package.
The package statement defines a name space in which classes are stored.
If you omit the package statement, the class names are put into the default
package, which has no name.
package pkg;
Here, pkg is the name of the package. For example, package MyPackage;
Socket Programming
What is a socket?
Using sockets
Types (Protocols)
Associated functions
Styles
What is a socket?
3
Connect
Two essential types of sockets
• DATAGRAM
– a.k.a. UDP
Send to recipient
Indeterminate
path
Process
27
Scanner syntax
The Scanner class is found in the java.util package.
import java.util.Scanner;
Example:
Scanner console = new Scanner(System.in);
28
Scanner methods
Method Description
nextInt() reads an int from the user and returns it
nextDouble() reads a double from the user
nextLine() reads a one-line String from the user
next() reads a one-word String from the user
Avoid when Scanner connected to System.in
29 underlined):
Console (user input
How old are you?
36 years until retirement!
30
Scanner example 2
The Scanner can read multiple values from one line.
import java.util.Scanner;
public class ScannerMultiply {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
31
Clicker 1 - Input tokens
token: A unit of user input, as read by the Scanner.
Tokens are separated by whitespace (spaces, tabs, new
lines).
How many tokens appear on the following line of input?
23 John Smith 42.0 "Hello world" $2.50 " 19"
A. 2 B. 6 C. 7 D. 8 E. 9
32
input tokens
When a token is the wrong type, the
program crashes. (runtime error)
System.out.print("What is your age? ");
int age = console.nextInt();
Output:
What is your age? Timmy
java.util.InputMismatchException
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
...
33
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
// takes input from the keyboard
String name = input.nextLine(); // prints the name
System.out.println("My name is " + name); // closes the scanner
input.close();
}
}
Output
Enter your name: Kelvin My name is Kelvin
The System.in parameter is used to take input from the standard input. It works just
We have then used the nextLine() method of the Scanner class to read a line of text