Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
27/03/2017
1
KTH ROYAL INSTITUTEOF TECHNOLOGY
EH2745 – Lecture 5
Access, Exceptions & I/O
The lecture slides are somewhat based on the MIT OpenCourseware course 6.092 – Introduction to Java
27/03/2017
2
Outline
Access control & Class scopeExceptionsI/O
”Protecting” data and methods
When designing your program, you may want tto definecertain rules for how variables and methods should be accessed.
Like avoiding accessing an Array outside of its index.
For this matter, methods and variables can be marked as
private and public
27/03/2017
3
An example for consideration
Access control done properly
Will not compileVariable ”not visible”
27/03/2017
4
The right way to do it
Why Access control?
Keeping the interface of the Class separatefrom the implementation
Protecting the data of the class
Making sure others use your class the right way.
27/03/2017
5
Scope of variables – method scope
Scope of variable – Class scope
27/03/2017
6
The this keyword
this keyword refers to the object you areworking with
Clarifies the scope of the variable you arereferencing.
Updating the total ampsDisconnected
27/03/2017
7
Updating the object variable
Outline
Access control & Class scopeExceptionsI/O
27/03/2017
8
What to do when something goes wrong?
Like when accessing an Array outside of bounds?Or when dividing by zero?
The default solution is that Java stops executionand prints an error messahge to the console
Not very nice….....
What if we could catch this error, and try to fix the problem?
Throwing and catching Exceptions
An exception is an unexpected event during run-time.
For instance (again) accessing an array out of the index
This is the result of Java creating an Exception object and filling it with data.
27/03/2017
9
Warning Java about Exceptions
throws tells Java that get may throw the ArrayOutOfBoundsExceptionthrow actually throws the Exception
OK, but who should deal with it?
The code that calls the method (get in this case) needs to implement code that catches the exception.
try to run some code that may throw an exception
Tell Java what to do if it sees the exception catch
27/03/2017
10
So, in the code that calls get you need
If you do not want to fix the problem (catch the exception) just rethrow it!
A more complete example
27/03/2017
11
Outline
Access control & Class scopeExceptionsI/O
A computer (slightly simplified)
The CPU exectues machine codeinstructions stored in memory
The memory is used both to store the program and the data needed
Communication with the real world through I/O units (keyboard, monitor, speaker, harddrive, ethernet,…)
27/03/2017
12
Output example
Input – the full picture
27/03/2017
13
InputStream
InputStreamReader
27/03/2017
14
BufferedReader
Input – the full picture
System.in is the defaultinput (often keyboard)
27/03/2017
15
So what about input from the keyboard
Input from a file
27/03/2017
16
Code example for reading a file
That was again quite a lot
Make sure to get started programming!!!