You are on page 1of 20

CMPE 112

Fundamentals of Programming I

Chapter 6, Section 6.1 , 6.3

Objects and Methods

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

public Pet(int initialAge){


name = "No name yet";
age = initialAge;
weight =0;
}

public Pet(String initialName, int initialAge, double initialWeight){

name = initialName;
age = initialAge;
weight =initialWeight;
} JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
(continued)
}
import java.util.Scanner;

public class PetDemo


{
public static void main(String[] args)
{
Pet yourPet1 = new Pet();

Pet yourPet2 = new Pet(10);

Pet yourPet3 = new Pet("Balto", 8, 0);


}
}

JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Note:
• You can not use an existing object to call a
constructor,

Pet myPet = new Pet();


myPet.Pet("Fang",1 , 150.0);

//Invalid

JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Calling Methods from Other Constructors

• Constructor can call other class methods

• View sample code, listing 6.3


class Pet2
– Note method setPet
– Keeps from repeating code
JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
Local Variables

• Variables declared inside a method are called


local variables
– May be used only inside the method
– All variables declared in method main are local
to main
• Local variables having the same name and
declared in different methods are different
variables

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

• It’s state can include the account number, the


current balance, and the name of the owner
• An account’s behaviors (or services) include
deposits and withdrawals, and adding interest

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

• Driver programs are often used to test other parts


of the software
• The Transactions class contains a main method
that drives the use of the Account class,
exercising its services
• See Transactions.java
• See Account.java

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;

public class Account


{
private final double RATE = 0.035; // interest rate of 3.5%

private long acctNumber;


private double balance;
private String name;

//-----------------------------------------------------------------
// 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.
//********************************************************************

public class Transactions


{
//-----------------------------------------------------------------
// Creates some bank accounts and requests various services.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Account acct1 = new Account ("Ted Murphy", 72354, 102.56);
Account acct2 = new Account ("Jane Smith", 69713, 40.00);
Account acct3 = new Account ("Edward Demsey", 93757, 759.32);

acct1.deposit (25.85);

double smithBalance = acct2.deposit (500.00);


System.out.println ("Smith balance after deposit: " +
smithBalance);

continue

JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch
continue

System.out.println ("Smith balance after withdrawal: " +


acct2.withdraw (430.75, 1.50));

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

acct1 acctNumber 72354

balance 102.56

name "Ted Murphy"

acct2 acctNumber 69713

balance 40.00

name "Jane Smith"

JAVA: An Introduction to Problem Solving & Programming, 6th Ed. By Walter Savitch

You might also like