Download presentation
Presentation is loading. Please wait.
1
Java Utilities and Bit Manipulation
Vector Class Enumeration Interface Bit Manipulation and the Bitwise Operators
2
Why or when do we use Vector object?
Let us see an example below. Input student names from a TextField and store them in an array. To store student names, you have to use an array of string objects. String st_name = new String[???]; To create an array of String objects, you have to decide its size. ??? should be a certain integer number. When you know how many students and you can fix the size of array. However, it is often a case that you could not fix the size. For instance, a swimming club, students can join and withdraw anytime such that the student number is dynamically changing.
3
Why or when do we use Vector object?
The problem is that The size of the array can not be fixed. The solution are to use a much larger size of array than it is necessary => inefficient and waste in terms of memory to use Vector Class because the Vector Class enables us to create array-like objects that can grow and shrink dynamically as a program’s data storage requirements change. => like array but the size can be changed during runtime.
4
What is Java Vector Class?
Java class Vector provides the capabilities of array-like data structures that can dynamically resize themselves. At any time the Vector contains a certain number of elements which is less than or equal to its capacity. The capacity is the space that has been reserved for the array. If a Vector needs to grow, it grows by an increment that you specify. If you do not specify a capacity increment, the system will automatically double the size of the Vector each time additional capacity is needed.
5
Class Vector constructors
public Vector(); Constructs an empty vector so that its internal data array has size 10. e.g. Vector v = new Vector(); public Vector(int initialCapacity); Constructs an empty vector with the specified initial capacity. e.g. Vector v = new Vector(1); public Vector(int initialCapacity, int capacityIncrement); Constructs an empty vector with the specified initial capacity and capacity increment. e.g. Vector v = new Vector(4, 2);
6
Class Vector methods public void addElement(Object obj)
Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity. e.g. v.addElement(input.getText()); public boolean removeElement(Object obj) Removes the first (lowest-indexed) occurrence of the argument from this vector. If the object is found in this vector, each component in the vector with an index greater or equal to the object's index is shifted downward to have an index one smaller than the value it had previously. e.g. if (v.removeElement(input.getText())) showStatus(“Removed: ” + input.getText());
7
Class Vector methods public Object firstElement()
Returns the first component (the item at index 0) of this vector. e.g. showStatus(v.firstElement()); public Object lastElement() Returns the last component of the vector. e.g. showStatus(v.lastElement() ); public boolean isEmpty() Tests if this vector has no components. e.g. showStatus(v.isEmptyt()? “Vector is empty” : “Vector is not empty” );
8
Class Vector methods public boolean contains(Object elem)
Tests if the specified object is a component in this vector. e.g. if (v.contains(input.getText())) showStatus(“Vector contains: ” + input.getText()); public int indexOf(Object elem) Searches for the first occurence of the given argument, testing for equality using the equals method. e.g. showStatus(“Element is at location” + v.indexOf(input.getText()) ); public int size() Returns the number of components in this vector. e.g. showStatus(“Size is ” + v.size());
9
Class Vector methods public int capacity()
Returns the current capacity of this vector. e.g. showStatus(“Capacity is ” + v.capacity()); public void trimToSize() Trims the capacity of this vector to be the vector's current size. If the capacity of this vector is larger than its current size, then the capacity is changed to equal the size by replacing its internal data array, kept in the field elementData, with a smaller one. An application can use this operation to minimize the storage of a vector. e.g. v.trimToSize(); public Enumeration elements() Returns an enumeration of the components of this vector. The returned Enumeration object will enerate all items in this vector. The first item generated is the item at index 0, then the item at index 1, and so on.
10
Interface Enumeration and its methods
public abstract interface Enumeration An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series. e.g. Enumeration enum = v.elements(); public Object nextElement() Returns the next element of this enumeration if this enumeration object has at least one more element to provide. public boolean hasMoreElements() Tests if this enumeration contains more elements. e.g. While (enum.hasMoreElements()) showStatus(enum.nextElement());
11
An example of using class Vector
Run Java applet => VectorTest.html Take a look at source code => VectorTest.java
12
When and why do we use Bit Manipulation
When a program communicates “directly with the hardware”, he or she needs to get down to the so-called “bits-and-bytes” level and proceeds bit manipulation. Why? Because all data is represented internally by computers as a sequence of bits. Each bit can assume the value 0 or the value 1. How? The bitwise operators (&, |, ^, <<, >>, >>>, ~) are used to manipulate the bits of integral operands (byte, char, short, int, long).
13
Bitwise Operators & | ^ ~ Opeartor Name e.g. a= 0101, b= 0011
bitwise AND c=a&b= bitwise inclusive OR c= a|b= bitwise exclusive OR c=a^b= one’s complement c= ~b= Run Java applet => MiscBitOps.html Take a look at source code => MiscBitOps.java
14
Bitwise Operators << >> >>>
Opeartor Name e.g. a=5= (b = ~a +1) => b=-5= << >> >>> a<<= left shift right shift with sign extension a>>= b>>= right shift with zero extension a>>>= b>>= Run Java applet => BitShift.html Take a look at source code => BitShift.java
15
The Bit representation of the integer
public String getBits( int value ) { int displayMask = 1 << 31; StringBuffer buf = new StringBuffer( 35 ); for ( int c = 1; c <= 32; c++ ) { buf.append( ( value & displayMask ) == 0 ? '0' : '1' ); value <<= 1; if ( c % 8 == 0 ) buf.append( ' ' ); } return buf.toString(); value = 2 = 1<<31 = When c=1 to 30, c=32, & => buf.append(‘0’) When c=31, => buf.append(‘1’) but.toString() = C = … Create a StringBuffer object buf with capacity 35 If (value&displayMask ==0) buf.append(‘0’); else buf.append(‘1’); 32 charaters are converted into a string
16
Exercises 1 Modify the java program, VectorTest.java, so that the modified java program has two TextField objects, for inputting student name and student ID, respectively. You can use two Vector objects, one (v_name) stores student name and one (v_id) stores student ID. The modified program has same buttons to implements Add, Remove, First, Last, Is Empty?, Contains, Location, Trim, Statistics, Display functions. ex1.html Refer to the java program, BitShift.java, (especially the method, public String getBits( int value ) ) and make a Java applet program that converts a decimal into hexadecimal. The applet has three TextField objects. One is for inputting a decimal value, one is for display bit representation and one is for displaying hexadecimal value. ex2.html (optinal)Change the java applet program of exercise problem 2 into a java application using Frame. 2 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.