User Input, Exceptions, and Reading and Writing Text Files

[Pages:39]User Input, Exceptions, and Reading and Writing Text Files

15-121 Fall 2020 Margaret Reid-Miller

Today

? Homework3: is available ? due Monday September 21th at 11:55pm

? From last time: Encapsulation ? Reading User Input ? Handling exceptions ? Reading a text file ? Writing to a text file

Fall 2020

15-121 (Reid-Miller)

2

Writing classes

What 3 things may appear inside a class?

fields constructors methods What should I determine first?

the fields Think: "What does my object need to remember?"

How do you declare a field?

_p_r_iv_a_t_e_ ____ _____; type name

Fall 2020

15-121 (Reid-Miller)

3

Inside a constructor or non-static method, what variables does it have access to?

1. local variables declared inside the method 2. parameters 3. fields

In a static method, we cannot access __fie_l_d_s__.

(or anything that isn't static without creating an object first)

What are Java's visibility modifiers?

Fall 2020

15-121 (Reid-Miller)

4

Classes (and their parts) have visibility modifiers:

? public: accessible to everyone ? protected: inside package, inside class,

inside subclass ? package-private (default, no modifier used):

inside package, inside class ? private: accessible only within the class

Fall 2020

15-121 (Reid-Miller)

5

Data fields are usually private

Data (fields): ? Whatever the object needs to store ? Available to clients via "getter" and "setter"

methods. ? Are private to encapsulate the object data

from clients and to ensure class invariants.

Fall 2020

15-121 (Reid-Miller)

6

Methods are usually are either public or private.

public: Only those methods that are part of the interface (the methods the client can call).

? They can't assume the client will supply correct arguments.

? These methods must ensure that the pre- and postconditions are met and post-conditions maintain the class invariants.

private: "helper" methods inside the class

? Since clients cannot call these methods, and the server can ensure its calls meet the pre-conditions, these methods need not check the pre-conditions.

Fall 2020

15-121 (Reid-Miller)

7

Text Input

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download