Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…

Similar presentations


Presentation on theme: "Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…"— Presentation transcript:

1 Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…

2 Problems with Homework 1 In some cases, misunderstanding of what to do, pls come to talk to me after class How to convert the input strings for the old and new base to int: –not Integer.parseInt –not Math.pow –can just call str2int –or, special method for converting base since it cannot be more than 2 characters Bugs: easy to check correctness by converting output string back to original base Program was supposed to keep prompting for input until user enters empty string Nice output format is very helpful Identifier names are important The way errors are detected is important: does not have to be complicated, but should not be misleading to user

3 How we can best learn I propose to show the best program that was submitted, we test it, look at the code, comment on its good and bad points Idea is not to put anyone down, but to learn to be a better programmer I also expect to learn things from you during this course… How do you like this idea? Rule about homework: talking to people is good, acknowledging it is even better, but copying (or editing) other students’ solutions is absolutely forbidden (even with their permission)

4 Classes All Java programs are organized into classes Most classes have instance variables and instance methods, allowing us to construct objects of the class Exception: some classes just have static variables and static methods, such as the Math class. You cannot construct an object of the Math class.

5 An Example: the Player class int heads; // data field (instance variable) Player() { // constructor // initialize heads to 0 } void flip() { // instance method // increment heads with probability 1/2 }

6 Constructing 2 Player objects Player jack = new Player(); Or Player jack; jack = new Player(); Player jill = new Player(); while jack.heads < 10 & jill.heads < 10 { jack.flip(); jill.flip(); }

7 Private vs Public Considered good practice to make data fields of an object private Then they can only be accessed or changed by methods of the same class Methods that do this are called accessor and mutator methods (getter and setter) methods respectively Likewise private methods cannot be called outside the class Public data fields and methods can be called from anywhere; those that are neither public nor private can be called anywhere from with the same package

8 Object variables are really references What does the following do: Player jack = New Player(); Player jill = New Player(); jill = jack; while jack.heads < 10 & jill.heads < 10 { jack.flip(); jill.flip(); }

9


Download ppt "Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…"

Similar presentations


Ads by Google