Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
MSc IT Programming Methodology (2). number name number.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Lecture 171 CS110 Lecture 17 Thursday, April 1, 2004 Announcements –hw7 due tonight –pass/fail, withdraw deadline April 8 Agenda –Questions –Juno –JFile.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Lecture 61 CS110 Lecture 6 Thursday, February 12, 2004 Announcements –hw2 due today Agenda –questions –testing –modeling text files –declarations (classes,
Introduction to Object-Oriented Programming
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
ArrayList, Multidimensional Arrays
Classes CS 21a: Introduction to Computing I First Semester,
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Lecture 21 CS110 Lecture 2 January 29, 2004 Announcements –hw1 part 1 – due right now –hw1 part 2 – due Tuesday night Questions Agenda –turnin –Object.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Lecture 71 CS110 Lecture 7 February 17, 2004 Announcements –hw3 due Thursday Agenda –questions –hw3 tips –getters and setters – information hiding –delegation.
Introduction to Java Java Translation Program Structure
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
Chapter 3 Implementing Classes
Chapter 9 Introduction to Arrays Fundamentals of Java.
More on Arrays Review of Arrays of ints, doubles, chars
The need for Programming Languages
Lecture 3 John Woodward.
Chapter 3 – Implementing Classes
Yanal Alahmad Java Workshop Yanal Alahmad
Implementing Classes Yonglei Tao.
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
An Introduction to Java – Part I, language basics
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Arrays and Array Lists CS 21a.
JAVA CLASSES.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
In this class, we will cover:
Exceptions.
Exceptions.
Presentation transcript:

Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on the exam? –collections (Chapter 4) –arrays –better Banks and BankAccounts

Lecture 102 Exam preview Based on course work so far (JOI Chapters 1,2,3) Given source code Foo.java to read: –what does java Foo do? –find tokens, keywords, identifiers, messages … –comment on programming conventions –draw a box-and-arrow picture Write simple code using if/else, while, for Sample posted on course web page

Lecture 103 Vocabulary java, token, keyword, identifier, convention, API, message, method, int, double, boolean, if, else, while, for, declaration, delegation, variable, class, instance, field, source code, static, scope, client, comment, javadoc, compile, constructor, final, flow control, object, main, string, syntax, semantics, this, true, false, unit test, self documenting test, new, null, void, public, private, pseudocode, …

Lecture 104 Applications Bank, BankAccount LinearEquation, Temperatures, PhoneBill IntArithmetic, DoubleArithmetic, Exponentiate TextFile HLine, Box, Screen, VLine, Frame, …

Lecture 105 hw4 - Exponentiate needs just main pseudocode –create a Terminal –get base and exponent from user –create any BigIntegers you need –send a message to do the computation –print the result

Lecture 106 Collections More programs manipulate data than do arithmetic –Library catalog manages a collection of books –Registrar maintains a database of student records –EStore has list of Items in Warehouse and in ShoppingCart –Bank deals with a list of BankAccounts –Screen manages a set of pixels –Windows folder holds files (and other folders) … list, set, database, group, collection, container...

Lecture 107 Collections A Collection is an object that stores things Collection API must provide ways to –make an empty container (constructor) –put things in: put, insert, add, store,... –know what’s there: get, retrieve, find, lookup,... loop on contents –throw things out: delete, remove, kill,... Java provides array, List, Map, Set

Lecture 108 A better Bank (Version 4)... Maintains a list of BankAccounts Allows banker to create new accounts while the Bank is open Keeps track of the total balance of all the accounts in it, and of the total number of transactions performed by its accounts

Lecture 109 Array Simplest Java object for managing a list array syntax uses square brackets [] several ways Bank.java (version 4) uses an array Declare field accountList of type “array of BankAccounts” (line 32) private BankAccount[] accountList ; Create the array, ready to hold 3 Items (60) accountList = new BankAccount[NUM_ACCOUNTS];

Lecture 1010 An array for 3 BankAccounts // fill array on lines accountList [0] = new BA( 0, this); accountList [1] = new BA(100, this); accountList [2] = new BA(200, this); Improved BankAccount object has a field to hold a reference to the Bank it belongs to (more later)

Lecture 1011 boxes and arrows for arrays aList BA[]; declares array of BA (line 32) 0: BankAccount[] 1: 2: Bank account List: 0: BankAccount[] 1: 2: BankAccount BankAccount[] Bank account List: null Bank account List: aList = new BA[3]; creates array object (60) BankAccount[] aList[0] =new BA(); fills array object (66-)

Lecture 1012 Seeing what’s in an array BA acct = accountList[1]; atm.println(“Account 1 balance: ” + acct.getBalance()); prints Account 1 balance: 100

Lecture 1013 looping over an array // lines in report method for (int i = 0; i < NUM_ACCOUNTS; i++) { terminal.println( i + “\t” + accountList[i].getBalance() + … ) } accountList[i] is (a reference to) the Object at position i. send a getBalance message to each account in the Bank in succession prints00 x 1100 x x \t is tab

Lecture 1014 array summary declare: Type[ ] myArray; // Type is class name, // or primitive create: myArray = new Type[size]; // int size put: myArray[position] = …; // int position get: Type x = myArray[position]; myArray[position].message(); length: myArray.length; // final public field range: 0,1,…, myArray.length-1 read short self documenting program Array.java

Lecture 1015 arrays are Objects Created with new Size is determined at creation time but not at declaration time The square brackets provide the special syntax for sending an array a message myArray[i] refers to the value stored at index i (which must be an integer) Stored values may be primitive (in the box) or a reference to an object (an arrow) but all values must have the same type

Lecture 1016 Improved BankAccount Private fields (all have getter methods) int balance; int transactionCount; Bank issuingBank; Constructor public BankAccount(int initialBalance, Bank issuingBank) { this.issuingBank = issuingBank; this.deposit(initialBalance); }

Lecture 1017 Command line arguments in Java public static void main(String[] args) args is a parameter for the main method The declaration says it’s an array of String objects Its contents are the words on the command line after java ClassName Argument array can have any name you wish –args is conventional –old C programmers may call it argv

Lecture 1018 Command line arguments in Java class CommandLineArgsDemo public static void main( String[] args ) { for (int i = 0; i < args.length; i++){ System.out.println('|'+args[i]+'|'); } %> java CLID message is “hello, world” |message| |is| |hello, world| Experiment with %> java Bank -e

Lecture 1019 Improved Bank banker commands –create new account –report on totals –deal with a customer deposit withdraw get balance transfer

Lecture 1020 Bank and BankAccount cooperate public int deposit(int amount) { this.incrementBalance( amount); this.countTransaction(); return amount ; } public void incrementBalance(int amount) { balance += amount; this.getIssuingBank(). incrementBalance( amount ); } this BankAccount asks the Bank it is in to update its own balance field

Lecture 1021 A Bank object and its fields Bank atm: account List: balance: transaction Count: BankAccount[] Terminal balance: int transaction Count: issuing Bank: int Bank BankAccount bankName: BankAccount[] 0: 1: 2: int Terminal String “River Bank” balance: int transaction Count: issuing Bank: int Bank BankAccount balance: int transaction Count: issuing Bank: int Bank BankAccount 100 2