Lab 5: Bank Account. Defining objects & classes



Similar documents
Comp 248 Introduction to Programming

Introduction to Java. CS 3: Computer Programming in Java

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Interactive Programs and Graphics in Java

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Basics of Java Programming Input and the Scanner class

AP Computer Science Static Methods, Strings, User Input

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Part I. Multiple Choice Questions (2 points each):

Chapter 2 Introduction to Java programming

Programming Fundamentals I CS 110, Central Washington University. November 2015

CS170 Lab 11 Abstract Data Types & Objects

objectives chapter Define classes that act like blueprints for new objects, made of variables and methods. Explain encapsulation and Java modifiers.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Some Scanner Class Methods

Sample CSE8A midterm Multiple Choice (circle one)

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

AP Computer Science Java Subset

6.1. Example: A Tip Calculator 6-1

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Introduction to Programming

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

(Eng. Hayam Reda Seireg) Sheet Java

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Week 1: Review of Java Programming Basics

Understanding class definitions

LAB4 Making Classes and Objects

More on Objects and Classes

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Java: overview by example

MAT 2170: Laboratory 3

Chapter 3. Input and output. 3.1 The System class

CSE 8B Midterm Fall 2015

Building Java Programs

Programming Languages CIS 443

Chapter 2: Elements of Java

Topic 11 Scanner object, conditional execution

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Java 進 階 程 式 設 計 03/14~03/21

Introduction to Java Lecture Notes. Ryan Dougherty

Chapter 2 Elementary Programming

Building Java Programs

CSC 221: Computer Programming I. Fall 2006

Java Crash Course Part I

Solving Compound Interest Problems

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Introduction to Java

J a v a Quiz (Unit 3, Test 0 Practice)

Lecture 1 Introduction to Java

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Object-Oriented Programming in Java

AP Computer Science Java Mr. Clausen Program 9A, 9B

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

In this Chapter you ll learn:

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

CS 106 Introduction to Computer Science I

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Java CPD (I) Frans Coenen Department of Computer Science

CS114: Introduction to Java

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Inheritance, overloading and overriding

Part 1 Foundations of object orientation

ECE 122. Engineering Problem Solving with Java

Moving from CS 61A Scheme to CS 61B Java

Contents. 9-1 Copyright (c) N. Afshartous

Install Java Development Kit (JDK) 1.8

Chapter 8. Living with Java. 8.1 Standard Output

Programmierpraktikum

Using Files as Input/Output in Java 5.0 Applications

Generating Unit Tests for Checking Refactoring Safety

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Programming in Java Course Technology, a part of Cengage Learning.

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

13 File Output and Input

java Features Version April 19, 2013 by Thorsten Kracht

JAVA.UTIL.SCANNER CLASS

Part I. The Picture class

Basic Object-Oriented Programming in Java

Interactive Applications (CLI) and Math

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Building Java Programs

Transcription:

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