Programming in Java: lecture 5
Transcript of Programming in Java: lecture 5
![Page 1: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/1.jpg)
1
Programming in Java: lecture 5
Return Values APIs, Packages and Javadoc More on Program Design Declarations Something about learning Repetition
Slides made for use with ”Introuction to Programming Using Java, Version 5.0” by David J. EckSome figures are taken from ”Introuction to Programming Using Java, Version 5.0” by David J. EckLecture 3 covers Section 4.4 to 4.7 + some repitition
![Page 2: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/2.jpg)
2
Lecture 4
Exceptions and try...catch Overview – static vs. non static GUI programming – Applets Black Boxes
Subroutines Local and Global variables Parameters – formal and actual Overloading
![Page 3: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/3.jpg)
3
Exceptions
Lecture 3: Normal flow of control Why do we need something different Handle errors somewhere else then where they
happen
Exception – the exception is an Object try...catch statements
![Page 4: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/4.jpg)
4
try...catch
Formal syntax
![Page 5: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/5.jpg)
5
try...catch
Example
![Page 6: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/6.jpg)
6
Bad parameter values
This is an error What do you do? Throw an exception
![Page 7: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/7.jpg)
7
Return Values
Function – subroutines with a return value Can only return one specific type
Can be used as expressions or statements Statement: return value is ignored Test condition: boolean value
![Page 8: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/8.jpg)
8
The return statement
return <expression>; Should give some result of the same type as
the return value of the function Must be inside function Example
![Page 9: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/9.jpg)
9
Function Examples
The 3N+1 Sequence
return type void. return;
![Page 10: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/10.jpg)
10
One return statement
Some people prefer having only one return statement per function
![Page 11: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/11.jpg)
11
Use of Functions
![Page 12: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/12.jpg)
12
Return type can be any type
static boolean isPrime(int N);
static String reverse(String str);
![Page 13: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/13.jpg)
13
APIs, Packages and Javadoc
API – Application Programmers Interface What you need to know from the outside Windows, MacOS, linux (gtk, gnome), Java Math Toolboxes
![Page 14: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/14.jpg)
14
Packages
Too much functionality to expose it all at once
![Page 15: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/15.jpg)
15
Import directives
Technically not a statement java.lang.*; // automatically imported, contains
String
Import java.* does not import everything
GUI program: typical import import java.awt.*; import java.awt.event.*; // still needed import javax.swing.*;
Javax is additions from java 1.2
![Page 16: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/16.jpg)
16
Name conflicts
Two classes in different packages with the same name
java.awt.List java.util.List Only importing specific packages or Using fully qualified names
![Page 17: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/17.jpg)
17
Create your own package
Eclipse warns about using the default package Packages are stored in Java Archives
.jar files
![Page 18: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/18.jpg)
18
Javadoc
Comments used for generating documentation Begins with /**
![Page 19: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/19.jpg)
19
Semantic description
Syntactic information in function name, return type and argument types
Javadoc can contain HTML code doc tags
![Page 20: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/20.jpg)
20
Example
![Page 21: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/21.jpg)
21
More on Program Design
Preconditions and Postcondition
![Page 22: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/22.jpg)
22
Declarations
Initialization in declarations
is the same as
Multiple initializations
![Page 23: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/23.jpg)
23
For loops
Initialization in for loops
is the same as
![Page 24: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/24.jpg)
24
Static member variables
Can be initialized when declared
No statements outside functions
![Page 25: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/25.jpg)
25
Named Constants
Can easily be changed between compiles
final static double interestRate = 0.05;
final static double INTEREST_RATE = 0.05;
Math.PI;
Enumerated type constants
Color.RED
![Page 26: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/26.jpg)
26
Naming and Scope Rules
Scope – Hvad man kan se Member variables are in scope in the Class Hiding outer variable with the same name Game.count to get member variable
![Page 27: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/27.jpg)
27
Only one level of nesting
You can only have one level of nesting of variables with the same name
Ok with multiple on the same level
![Page 28: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/28.jpg)
28
Insanity
static Insanity Insanity( Insanity Insanity) { ... }
Do not do this! Remember the pragmatics
![Page 29: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/29.jpg)
29
Something about learning
Repetition – teaches your brain to remember Programming is an activity not facts Doing is learning You should be able to do the exercises Watching others do the exercises will not teach
you much
![Page 30: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/30.jpg)
30
Repetition
![Page 31: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/31.jpg)
Java Virtual Machine
Why a virtual machine?
![Page 32: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/32.jpg)
Identifiers
Structure Must start with letter or “_” Can contain numbers Examples: _local, x2, variableName
Simple identifiers – local Contains no “.”
Compound identifiers – “global” System.out.println
![Page 33: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/33.jpg)
Reserved Words
abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
![Page 34: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/34.jpg)
Variables
A box that contains data A location in memory
The data inside the box A value
Example: x = x + 2 5
Data
Data
memory locationsize
![Page 35: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/35.jpg)
Types
Java is strongly typed Weakly typed: Hope for the best
Apples and oranges Automatic conversion or Compile error
Types: Primitive Types:
boolean, int, short, ... Classes:
String, ...
![Page 36: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/36.jpg)
Variable Declarations
Reserves space in memory Makes the name (identifier)
usable after this point. <type-name> <variable-name-or-names>
int numberOfStudents; String name; // First, middle and last name double x, y; // represents coordinates boolean isFinished; char firstInitial, middleInitial, lastInitial;
Space for Data
![Page 37: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/37.jpg)
Type Conversion of Strings
Integer.parseInt(“123”) Double.parseDouble(“3.14”) Double.parseDouble(“12.3e-7”) Same as literals Enum
Season.valueOf(“SUMMER”)
![Page 38: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/38.jpg)
TextIO
Class file from textbook with modifications Hides details of getting Input System.out.println(“String”) TextIO.put(“String”)
![Page 39: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/39.jpg)
TextIO – printf – putf
System.out.printf("The product of %d and %d is %d", x, y, x*y);
Variable number of arguments %d – integer (decimal) number
%12d, minimum 12 characters %s – String (converted into string)
%10s, minimum 10 characters
![Page 40: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/40.jpg)
TextIO – printf – putf 2
%f – floating point %12.3f – 12 characters, 3 digits after decimal point
%e – exponential %15.8e – 8 digits after the decimal point
%g – floating point or exponential %12.4g – a total of 4 digits in the answer
“ 5.345”
“ 34.453”
“ 123.875”
![Page 41: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/41.jpg)
TextIO 2
j = TextIO.getlnInt(); // Reads a value of type int.
y = TextIO.getlnDouble(); // Reads a value of type double.
a = TextIO.getlnBoolean(); // Reads a value of type boolean.
c = TextIO.getlnChar(); // Reads a value of type char.
w = TextIO.getlnWord(); // Reads one "word" as a value of type String.
s = TextIO.getln(); // Reads an entire input line as a String.
![Page 42: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/42.jpg)
TextIO – File I/O
TextIO.writeFile("result.txt") TextIO.writeUserSelectedFile() TextIO.writeStandardOutput() TextIO.readFile("data.txt") TextIO.readUserSelectedFile() TextIO.readStandardInput()
![Page 43: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/43.jpg)
43
Loops
While loop
Do while loop
For loop
We only need one of these to have a complete language
We have several for convenience
![Page 44: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/44.jpg)
44
While loops
do {<statements>
} while (<boolean-expression)
while (<boolean-expression) {<statement>
}
Two variants
![Page 45: Programming in Java: lecture 5](https://reader035.fdocuments.net/reader035/viewer/2022072800/62e0b8f07ab9911476291e5a/html5/thumbnails/45.jpg)
45
For loop examples
Simplificationyears = 0; // initialize the variable yearswhile ( years < 5 ) { // condition for continuing loop interest = principal * rate; // principal += interest; // do three statements System.out.println(principal); // years++; // update the value of the variable, years}
for ( years = 0; years < 5; years++ ) { interest = principal * rate; principal += interest; System.out.println(principal);}
Becomes