2BA19CS011 Javalab-Merged

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

//2BA19CS011

//LAB ASSIGNMENT NO-1

class Box

double width;

double height;

double depth;

// compute and return volume

double volume()

return (width * height * depth);

public class BoxDemo

public static void main (String args[])

Box mybox1 = new Box();

Box mybox2 = new Box();

double vol;

// assign values to mybox1’s instance variables

mybox1.width = 10;

mybox1.height = 20;

mybox1.depth = 15;

/* assign different values to mybox2’s

instance variables */
mybox2.width = 3;

mybox2.height = 6;

mybox2.depth = 9;

// get volume of first box

vol = mybox1.volume();

System.out.println("Volume is" + vol);

// get volume of second box

vol= mybox2.volume();

System.out.println("Volume is" + vol);

}
//2BA19CS011

//JAVA LAB ASSIGNMENT-2

import java.util.Scanner;

class TestStack //class for testing stack operation

public static void main(String[] args)

int type_of_op; //variable used to store tupe


of operation

Mystack ob1=new Mystack(); //created stack

Scanner s = new Scanner(System.in);

while(true)

System.out.println("ente your choice");

System.out.println("enter 1 for push a numer");

System.out.println("enter 2 for pop a number");

System.out.println("enter 3 for printing a stack elements");

System.out.println("enter 4 for exit");

type_of_op = s.nextInt();
//reading type value

switch(type_of_op)

case 1: System.out.println("Enter your element which you want to push");

//s.nextInt();

ob1.push(s.nextInt()); //reading integer


and pushed it into the stack

break;
case 2: System.out.println("Element you want to pop is "+ ob1.pop());

break;

case 3: System.out.println("stack element is ");

ob1.print();

break;

default: System.out.println("You have entered the wrong choice");

return ;

class Mystack

int st_elements[]=new int[3];

int tos;

Mystack()

tos = -1; //indicate it's empty

public void push(int item) //for inserting element into stack in stack order

if(tos==2)
{

System.out.println("stack is full");

return;

else

st_elements[++tos] = item;

public int pop() //for retriving element from stack

if(tos<0)

System.out.println("stack underflow");

return 0;

else

System.out.println("top"+tos);

return st_elements[tos--];

//public boolean empty_stack() //check that stack is empty or not

//{

//if (tos == -1)

//return true;
//else return false;

//}

public void print() //for printing the stack contains

int temp=0;

if(tos<0)

System.out.println("stack underflow");

else

for (temp=tos;temp>=0;temp--)

System.out.println("element"+st_elements[temp]);

};
//2BA19CS011

//JAVA LAB ASSIGNMENT-3

import java.util.Scanner;

public class Time1

public static void main(String[] args)

Scanner s1 = new Scanner(System.in);

Time t1 = new Time();

Time t2 = new Time();

Time t3 = new Time();

Time t4 = new Time();

int n1;

System.out.println(" Enter the first time ");

t1.setTime();

System.out.println("Enter the second time ");

t2.setTime();

System.out.println("\n First time ");

t1.show();

System.out.println("\n Second time ");

t2.show();
t3.ADD(t1,t2);

System.out.println("\n Sum of times ");

t3.show();

System.out.println("Enter integer");

n1=s1.nextInt();

t4.ADD(t1,n1);

System.out.println("\nSum of Time and Integer\n");

t4.show();

class Time

{ int h,m,s;

public Time()

{ h=0; m=0; s=0; }

// Time ADD(Time T1,Time T2);

// Time ADD(Time T1,int n);

// void setTime();

// void show() { System.out.println(h+" "+m+" "+s);

public void ADD(Time t1,Time t2)


{ Time t;

int a,b;

a = t1.s+t2.s;

s = a%60;

b = (a/60)+t1.m+t2.m;

m = b%60;

h = (b/60)+t1.h+t2.h;

h = h%12;

// return t;

public void ADD(Time t1,int n)

Time t;

int a,b;

a = t1.s+n;

s = a%60;

b = (a/60)+t1.m;

m = b%60;

h = (b/60)+t1.h;

h= h%12;

// return t;

public void setTime()

{
Scanner s1 = new Scanner(System.in);

System.out.println("enter hour");

h=s1.nextInt();

System.out.println("enter minute");

m=s1.nextInt();

System.out.println("enter seconds");

s=s1.nextInt();

public void show()

System.out.println(h +" "+ m +" "+ s);

}
//2BA19CS011

//JAVA LAB ASSIGNMENT-4

class Point

int x;

int y;

Point()

x = 10;

y = 20;

System.out.println("Inside 1st Constructor");

Point(int a,int b)

x = a;

y = b;

System.out.println("Inside 2nd Constructor");

Point(Point p)

x=p.x;

y=p.y;

public void display()


{

System.out.println("x === "+x);

System.out.println("y === "+y);

public static void main(String args[])

Point p1 = new Point();

p1.display();

Point p2 = new Point(30,40);

p2.display();

Point p3= new Point(p2);

p3.display();

}
//2BA19CS011

//JAVA LAB ASSIGNMENT 8a

import java.util.Scanner;

import java.io.*;

class string {

public static void main(String[] args) {

// create a string

String Given_string ;

String sub_str;

String str2 ;

Scanner sc = new Scanner(System.in);

System.out.println("Enter string ");

Given_string = sc.nextLine();

System.out.println("Enter the sub string to be searched");

sub_str=sc.nextLine();

// check if substring is present in Given_string

// using contains()

boolean result = Given_string.contains(sub_str);

if(result) {

System.out.println(sub_str + " is present in the given string.");

else {

System.out.println(sub_str + " is not present in the given string.");

// Returns index of first occurrence of character.


System.out.println("Enter the character to be searched\n");

String ch = sc.nextLine();

int Index=0;

Index = Given_string.indexOf(ch);

boolean r = Given_string.contains(ch);

if (r) {

System.out.println(ch + " character is present in the Given string\n");

System.out.println("First occurrence of char " + ch + " is found at : " + Index);

else{

System.out.println( ch + " character is not present in the string.");

}
//2BS19CS011

//JAVA LAB ASSIGNMENT-8b

import java.util.Scanner;

class Exception {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

boolean tryAgain = true;

do {

try {

System.out.println("Please enter an integer between 15 and 25");

int age = input.nextInt();

if (age >= 15 && age <= 25){

System.out.println("Thank you. Age is correct\n");

tryAgain = false;

else if (age < 15 || age > 25 ){

throw new NumberFormatException("Integer is out of range.");

catch (NumberFormatException e1) { // Range check

System.out.println("* The number you entered is not between 15 and 25. Try again.");

System.out.println();

input.nextLine();

}
} while(tryAgain);

}
//2BA19CS011
//LAB ASSIGNMENT-5

import java.util.Scanner;
class person
{
private String name;
private int age;
private String addr;
void input()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the name, adress and age");
name=s.nextLine();
addr=s.nextLine();
age=s.nextInt();

}
void output()
{
System.out.println("name : "+name+"\nage : "+age+"\naddress : "+addr);
}
}
class student extends person
{
private float cgpa;
void input()
{
Scanner in=new Scanner(System.in);
super.input();
System.out.println("enter the cgpa");
cgpa=in.nextFloat();
}
void output()
{
{
if(cgpa>8.5)
{
super.output();
System.out.println("cgpa : "+cgpa);
}
}
}
}
class professor extends person
{
private int p;
void input()
{
Scanner in=new Scanner(System.in);
super.input();
System.out.println("enter the number of publication");
p=in.nextInt();
}
void output()
{
{
if(p>25)
{
super.output();
System.out.println(" number of publication : "+p);
}
}
}
}

public class Ass5


{
public static void main(String[] args)
{
Scanner in=new Scanner (System.in);
System.out.println("1 : for student information processing");
System.out.println("2 : for professor information processing");
System.out.println("3 : for exit");

while(true)
{
System.out.println("enter your choice");
int c=in.nextInt();
if(c==3)
break;
switch(c)
{

case 1:
System.out.println("enter the number of
students");
int n=in.nextInt();
student s[]=new student[n];

for(int i=0;i<n;i++)
s[i]=new student();

for(int i=0;i<n;i++)
{
System.out.println("enter "+(i+1)+"
student information");
s[i].input();
}
System.out.println("students having cgpa
more than 8.5 are");
for(int i=0;i<n;i++)
{
s[i].output();
}
break;
case 2:
System.out.println("enter the number of
professor");
int m=in.nextInt();
professor q[]=new professor[m];

for(int i=0;i<m;i++)
q[i]=new professor();

for(int i=0;i<m;i++)
{
System.out.println("enter "+(i+1)+"
professor information");
q[i].input();
}
System.out.println("prfessor having
more than 25 publication are");
for(int i=0;i<m;i++)
{
q[i].output();
}
break;
default:

System.out.println("invalied
option");
break;

}
}
}
}
//2BA19CS011

//LAB ASSIGNMENT-6
import java.util.*;
interface geomatricobjects
{
void getparameter();
void getarea();
}
class circle implements geomatricobjects
{
protected float radius;
public void getparameter()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the radius of the circle");
radius=s.nextFloat();
}
public void getarea()
{
float area;
area=3.142f*radius*radius;
System.out.println("area of the circle having radius
"+radius+" is "+area);
}
}
interface resizable
{
void resize();
}
class resizeablecircle extends circle implements resizable
{
public void resize()
{
super.getparameter();
super.getarea();
Scanner s=new Scanner(System.in);
System.out.println("enter the percentage of resize");
float per;
per=s.nextFloat();
per=per/100.0f;
radius=radius*per;
}
public void getarea()
{
float area;
area=3.142f*radius*radius;
System.out.println("area of the circle of changed radius
"+radius+" is"+area);
}
}
class Ass6
{
public static void main(String[] args)
{
resizeablecircle r=new resizeablecircle();
r.resize();
r.getarea();
}
}
//2BA19CS011

//LAB ASSIGNMENT-9
// Java Program to Write a java program to implement interthread communication
// for producer and consumers problem.
import java.io.*;

class Thread1
{

int n;
boolean valueset=false;

synchronized void get()


{

while(!valueset)
try
{
wait();

}catch(InterruptedException e)
{
System.out.println("Interrupted Excepton occur at : "+e);
}
System.out.println("get" +n);

valueset = false;
notify();
}

synchronized void put(int n)


{

while (valueset)
try
{
wait();
}catch(InterruptedException e)
{

System.out.println("Excepton occur at : "+e);

this.n=n;
valueset=true;
System.out.println("put"+n);
notify();
}

}
class Producer implements Runnable
{
Thread1 t;
Producer(Thread1 t)
{
this.t=t;
}
public void run()
{

for(int k=1;k<=5;k++)
{
t.put(k);
}
}
}

class Consumer implements Runnable


{
Thread1 t;
Consumer(Thread1 t)
{
this.t=t;
}
public void run()
{

for(int i=1;i<=5;i++)
{
t.get();
}
}
}

class Ass9
{
public static void main(String[] args)
{
Thread1 t=new Thread1();
Producer p=new Producer(t);
Consumer c=new Consumer(t);
Thread p1 = new Thread(p);
Thread c1 = new Thread(c);
p1.start();
c1.start();
System.out.println("Press Control+c to exit");
}
}
//2BA19CS011

//LAB ASSIGNMENT- 11a


import java.util.*;
interface factorial
{
int fact(int n);
}
public class Ass11a
{
public static void main(String[] args)
{factorial r;
r=(m)->{
int result=1;
for(int i=1;i<=m;i++)
result*=i;
return result;
};
Scanner s=new Scanner (System.in);
System.out.println("enter the number");
int d=s.nextInt();
int h=r.fact(d);
System.out.println("factorial value is "+h);
}
}
//2BA19CS011

//LAB ASSIGNMENT-11b
import java.util.*;
interface division
{
int divby7(int n);
}
public class Ass11b
{
public static void main(String[] args)
{division d;
d=(n)->{

if((n%7)==0)
return 1;
else
return 0;
};
Scanner s=new Scanner (System.in);
System.out.println("enter the number");
int m=s.nextInt();
int res=d.divby7(m);
if (res==1)
System.out.println("The given number "+m+" is divisible by
7 ");
else
System.out.println("The given number "+m+" is not divisible
by 7 ");

}
}
//2BA19CS011

//LAB ASSIGNMENT-12
import java.util.*;
import java.util.Scanner;
class Ass12
{
public static void main(String[] args)
{
int i,j,n,t;
ArrayList<Integer> a=new ArrayList<Integer>();

Scanner s=new Scanner(System.in);


System.out.println("enter the size of the array");

n=s.nextInt();
System.out.println("enter the elements");

for(i=0;i<n;i++)
a.add(s.nextInt());

System.out.println("Array elements are"+a);

Integer p[]=new Integer[a.size()];


p=a.toArray(p);

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i]>p[j])
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}
System.out.println("sorted array is");

for(i=0;i<n;i++)
System.out.println(p[i]);
}
}

You might also like