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

class Example

{
public static void main(String args[])
{
System.out.println("welcome to JPR 22412");
}
}
—-------------------------------------------------------------------------------------------------------------------
class Example1
{

public static void main(String args[])


{
int x,y;

x=21;
y=12;

//int x=21;
//int y=12;

//int x=21,y=12;

int result1=x+y; //run-time initialization


int result2=x-y; //run-time initialization
int result3=x*y; //run-time initialization
int result4=x/y; //run-time initialization
int result5=x%y; //run-time initialization

System.out.println("subtraction of two numbers:"+result2);


System.out.println("modulo division of two numbers:"+result5);
}

}
—----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Example2
{
public static void main(String args[])
{
int x,y;
Scanner scan=new Scanner(System.in);
System.out.println("enter numbers to compare:");
x=scan.nextInt();
y=scan.nextInt();

if(x>y)
{
System.out.println("x is larger");
}
else if(x<y)
{
System.out.println("y is larger");
}
else
{
System.out.println("both are equal");
}

}
}
—-------------------------------------------------------------------------------------------------------------------------
import java.util.*;
class Example4
{
public static void main(String args[])
{
//program demonstrate the use of switch case
char ch;
Scanner scan=new Scanner(System.in);
System.out.println("enter character");

ch=scan.next().charAt(0);

switch(ch)
{
case 'a':
System.out.println("under a");
break;
case 'b':
System.out.println("under b");
break;

}
}
—---------------------------------------------------------------------------------------------------------------------------
class SwitchCaseExample1
{

public static void main(String args[])


{
int num=1;
switch(num+2)
{
case 1:
System.out.println("Case 1: Value is:"+num);
break;
case 1:
System.out.println("Value is:"+num);
break;
case 3:
System.out.println("Case 3: Value is:"+num);
break;
default:
System.out.println("Default: Value is:"+num);

}
—---------------------------------------------------------------------------------------------------------------------
class SwitchCaseExample2
{

public static void main(String args[])


{
int value=100;
switch(value)
{
case 100:
System.out.println(true);
break;
case 100:
System.out.println(true);
break;
}

}
—-----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class TempratureConversion
{

public static void main(String args[])


{

double tempratureincelcius,tempratureinfarenhit;
Scanner scan=new Scanner(System.in);
System.out.println("enter temprature in celcius:");
tempratureincelcius=scan.nextDouble();

tempratureinfarenhit=(tempratureincelcius*1.8)+32;

System.out.println("temprature in farenhit is:"+tempratureinfarenhit);

—----------------------------------------------------------------------------------------------------------------------------
//Expt 5_3
class Complex
{
int real,img;
Complex()
{
}
Complex(int r,int i) //r=2,i=3—--->c1 //r=7,i=8—--->c2
{
real=r;
img=i;
}
public static void addition(Complex c1,Complex c2)
{
Complex c3=new Complex();
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
System.out.println("real part:"+c3.real);
System.out.println("img part:"+c3.img);
}
}
class ComplexAddition
{

public static void main(String ar[])


{
Complex c1=new Complex(2,3);
Complex c2=new Complex(7,8);
Complex.addition(c1,c2);

}
}
—---------------------------------------------------------------------------------------------------------------------------
Program 1
//command line arguments using for loop
public class CommandLineArgumentsForLoop
{
public static void main(String args[])
{
for(int i=0;i<5;i++)
{
System.out.println("command line arguments:"+args[i]);
}
}
}

—----------------------------------------------------------------------------------------------------------------------------
Program 2
//command line arguments using while loop
public class CommandLineArgumentsWhileLoop
{
public static void main(String ar[])
{

int i=0;
while(i<ar.length)
{
System.out.println("command line arguments:"+ar[i]);
i++;
}

}
}
—------------------------------------------------------------------------------------------------------------------
Program 3
//armstromg number

import java.util.*;
class ExampleArmstrongNumber
{
public static void main(String args[])
{
int num,num1,r;
int sum=0;
Scanner scan=new Scanner(System.in);
System.out.println("enter number to check whether armstrong or not");
num=scan.nextInt();
num1=num;
//logic to test
while(num1>0)
{
r=num1%10; //r=3 r=5 r=1
sum=sum+(r*r*r); //sum=0+(3*3*3)=0+27=27
sum=27+(5*5*5)=27+125=152 sum=152+(1*1*1)=153
num1=num1/10; //num1=153/10=15 num1=15/10=1
num1=1/10=0
}
if(sum==num)
System.out.println("it is armstrong number");
else
System.out.println("it is not armstrong number");
}
}

—----------------------------------------------------------------------------------------------------------------------------
Program 4
//demonstration of array

import java.util.Scanner;
class ArrayDemo
{
public static void main(String [] args)
{
int numbers[]=new int[10];
Scanner scan=new Scanner(System.in);
System.out.println("enter array elements:");
for(int i=0;i<10;i++)
{
numbers[i]=scan.nextInt();
}
System.out.println("array content is");
for(int i=0;i<10;i++)
{
System.out.print(numbers[i]+"\t");
}
}
}
—---------------------------------------------------------------------------------------------------------------------
Program 5
//Vector Demo

import java.util.*;
class VectorDemoQ5aS22
{
public static void main(String [] args)
{
Scanner scan=new Scanner(System.in);
Vector v1=new Vector(5);
System.out.println("enter vector elements:");
for(int i=0;i<5;i++)
{
int num=scan.nextInt();
v1.add(num);
}
System.out.println("vector elements:");
for(int i=0;i<5;i++)
{

System.out.print(v1.get(i)+"\t");
}

v1.insertElementAt(21,2);

System.out.println("\nupdated vector elements after insertion:");


for(int i=0;i<v1.size();i++)
{
System.out.print(v1.get(i)+"\t");
}

v1.remove(1);
v1.remove(4);

System.out.println("\nupdated vector elements after removal:");


for(int i=0;i<v1.size();i++)
{
System.out.print(v1.get(i)+"\t");
}

}
}
—-----------------------------------------------------------------
Program 6
//bubble sort

class BubbleSortDemo
{
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
/* Prints the array */
void printArray(int arr[])
{
int n = arr.length;

for (int i=0; i<n; ++i)


{
System.out.print(arr[i] + " ");
}
System.out.println();
}
// Driver method to test above
public static void main(String args[])
{
BubbleSortDemo ob = new BubbleSortDemo();
int arr[] = {64, 34, 25, 12, 22, 11, 90};
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
—-------------------------------------------------------------------------------------------------------------------
Program 7

//explicit type conversion


public class Example91
{
public static void main(String[] args)
{
/*
double d = 100.04;
long l=(long)d;
int i= (int) l; // Explicit casting: double to int
System.out.println(d);
System.out.println(l);
System.out.println(i);
*/
/*
byte b=50;
b=(byte)b*2;
System.out.println(b);
*/

byte a=4;
char b='z';
short c=102;
int i=5000;
float f=5.7f;
double d=0.124;

double result=(f*a)+(i/b)-(d*c);
System.out.println("result="+result);
}
}
—----------------------------------------------------------------------------------------------------
Program 8
//matrix addition

import java.util.Scanner;
class MatrixAddition
{

public static void main(String args[])


{
int i,j;
int A[][]=new int[2][2];
int B[][]=new int[2][2];
int C[][]=new int[2][2];

Scanner scan=new Scanner(System.in);


System.out.println("enter elements of matrix A");
for(i=0;i<2;i=i+1)
{
for(j=0;j<2;j=j+1)
{
A[i][j]=scan.nextInt();
}
}
System.out.println("Matrix A data:");
for(i=0;i<2;i=i+1)
{
for(j=0;j<2;j=j+1)
{
System.out.print
(A[i][j]+" ");
}
System.out.println();
}
//matrix B
System.out.println("enter elements of matrix B");
for(i=0;i<2;i=i+1)
{
for(j=0;j<2;j=j+1)
{
B[i][j]=scan.nextInt();
}
}
System.out.println("Matrix B data:");
for(i=0;i<2;i=i+1)
{
for(j=0;j<2;j=j+1)
{
System.out.print(B[i][j]+" ");
}
System.out.println();
}
//addition of matrices
//System.out.println("addition:");
for(i=0;i<2;i=i+1)
{
for(j=0;j<2;j=j+1)
{
C[i][j]=A[i][j]+B[i][j];
}
}

System.out.println("after addition:");
for(i=0;i<2;i=i+1)
{
for(j=0;j<2;j=j+1)
{
System.out.print(C[i][j]+" ");
}
System.out.println();
}
}

}
—------------------------------------------------------------------------------------------------------------------------
Program 9
//temperature conversion

import java.util.Scanner;
class TempratureConversion
{

public static void main(String args[])


{

double tempratureincelcius,tempratureinfarenhit;
Scanner scan=new Scanner(System.in);
System.out.println("enter temprature in celcius:");
tempratureincelcius=scan.nextDouble();
tempratureinfarenhit=(tempratureincelcius*1.8)+32; //temp in F=(temp. in
C*1.8)+32

System.out.println("temprature in farenhit is:"+tempratureinfarenhit);

}
—------------------------------------------------------------------------------------------------------------
Program 10
//palindrome
//e.g. M A D A M

// 0 1 2 3 4

import java.util.Scanner;
class ExamplePalindrome
{
public static void main(String ar[])
{
int done=1;
String s1;
Scanner sc=new Scanner(System.in);
System.out.println("enter string to check");
s1=sc.next();
int n=s1.length();
for(int i=0,j=(n-1);i<=(n/2);i++,j--)
{
if(s1.charAt(i)!=s1.charAt(j))
{
done=0;
break;
}
}
if(done==1)
System.out.println("palindrome");
else
System.out.println("not palindrome");
}
}
—-----------------------------------------------------------------------------------------------------------------
Program 11
//variable arguments

import java.util.*;
class ExampleVarargs
{
public static void main(String args[])
{
ExampleVarargs ev=new ExampleVarargs();
ev.computeAvg(15,25);
ev.computeAvg(12,34,45,78);

}//
public void computeAvg(int ... n)
{
double sum=0.0;
double avg;
for(int i=0;i<n.length;i++)
{
sum=sum+n[i];
}
avg=sum/(n.length);
System.out.println(":"+avg);
}
}
—----------------------------------------------------------------------------------------------------------------
Program 12
//for each example
class ForEachExample
{
public static void main(String[] arg)
{

int[] marks = { 125, 132, 95, 116, 110 };

int highest_marks = maximum(marks);


System.out.println("The highest score is " + highest_marks);

} //main ends
public static int maximum(int[] numbers)
{
int max = numbers[0];
/*without for each loop
for(int i=1;i<numbers.length;i++)
{
if(numbers[i]>max)
{
max=numbers[i];
}
}
*/
// for each loop
for (int x:numbers)
{
if (x > max)
{
max = x;
}
}
return max;
}
}
—----------------------------------------------------------------------------------------------------------------
Summer 2019 Q.6 b
/*Write a program to input name and salary of employee and
throw user defined exception if entered salary is negative. */
import java.io.*;
class NegativeSalaryException extends Exception
{
public NegativeSalaryException (String str)
{
super(str);
}
}
public class Summer19_Q6b
{
public static void main(String[] args)
{

try
{
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter Name of employee");
String name = br.readLine();
System.out.print("Enter Salary of employee");
int salary = Integer.parseInt(br.readLine());
if(salary<0)
{
throw new NegativeSalaryException("Enter Salary amount is
negative");
}
else
{
System.out.println("Salary is "+salary);
}
}
catch (NegativeSalaryException a)
{
System.out.println(a);
}
catch(Exception e)
{
}
}
}
—---------------------------------------------------------------------------------------------------------------------------
Applet Programming

Program 1

import java.awt.*;

import java.applet.*;

public class ArcDrawing extends Applet

public void paint(Graphics g)

g.drawArc(10,40,70,70,180,270);

g.setColor(Color.red);

g.fillArc(100,40,70,70,0,75);

g.drawArc(10,100,70,80,0,175);

g.setColor(Color.green);

g.fillArc(100,100,70,90,0,270);

g.drawArc(200,80,80,80,0,180);

/*

<applet code="ArcDrawing.class" width="300" height="300">

</applet>

*/
Program 2

import java.applet.*;

import java.awt.*;

public class ExampleFont extends Applet {

public void paint(Graphics g)

g.drawLine(20,40,60,80);

Font f1=new Font("Times New Roman",Font.BOLD,50);

g.setFont(f1);

g.drawString("welcome",200,200);

/* A(10,100) B(50,50) C(100,100)*/

int xcordinate[]={10,50,100};

int ycordinate[]={100,50,100};

int numpoints=3;

g.setColor(Color.RED);

g.fillPolygon(xcordinate,ycordinate,numpoints);

/*

<applet code="ExampleFont.class" width="300" height="300">

</applet>

*/
Program 3

import java.awt.*;

import java.applet.*;

public class SetBackgroundColorExample extends Applet

public void paint(Graphics g)

setBackground(Color.RED);

g.setColor(Color.blue);

g.fillOval(20,20,80,80);

/*

<applet code="SetBackgroundColorExample.class" width="300" height="300">

</applet>

*/
Program 4

import java.awt.*;

import java.applet.*;

public class Shapes extends Applet

public void paint(Graphics g)

/*Cylinder*/

g.drawString("(a).Cylinder",10,110);

g.drawOval(10,10,50,10);

g.drawOval(10,80,50,10);

g.drawLine(10,15,10,85);

g.drawLine(60,15,60,85);

/*Cube*/

g.drawString("(b).Cube",95,110);

g.drawRect(80,10,50,50);

g.drawRect(95,25,50,50);

g.drawLine(80,10,95,25);

g.drawLine(130,10,145,25);

g.drawLine(80,60,95,75);

g.drawLine(130,60,145,75);

/*Squar Inside A Circle*/

g.drawString("(c).Squar Inside A Circle",150,110);

g.drawOval(180,10,80,80);
g.drawRect(192,22,55,55);

/*Circle Inside a Squar*/

g.drawString("(d).Circle Inside a Squar",290,110);

g.drawRect(290,10,80,80);

g.drawOval(290,10,80,80);

/*Cone*/

g.drawString("(e).Cone",460,110);

g.drawLine(460,85,485,10);

g.drawLine(510,85,485,10);

g.drawOval(460,80,50,10);

/*Polygon*/

g.drawString("(f).Polygon",90,250);

int a[]={200,400,200,400};

int b[]={200,500,500,200};

g.drawPolygon(a,b,4);

/*

<applet code="Shapes.class" width="300" height="300">

</applet>

*/
Program 5

import java.applet.*;

import java.awt.*;

public class SmileyFace extends Applet {

public void paint(Graphics g)

// Oval for face outline

g.drawOval(80, 70, 150, 150);

// Ovals for eyes

// with black color filled

g.setColor(Color.BLACK);

g.fillOval(120, 120, 15, 15);

g.fillOval(170, 120, 15, 15);

// Arc for the smile

g.drawArc(130, 180, 50, 20, 180, 180);

/*

<applet code="SmileyFace.class" width="300" height="300">

</applet>

*/
Program 6

import java.applet.*;

import java.awt.*;

public class ExampleApplet1 extends Applet{

public void paint(Graphics g) {

g.drawString("welcome to java applet", 40, 40);

/*

<applet code="ExampleApplet1.class" width="300" height="300">

</applet>

*/

Program 7

//Control loops in applet

import java.applet.*;

import java.awt.*;

public class Example6 extends Applet{

public void paint(Graphics g) {

// TODO Auto-generated method stub

for(int i=20,j=40;i<=100;i=i+20,j=j+40)

g.setColor(Color.RED);

g.drawString("control loops", i, j);

}
}

/*

<applet code="Example6.class" width="300" height="300">

</applet>

*/

Program 8

//using parameters in applet

import java.applet.Applet;

import java.awt.Graphics;

public class UseParam extends Applet

public void paint(Graphics g){

String str=getParameter("msg");

g.drawString(str,50, 50);

/*

<applet code="Example6.class" width="300" height="300">

<param name=”msg” value=”welcome”/>

</applet>

*/
Summer 2022 Q6 c

//Write a Java applet to draw a bar chart for the following

values

import java.applet.*;

import java.awt.*;

/*

<applet code = "BarChart.class" width=400 height=400>

<param name="year1" value="2011">

<param name="year2" value="2012">

<param name="year3" value="2013">

<param name="year4" value="2014">

<param name="result1" value="110">

<param name="result2" value="120">

<param name="result3" value="170">

<param name="result4" value="160">

</applet>

*/

public class BarChart extends Applet

int n;

String year[];

int value[];
public void init()

n = 4;

year = new String[n];

value = new int[n];

year[0] = getParameter("year1");

year[1] = getParameter("year2");

year[2] = getParameter("year3");

year[3] = getParameter("year4");

value[0] = Integer.parseInt(getParameter("result1"));

value[1] = Integer.parseInt(getParameter("result2"));

value[2] = Integer.parseInt(getParameter("result3"));

value[3] = Integer.parseInt(getParameter("result4"));

public void paint(Graphics g)

Font font = new Font("Arial",Font.BOLD,15);

g.setFont(font);

for(int i = 0; i < n; i ++)

g.setColor(Color.BLUE);

g.drawString(year[i], 20, i * 50 + 30);


g.setColor(Color.RED);

g.fillRect(70, i * 50 + 10, value[i], 40);

g.drawString(String.valueOf(value[i]) + "%", 250, i * 50 + 35);

String msg = "Bar Chart from Year 2011 - 2014";

g.setColor(Color.darkGray);

font = new Font("Arial",Font.BOLD,20);

g.setFont(font);

g.drawString(msg, 50, 300);

—------------------------------------------------------------------------------------------------------------------------
File handling programs

Program 1

import java.io.*;
public class CreateFile
{
public static void main(String[] args)
{
File f=null;
FileWriter fwr=null;
BufferedWriter bwr=null;
try
{
f=new File("D://Even Sem 2020_21/JPR
22412/JPR_backup_2019_20/JPR/program/simple.txt");
if(f.createNewFile())
{
System.out.println("success");
fwr=new FileWriter(f);
bwr=new BufferedWriter(fwr);
bwr.write("india is my country");

}
else
{
System.out.println("failure");
}
bwr.close();
}

catch(Exception e2)
{
System.err.println("exception occurs:");
}
}//main ends
}//class ends
Program 2

import java.io.*;

public class ReadFromFile1

public static void main(String[] args)

try

//String path="D://Even Sem 2020_21/JPR


22412/JPR_backup_2019_20/JPR/program/test.txt";

String path=args[0];

File f = new File(args[0]);

FileReader fr=new FileReader(f);

BufferedReader br = new BufferedReader(fr);


do

System.out.println(br.readLine());

while(br.readLine()!=null);

catch(FileNotFoundException e1)

System.err.println("1 exception occurs:");

catch(Exception e2)

System.err.println("exception occurs:");

—---------------------------------------------------------------------------------------------------------------------------

Program 3

import java.io.File;

import java.io.FileInputStream;

import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XLSXReaderExample {

//.xlsx extension

public static void main(String[] args) {

// TODO Auto-generated method stub

try

File file = new File("D://Even Sem


2020_21/GPMalvan-Eqpt-List-20-21_ComputerDept.xlsx"); //creating a new file instance

FileInputStream fis = new FileInputStream(file); //obtaining bytes from


the file

//creating Workbook instance that refers to .xlsx file

XSSFWorkbook wb = new XSSFWorkbook(fis);

XSSFSheet sheet = wb.getSheetAt(0); //creating a Sheet object to


retrieve object

Iterator<Row> itr = sheet.iterator(); //iterating over excel file

while (itr.hasNext())

Row row = itr.next();

Iterator<Cell> cellIterator = row.cellIterator(); //iterating over each column

while (cellIterator.hasNext())

Cell cell = cellIterator.next();

switch (cell.getCellType())

case Cell.CELL_TYPE_STRING: //field that represents string cell type


System.out.print(cell.getStringCellValue() + "\t\t\t");

break;

case Cell.CELL_TYPE_NUMERIC: //field that represents number cell


type

System.out.print(cell.getNumericCellValue() + "\t\t\t");

break;

default:

System.out.println("");

catch(Exception e)

e.printStackTrace();

—---------------------------------------------------------------------------------------------------------------------------

Program 4

//copy characters from one file to another in java

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFiles {

public static void main(String[] args) throws IOException {

//Creating a File object to hold the source file

File source = new File("D:\\ExampleDirectory\\SampleFile.txt");

//Creating a File object to hold the destination file

File destination = new File("D:\\ExampleDirectory\\outputFile.txt");

//Creating an FileInputStream object

FileInputStream inputStream = new FileInputStream(source);

//Creating an FileOutputStream object

FileOutputStream outputStream = new FileOutputStream(destination);

//Creating a buffer to hold the data

int length = (int) source.length();

byte[] buffer = new byte[length];

while ((length = inputStream.read(buffer)) > 0) {

outputStream.write(buffer, 0, length);

inputStream.close();

outputStream.close();

System.out.println("File copied successfully.......");

—----------------------------------------------------------------------------------------------------------------------------
Exception handling programs

Program 1

import java.io.*;

class ExceptionDemo1

public static void main(String args[])throws Exception

ExceptionDemo1 ed1=new ExceptionDemo1();

ed1.compute();

public void compute()throws Exception

DataInputStream ds=new DataInputStream(System.in);

System.out.println("enter data");
String msg=ds.readLine();

System.out.println(msg);

Program 2

import java.io.*;

class ExceptionDemo2

public static void main(String arg[])

try

DataInputStream ds=new DataInputStream(System.in);

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

String msg=ds.readLine();

System.out.println(msg);

}//

catch(Exception ex)
{

System.out.println(ex);

Multithreading programs

Sample programs:

1 //multithreading

class ThreadDemo1 extends Thread

public static void main(String args[])

System.out.println("under main thread");

ThreadDemo1 td1=new ThreadDemo1();

td1.start();

public void run()

{
System.out.println("under Thread-1");

2//multithreading Runnable interface

class ThreadDemo2Main

public static void main(String args[])

System.out.println("under main thread");

ThreadDemo2 td2=new ThreadDemo2();

Thread t1=new Thread(td2);

t1.start();

class ThreadDemo2 implements Runnable

public void run()

System.out.println("under Thread-1 of Runnable");

}
Program 1

class ExampleCode1

synchronized void display(String name,int num)

System.out.println(name+": <" + num);

try

Thread.sleep(2000);

catch(InterruptedException e)

System.out.println();

}
}

class ExampleThread1 implements Runnable

Thread t1;

ExampleCode1 ec1;

String msg;

ExampleThread1(ExampleCode1 ecode1,String message)

ec1=ecode1;

msg=message;

t1=new Thread(this,"Example Thread 1");

t1.start();

@Override

public void run(){

// TODO Auto-generated method stub

try

for (int i = 1;i<=5; i++) {

ec1.display(msg, i);

Thread.sleep(2000);
}

catch (Exception e) {

public class SynchronizationExample {

public static void main(String[] args) {

// TODO Auto-generated method stub

try

ExampleCode1 ecode=new ExampleCode1();

ExampleThread1 et1=new ExampleThread1(ecode, "first thread");

ExampleThread1 et2=new ExampleThread1(ecode, "second thread");

//ExampleThread1 et3=new ExampleThread1(ecode, "third thread");

et1.t1.join();

et2.t1.join();

//et3.t1.join();
}

catch (Exception e) {

Program 2

class Queue
{
int num;
boolean flag=false;
synchronized int consume()
{
try
{
if(flag!=true)
{
wait();
}
}
catch (Exception e) {

}
System.out.println("Consumed: " + num);
flag=false;
notify();
return num;
}
synchronized void produce(int n)
{
try
{
if(flag==true)
{
wait();
}
}
catch (Exception e) {

}
num=n;
System.out.println("Produced: " + num);
flag=true;
notify();
}
}
class Producer implements Runnable
{
Queue q1;
Thread t1;
public Producer(Queue q1) {
this.q1=q1;
t1=new Thread(this, "Producer 1");
t1.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
int i=0;
while(i<5)
{
q1.produce(++i);
}
}

}
class Consumer implements Runnable
{
Queue q1;
Thread t2;
public Consumer(Queue q1) {
this.q1=q1;
t2=new Thread(this,"Consumer 1");
t2.start();
}
@Override
public void run() {
while(q1.consume()<5);
}

}
public class ProducerConsumerMain {

public static void main(String[] args) {


// TODO Auto-generated method stub
Queue q1=new Queue();
Producer p1=new Producer(q1);
Consumer c1=new Consumer(q1);
}
}
—---------------------------------------------------------------------------------------------------------------

Program 3
public class OddEvenSeriesMT {

public static void main(String[] args) {


// TODO Auto-generated method stub
OddSeries os=new OddSeries();
EvenSeries es=new EvenSeries();
os.start();
es.start();
}

class OddSeries extends Thread


{
public void run()
{
try
{
for(int i=1;i<=50;i=i+2)
{
System.out.println("ODD"+i);
//Thread.sleep(500);
}

}
catch (Exception e) {
System.out.println(e);
}
}
}//
class EvenSeries extends Thread
{
public void run()
{
try
{
for(int i=2;i<=50;i=i+2)
{
System.out.println("EVEN"+i);
Thread.sleep(500);
}

}
catch (Exception e) {
System.out.println(e);
}
}
}
Program 4

public class MultithreadingDemo {

public static void main(String[] args) {

// TODO Auto-generated method stub

int n = 8; // Number of threads

for (int i = 0; i < n; i++) {

Multithread m= new Multithread();

m.start();

}
}

class Multithread extends Thread

public void run()

try {

// Displaying the thread that is running

System.out.println("Thread " + Thread.currentThread().getId()+ " is running");

catch (Exception e) {

// Throwing an exception

System.out.println("Exception is caught");

—------------------------------------------------------------------------------------
Additional Programs

import java.util.*;

class Student

int id;

String name;

void setData()

Scanner scan=new Scanner(System.in);

System.out.println("enter id and name of student");


id=scan.nextInt();

name=scan.next();

void display()

System.out.println("id of student:"+id+"\tname of student:"+name);

}//Student ends

class Q3aSummer2019

public static void main(String args[])

Scanner scan=new Scanner(System.in);

System.out.println("enter no of students:");

int n=scan.nextInt();

//array of Student class instances

Student stud[]=new Student[n];

for(int i=0;i<n;i++)

stud[i]=new Student();

}
for(int i=0;i<n;i++)

stud[i].setData();

for(int i=0;i<n;i++)

stud[i].display();

—--------------------------------------------------------------------------------------

//dynamic method dispatch

import java.util.*;

class A

public void display()

System.out.println("under class A");

class B extends A

{
public void display()

System.out.println("under class B");

class C extends A

public void display()

System.out.println("under class C");

class Q3bSummer2019

public static void main(String args[])

A a1=new A();

a1.display();

B b1=new B();

b1.display();
//dynamic calling

A a2=new B();

a2.display();

A a3=new C();

a3.display();

—-----------------------------------------------------------------------------------------

/*

throw user defined exception NoMatchException */

import java.io.*;

class NoMatchException extends Exception

public NoMatchException (String str)

super(str);

}//

public class Summer19_Q6b1


{

public static void main(String[] args)

try

BufferedReader br= new BufferedReader(new


InputStreamReader(System.in));

System.out.print("Enter value");

String value = br.readLine();

if(!value.equalsIgnoreCase("MSBTE"))

throw new NoMatchException("Entered value is incorrect");

else

System.out.println("Entered value is correct");

catch (NoMatchException a)

System.out.println(a);

}
catch(Exception e)

—----------------------------------------------------------------------------------------------------------------------------

/*Design an applet which displays three circles one below the other and fill

them red, green and yellow color respectively.*/

import java.applet.*;

import java.awt.*;

public class Q2cW2014 extends Applet

public void paint(Graphics g)

g.setColor(Color.RED);

g.fillOval(50,10,40,40);//1st circle

g.setColor(Color.GREEN);

g.fillOval(50,60,40,40);//2nd circle

g.setColor(Color.YELLOW);

g.fillOval(50,110,40,40);//3rd circle

/*

<applet code="Q2cW2014.class" width=400 height=400>


</applet>

*/

—----------------------------------------------------------------------------------------------------------------------------

/*Design an Applet program which displays a rectangle filled with red color and message as

"Hello Third year Students" in blue color.*/

import java.applet.*;

import java.awt.*;

public class Q6cW2016 extends Applet

public void paint(Graphics g)

g.setColor(Color.RED);

g.fillRect(20,20,80,40);

g.setColor(Color.BLUE);

g.drawString("Hello Third year Students",120,40);

/*

<applet code="Q6cW2016.class" width=400 height=400>

</applet>

*/

—----------------------------------------------------------------------------------------------------------------------------

/*Write a program to accept a number as command line argument and print the

number is even or odd.*/


import java.io.*;

class CommandLineQ3bS2015

public static void main(String args[])

int num=Integer.parseInt(args[0]);

if(num%2==0)

System.out.println("number is even");

else

System.out.println("number is odd");

—---------------------------------------------------------------------------------------------------------------------

/*Define a class ‘employee’ with data members empid, name and salary.

Accept data for five objects using Array of objects and print it.Q1b a summer 2015

*/

import java.util.*;

class Employee

int empid;
String name;

double salary;

void setData()

Scanner scan=new Scanner(System.in);

System.out.println("enter details of employee:");

empid=scan.nextInt();

name=scan.next();

salary=scan.nextDouble();

void display()

System.out.println("empid:"+empid+"\tname:"+name+"\tsalary:"+salary);

class EmployeeMain

public static void main(String args[])

Employee emp[]=new Employee[5];

for(int i=0;i<5;i++)

emp[i]=new Employee();
}

for(int i=0;i<5;i++)

emp[i].setData();

for(int i=0;i<5;i++)

emp[i].display();

You might also like