Java Objects and Methods
Java Objects and Methods
Fundamentals of Programming I
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch 1
Defining Constructors
• A special method called when instance of an
object created with new
– Create objects
– Initialize values of instance variables
• Can have parameters
– To specify initial values if desired
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
} inside main
} Point originOne = new Point(23, 94);
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Defining Constructors
• Constructor without parameters is the default
constructor
– Java will define this automatically if the class
designer does not define any constructors
– If you do define a constructor, Java will not
automatically define a default constructor
• Usually default constructors not included in
class diagram
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
/**
Class for basic pet data: name, age, and weight.
*/
public class Pet
{
private String name;
private int age; //in years
private double weight;//in pounds
public Pet(){
name = "No name yet.";
age = 0;
weight =0;
}
name = initialName;
age = initialAge;
weight =initialWeight;
} JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
(continued)
}
import java.util.Scanner;
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Note:
• You can not use an existing object to call a
constructor,
//Invalid
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Calling Methods from Other Constructors
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch 11
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Local Variables
• View sample file, listing 5.5A
class BankAccount
• View sample file, listing 5.5B
class LocalVariablesDemoProgram
• Note two different variables newAmount
– Note different values output
Sample
screen
output
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch 13
Bank Account Example
• Let’s look at another example that demonstrates
the implementation details of classes and methods
• We’ll represent a bank account by a class named
Account
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Driver Programs
• A driver program drives the use of other, more
interesting parts of a program
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
//********************************************************************
// Account.java Author: Lewis/Loftus
//
// Represents a bank account with basic services such as deposit
// and withdraw.
//********************************************************************
import java.text.NumberFormat;
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account (String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
continue
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
continue
//-----------------------------------------------------------------
// Deposits the specified amount into the account. Returns the
// new balance.
//-----------------------------------------------------------------
public double deposit (double amount)
{
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Withdraws the specified amount from the account and applies
// the fee. Returns the new balance.
//-----------------------------------------------------------------
public double withdraw (double amount, double fee)
{
balance = balance - amount - fee;
return balance;
}
continue
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
continue
//-----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of the account.
//-----------------------------------------------------------------
public double getBalance ()
{
return balance;
}
//-----------------------------------------------------------------
// Returns a one-line description of the account as a string.
//-----------------------------------------------------------------
public String toString ()
{
return (acctNumber + "\t" + name + "\t" + balance);
}
}
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
//********************************************************************
// Transactions.java Author: Lewis/Loftus
//
// Demonstrates the creation and use of multiple Account objects.
//********************************************************************
acct1.deposit (25.85);
continue
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
continue
acct1.addInterest();
acct2.addInterest();
acct3.addInterest();
System.out.println ();
System.out.println (acct1);
System.out.println (acct2);
System.out.println (acct3);
}
}
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Output
continue
Smith balance after deposit: 540.0
Smith balance
System.out.println after
("Smith withdrawal:
balance 107.55 " +
after withdrawal:
acct2.withdraw (430.75, 1.50));
72354 Ted Murphy $132.90
acct1.addInterest();
69713 Jane Smith $111.52
acct2.addInterest();
93757 Edward Demsey
acct3.addInterest();
$785.90
System.out.println ();
System.out.println (acct1);
System.out.println (acct2);
System.out.println (acct3);
}
}
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Bank Account Example
balance 102.56
balance 40.00
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch