16
27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 – Lecture 5 Access, Exceptions & I/O The lecture slides are somewhat based on the MIT OpenCourseware course 6.092 – Introduction to Java

Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

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

Page 2: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

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

Page 3: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

27/03/2017

3

An example for consideration

Access control done properly

Will not compileVariable ”not visible”

Page 4: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

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.

Page 5: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

27/03/2017

5

Scope of variables – method scope

Scope of variable – Class scope

Page 6: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

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

Page 7: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

27/03/2017

7

Updating the object variable

Outline

Access control & Class scopeExceptionsI/O

Page 8: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

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.

Page 9: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

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

Page 10: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

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

Page 11: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

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,…)

Page 12: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

27/03/2017

12

Output example

Input – the full picture

Page 13: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

27/03/2017

13

InputStream

InputStreamReader

Page 14: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

27/03/2017

14

BufferedReader

Input – the full picture

System.in is the defaultinput (often keyboard)

Page 15: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

27/03/2017

15

So what about input from the keyboard

Input from a file

Page 16: Lecture 5 HT17 - KTH · 2017. 3. 27. · 27/03/2017 1 KTH ROYAL INSTITUTE OF TECHNOLOGY EH2745 –Lecture5 Access, Exceptions& I/O The lectureslidesaresomewhatbasedon the MIT OpenCoursewarecourse6.092

27/03/2017

16

Code example for reading a file

That was again quite a lot

Make sure to get started programming!!!