Lab Java
Lab Java
PART - B
1.Java Program to Check If a Given Number is ArmStrong Number
2.Write a program to check whether a given character is a vowel or consonant.
3.Java Program to Find the GCD and LCM of two Numbers.
4.Write a program for generating Fibonacci Sequence.
5.How to Copy a File to another File in Java.
6.Write a program in Java for Basic Vector Operations.
7.Write a program in Java to find area of geometrical figures using method overloading
8. Write a program in Java for displaying Rhombus pattern
9.Write a program in JAVA to create a thread.
10.Write a program to play sound using Applet in JAVA
Part – A
//Part A Program 1:WAP in Java for finding factorial of command line arguments.
class FactDemo
{
public static void main(String args[])
Output : java FactDemo 2 3 4
{
Factorial is 2 is 2
int n;
Factorial is 3 is 6
if(args.length==0)
Factorial is 4 is 24
System.out.println("no command line arguments");
for(int i=0;i<args.length;i++)
{
n=Integer.parseInt(args[i]);
int f=1;
for(int j=n;j>=1;j--)
{
f=f*j;
}
System.out.println("Factorial of " + n + "is "+f);
}
}
}
try
{
System.out.println("enter the no of elements");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
a=new int[6];
System.out.println("enter the elements");
for(i=0;i<n;i++)
a[i]=sc.nextInt();
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
System.out.println("the sorted array in ascending order");
for(i=0;i<n;i++)
System.out.print(a[i] + " ");
System.out.println("\nthe sorted array in descending order");
for(i=n-1;i>=0;i--)
System.out.print(a[i] + " ");
}
//try block end
catch(ArrayIndexOutOfBoundsException e)
{ Output : Enter the no of elements
System.out.println("Check no of elements "); 5
} enter the elements
catch(InputMismatchException e) 4
{ 5
System.out.println("Check the input "); 9
} 1
} 2
} the sorted array in ascending order
12459
the sorted array in descending order
95421
//Part A : Program 4 WAP for all string operations.
import java.util.Scanner;
class StringDemo
{
public static void main(String args[])
{
String str,str1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
str=sc.nextLine();
System.out.println("upper="+str.toUpperCase());
System.out.println("original string"+ str);// strings are immutable
System.out.println("Comparison with string dscasc ="+str.compareTo("dscasc"));
System.out.println("lastindexOf('c')="+str.lastIndexOf('c'));
String str2="Apple";
System.out.println("Apple-indexOf(A)="+str2.indexOf('A'));
S0ystem.out.println("replace="+str.replace('c','d'));
System.out.println(str);
str1=str.concat(" hello");
System.out.println("after Concat"+str1);
str1="hello bca ";
System.out.println("After trim()"+str1.trim());
char ch[];
ch=str1.toCharArray();
for (int i=0;i<ch.length;i++)
System.out.print(ch[i]);
}
}
Output :
Enter a string
HelloDsi
upper=HELLODSI
original stringHelloDsi
Comparison with string dscsac =-28
lastindexOf()=-1
Apple- indexOf(A)=0
replace=HelloDsi
HelloDsi
HelloDsi hello
trim()hello bca
h
e
l
l
o
b
c
a
/*Part A: Prog 5:WAP in Java to find area of geometrical figures using method */
import java.util.Scanner; Output :
class Area enter the raidus of circle
{ 4
static double circle(double r) area of circle is48.0
{ enter the side of square
return(22/7*r*r); 4
} area of square16
static int rectangle (int l,int b) enter the l,b of rectangle
{ 4
return(l*b); 5
} area of a rectangle20
static int square (int s) enter the base and height of a triangel
{ 46
return(s*s); area of triangle is 12.0
}
static double triangle(double base,double height)
{
return(0.5*base*height);
}
int b=sc.nextInt();
System.out.println("area of a rectangle"+rectangle(l,b));
System.out.println("enter the base and height of a triangel");
double base=sc.nextDouble();
double height=sc.nextDouble();
System.out.println("area of triangle is "+triangle(base,height));
}
}
/* Part A program 6 Write a program to implement constructor overloading by passing
different number of parameter of different types.*/
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
class OverloadCons {
public static void main(String args[]) {
Output :
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0
//Part A 7:Write a program to create student report
//using applet, read the input using text boxes and display the o/p using buttons.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="StudReport" width=300 height=300>
</applet>*/
public class StudReport extends Applet implements ActionListener
{
Label lblTitle,lblRegNo,lblName,lblJava,lblSE,lblCA,lblBI,lblSSPD;
TextField txtRegNo,txtName,txtJava,txtSE,txtCA,txtBI,txtSSPD;
Button cmdReport;
int total;
float avg;
public void init()
{
setLayout(null);
lblTitle=new Label("Enter Student’s Details");
lblRegNo=new Label("Reg. No:");
lblName=new Label("Name:");
lblJava=new Label("Java:");
lblSE=new Label("SE:");
lblCA=new Label("CA:");
lblBI=new Label("BI:");
lblSSPD=new Label("SSPD:");
txtRegNo=new TextField(10);
txtName=new TextField(25);
txtJava=new TextField(3);
txtSE=new TextField(3);
txtCA=new TextField(3);
txtBI=new TextField(3);
txtSSPD=new TextField(3);
cmdReport=new Button("View Student Result");
lblTitle.setBounds(100,0,200,20);
lblRegNo.setBounds(0,50,100,20);
txtRegNo.setBounds(120,50,100,20);
lblName.setBounds(0,75,100,20);
txtName.setBounds(120,75,250,20);
lblJava.setBounds(0,100,100,20);
txtJava.setBounds(120,100,40,20);
lblSE.setBounds(0,125,100,20);
txtSE.setBounds(120,125,40,20);
lblCA.setBounds(0,150,100,20);
txtCA.setBounds(120,150,40,20);
lblBI.setBounds(0,175,100,20);
txtBI.setBounds(120,175,40,20);
lblSSPD.setBounds(0,200,100,20);
txtSSPD.setBounds(120,200,40,20);
cmdReport.setBounds(100,225,150,30);
add(lblTitle);
add(lblRegNo);add(txtRegNo);
add(lblName);add(txtName);
add(lblJava);add(txtJava);
add(lblSE);add(txtSE);
add(lblCA);add(txtCA);
add(lblBI);add(txtBI);
add(lblSSPD);add(txtSSPD);
add(cmdReport);
cmdReport.addActionListener(this);//registering the event with te event handler
(ActionListener)
}
public void actionPerformed(ActionEvent ae)
{
try
{
int java=Integer.parseInt(txtJava.getText());
int se=Integer.parseInt(txtSE.getText());
int ca=Integer.parseInt(txtCA.getText());
int bi=Integer.parseInt(txtBI.getText());
int sspd=Integer.parseInt(txtSSPD.getText());
total=(java+se+ca+bi+sspd);
avg=total/5;
}
catch(NumberFormatException e)
{
}
repaint();
}
public void paint(Graphics g)
{
g.drawString("STUDENT REPORT",100,275);
g.drawString("Reg. No.: "+txtRegNo.getText(),0,300);
g.drawString("Name : "+txtName.getText(),0,325);
g.drawString("Java: "+txtJava.getText(),0,350);
g.drawString("Software Engineering : "+txtSE.getText(),0,375);
g.drawString("Computer Architecture : "+txtCA.getText(),0,400);
g.drawString("Banking & Insurance : "+txtBI.getText(),0,425);
g.drawString("SSPD : "+txtSSPD.getText(),0,450);
g.drawString("Total: "+total,0,475);
g.drawString("Average: "+avg,0,500);
}
}
/*Part A Prog 8: WAP in Java to calculate and
display bonus of sales and accounts department using method overriding*/
abstract class Department
{
double salary,bonus,totalsalary;
public abstract void calBonus(double salary);
public void displaytotalsalary(String dept)
{
System.out.println(dept+"\t"+salary+"\t\t"+bonus+"\t"+totalsalary);
}
}
class Accounts extends Department
{
public void calBonus(double sal)
{
salary=sal;
bonus=sal*0.2;
totalsalary=salary+bonus;
}
}
class sales extends Department
{
public void calBonus(double sal)
{
salary=sal;
bonus=sal*0.3;
totalsalary=salary+bonus;
}
}
Output :
Department BasicSalary Bonus TotalSalary
-----------------------------------------------------
Accounts 10000.0 2000.0 12000.0
sales Dept 20000.0 6000.0 26000.0
-----------------------------------------------------
/*Part A-9 WAP to implement thread,applet and graphics by
implementing animation of moving ball */
import java.applet.*;
import java.awt.*;
import java.util.Scanner;
public class ArmStrong
{
public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer you want to check:");
n = s.nextInt();
a = n;
c = n;
while(a > 0)
{
a = a / 10;
count++;
}
while(n > 0)
{
b = n % 10;
sum = (int) (sum+Math.pow(b, count));
n = n / 10;
}
if(sum == c)
{
System.out.println("Given number is Armstrong");
}
else
{
System.out.println("Given number is not Armstrong");
}
}
}
Output:
$ javac ArmStrong.java
$ java ArmStrong
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
static int lcm(int x, int y)
{
int a;
a = (x > y) ? x : y; // a is greater number
while(true)
{
if(a % x == 0 && a % y == 0)
return a;
++a;
}
}
/*
Output:
$ javac GCD_LCM.java
$ java GCD_LCM
Output:
Javac Fibonacci.java
Java Fibonacci
Enter Limit:
5
01123
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyExample
{
public static void main(String[] args)
{
FileInputStream instream = null;
FileOutputStream outstream = null;
try{
File infile =new File("C:\\MyInputFile.txt");
File outfile =new File("C:\\MyOutputFile.txt");
int length;
instream.close();
outstream.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
/*
Output:
File copied successfully!!
*/
}
}
/*
10.WAP to play sound using Applet in JAVA
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
Output :