Lab 5: Bank Account Defining objects & classes
Review: Basic class structure public class ClassName Fields Constructors Methods 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
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. visibility modifier type variable name private int size; Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling
Constructors public Square() x = 0; y = 0; size = 0; color = Color.blue; 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
Methods method header/signature visibility modifier return type method name /** * Gets the size of the square. */ public int getsize() return size; start and end of method body (block) parameter list (empty) return statement Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling
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.
//******************************************************************** // 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 continued int quantity; double subtotal, tax, totalcost, unitprice; Scanner scan = new Scanner (System.in); 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 Copyright 2012 Pearson Education, Inc.
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.
//******************************************************************** // 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; continued Scanner scan = new Scanner (System.in); 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 Copyright 2012 Pearson Education, Inc.
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