OBJECT ORIENTED PROGRAMMING CIS 3100

S1indexofx stringbuffer class stringbuffer is a peer

This preview shows 44 out of 47 pages.

s1.indexOf(‘x’) StringBuffer class StringBuffer is a peer class of String. While String creates strings of fixed-length, StringBuffer creates strings of flexible length that can be modified in terms of both length and content. We can insert characters and substrings in the middle of a string, or append another string to the end. Some most commonly used stringbuffer methods are, s1.setCharAt(n,’x’) s1.append(s2) s1.insert(n,s2) s1.setLength(n) 22. Explain about Thread. Thread is similar to a program that has a single flow of control. It has a beginning , a body, and an end and executes commands sequentially.All main programs can be called as a single-threaded programs.the states are, Newborn state Runnable state Running state Blocked state Dead state class sumthread implements Runnable { int i,sum=0; public void run() { for(i=1;i<=5;i++) { sum+=i; System.out.println("Sum of numbers from 1 to "+i+"="+sum); if(i==5) Thread.yield(); } } } class factthread implements Runnable { int i,n,fact=i; public void run() { for(i=1;i<=5;i++) { fact*=i; System.out.println("factorial of"+i+fact); } } }
Image of page 44

Subscribe to view the full document.

class twothread { public static void main(String args[]) { Thread ct=Thread.currentThread(); System.out.println("\n the main thread is:"+ct.getName()); sumthread st=new sumthread(); factthread ft=new factthread(); Thread sumt=new Thread(st, "sum thread"); Thread factt=new Thread(ft,"factorial thread"); sumt.start(); System.out.println("\n The thread created is :"+sumt.getName()); factt.start(); System.out.println("\n The thread created is :"+factt.getName()); } } 23. Explain about Exception handling in Java? (April/May 2011) (Nov/Dec 2011) An exception is a condition that is caused by a runtime error in the program. When java interpreter encounters an error such as dividing b zero, it creates an exception object and throws it. Syntax: …….. Try { Statements; } Catch( Exception-type e) { Statement; } …….. …….. Example program to read and write characters: class clineinput { Public static void main(string args[]) { Int invalid = 0; Int number,count = 0; For(int i=0;i<args.length;i++) { Try { Number = integer.parseInt(args[i]); }
Image of page 45
Catch(NumberFormatException e) { Invalid=invalid+1; System.out.println(“invalid number: “ +args[i]); Continue; } Count = count + 1; } System.out.println(‘valid numbers: = “ +count); System.out.println(“invalid numbers: = “+invalid); } } 24. Explain about Applet Lifecycle? (Nov/Dec 2011) An applet is a small java program that is primarily used in internet computing. The types of applets are, • Local applets • Remote applets The various states in an applet life cycle are, • Born or initialization state • Running state • Display state • Idle state • Dead or destroyed state 26 Explain about operators in Java? (Nov/Dec 2011) Java operators can be classified as unary, binary, or ternary—meaning taking one, two, or three arguments, respectively. A unary operator may appear before (prefix) its argument or after (postfix) its argument. A binary or ternary operator appears between its arguments.
Image of page 46

Subscribe to view the full document.

Image of page 47
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern