Answer CIA FINAL Java
Answer CIA FINAL Java
Ans:
import java.util.Scanner;
public class ca1 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("1.Reverse of number");
System.out.println("2.Sum of digits of number");
System.out.println("3.Prime number between 1 to 100");
System.out.println("Choose any option:");
int ch=s.nextInt();
switch (ch)
{
case 1:
int rem,rev=0;
System.out.println("Enter the number");
int n=s.nextInt();
while (n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
System.out.println("Reverse of number is:" +rev);
break;
case 2:
int rem1,sum=0;
System.out.println("Enter the number");
int n1=s.nextInt();
while (n1>0)
{
rem=n1%10;
sum=sum+rem;
n1=n1/10;
}
System.out.println("The sum of the digits of the number is:"+ sum);
break;
case 3:
int factor=0;
for (int j=1;j<=100;j++)
{
for (int i=1;i<=j;i++)
{
if (j%i==0)
factor++;
}
if (factor==2)
System.out.print(j+ " ");
factor=0;
}
break;
default:System.out.println("Invalid entry");
}
}
}
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
System.out.println("uppercase:"+str1.toUpperCase());
System.out.println("lowercase:"+str1.toLowerCase());
String s=" hello ";
System.out.println(s);
System.out.println(s.trim());
String s2="hello";
String s3="hemlo";
System.out.println(s2.compareTo(s3));
String replaceString=str1.replace('i','z');
System.out.println(replaceString);
break;
case 2: StringBuffer sb1 = new StringBuffer("Jain");
System.out.println("Concat using StringBuffer:");
sb1.append("University");
System.out.println("StringBuffer: " + sb1);
sb1.insert(1,"Java");
System.out.println("String after inserting:"+sb1);
sb1.delete(1,5);
System.out.println("string after deleting:"+sb1);
sb1.replace(1,3,"Java");
System.out.println(sb1);
sb1.reverse();
System.out.println(sb1);
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());
break;
}
}
}
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
3. Write a program to find the volume of the box and use the
constructors to initialize the different dimensions of the box
(Constructor Overloading).
Ans:
class Constructor
{
int width,height,length;
Constructor()
{
width=10;
height=10;
length=10;
}
Constructor(int w,int h,int l)
{
width=w;
height=h;
length=l;
}
Constructor(Constructor c)
{
width=c.width;
height=c.height;
length=c.length;
}
float volume()
{
return(width*height*length);
}
}
class Consoverload
{
public static void main(String args[])
{
Constructor c1=new Constructor();
Constructor c2=new Constructor(20,30,40);
Constructor c3=new Constructor(c1);
System.out.println(c1.volume());
System.out.println(c2.volume());
System.out.println(c3.volume());
}
}
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
t=new TextField(20);
add(t);
t1=new TextField(20);
add(t1);
t1.setEchoChar('*');
Button b = new Button("login");
add(b);
b.addActionListener(this);
}
String s1=t.getText();
String s2=t1.getText();
int i=1;
{
if(s1.equals("adlin") && s2.equals("adlin"))
{
s3="Login Successful";
t.setText("");
t1.setText("");
repaint();
}
else
{
i++;
t.setText("");
t1.setText("");}
if(i==3)
System.exit(0);
}
}
g.drawString(s3,500,400);
}
}
OUTPUT:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
_________________________________________________________
7. Write a program to create the class Course which contain static variable
course_name=”BCA” and instance variable subject1,subject2, subject3.
class College
{
static char [] course_name=”BCA”;
char [] subject1;
char [] subject2;
cahr [] subject3;
College(int a,int b,int c)
{
this.subject1=a;
this.subject2=b;
this.subject3=c;
}
void display(){
System.out.println(“College” +course_name);
System.out.println(“Subjects”+subject1+”,”+subject2+”,”+subject3);}
}
class Demo_Static{
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
10.Write a program to display the records from the table Person having field
id, name,dob.
import java.sql.*;
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
con.setAutoCommit(false);
st = con.createStatement();
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
11. Program to demonstrate the AWT Action Listener.
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
public AwtListenerDemo(){
prepareGUI();
}
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
okButton.addActionListener(new CustomActionListener());
panel.add(okButton);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
class Customer{
int amount=10000;
Synchronized void withdraw(int amount){
System.out.println("going to withdraw...");
if(this.amount<amount){
System.out.println("Less balance; waiting for deposit...");
try{wait();}catch(Exception e){}
}
this.amount-=amount;
System.out.println("withdraw completed...");
}
class Test
{
public static void main(String args[]){
final Customer c=new Customer();
new Thread(){
public void run(){
c.withdraw(15000);
}
}.start();
new Thread()
{
public void run(){c.deposit(10000);}
}.start();
}
}
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
12. Write a program to perform multi-threading operation.
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________
13. WAP to demonstrate the exceptions in a menu driven format. The program must have
ArithmeticException, NullPointerException, StringIndexOutOfBoundsException,
FileNotFoundException, ArrayIndexOutOfBoundsException, NumberFormatException and a
finally block.
package firstclass;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
System.out.println(num);
} catch(NumberFormatException e) {
System.out.println("Number format exception");
}
try{
int a[] = new int[5];
a[6] = 9; // accessing 7th element in an array of
// size 5
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out Of Bounds");
}
finally{System.out.println("finally block is always executed");}
}
OUTPUT:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_____________________