0% found this document useful (0 votes)
190 views41 pages

Java Lab Assignment

The document contains details about an assignment on OOP in Java lab for a student named Akash Chakraborty studying in the 8th semester of the iMCA2 stream with student ID 161001102037. It lists 18 questions to write Java programs related to patterns, calculators, areas, constructors, inheritance, interfaces, and abstract classes.

Uploaded by

Nkj242089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views41 pages

Java Lab Assignment

The document contains details about an assignment on OOP in Java lab for a student named Akash Chakraborty studying in the 8th semester of the iMCA2 stream with student ID 161001102037. It lists 18 questions to write Java programs related to patterns, calculators, areas, constructors, inheritance, interfaces, and abstract classes.

Uploaded by

Nkj242089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

NAME – Akash chakraborty

STUDENT ID – 161001102037
STREAM – iMCA2
SEMESTER – 8TH
SUBJECT – OOP IN JAVA LAB ASSIGNMENT

1. Write a java program to draw different patterns.


import java.util.*;
class Pattern{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n,i,j;
System.out.println("Enter a number:");
n=s.nextInt();
for(i=0;i<n;i++){
for(j=0;j<=i;j++)
System.out.print("*");
System.out.println(" ");
}
}
}

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:

2. Write a java program to implement menu driven calculator.


import java.util.*;
class Menu{
int result;
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("Enter two numbers:");
int a=s.nextInt();
int b=s.nextInt();
System.out.println("For Addition press 1");
System.out.println("For Subtraction press 2");
System.out.println("For Multiplication press 3");
System.out.println("For Division press 4");
System.out.println("For Exit press 9");
System.out.println("Enter choice:");
int ch=s.nextInt();
int result;
switch(ch){
case 1:
result=a+b;
System.out.println("Result of addition="+result);
break;
case 2:
result=a-b;
System.out.println("Result of subtraction="+result);
break;
case 3:
result=a*b;
System.out.println("Result of multiplication="+result);
break;
case 4:
result=a/b;
System.out.println("Result of multiplication="+result);
break;
case 9:
System.out.println("Thanks for watching:");
System.exit(0);
break;
default:
System.out.println("Wrong choice...enter again");
break;
}
}
}

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:

4. Write a java program to calculate area of a box by using a


method.
class Area{
double l,w,area;
void cal(double x,double y){
l=x;
w=y;
area=l*w;
}
void display(){
System.out.println("Area of the box="+area);
}
public static void main(String args[]){
Area a=new Area();
Area b=new Area();
a.cal(4.7,5.7);
b.cal(3.7,8.4);
b.display();
a.display();
}
}
Output:

5. Write a java program to calculate area of a box by using


constructor.
class Box{

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;

System.out.println("Area of the box:"+area);

class RoomTest{

public static void main(String args[ ]){

Box a=new Box(10.5,12.2);

Box b=new Box(15.5);

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{

public static void main(String args[]){

String name;

int roll_no,phy,math,eng,ben,che;

Scanner s=new Scanner(System.in);

System.out.println("Enter the Student name:");

name=s.nextLine();

System.out.println("Enter the Student roll_no:");

roll_no=s.nextInt();

System.out.println("Enter the marks of 5 subject of the Student:");

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("Display the details of the student:");

System.out.println("Student
name="+name+"\troll_no="+roll_no+"\tTotal="+total+"\tPercentage="+p);

}
}

Output:

7. Write a java program to implement command line argument.


class Command{

public static void main(String x[]){

int i;

for(i=0;i<x.length;i++){

System.out.println("The elements of args["+i+"] ="+x[i]);

Output:
8. Write a java program to perform addition of two numbers by
using scanner class.
import java.util.*;

class Add{

public static void main(String args[]){

Scanner s=new Scanner(System.in);

System.out.println("Enter 1st number:");

int a=s.nextInt();

System.out.println("Enter 2nd number:");

int b=s.nextInt();

int result=a+b;

System.out.println("Addition="+result);

Output:

9. Write a java program to implement constructor overloading.


class Room{
double len,wid;
Room(){
len=0;
wid=0;
}
Room(double x,double y){
len=x;
wid=y;
}
Room(double x){
len=x;
wid=x;
}
void display(){
double area=len*wid;
System.out.println("Area of a Room="+area);
}
}
class Constructor{
public static void main(String args[]){
Room b=new Room();
Room c=new Room(3);
Room d=new Room(2.7,8.4);
b.display();
c.display();
d.display();
}
}
Output:

10. Write a java program to implement method overloading.


class Over{
void volumn(double r,double h){
double v=3.14*(r*r)*h;
System.out.println("Volumn of the cylinder is = "+v);
}
void volumn(double r,int h){
double v=(3.14*(r*r)*h)/3;
System.out.println("Volumn of the circular cone is = "+v);
}
void volumn(double r){
double v=(4*3.14*(r*r*r))/3;
System.out.println("Volumn of the sphere is = "+v);
}
int volumn(int r){
return(r*r*r);
}
}
class Overload{
public static void main(String args[]){
Over o=new Over();
o.volumn(1.2,4);
o.volumn(5.2,4.3);
o.volumn(2.1);
int v=o.volumn(3);
System.out.println("Volumn of the cube is = "+v);
}
}

Output:

11. Write a java program to implement method overriding.


class A{
int a=2;
void show(){
System.out.println("The results of a="+a);
}
}
class B extends A{
int b=3;
void show(){
System.out.println("The results of b="+b);
}
}
class Override{
public static void main(String args[]){
B ob=new B();
A oc=new B();
A od=new A();
ob.show();
oc.show();
od.show();
}
}

Output:

12. Write a java program to implement single inheritance.


class A{
int a=2;
void showA(){
System.out.println("a="+(a+2));
}
}
class B extends A{
int b=6;
void showB(){
System.out.println("b="+(b*2));
}
}
class Single{
public static void main(String args[]){
B ob=new B();
ob.showA();
ob.showB();
}
}

Output:

13. Write a java program to implement multilevel inheritance.


class X
{
public void methodX()
{
System.out.println("class X method");
}
}
class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
}
class Multilevel
{
public static void main(String args[])
{
Z obj = new Z();
obj.methodX();
obj.methodY();
obj.methodZ();
}
}
14. Write a java program to implement hierarchical inheritance.
class E{
int a,b;
void addE(int x,int y){
a=x;
b=y;
int s=a+b;
System.out.println("Sum ="+s);
}
}
class F extends E{
void showF(){
System.out.println("method of F class");
}
}
class G extends E{
void showG(){
System.out.println("method of G class");
}
}
class H extends E{
void showH(){
System.out.println("method of H class");
}
}
class Hierarchical{
public static void main(String args[]){
F f=new F();
G g=new G();
H h=new H();
f.showF();
f.addE(12,14);
g.showG();
g.addE(42,13);
h.showH();
h.addE(6,52);
}
}

Output:

15. Write a java program to implement multiple inheritance.


class Student
{
String name;
int roll;
Student(String n, int r)
{
name=n;
roll=r;
}
void show()
{
System.out.println("Name is: " +name);
System.out.println("Roll is: " +roll);
}
}
class Test extends Student
{
int part1,part2;
Test(String n, int r, int p1, int p2)
{
super(n, r);
part1=p1;
part2=p2;
}
void show()
{
super.show();
System.out.println("Marks of part1: " +part1);
System.out.println("Marks of part2: " +part2);
}
}
interface Sports
{
int score=10;
void showSports();
}
class Result extends Test implements Sports
{
int total;
Result (String n, int r, int p1, int p2)
{
super (n,r,p1,p2);
total = part1 + part2 + score;
}
public void showSports()
{
System.out.println("Score in Sports: " +score);
}
void show()
{
super.show();
showSports();
System.out.println("Total is: " +total);
}
}
class TestResult
{
public static void main(String args[])
{
Result r = new Result("Niru", 06, 73, 74);
r.show();
}
}

16. Write a java program to implement hybrid inheritance.


class C
{
public void disp()
{
System.out.println("C");
}
}
class A extends C
{
public void disp()
{
System.out.println("A");
}
}
class B extends C
{
public void disp()
{
System.out.println("B");
}
}
class D extends A
{
public void disp()
{
System.out.println("D");
}
}
class Hybrid
{
public static void main(String args[])
{
D obj = new D();
obj.disp();
}
}
OUTPUT:-

17. Write a java program to implement abstract class.


abstract class P{
int p;
int m;
public P(){
p=2;
m=6;
}
abstract void f();
void display(){
System.out.println(p);
System.out.println(m=m+2);
}
}
abstract class Q extends P{
abstract void f1();
}
class R extends Q{
int r;
public R(){r=3;}
void f(){
System.out.println("In class R "+r);
}
void f1(){
System.out.println("In class R from Q");
}
}
class TestAbs{
public static void main(String ar[]){
R or=new R();
or.f();
or.f1();
or.display();
}
}

Output:

18. Write a java program to implement interface.


interface I1{
public static final int x=5;
public static final int y=10;
public abstract void m();
}
interface I2 extends I1{
public static final int z=7;
public abstract void m1();
}
class A implements I2{
int a=4;
int c;
void get_a()
{
System.out.println(a);
}
public void m(){
c=x+y;
System.out.println(c);
System.out.println(y);
}
public void m1(){
System.out.println(z);
}
}
class TextB extends A{
public static void main(String args[]){
TextB ob=new TextB();
System.out.println("Output are..");
ob.get_a();
ob.m();
ob.m1();
System.out.println(x);
}
}

Output:

19. Write a java program to implement try catch.


class Excep1{
public static void main(String args[]){
try{
int n=Integer.parseInt(args[0]);
int n1=Integer.parseInt(args[1]);
int n2=n+n1;
System.out.println("addition="+n2);
}
catch(Exception ex){
System.out.println(ex);
}
}
}

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:

21. Write a java program to implement use of throws.


import java.io.*;
class Testthrows{
void m()throws IOException{
throw new IOException("Device error");
}
void n()throws IOException{
m();
}
void p(){
try{
n();}
catch(Exception e){
System.out.println("Exception handled");}
}
public static void main(String args[]){
Testthrows obj=new Testthrows();
obj.p();
System.out.println("Normal flow...");
}
}

Output:

22. Write a java program to implement user defined exception.


import java.lang.Exception;
class MyException extends Exception{
MyException(String message){
super(message);
}
}
class TestMyException{
public static void main(String args[]){
int x=5,y=1000;
try{
float z=(float) x/(float) y;
if(z<0.01){
throw new MyException("Number is too small");
}
}
catch(MyException e){
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
finally{
System.out.println("I am always here");
}
}
}

Output:

23. Write a java program to implement user defined package.


/*package treepackage;
public class tree
{
public void disp()
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j?)
{
System.out.print(? ?);
}
for(k=1;k<=i;k++)
{
System.out.print(?*?);
}
for(l=i-1;l>=1;l?)
{
System.out.print(?*?);
}
System.out.print(?\n?);
}}
public void root()
{
int k,i,j;
for( i=1;i<=5;i++)
-{
for( j=1;j<5;j++)
{
System.out.print(? ?);
}
for(k=1;k<=3;k++)
{
System.out.print(?*?);
}
System.out.println(? ?);
}
}
}//Save this package as tree.java
*/
import treepackage.tree;
class UserDefinedPackageDemo{
public static void main(String args[]){
UserDefinedPackageDemo obj=new UserDefinedPackageDemo();
obj.disp();
obj.disp();
obj.disp();
obj.disp();
obj.root();
}
}Sample Output
Output is:
*
***
*****
*******
*********
*
***
*****
*******
*********
*
***
*****
*******
*********
*
***
*****
*******
*********
***
***
***
***
***
24. Write a java program to implement different string class
method.
import java.io.*;
class TR2{
public static void main(String args[]){
try{
File f=new File("TestA.txt");
BufferedReader br=new BufferedReader(new FileReader(f));
String st=br.readLine();
System.out.println(st);
PrintWriter pw=new PrintWriter(new FileWriter(f,true));
pw.write("..Welcome to tamluk");
pw.close();
System.out.println("after write");
br=new BufferedReader(new FileReader(f));
st=br.readLine();
System.out.println(st);
}catch(Exception ie){System.out.println(ie);}
}
}

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

27.) Write a java program to draw a line.


import java.awt.*;
import javax.swing.*;
import java.awt.geom.Line2D;
  
class MyCanvas extends JComponent {
  
    public void paint(Graphics g)
    {
  
        // draw and display the line
        g.drawLine(30, 20, 80, 90);
    }
}
  
public class GFG1 {
    public static void main(String[] a)
    {
  
        // creating object of JFrame(Window popup)
        JFrame window = new JFrame();
  
        // setting closing operation
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
        // setting size of the pop window
        window.setBounds(30, 30, 200, 200);
  
        // setting canvas for draw
        window.getContentPane().add(new MyCanvas());
  
        // set visibility
        window.setVisible(true);
    }
}
Output :

28.) Write a java program to draw a triangle.


import java.lang.*;
import java.util.*;
import java.util.List;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class DrawTriangle extends Frame {

public Polygon mypolygon = new Polygon();

public void paint(Graphics g) {


Graphics2D ga = (Graphics2D)g;
ga.setPaint(Color.red);
ga.drawPolygon(mypolygon);
}

public static void main(String args[]) {


List< Integer > srcpoints = new ArrayList< Integer >();
srcpoints.add(200);srcpoints.add(200);
srcpoints.add(75);srcpoints.add(75);
srcpoints.add(100);srcpoints.add(200);
srcpoints.add(srcpoints.get(0));
srcpoints.add(srcpoints.get(1));

DrawTriangle frame = new DrawTriangle();


for(int i = 0; i < srcpoints.size(); i++)
{
int x = srcpoints.get(i++);
int y = srcpoints.get(i);
frame.mypolygon.addPoint(x, y);
}

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

30.) Write a java program to implement thread by runnable interface.


class Multi3 implements Runnable
{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi3 m1 = new Multi3();
Thread t1 = new Thread(m1);
t1.start();
}
}

OUTPUT:-
thread is running………

You might also like