Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110 Classes Tabitha Peck M.S. March 3, 2008 MWF 3-3:50 pm Philips 367 1.

Similar presentations


Presentation on theme: "COMP 110 Classes Tabitha Peck M.S. March 3, 2008 MWF 3-3:50 pm Philips 367 1."— Presentation transcript:

1 COMP 110 Classes Tabitha Peck M.S. March 3, 2008 MWF 3-3:50 pm Philips 367 1

2 Announcements Program 3 due tomorrow by midnight Wednesday - come with review questions Wednesday office hours? Friday - midterm 2

3 Questions? 3

4 Today in COMP 110 Write a bank account class for Harry Potter A.K.A. more with classes 4

5 Example run Would you like to make a transaction? y/n y Would you like to make a deposit (d), withdrawal (w), or inquire (i)? d How many galleons, sickles, and knuts would you like to deposit? Galleons: 5 Sickles: 6 Knuts: 4 Would you like to make a transaction? y/n y Would you like to make a deposit (d), withdrawal (w), or inquire (i)? w How many galleons, sickles, and knuts would you like to withdrawal? Galleons: 4 Sickles: 0 Knuts: 0 Would you like to make a transaction? y/n y Would you like to make a deposit (d), withdrawal (w), or inquire (i)? i You now have: 1 galleons 6 sickles 4 knuts 5

6 Private Instance Variables What are instance variables? Why private? What are the instance variables for our bank account program? What do we need to keep track of? 6

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

8 Accessor and Mutator Methods getGalleons: int getSickles: int getKnuts: int setGalleons(int) setSickles(int) setKnuts(int) 8

9 Deposit 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; } 9

10 Withdrawal 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; } } public static void main(String[] args) { Account potter = new Account();. potter.withdrawal(5, 7, 9); 10

11 Inquire 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(); 11

12 Type Casting (int)var (float)var (double)var 12

13 Type Casting var56.234…0.321 (int)var560 (double)var5.006.234…0.321 (float)var5.006.234…0.321 (short)var560 13

14 byteshortintlongfloatdouble byte short int long float double Type = ? 14

15 Type Case I want to put an int into a double? What do I do? Double myDouble = (double)myInt; 15

16 Wednesday Exam review Come with questions 16


Download ppt "COMP 110 Classes Tabitha Peck M.S. March 3, 2008 MWF 3-3:50 pm Philips 367 1."

Similar presentations


Ads by Google