Lab Manual: "Programming in Java Lab"
Lab Manual: "Programming in Java Lab"
Lab Manual: "Programming in Java Lab"
For
V Semester BCA
3 Write a program to sort the elements in ascending and descending order and show
the exception handling.
7 Write a program to create student report using applet, read the input using text
boxes and display the o/p using buttons.
PART-A PROGRAMS
Source Code
num[i]=Integer.parseInt(args[i]);
for(int i=0;i<args.length;i++)
{
int fact=1;
for(int j=1;j<=num[i];j++)
fact *=j;
System.out.println("the factorial of"+args[i]+" is : " +fact);
}
}
}
Output:
The factorial of 6 is:120
Source Code
class Prime
{
public static void main(String args[])
int n=i%j;
if(n==0)
{
break;
}
}
if(i==j)
{
System.out.println(" "+i);
}
}
}
Output:
Prime number between1and10 are:
2
3
5
Source code:
class Sorting
{
try
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(args[i]);
System.out.println("Before Sorting\n");
for(int i=0;i<5;i++)
bubbleSort(a,5);
for(int i=0;i<5;i++)
System.out.print(" "+a[i]);
System.out.print(" "+a[i]);
catch(NumberFormatException e)
catch(ArrayIndexOutOfBoundsException e)
int temp,i,j;
for(i=0;i<length-1;i++)
for(j=0;j<length-1-i;j++)
if(arr[j]>arr[j+1])
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
Output:
Before Sorting 45 6 32 5 64
After Sorting
Ascending order 5 6 32 45 64
Descending order 64 45 32 6 5
Source code:
public class string1 {
public static void main(String args[])
{
String s1="java";
String s2="programmig";
System.out.println("thr string sre "+s1+"and"+s2);
int len1=s1.length();
int len2=s2.length();
System.out.println("length of "+s1+"is = "+len1);
System.out.println("length of "+s2+"is = "+len2);
System.out.println("the concation of 2 strings "+s1.concat(s2));
System.out.println("first char of "+s1+" is ="+s1.charAt(0));
System.out.println("second char of "+s2+" is ="+s2.charAt(0));
System.out.println("string first "+s1+" is ="+s1.toUpperCase());
System.out.println("second string of "+s2+" is ="+s2.toLowerCase());
System.out.println(" v occurs at position "+s1.indexOf("v")+" in "+s1);
System.out.println("substrig of"+s2+"starting from index 3 and ending at 7 is =
"+s2.substring(3,7));
System.out.println("replacing 'v' with 'z' in "+s1+" is = "+s1.replace('v','z'));
boolean check=s1.equals(s2);
if(check==false)
}
else
{
System.out.println(" "+s1+" and " +s2+" are same");
}
}
}
Output:
The string are java and programming
Length of java is = 4
Length of programming is = 10
The concatenation of 2 strings javaprogramming
First char of java is =j
Second char of programming is =p
String first java is =JAVA
Second string of programming is =programming
v occurs at position 2 in java
substring of programming starting from index 3 and ending at 7 is = gram
replacing 'v' with 'z' in java is = jaza
java and programming are not same
Source code:
import java.io.*;
class Area
{
public static double circleArea(double r)
{
return Math.PI*r*r;
}
return input;
}
}
Output:
Enter the radius: 5
Area of Circle =78.53981633974483
Enter the side:3
Area of square=9.0
Enter the width:4
Enter the height:8
Area of rectangle:32.0
Enter the base:2
Enter the Heigh9t:
Area of Triangle:9.0
Box(int l, int b)
{
length=l;
breadth=b;
height=2;
System.out.println("Initialized with parameterized constructor having 2 params");
}
}
Output:
Initialized with default constructor
Volume of cube1 is:8
Initialized with parameterized construcror having 2 params
Volume of cube2 is:400
Initialized with parameterized construcror having 3 params
Volume of cube3 is:6000
7. Write a program to create student report using applet, read the input using
text boxes and display the o/p using buttons.
Source code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
TextField txtRegno,txtCourse,txtSemester,txtSub1,txtSub2;
Button cmdReport;
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblRegno,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtRegno,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblCourse,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtCourse,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblSemester,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtSemester,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblSub1,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtSub1,gbc);
gbc.anchor=GridBagConstraints.CENTER;
gbag.setConstraints(cmdReport,gbc);
add(lblTitle);
add(lblRegno);
add(txtRegno);
add(lblCourse);
add(txtCourse);
add(lblSemester);
add(txtSemester);
add(lblSub1);
add(txtSub1);
add(lblSub2);
add(txtSub2);
add(cmdReport);
cmdReport.addActionListener(this);
}
rno=txtRegno.getText().trim();
course=txtCourse.getText().trim();
sem=txtSemester.getText().trim();
sub1=txtSub1.getText().trim();
sub2=txtSub2.getText().trim();
avg="Avg Marks:" + ((Integer.parseInt(sub1) + Integer.parseInt(sub2))/2);
heading="Student Report";
removeAll();
showStatus("");
repaint();
}
}catch(NumberFormatException e)
{
showStatus("Invalid Data");
}
}
public void paint(Graphics g)
{
g.drawString(heading,30,30);
g.drawString(rno,30,80);
g.drawString(course,30,100);
g.drawString(sem,30,120);
g.drawString(sub1,30,140);
g.drawString(sub2,30,160);
g.drawString(avg,30,180);
}
}
Output:
Source code
abstract class Department
{
double salary,bonus,totalsalary;
public abstract void calBonus(double salary);
}
}
System.out.println("--------------------------------------------------------------");
acc.displayTotalSalary("Accounts Dept");
sales.displayTotalSalary("Sales Dept");
System.out.println("---------------------------------------------------------------");
}
}
Output:
Department BasicSalary Bonus TotalSalary
Source code :
import java.awt.*;
import java.applet.*;
/* <applet code="MovingBall.class" height=300 width=300></applet> */
public class MovingBall extends Applet implements Runnable
{
int x,y,dx,dy,w,h;
Thread t;
boolean flag;
public void init()
{
w=getWidth();
h=getHeight();
setBackground(Color.yellow);
x=100;
y=10;
import java.awt.event.*;
import java.applet.*;
addKeyListener(this);
requestFocus();
repaint(0);
showStatus("KEY PRESSED");
showStatus("KEY RELEASED");
g.drawString(str,15,15);
Output:
Source code:
import java.util.Scanner;
public class AddTwoNumbers2 {
sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
Output:
Enter First Number:
121
Enter Second Number:
19
Sum of these numbers: 140
Output:
contpara(int a,int b)
Output:
Output:
This is Class B1
This is Class B2
Source code:
import java.util.Vector;
Output:
Output:
Source code:
System.out.println("Thread is running");
t.start();
Output:
Thread is running
import java.awt.*;
import java.applet.*;
g.setColor(Color.red);
Output:
Source code:
import java.awt.*;
import java.applet.*;
Output:
Source code:
import java.awt.Graphics;
public class Barchart extends java.applet.Applet {
public void paint(Graphics g){
String year[]={"2006", "2007", "2008", "2009", "2010"};
int amount[]={120,140,135,150,170};
for(int i=0;i<5;i++){
g.drawString(year[i], 20, i*50+30);
g.fillRect(50, i*50+10, amount[i], 40);