Java Programming II: Object-Oriented Approach - Stream I/O, Initialization, Variables - Pr, Study notes of Computer Science

An overview of various java constructs covered in the object-oriented programming ii course at the university of maryland, college park's department of computer science. Topics include stream i/o, initialization blocks, variable initialization, annotations, bitset class, and two-dimensional arrays. Stream i/o covers the concept of streams, their usage, and examples. Initialization blocks discuss static and instance variable initialization, their motivation, and types. Variable initialization explains the different ways to initialize variables. Annotations introduce java constructs that add validity constraints to classes. The document also covers bitwise operators and the bitset class, as well as two-dimensional arrays of primitives and objects.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-oqx
koofers-user-oqx ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Java Constructs
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Java Programming II: Object-Oriented Approach - Stream I/O, Initialization, Variables - Pr and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Java Constructs Department of Computer ScienceUniversity of Maryland, College Park

Overview

Stream I/O Initialization Blocks Variable Initialization Annotations BitSet class Two-dimensional array

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

Initialization Block

Definition^ Block of code used to initialize static & instancevariables for class Motivation^ Enable complex initializations for static variables^ Control flow^ Exceptions^ Share code between multiple constructors forsame class

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

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

Reviewing Bit-Operations and

x^11010 y^10110 x and y 10010 or

x^11010 y^10110 x and y 11110 xor

x^11010 y^10110 x and y 01100 Java Bitwise operators^ & and^ |^ or^ ^ exclusive or^ ~ complement

BitSet Class

Implements a vector of bits where the bits of the set are indexed bynonnegative 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 containsno 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 isincluded in the result. Example (See Computers.java)

Two-Dimensional Arrays of Objects^ Each row in a two-dimensional array is anarray^ The rows can have different lengths^ Defining an array where rows have the samelength

String [ ][ ] data = new String[3][4]; Important: Keep in mind we have created atwo-dimensional array of references to Stringobjects. No String object is present yet. We can also have ragged arrays Example (See Roster.java)