Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 5: Bank Account
Defining objects & classes
Review: Basic class structure
Three major components of a class:
– Fields – store data for the object to use
– Constructors – allow the object to be set up properly 
when first created
– Methods – implement the behavior of the object
public class ClassName
{
Fields
Constructors
Methods
} 
Objects First with Java - A Practical Introduction using BlueJ, © David J. 
Barnes, Michael Kölling
Fields
• Fields store values 
for an object.
• They are also known 
as instance variables.
• Fields define the 
state of an object.
public class Square
{
private int x;
private int y;
private int size;
private Color fillColor;
// Further details omitted.
} 
private int size;
visibility modifier
type
variable name
Constructors
• Constructors initialize an object.
• They have the same name as their class.
• They store initial values into the fields.
• They often receive external parameter values for this.
Objects First with Java - A Practical Introduction using BlueJ, © David J. 
Barnes, Michael Kölling
public Square()
{
x = 0;
y = 0;
size = 0;
color = Color.blue;
} 
/**
* Gets the size of the square.
*/ 
Objects First with Java - A Practical Introduction using BlueJ, © David J. 
Barnes, Michael Kölling
Methods
public int getSize()
{
return size;
} 
return type
method name
parameter list 
(empty)
start and end of method body (block)
return statement
visibility modifier
method header/signature
Formatting Output
• The NumberFormat class allows you to format 
values as currency or percentages
• The DecimalFormat class allows you to format 
values based on any pattern
• Both are part of the java.text package
• The NumberFormat class has static methods that 
return a formatter object
getCurrencyInstance()
getPercentInstance()
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
//  Purchase.java Author: Lewis/Loftus
//
//  Demonstrates the use of the NumberFormat class to format output.
//********************************************************************
import java.util.Scanner;
import java.text.NumberFormat;
public class Purchase
{
//-----------------------------------------------------------------
//  Calculates the final price of a purchased item using values
//  entered by the user.
//-----------------------------------------------------------------
public static void main (String[] args)
{
final double TAX_RATE = 0.06;  // 6% sales tax
int quantity;
double subtotal, tax, totalCost, unitPrice;
Scanner scan = new Scanner (System.in);
continued
Copyright © 2012 Pearson Education, Inc.
continued
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
NumberFormat fmt2 = NumberFormat.getPercentInstance();
System.out.print ("Enter the quantity: ");
quantity = scan.nextInt();
System.out.print ("Enter the unit price: ");
unitPrice = scan.nextDouble();
subtotal = quantity * unitPrice;
tax = subtotal * TAX_RATE;
totalCost = subtotal + tax;
// Print output with appropriate formatting
System.out.println ("Subtotal: " + fmt1.format(subtotal));
System.out.println ("Tax: " + fmt1.format(tax) + " at "
+ fmt2.format(TAX_RATE));
System.out.println ("Total: " + fmt1.format(totalCost));
}
} Sample Run
Enter the quantity: 5
Enter the unit price: 3.87
Subtotal: $19.35
Tax: $1.16 at 6%
Total: $20.51
Formatting Output
• The DecimalFormat class can be used to format 
a floating point value in various ways
• For example, you can specify that the number 
should be truncated to three decimal places
• The constructor of the DecimalFormat class 
takes a string that represents a pattern for the 
formatted number
• See CircleStats.java 
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
//  CircleStats.java Author: Lewis/Loftus
//
//  Demonstrates the formatting of decimal values using the
//  DecimalFormat class.
//********************************************************************
import java.util.Scanner;
import java.text.DecimalFormat;
public class CircleStats
{
//-----------------------------------------------------------------
//  Calculates the area and circumference of a circle given its
//  radius.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int radius;
double area, circumference;
Scanner scan = new Scanner (System.in);
continued
Copyright © 2012 Pearson Education, Inc.
continued
System.out.print ("Enter the circle's radius: ");
radius = scan.nextInt();
area = Math.PI * Math.pow(radius, 2);
circumference = 2 * Math.PI * radius;
// Round the output to three decimal places
DecimalFormat fmt = new DecimalFormat ("0.###");
System.out.println ("The circle's area: " + fmt.format(area));
System.out.println ("The circle's circumference: "
+ fmt.format(circumference));
}
}
Sample Run
Enter the circle's radius: 5
The circle's area: 78.54
The circle's circumference: 31.416
Create a new project!
• File > New > Java Project
– Give it a name (like BankAccount_netid)
• Right click on your new project & go to 
New > Class
– Give it a name (BankAccount)
– Click the check box to create a main method
– Click finish
Create a BankAccount class with the following:
1. Fields to store:
– Current balance
– Account number
– Customer name
– Customer address
* Think about what types these should be. For example, 
should the account number actually be stored as a number? 
What about leading 0’s?
2. Write a constructor that takes the customer’s name, 
address, and account number, and initializes the 
balance to 0.
3. Write getter and setter methods for the name and 
address fields.
4. Write a deposit and a debit method.The method 
signatures are similar in that there is no return value
and one parameter. Created by Emily Hill & Jerry Alan Fails
Testing your BankAccount with main
4. Write a print method that prints out the account information, 
including the current balance. Make sure to use proper 
formatting so both dollars and cents are displayed 
correctly.
5. Write a main method that creates a new, empty 
BankAccount stored in local variable myBankAccount.
– Deposit $5.00 into your BankAccount & print the balance
– Debit $1.50 into your BankAccount & print the balance
6. Before submitting make sure your BankAccount has the 
following methods:
– 1 constructor only (not 2) that takes 3 parameters
– getCustomerName, getCustomerAddress, getAccountNumber
– setCustomerName, setCustomerAddress
– deposit, debit, print, main
Created by Emily Hill & Jerry Alan Fails
Homework
• Finish lab & submit
• Work on CodingBat
• Read Chapter 4
Created by Emily Hill & Jerry Alan Fails