More on Classes and Objects 2-15-2018 Review Dry running a program that uses objects and a client Finish up the Car and Fraction Classes
Dry run the above code using the following values for input: 25, 123 import java.util.Scanner; public class AccountTest { public static void main( String args[] ) Account account1 = new Account( 50 ); Account account2 = new Account( -7 ); System.out.print(account1.getBalance() ); System.out.println(account2.getBalance() ); Scanner input = new Scanner( System.in ); System.out.print( "Enter deposit amount for account1: " ); int depositAmount = input.nextInt(); account1.credit( depositAmount ); System.out.print( "Enter deposit amount for account2: " ); depositAmount = input.nextDouble(); account2.credit( depositAmount ); } // end main } // end class AccountTest Dry run the above code using the following values for input: 25, 123 public class Account { private double balance=0; public Account( double initialBalance ) if ( initialBalance > 0.0 ) balance = initialBalance; } public void credit( double amount ) balance = balance + amount; public double getBalance() return balance;
Review: Are you allowed to define a class with no constructors? Can a class have two different no-args constructors? What is a good style for naming constructors? What is the common Java style for method names? Yes. Then Java provides one default no-args constructor No. Constructors must have different numbers or types of parameters There is no choice. The names of all of the constructors must match the name of the class. Are you allowed to define a class with no constructors? Yes. Then Java provides one default no-args constructor. Can a class have two different no-args constructors? No. Constructors must have different numbers or types of parameters. What is a good style for naming constructors? There is no choice: the names of all constructors are the same as the name of the class. What is the common Java style for method names? Start with a lowercase letter, with subsequent words capitalized. Methods should sound like verbs. Start with a lowercase letter, with subsequent words capitalized. Methods should sound like verbs.
Review (cont’d): Are parameters of primitive data types passed to methods “by value” or “by reference”? Can a method have more than one return statement? Can a method have no return statements? Can a method create a new object and return it to the calling method? By value. Yes, but all except one must be inside an if or else (or a switch). Are parameters of primitive data types passed to methods “by value” or “by reference”? By value. Can a method have more than one return statement? Yes, but all except one must be inside an if or else (or a switch). Can a method have no return statements? Yes, a void method doesn’t have to have a return. Can a method create a new object and return it to the calling method? Yes. Yes, a void method doesn’t have to have a return. Yes
Review (cont’d): When is it appropriate to define overloaded methods? Describe the difference between static and instance variables. What is the syntax for referring to a public static field outside the class? When two methods perform similar tasks for different types or numbers of parameters. A class variable (declared static) is shared by all objects of the class; an instance variable may have different values in different objects. When is it appropriate to define overloaded methods? When two methods perform similar tasks for different types or numbers of parameters. Describe the difference between class and instance variables. A class variable (declared static) is shared by all objects of the class; an instance variable may have different values in different objects. What is the syntax for referring to a public static field outside the class? ClassName.fieldName ClassName.fieldName
Review (cont’d): Can non-static methods access static fields? Can static methods access instance variables? Can static methods have local variables? Yes. No. Static methods are not associated with any particular object, so how would they know which object’s instance variables to access? Can non-static methods access static fields? Yes. Can static methods access instance variables? No. Static methods are not associated with any particular object, so how would they know which object’s instance variables to access? Can static methods have local variables? Sure, why not? Yes
Parking that Car Instance Variables Constructors Methods Model, numberOfPassengers, amountOfGas Constructors Default, given the above information Methods addPassenger removePassenger fillTank outOfGas toString Accessors and Modifiers (Mutators) for all instance variables amountOfGasPerPassenger
Code Break: Complete the Fraction Class Constructors Default given numerator and denominator Given a numerator Given another Fraction object (Will make a duplicate Fraction) Multiply (overloaded) Multiply by a Fraction object Multiply by an integer Multiply given a numerator and denominator toString() Divide By a Fraction Object By an integer add Pushes subtract Reduce