You are on page 1of 26
SKL-OOP (JAVA) Course Code: CSE304 earguroh Farhan Iqbal. Nam Department; Computer Engineering Class & Semester: S.E (SECOND Year), Sem Il Subject: SKL-OOP (JAVA) Expt. No. 06 Title: Write a program in java to perform (i) Vector operations (ii) String Buffer functions. Date: Subject In-charge Sign: RIZVI COLLEGE OF ENGINEERING | Department of Computer Engineering, Page | 1 SKL-OOP (JAVA) Course Code: CSLIO Experiment No. 6 Alm: Write a program in java to perform (i) Vector operations (ii) String Buffer functions Software: JDKU.04 Notepad ‘Theory: — This cavers the following topics: 1) Vector class, 2) Vector constructors, 3) Methanls of Vector. 4) StringBuffer class, 5) StringBuffer Constructors. 6) StringBuffer Methods. & Vector class: Vector implements a dynamic array. It is similar to Array List, but with two differences: + Vector is synchronized. ‘© Vector contains many legacy methods that are not part of the collections framework. Vector proves to he very uscful if you don't know the size of the array in advance of you just need one that can change sizes over the lifetime of a program, % Vector constructors: ‘Vector () This constructor creates a default vector, which hax an initial size of 10. Vector (int size) This constructor accepis an argument thal equals to the required size, and creates a vector whose initial capacity is specified bys size, Vector (int size, Int incr) This constructor: ‘whose Initial capacity 1s specified by ne and whose increment is specified by lot. The Increment specifies he wmnber of elements to allocate each time that a vector is resized upward. ‘Vector(Collection ¢) This constructor creates a vector that contains the elements of collection c. _ | NEERING | Deparimvat of Computer Engineering RIZVECOLLEG SKL-OOP (JAVA) Course Code: CSLI04 > Vector Methods: re ey jection ¢) Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection's 4 ction into this Vector at the specified position. 5 | void addElement(Object obj) Adds the specified component to the end of this vector, increasing its size by one. ‘ 6 | int capueity() Returns the current capacity of this vector. 7 void clear() | Removes all of the elements from this vector. B | Object clone() Retums a clone of this vector. 9 | Boolean rontains(Object elem) Tests if the specified object is a component in this vector. sti} boolean containsAll{ Collection c) Returns tue if this vector contains all of the elements in the specified Collection. AL | void copyInta(Object[] anArray} Copies the components of this vector into the specified array. = = = 12 | Object elementAtfint index) Returns the component at the specified index. 13 | Enumeration elements() Returns an enumeration of the components of this vector. 14 | void ensureCapacity(int minCapacity) Increases the capacity of this vector, if necessary, to ensure that it-can hold at least the number of components specified by the minimum. | int indexOf(Object elem) Searches for the first occurence of the given argument, testing int hashCode() Returns the hash code value for this vector. for equality using the equals method. “i int indexON Object elem, int index) Searches for the first occurence of the give argument, begioning the search at index, and testing for equality using the equals method. _ ‘void insertElementAt(Object obj, int index) Inserts the specified object as a component in this vector at the specified i ———— boolean isEmpty() Tests if this vector has no components. ‘Object LasiElement() Retums the last component of the vector. SKI-OOF (AVA) Bagi fadegentoes 26 | Object remove(int Index) Removes the element at the specified position in this vector, ) 27 | boolean remove(Object o) Removes the first occurrence of the specified element in this |_| vector, If the vector does net contain the element, itis unchanged, 28 | boolean remaveAll(Callection c) Removes is vector all of its elements that are __| contained in the specified Collection, = ee 29 | vold remaveAllElements() Removes all components from this vector and sets its size to |_| zero. 30 | boolean emans eee ache! obj) Removes the first (lowest-in ___| the argument eee tl er 32 | protected vold removeRange(int fromsindex, Int tolndex) Removes from this List all of |__| the clements whose index is between fromladex, e and tolndex, exc! 33° | boolean retainAll(Collection ¢) Retains only the elements in this vector that are contained | ___| inthe specified Collection, 34) Object set(int Index, Object clement) Replaces the clement at the specified position in Ahis vector with the spevificd element, 3S | vold sctElementAttObject obj, int Index) Sets the component at the specified index of | L this vector to be the specified object, 36 | vold setSize(int newSize) Sets the size af this vector, ) 37 | int size() Returns the umber of components in this vector, — = 38 | List subListtint fromindex, Int tolndex) between fromfndes, inclusive, and tolnd: 39 | Object{] toArray() Returns an array conta tf correct onder. 40 / Object toArray(Object{] w) Rewrns an array containing all ofthe elements inthis vector nce; the nuntime type of the 41) String toString() Returns a string representation of this vector, containing the String I representation of each element. 42 | vold trimToSize() Trims the capacity of this vector to be the vector’s current size. @ StringBuffer Class: Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except itis mutable i, t can be changed. Note: Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So itis safe and will result in an order, StringBuffer Constructors: |__ StringBuffer) Creates an emply string buffer with the initial capacity of 16. |__ StringBuffertStrin; ates u string buffer with the specified string, StringBuffertint capacity) | Creates an empty string buffer with the specified capacity as RIZVIECOLLEGE OF ENGINEERING | Departnent of Computer Engineering Page length. SKL-OOP (IAVA) | public synchronized StringBuffer public synchronized StringBuffer public | synchronized | StringBuffer public synchronized StringBuffer public synchronized StringBuffer | public int public void publie char public int public String " public String > Methods append(String s) Course Code: CSL304 is used to append the specified string with this string. The append() method is overloaded like append{char), append(boolean). append (int), append (float). __append{(double) etc. insertfint offset, String 8) replace(int startIndex, int endindex, String str) delete(int startindex, int endindex) reverse() capacity() ensureCapacity(int thininumCapacity) charAuint index) length() substring(int beginIndex) substring(int beginIndex, int endIndex) is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), inseri(int, boolean), inseri(int, int) inseri(int, float), inserint, double) etc. is used to replace the string from specified startindex and endindex. is used to delete the string from specified startindex and endindex. is used to reverse the string. is used to return the current cupacity, is used to ensure the capacity at least equal to the given minimum, is used to return the character at the specified position | is used to return the length of the string ie. total number of characters, : | is used te return the substring from the specified beginladex is used to return the substring from the specified beginlndex and endindex. RIZV1 COLLEGE OF ENGINEERING | Department of Computer Engincerimg i Algorithm 01; Algorithm 02: RIZVI COLLEGE, Step 1: Sturt Step 2; Create a class Shoppingliem. Step 3 Define vector constructors. Step 4: Add clements from command line into vector. Step 5: Copy vector element into String. Step 6: Display content of string. Step 7: Stop, Step 1: Sun Step 2: Create a clans Palindrome, Step 3 Define StringBuffer class constructors, Step 4: Read String. ‘onvent StringBuffer data into String. step 7: Compare and print result. Step B: Stop, F ENGINEERING | Department of Computer Engineering s ee Append string 10 StringBuffer object and reverse it. Programn-01 #* Write 1 program to implement a Vector that accepts five items from the commant! line and store them ina Vector and display the objects stored ina Vector. "¢ Program-O2: (* write program in javit to check string is palindrome or not using string buffer object */ veil LE weet aaa) Sas eae) ch ea str1.append(str) stri.reverse() Seis Enter a String :- Not a Palindrome Post-lab Question: 1. Write a program to create vector objects with student name. Program should perform following operations based an choice: i) Add student name Gi) Remove student name Display—display content of Vector using enumenition interface. 2. ‘Write a program that accepts a shopping fist of items and perform the following operations: Add an item at specified location, delete an item in the list, and print the content of vector, 3. Write program for delete micthod of StringBuffer class 4. Write a program for eeplace method of StringBuffer class. STE EV UCaeae! Der ertre aril Dery Peper heseac U1ey esta) v= NV isersat ca a ‘ ENT) printtn( ) Sas Ben elie Sis ) NaS Pietra

You might also like