Presentation is loading. Please wait.

Presentation is loading. Please wait.

More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.

Similar presentations


Presentation on theme: "More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13."— Presentation transcript:

1 More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13

2 What is displayed? 1.0 2.60 3.Nothing 4.IDK 2 public class MyTime { private int hour; private int min; public static final int MIN_IN_HOUR = 60; } public static void main(String[] args) { MyTime myTime = new MyTime(); System.out.print(MyTime.hour); }

3 What is displayed? 1.0 2.60 3.Nothing 4.IDK 3 public class MyTime { private int hour; private int min; public static final int MIN_IN_HOUR = 60; } public static void main(String[] args) { MyTime myTime = new MyTime(); System.out.print(MyTime. MIN_IN_HOUR ); }

4 What should the return type be? 1.void 2.int 3.float 4.none of these 4 public class MyTime { private int hour; private int min; public MyTime(){ hour = 0; min = 0; } //compare two MyTime objects public _______equals(){ //TODO comparison logic }

5 What is should the parameter type be? 1.void 2.int 3.MyTime 4.none of these 5 public class MyTime { private int hour; private int min; public MyTime(){ hour = 0; min = 0; } //compare two MyTime objects public boolean equals(___ obj){ //TODO comparison logic }

6 What is missing? 1.this. 2.foo. 3.MyTime. 4.none of these 6 public class MyTime { private int hour; private int min; public MyTime(){ hour = 0; min = 0; } public boolean equals(MyTime foo){ if( ___ min == min && ___ hour == hour) return true; else return false; }

7 How many copies of bc? 1.1 2.2 3.one per object 4.0 7 public class Ball { private int x, y; private int velocityY; private int velocityX; private static final int RADIUS = 10; private static final int GRAVITY = 3; private static int bc = 0; public Ball() { x = 0; y = 0; velocityY = 0; velocityX = 0; }

8 How many copies of totalDeposits? 1.1 2.2 3.one per object 4.0 8 public class BankAcct{ int acctNum; double balance; private static double interest = 1.9; private static double totalDeposits = 0; BankAcct(int number) { acctNum = number; balance = 0.0; }

9 How to update totalDeposits? 1.totalDeposits += deposit 2.this.totalDeposits += deposit 3.BankAcct.totalDeposits += deposit 4.IDK 9 public class BankAcct{ int acctNum; double balance; private static double interest = 1.9; //sum of account balances private static double totalDeposits = 0; BankAcct(int number) { acctNum = number; balance = 0.0; } public void deposit(double deposit){ balance += deposit; ____________________; }

10 Two add() methods ? 1.Error 2.OK if the parameter types differ 3.OK if the parameter names differ 4.IDK 10 class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public void add(int min){ this.min += min; } public void add(int hour){ this.hour += hour; }

11 Two add() methods ? 1.Error 2.OK if param. type and/or number of param. differ 3.OK if the parameter names differ 4.IDK 11 class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public void add(int m, int h){ min += m; hour += h; } public void add(int hour){ this.hour += hour; }

12 Two add() methods ? 1.Error 2.OK if the return types differ 3.OK if the parameter names differ 4.OK if param. type and/or number of param. differ 12 class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public void add(int min){ this.min += min; } public int add(int min){ this.min += min; return this.min; } }

13 private final String color ? 1.Member of Suit objects 2.Method of Suit objects 3.Constructor of Suit objects 4.IDK 13 11111111 enum Suit { CLUBS("black"), DIAMONDS("red"), HEARTS("red"), SPADES("black"); private final String color; private Suit(String sColor) { color = sColor; } public String getColor() { return color; } }

14 private Suit(String sColor) ? 1.Member of Suit objects 2.Method of Suit objects 3.Constructor of Suit objects 4.( ╯ °□°) ╯︵ ┻━┻ 14 enum Suit { CLUBS("black"), DIAMONDS("red"), HEARTS("red"), SPADES("black"); private final String color; private Suit(String sColor) { color = sColor; } public String getColor() { return color; } }


Download ppt "More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13."

Similar presentations


Ads by Google