Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.

Slides:



Advertisements
Similar presentations
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
Advertisements

JAVA Revision Lecture Electronic Voting System Marina De Vos.
Understanding class definitions Looking inside classes 5.0.
Looking inside classes Fields, Constructors & Methods Week 3.
1 User-Defined Classes The String class is a pre-defined class that is provided by the Java Designer. Sometimes programmers would like to create their.
CSCI 160 Midterm Review Rasanjalee DM.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Lab 10: Bank Account II Transaction List, error checking & aggregation.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
TA: Nouf Al-Harbi NoufNaief.net :::
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
Understanding class definitions Looking inside classes 3.0.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Understanding class definitions Looking inside classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Writing Classes (Chapter 4)
Introduction to Object-Oriented Programming
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from ) Room London Knowledge Lab Emerald.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
ECE122 Feb. 22, Any question on Vehicle sample code?
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Introduction to Java Java Translation Program Structure
1 CSC 221: Computer Programming I Fall 2004 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
Review Creating Objects Pepper many references from rial/java/objects/object.html
Looking inside classes Conditional Statements Week 4.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
CO320 Catchup David Barnes with material from Dermot Shinners-Kennedy.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
Understanding class definitions Exploring source code 6.0.
Designing Classes Lab. The object that you brought to class Put it in the basket we will exchange them now.
SOFTWARE AND PROGRAMMING 1 Advert : NO TEST1 on 7/02: TEST1 will be 14/02 Lab: SH131, BBK536 6:00-7:30 (from ) [each student must have obtained.
SOFTWARE AND PROGRAMMING 1
CSC 222: Object-Oriented Programming Fall 2015
CSC111 Quick Revision.
Understanding class definitions
public class BankAccount{
CLASS DEFINITION (> 1 CONSTRUCTOR)
Java Programming with BlueJ
Understanding class definitions
An Introduction to Java – Part II
Creating Objects in a Few Simple Steps
String Methods: length substring
COS 260 DAY 3 Tony Gauvin.
CS139 October 11, 2004.
Understanding class definitions
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
JAVA CLASSES.
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Special instance methods: toString
Agenda About Homework for BPJ lesson 15 Overloading constructor
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine (2.1)

copyright by Scott GrissomCh 2 Class Definition Slide 2 Fields stores information also called instance variables use lower case for names identify data type Examples int balance; int accountNum; String name; Draw object diagram

copyright by Scott GrissomCh 2 Class Definition Slide 3 Constructors initializes the object to an appropriate state same name as Class public BankAccount (String name, int id){ name = last; accountNum = num; balance = 0; }

copyright by Scott GrissomCh 2 Class Definition Slide 4 Accessor Methods ask object about its state method signature body generally contains a return statement recommend a name starting with ‘get’ public int getBalance ( ){ return balance; }

copyright by Scott GrissomCh 2 Class Definition Slide 5 Mutator Methods generally changes the object state might ask object to do something public void makeDeposit (int amount){ balance = balance + amount; }

copyright by Scott GrissomCh 2 Class Definition Slide 6 Print Statements displays results in the terminal window public void printStatement( ){ System.out.println(“Bank of GVSU”); System.out.println(“Name: “ + name); System.out.println(“Account #: “ + id); System.out.println(“Balance: “ + balance); System.out.println(); }

copyright by Scott GrissomCh 2 Class Definition Slide 7 Group Exercises ask questions about the TicketMachine code write a method called identityTheft that sets the balance to zero write a method makeWithdrawal that removes the provided amount from the balance write a method setBalance that replaces the existing balance with the provided amount

copyright by Scott GrissomCh 2 Class Definition Slide 8 Conditional Statements (2.11) Sample Code (2.8) if (some condition){ do if true }else{ do if false } boolean expressions –six relational operators –>, =, <=, !=

copyright by Scott GrissomCh 2 Class Definition Slide 9 Local variables temporary values public int refundBalance(){ balance = 0; return balance; } public int refundBalance(){ return balance; balance = 0; } public int refundBalance(){ int amountToRefund = balance; balance = 0; return amountToRefund; }

copyright by Scott GrissomCh 2 Class Definition Slide 10 Group Exercises result = x + y + z; result = x + y * z; result = (x + ) * z;