1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Advertisements

Lecture 15: I/O and Parsing
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
CSCI 160 Midterm Review Rasanjalee DM.
Written by: Dr. JJ Shepherd
Chapter 9 Imperative and object-oriented languages 1.
Java review and more. Class Header  Public class Welcome  Case sensitive  Body of the class must be enclosed by braces.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
HST 952 Computing for Biomedical Scientists Lecture 8.
Java Syntax Primitive data types Operators Control statements.
Alice in Action with Java
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
JavaScript, Third Edition
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
VB .NET Programming Fundamentals
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
CSC – Java Programming II Lecture 9 January 30, 2002.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Written by: Dr. JJ Shepherd
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 06 – Java Datatypes Webpage:
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Information and Computer Sciences University of Hawaii, Manoa
Basic Text File Input/Output
Text File Input/Output
Objects as a programming concept
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
I/O Basics.
The Building Blocks Classes: Java class library, over 1,800 classes:
Object Oriented Programming in java
Presentation transcript:

1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park

2 Stream Input/Output Stream A connection carrying a sequence of data Bytes  InputStream, OutputStream Characters  FileReader, PrintWriter From a source to a destination Keyboard File Network Memory Basis for modern I/O

3 Using Streams Opening a stream Connects program to external data Location of stream specified at opening Only need to refer to stream Usage 1. import java.io.* ; 2. Open stream connection 3. Use stream  read and / or write Catch exceptions if needed 4. Close stream Examples See fileExamples package

4 Initialization Block Definition Block of code used to initialize static & instance variables for class Motivation Enable complex initializations for static variables Control flow Exceptions Share code between multiple constructors for same class

5 Initialization Block Types Static initialization block Code executed when class loaded Initialization block Code executed when each object created (at beginning of call to constructor) Example class foo { static { A = 1; }// static initialization block { A = 2; }// initialization block }

6 Variable Initialization Variables may be initialized At time of declaration In initialization block In constructor Order of initialization 1. Declaration, initialization block (in the same order as in the class definition) 2. Constructor

7 Variable Initialization – Example class Foo { static { A = 1; }// static initialization block static int A = 2;// static variable declaration static { A = 3; }// static initialization block { B = 4; }// initialization block private int B = 5;// instance variable declaration { B = 6; }// initialization block Foo() {// constructor A = 7; B = 8; }// now A = 7, B = 8 }// initializations executed in order of number

8 Annotations Annotation – Java construct that allow us to add validity constraints to Java Classes Validity constraint example A instance variable cannot assume a negative value A parameter can not be null A method in a class must override a method in its superclass Syntax at-sign followed by annotation type and a parenthesized list of element-value pairs You can ignore annotations in the code distribution for class projects

9 Reviewing Bit-Operations and x y10110 x and y10010 or x y10110 x and y11110 xor x y10110 x and y01100 Java Bitwise operators & and | or ^ exclusive or ~ complement

10 BitSet Class Implements a vector of bits where the bits of the set are indexed by nonnegative integers Methods BitSet() – New bit set BitSet(int nbits) – Bit set large enough to represent bits with indices from 0 through nbits – 1 and(BitSet set) – Performs logical and between the current object and the set parameter (current object is updated with the result) or(BitSet set) – Performs logical or between the current object and the set parameter (current object is updated with the result) cardinality() – Returns number of bits set to 1 flip(int bitIndex) – Sets the bit at the specified index get(int bitIndex) – Returns true if the bit at bitIndex is set; false otherwise length() – Index of the highest set bit + 1. It returns zero if the BitSet contains no bits set. size() – Number of bits space used by the BitSet to represent bit values toString() – For every bit set, the decimal representation of that index is included in the result. Example (See Computers.java)

11 Two-Dimensional Arrays of Primitives Each row in a two-dimensional array is an array The rows can have different lengths Defining a primitive array where rows have the same length int [ ][ ] data = new int[3][4]; Defining a primitive data array where rows have different lengths (ragged array) int [ ][ ] ragged = new int[2][]; ragged[0] = new int[3]; ragged[1] = new int[1];

12 Two-Dimensional Arrays of Objects Each row in a two-dimensional array is an array The rows can have different lengths Defining an array where rows have the same length String [ ][ ] data = new String[3][4]; Important: Keep in mind we have created a two-dimensional array of references to String objects. No String object is present yet. We can also have ragged arrays Example (See Roster.java)