catie welsh march 2, 2011. program 3 due tonight by 11:59pm lab 5 due friday by 1pm sample...

29
Catie Welsh March 2, 2011

Upload: steven-mckinney

Post on 16-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Catie WelshMarch 2, 2011

Page 2: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Program 3 due tonight by 11:59pm

Lab 5 due Friday by 1pm

Sample Midterm is posted on course website◦ Solutions will be posted after class on Friday

Midterm is Monday, March 14th

2

Page 3: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

3

Page 4: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Objects and references

More on Classes

4

Page 5: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Classes Objects Instance variables Methods

◦ Return types◦ Parameters and arguments

Information hiding and encapsulation◦ public/private◦ accessors/mutators

5

Page 6: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Behave differently from variables of a primitive type

6

Page 7: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

When declaring a variable, a certain amount of memory is assigned based on the declared primitive type

What goes in this memory?

7

int age;

double length;

char letter;

memory

Page 8: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

A data value is stored in the location assigned to a variable of a primitive type

8

Page 9: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

What goes in these variables?

9

Student jack;

String inputString;

memory

Page 10: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Contain the memory address of the object named by the variable◦ NOT the object itself

What is an address? Object is stored in some other location in

memory The address to this other location is called a

reference to the object Class types are also called reference types

10

Page 11: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Assume we have a class named Book

Book jacksBook = new Book(“Java”);Book apusBook = new Book(“Java”);

vs.

Book jacksBook = new Book(“Java”);Book apusBook = jacksBook;

11

Page 12: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

12

jacksBook

apusBook

?

?

Memory Book jacksBook;Book apusBook;

jacksBook = new Book(“Java”);apusBook = new Book(“Java”);

jacksBook.setPage(137);apusBook.setPage(253);

apusBook = jacksBook;apusBook.setPage(509);

jacksBook is now on p. 509!

??

??

??

Java?

Java?

Java?

Java?

Java137

Java253

Java137

Java253

Java509

2078

?

2078

1056

?

?

?

2078

1056

2078

2078

2078

Page 13: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Variables of a class type contain memory addresses◦ NOT objects themselves

13

Page 14: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

String is a class type What happens when you have

String s1 = new String(“Hello”);String s2 = new String(“Hello”);boolean strEqual = (s1 == s2);

strEqual is false! Why? s1 and s2 store different addresses!

14

Page 15: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

What happens when you have

String s1 = new String(“Hello”);String s2 = new String(“Hello”);boolean strEqual = (s1.equals(s2));

strEqual is true! Why? String’s .equals() method checks if all the

characters in the two Strings are the same

15

Page 16: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

public class Book{ private String name; private int page;

public boolean equals(Book book) { return (this.name.equals(book.name) && this.page == book.page); }}

16

Page 17: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Every class has a default .equals() method if it is not explicitly written◦ Does not necessarily do what you want

You decide what it means for two objects of a specific class type to be considered equal◦ Perhaps books are equal if the names and page

numbers are equal◦ Perhaps only if the names are equal◦ Put this logic inside .equals() method

17

Page 18: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

public void increaseNum(int num){ num++;}

public void doStuff(){ int x = 5; increaseNum(x); System.out.println(x);}

Prints 5. Why? num is local to increaseNum method; does not

change x

18

Page 19: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

public void changeBook(Book book){ book = new Book(“Biology”);}

public void doStuff(){ Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName());}

Prints Java. Why? book is local to changeBook, does not change

jacksBook

19

Page 20: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

public void changeBook(Book book){ book.setName(“Biology”);}

public void doStuff(){ Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName());}

Prints Biology. Why? book contains the same address as jacksBook!

20

Page 21: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Write a bank account class for Harry Potter

It needs to have the ability to track◦ # of galleons◦ # of sickles◦ # of knuts◦ 3 transaction types

Deposit Withdrawal Inquiry

21

Page 22: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Would you like to make a transaction? y/nyWould you like to make a deposit (d), withdrawal (w), or inquire (i)?dHow many galleons, sickles, and knuts would you like to deposit?Galleons: 5Sickles: 6Knuts: 4

Would you like to make a transaction? y/nyWould you like to make a deposit (d), withdrawal (w), or inquire (i)?wHow many galleons, sickles, and knuts would you like to withdrawal?Galleons: 4Sickles: 0Knuts: 0Would you like to make a transaction? y/nyWould you like to make a deposit (d), withdrawal (w), or inquire (i)?iYou now have: 1 galleons6 sickles4 knuts

22

Page 23: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

What are instance variables?

Why private?

What are the instance variables for our bank account program? ◦ What do we need to keep track of?

23

Page 24: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

public class Account { private int galleons = 0; private int sickles = 0; private int knuts = 0;

24

Page 25: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

getGalleons: int getSickles: int getKnuts: int

setGalleons(int) setSickles(int) setKnuts(int)

25

Page 26: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

public static void main(String[] args) { Account potter = new Account();

.

.

.

potter.deposit(5, 7, 9);

public void deposit(int g, int s, int k){

galleons+=g; sickles+=s; knuts+=k;

}

26

Page 27: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

public static void main(String[] args)

{ Account potter = new Account();

.

.

.

potter.withdrawal(5, 7, 9);

public void withdrawal(int g, int s, int k){

if((g > galleons) || (s > sickles) || (k > knuts)) { System.out.println("You do not have enough money"); } else { galleons -= g; sickles -= s; knuts -= k; }

}

27

Page 28: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

public void inquire()

{ System.out.println("You now have: " + galleons + " galleons " + sickles + " sickles " + knuts + " knuts");

}

public static void main(String[] args) { Account potter = new Account();

.

.

.

potter.inquire();

28

Page 29: Catie Welsh March 2, 2011.  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will

Midterm Review

Bring questions about the Sample Midterm

29