Java Lab Assignment
Java Lab Assignment
Java Lab Assignment
STUDENT ID – 161001102037
STREAM – iMCA2
SEMESTER – 8TH
SUBJECT – OOP IN JAVA LAB ASSIGNMENT
Output:
import java.util.*;
class Pattern1{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n,i,j,k;
System.out.println("Enter a number:");
n=s.nextInt();
for(i=0;i<n;i++){
for(k=30-i;k>n;k--)
System.out.print(" ");
for(j=0;j<=2*i;j++)
System.out.print("*");
System.out.println(" ");
}
}
}
Output:
Output:
3. Write a java program to calculate area of a box by using dot
operator.
class Area1{
public double length, width;
public static void main(String args[]){
Area1 a=new Area1();
Area1 b=new Area1();
a.length=10.2;
b.length=19;
a.width=31.3;
b.width=24.5;
double a1=a.length*a.width;
double a2=b.length*b.width;
System.out.println("Area of box a is="+a1);
System.out.println("Area of box b is="+a2);
}
}
Output:
double len,br;
Box(double x,double y)
len=x;
br=y;
Box(double x){
len=x;
br=x;
void display()
double area;
area=len*br;
class RoomTest{
a.display();
b.display();
Output:
6. Write a java program to store and display information about
students by using scanner class.
import java.util.*;
class Information{
String name;
int roll_no,phy,math,eng,ben,che;
name=s.nextLine();
roll_no=s.nextInt();
phy=s.nextInt();
math=s.nextInt();
eng=s.nextInt();
ben=s.nextInt();
che=s.nextInt();
double total=phy+math+eng+ben+che;
double p=total/500*100;
System.out.println("Student
name="+name+"\troll_no="+roll_no+"\tTotal="+total+"\tPercentage="+p);
}
}
Output:
int i;
for(i=0;i<x.length;i++){
Output:
8. Write a java program to perform addition of two numbers by
using scanner class.
import java.util.*;
class Add{
int a=s.nextInt();
int b=s.nextInt();
int result=a+b;
System.out.println("Addition="+result);
Output:
Output:
Output:
Output:
Output:
Output:
Output:
Output:
20. Write a java program to implement multiple try catch.
import java.util.*;
class Excep2{
public static void main(String args[]){
try{
Scanner s=new Scanner(System.in);
int a,b,c,ar;
int arr[]={6,8,9};
System.out.println("Enter a,b:");
a=s.nextInt();
b=s.nextInt();
c=a/b;
System.out.println("division is "+c);
ar=arr[0]+arr[5];
}
catch(ArithmeticException ex){
System.out.println("Arithmetic Exception :"+ex.getMessage());
}
catch(NumberFormatException ex){
System.out.println("Format Exception :"+ex.getMessage());
}
catch(Exception ex){
System.out.println("Exception :"+ex);
}
}
}
Output:
Output:
Output:
Output:
25. Write a java program to implement sorting of 10 integer
numbers.
import java.util.*;
class BubbleSort{
void cal(int a[],int n){
int temp=0;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void display(int a[],int n){
System.out.println();
System.out.println("After sorting the results will be:");
System.out.println();
for(int i=0;i<n;i++){
System.out.print("\t"+a[i]);
}
}
}
class Sort2{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int a[],n;
System.out.println("Enter how many array elements will be present:");
n=s.nextInt();
a=new int [n];
System.out.println("Enter "+n+" array elements:");
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
System.out.println("Before sorting the array elements are:");
System.out.println();
for(int i=0;i<n;i++){
System.out.print("\t"+a[i]);
}
BubbleSort ob=new BubbleSort();
ob.cal(a,n);
ob.display(a,n);
}
}
Output:
26. Write a java program to display string by using applet.
import java.awt.*;
import java.applet.*;
public class AppletHelloJava extends Applet
{
public void init()
{
setBackground(Color.YELLOW);
setForeground(Color.RED);
}
public void paint (Graphics g)
{
g.drawString("Hello java", 20, 30);
}
}
Compile:-
Javac AppletHelloJava.java
Run:-
Java AppletHelloJava
frame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
Output
29) Write a java program to create child thread from main thread.
public class Test extends Thread
{
public static void main(String[] args)
{
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t.getName());
t.setName("Geeks");
System.out.println("After name change: " + t.getName());
System.out.println("Main thread priority: "+ t.getPriority());
t.setPriority(MAX_PRIORITY);
System.out.println("Main thread new priority: "+ t.getPriority());
for (int i = 0; i < 5; i++)
{
System.out.println("Main thread");
}
ChildThread ct = new ChildThread();
System.out.println("Child thread priority: "+ ct.getPriority());
ct.setPriority(MIN_PRIORITY);
System.out.println("Child thread new priority: "+ ct.getPriority());
ct.start();
}
}
class ChildThread extends Thread
{
@Override
public void run()
{
for (int i = 0; i < 5; i++)
{
System.out.println("Child thread");
}
}
}
Output:
Current thread: main
After name change: Geeks
Main thread priority: 5
Main thread new priority: 10
Main thread
Main thread
Main thread
Main thread
Main thread
Child thread priority: 10
Child thread new priority: 1
Child thread
Child thread
Child thread
Child thread
Child thread
OUTPUT:-
thread is running………