Вы находитесь на странице: 1из 8

Certificate

Assignment No.: 3 Course Code: CAP310

Course Instructor: Mr. Balraj kumar Course Tutor:

Date of Allotment: 15/3/11 Date of Submission: 21/03/11

Student’s Roll No: RTB903A04 Section No.: TB903

Declaration:

I declare that this assignment is my individual work. I have not copied from any other
student’s work or from any other source except where due to acknowledgement is made
explicitly in the text, nor has any part been written for me by another person.

Student’s Signature: Savi

Evaluator’s Comments:

__________________________________________________________________

Marks Obtained: out of______________________________________


Part A

Ans:-1)Demonstrate the concept of wrapper classes by implementing a program to


calculate the percentage of marks in five courses of an MCA student.
Ans: -
import java.util.Scanner;
class percentage
{
public static void main(String args[])
{
Float MARK1= new Float(0.0);
Float MARK2= new Float(0.0);
Float MARK3= new Float(0.0);
Float MARK4= new Float(0.0);
Float MARK5= new Float(0.0);
String c1,c2,c3,c4,c5,c6;
float mark1,mark2,mark3,mark4,mark5;
Scanner input = new Scanner(System.in);
System.out.println("Enter the marks of java\n");
c1= input.next();
System.out.println("Enter the marks of system software\n");
c2= input.next();
System.out.println("Enter the marks of computer graphics\n");
c3= input.next();
System.out.println("Enter the marks of software testing\n");
c4= input.next();
System.out.println("Enter the marks of real time\n");
c5= input.next();
MARK1= Float.valueOf(c1);
MARK2= Float.valueOf(c2);
MARK3= Float.valueOf(c3);
MARK4= Float.valueOf(c4);
MARK5= Float.valueOf(c5);
mark1= MARK1.floatValue();
mark2= MARK2.floatValue();
mark3= MARK3.floatValue();
mark4= MARK4.floatValue();
mark5= MARK5.floatValue();
float s=0.0f,a;
s= mark1+mark2+mark3+mark4+mark5;
a= s/5;
System.out.println("Average of marks =" +a);
}
}

Ans:-2)Write a program that inputs a sentence at runtime and prints the number of
occurrences of each vowel in it. Further the program must also print the word by word
reverse of the sentence.
Example: If the input string is “What is True in World”, then the output would
be as follows:
VOWEL COUNT
A 1
E 1
I 2
O 1
U 1
The word-wise Reverse is: TAHW SI EURT NI DLROW
Ans: -

Ques:3)Program to construct a vector that contains following values 72.56, “Lovely”,


555, true and prints each of the values on a separate line. Satisfy the constraint that
none of the above mentioned values shall be taken as String.
Ans: -
import java.util.Vector;
class vectr
{
public static void main(String args[])
{
Vector v = new Vector();
Float F= new Float(72.56);
Integer I= new Integer(555);
Int i;
StringBuffer s= new StringBuffer("Lovely");
Boolean b = new Boolean(true);
Int n=args.length;
For(i=0;i<n;i++)
V.addElement(args[i]);
V.addElement(s,1);
V.addElement(b,3);
V.addElement(F,0);
V.addElement(I,2);
int n= v.size();
for(int i=0;i<n;i++)
System.out.println(“ “+v.elementAt(i));
}
}

Part B
Ques:-4)Write a program that uses a recursive method odd() to print first n odd
numbers on screen. Each number is displayed on screen at an interval of 1 second.
Ans: -
import java.util.Scanner;
import java.lang.Thread;
class recursion extends Thread
{
Scanner input = new Scanner(System.in);
int n,i;
System.out.println("\nEnter the value of n:");
n= input.nextInt();
System.out.println("Odd numbers are:");
oddnum(n);
public void oddnum(int n)
{
if(n==0)
System.out.println(0);
else
{
if(n%2!=0)
System.out.println(n);
odd(n-1);

}
try
{
sleep(1000);
}
catch(Exception e)
{
}
}
}
Class XYZ
{
Public static void main(String args[])
{
Recursion r= new recursion();
r.start();
}
}

Ques:-5)Develop a program that uses two threads called EvenThread and OddThread.
EvenThread prints the first 10 even numbers whereas OddThread displays the first 10
odd numbers.

Ans: -
import java.lang.Thread;
class EvenThread extends Thread
{
public void show()
{
System.out.println(“Even thread started..”);
}
Public void run()
{
Show();
for(int i=1;i<=10;i++)
{
If(i%2==0)
System.out.println(“Even no.=”+i);
}
}
}
class OddThread implements Runnable
{
public void run()
{
for(j=1;j<=10;j++)
{
if(j%2!=0)
{
System.out.println(“Odd thread started..”);
System.out.println("Odd number= "+j);
}
}
}
}
class EvenOddThread
{
public static void main(String args[])
{
EvenThread eth= new EvenThread();
eth.start();
OddThread oth = new OddThread();
Thread th=new Thread(oth)
th.start();
}
}

Ques:-6)Write a user-defined exception class named MissingArgumentsException that


can be used to respond to the when command-line arguments are required but not
defined by the user during execution of program. Also, demonstrate the invocation of
this class/instance with the help of the suitable main() program.
Ans: -
import java.lang.Exception;
class MissingArgumentException extends Exception
{
MissingArgumentException(String msg)
{
super(msg);
}
}
class ArguException
{
public static void main(String args[])
{
int n;
n=args.length;
try
{
if(n==0)

throw new MissingArgumentException("No arguments are entered");


}
System.out.println(“Number of arguments=”+n);

While(i<n)
{
System.out.print(args[i]+"\t");
i++;
}
}

catch(MissingArgumentException e)
{
System.out.println(“Output is:”);
System.out.println(e.getMessage());
}

Finally
{
System.out.println(“Closing of the programme”);
}
}
}