Point
Rectangle
Ellipse
BankAccount
CashRegister
Scanner
Random // better name: RandomNumberGenerator
Math
public class CashRegister
{
public void enterPayment(int dollars, int quarters, int dimes,
int nickels, int pennies)
. . .
public static final double NICKEL_VALUE = 0.05;
public static final double DIME_VALUE = 0.1;
public static final double QUARTER_VALUE = 0.25;
. . .
}
public class Coin
{
public Coin(double aValue, String aName){ . . . }
public double getValue(){ . . . }
. . .
}
public class CashRegister
{
public void enterPayment(int coinCount, Coin coinType) { . . . }
. . .
}
double balance = account.getBalance();
account.deposit(1000);
String name = "John Q. Public";
String uppercased = name.toUpperCase(); // name is not changed
public void transfer(double amount, BankAccount other)
{
balance = balance - amount;
other.balance = other.balance + amount; // Modifies explicit parameter
}
public void printBalance() // Not recommendedBad idea: message is in English, and relies on System.out
{
System.out.println("The balance is now $" + balance);
}
void read(Scanner in)Does this method have a side effect?
{
while (in.hasNextDouble())
add(in.nextDouble());
}
void transfer(double amount, double otherBalance)
{
balance = balance - amount;
otherBalance = otherBalance + amount;
}
double savingsBalance = 1000;
harrysChecking.transfer(500, savingsBalance);
System.out.println(savingsBalance);
public class BankAccount
{
public void transfer(double amount, BankAccount otherAccount)
{
balance = balance - amount;
double newBalance = otherAccount.balance + amount;
otherAccount = new BankAccount(newBalance); // Won't work
}
}
/**
Deposits money into this account.
@param amount the amount of money to deposit
(Precondition: amount >= 0)
*/
if (amount < 0) throw new IllegalArgumentException();
balance = balance + amount;
// if this makes the balance negative, it's the caller's fault
balance = balance + amount;
assert amount >= 0;To enable assertion checking: java -enableassertions MyProg
balance = balance + amount;
if (amount < 0) return; // Not recommended; hard to debug
balance = balance + amount;
Example:
Purpose:To assert that a condition is fulfilled. If assertion checking is enabled and the condition is false, an assertion error is thrown. |
/**
Deposits money into this account.
(Postcondition: getBalance() >= 0)
@param amount the amount of money to deposit
(Precondition: amount >= 0)
*/
amount <= getBalance() // this is the way to state a postcondition
amount <= balance // wrong postcondition formulation
public class Financial
{
public static double percentOf(double p, double a)
{
return (p / 100) * a;
}
// More financial methods can be added here.
}
Financial.
percentOf(taxRate,
total);Math m = new Math();
y = m.sqrt(x);
public class BankAccountIf lastAssignedNumber was not static, each instance of BankAccount would have its own value of lastAssignedNumber
{
. . .
private double balance;
private int accountNumber;
private static int lastAssignedNumber = 1000;
}
public BankAccount()
{
// Generates next account number to be assigned
lastAssignedNumber++; // Updates the static field
// Assigns field to account number of this bank account
accountNumber = lastAssignedNumber; // Sets the instance field
}
public class BankAccount
{
. . .
private static int lastAssignedNumber = 1000; // Executed once,
// when class is loaded
}
public class BankAccount
{
. . .
public static final double OVERDRAFT_FEE = 5; // Refer to it as
// BankAccount.OVERDRAFT_FEE
}
public class RectangleTesterThese variables are independent from each other; their scopes are disjoint
{
public static double area(Rectangle rect)
{
double r = rect.getWidth() * rect.getHeight();
return r;
}
public static void main(String[] args)
{
Rectangle r = new Rectangle(5, 10, 20, 30);
double a = area(r);
System.out.println(r);
}
}
Rectangle r = new Rectangle(5, 10, 20, 30);
if (x >= 0)
{
double r = Math.sqrt(x);
// Error–can't declare another variable called r here
. . .
}
if (x >= 0)
{
double r = Math.sqrt(x);
. . .
} // Scope of r ends here
else
{
Rectangle r = new Rectangle(5, 10, 20, 30);
// OK–it is legal to declare another r here
. . .
}
Math.sqrt
harrysChecking.getBalance
public class BankAccount
{
public void transfer(double amount, BankAccount other)
{
withdraw(amount); // i.e., this.withdraw(amount);
other.deposit(amount);
}
. . .
}
public class Coin
{
. . .
public double getExchangeValue(double exchangeRate)
{
double value; // Local variable
. . .
return value;
}
private String name;
private double value; // Field with the same name
}
value = this.value * exchangeRate;
package packageName;as the first instruction in the source file containing the classes
package com.horstmann.bigjava;
public class Financial
{
. . .
}
Package |
Purpose |
Sample Class |
java.lang |
Language support |
Math |
java.util |
Utilities |
Random |
java.io |
Input and output |
PrintStream |
java.awt |
Abstract Windowing Toolkit |
Color |
java.applet |
Applets |
Applet |
java.net |
Networking |
Socket |
java.sql |
Database Access |
ResultSet |
javax.swing |
Swing user interface |
JButton |
org.omg.CORBA |
Common Object Request Broker Architecture |
IntHolder |
Example:
Purpose:To declare that all classes in this file belong to a particular package |
java.util.Scanner in = new java.util.Scanner(System.in);
import java.util.Scanner;
. . .
Scanner in = new Scanner(System.in)
import java.util.*;