1 / 17

Agenda

Classes and Objects Classes Methods Scope References. Agenda. public class BankAccount { private String myPassword; private double myBalance; public static final double OVERDRAWN_PENALTY=20.0 ; public BankAccount(){…} public BankAccount(String pswd, double bal){..}

lindsey
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes and Objects • Classes • Methods • Scope • References Agenda

  2. public class BankAccount { private String myPassword; private double myBalance; public static final double OVERDRAWN_PENALTY=20.0; public BankAccount(){…} public BankAccount(String pswd, double bal){..} public double getBalance(){..} public void deposit(String pswd, double bal){..} public void withdraw(String pswd, double bal){..} }

  3. Objects • By what is an object characterized? • What is a vehicle’s state and behavior? • What is an object reference?

  4. Classes • What is a class? • What is an object related to the class? • Can a class have more than one instance? • What will a state be represented by in Java? • What will a behavior be presented by in Java? • What is encapsulation? • Look at the bank account example. List the state variables and methods.

  5. public, private and static • What is information hiding? • Can private methods and variables in a class be accessed outside the class? • What is a static variable? • Does a static variable have to be constant? • Can the value of a static variable be changed? • Can the value of a static final variable be changed? • If there is a state(instance) variable public static final double OVERDRAWN_PENALTY; how do we call this method inside the class? how do we call this method in an instance?

  6. Methods • List the method headers(signature). • What 4 types of methods are mentioned in the book? • What is a constructor? • Does default constructors have arguments? • What does a constructor with parameter(s) do? • BankAccount aAcct = new BankAccount(); what is the object variables for above stmt? • What does an object variable store?

  7. Accessor methods • What is an accessor method? • Does an accessor return any information about the object? • Which is the accessor method in the BankAccount class(p86)?

  8. Mutator methods • What is a mutator method? • Which ones in the BankAccount class are mutator methods(p86)? • What does these mutator methods change? • How does a mutator metod got invoked in the main program?

  9. Static methods • What is the differences between static method and instance method? • Suppose there is a class called BankAccount, one public static method called getInterestRate(), one public nonstatic method called getCDRate(), how do you invoke these 2 methods in main()? • What is Drive class? • Is it correct? All the methods in the driver class must be static?

  10. Method overloading • What are overloaded methods? • Can the overloaded methods have different return type? • Are the two methods below overloaded? Why? public int product(int n){return n*n; } public double product(int n){return n*n; }

  11. Scope of a variable or method • What is the scope of a variable or method? • What does a block mean in Java? • What is the scope of instance variables and methods of a class? • Within the class all instance variables and methods are accessed simply by name(no dot operator), correct? • Can state variables and methods of a class be accessed directly even if they are private?

  12. Look at the next slide. Is it allowed that “pswd” is a state variable and also in the parameter list of methods? • Consider the “pswd” variable in constructor and in deposit and withdraw, what type of variables are they? What scope do they have? • How do you distinguish the state variable “pswd” and the local variable “pswd” inside the deposit() method? • If we have this.pswd inside deposit() method, which pswd does it refer to?

  13. public class BankAccount { private String pswd; private double bal; public static final double OVERDRAWN_PENALTY=20.0; public BankAccount(){…} public BankAccount(String pswd, double bal){..} public double getBalance(){..} public void deposit(String pswd, double bal){..} public void withdraw(String pswd, double bal){..} }

  14. References • What are the two data types? • What are reference data types? Name some examples. • What is the value of the object aAcct if we have BancAccount aAcct = new BankAccout(); • If we change any state variable like “pswd” in bAcct, will aAcct’s “pswd” also get changed? Why? • What do you call an uninitialized object variable?

  15. What would happen for the following statement? BankAcct aAcct; double balance = aAcct.getBalance(); • If you fail to initialize a variable before you use it, you get a compile-time error. But if you fail to initialize an instance variable, what happens?

  16. Method parameters • What are formal and actual parameters? • How are the parameters of primitive types passed to a method? • What does passed by value mean?

  17. Homework From the Chapter 2 multiple choice questions you can find some classes without implementaions. Finish the implementations for the following classes: • Time • Date • Rational(skip the reduce() method) • Temperature

More Related