Professional Documents
Culture Documents
Untitled
Untitled
{
public static void main(String args[])
{
System.out.println("welcome to JPR 22412");
}
}
—-------------------------------------------------------------------------------------------------------------------
class Example1
{
x=21;
y=12;
//int x=21;
//int y=12;
//int x=21,y=12;
}
—----------------------------------------------------------------------------------------------------------------------
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
{
}
—---------------------------------------------------------------------------------------------------------------------
class SwitchCaseExample2
{
}
—-----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class TempratureConversion
{
double tempratureincelcius,tempratureinfarenhit;
Scanner scan=new Scanner(System.in);
System.out.println("enter temprature in celcius:");
tempratureincelcius=scan.nextDouble();
tempratureinfarenhit=(tempratureincelcius*1.8)+32;
—----------------------------------------------------------------------------------------------------------------------------
//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
{
}
}
—---------------------------------------------------------------------------------------------------------------------------
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);
v1.remove(1);
v1.remove(4);
}
}
—-----------------------------------------------------------------
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;
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
{
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
{
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
}
—------------------------------------------------------------------------------------------------------------
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)
{
} //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.*;
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>
*/
Program 2
import java.applet.*;
import java.awt.*;
g.drawLine(20,40,60,80);
g.setFont(f1);
g.drawString("welcome",200,200);
int xcordinate[]={10,50,100};
int ycordinate[]={100,50,100};
int numpoints=3;
g.setColor(Color.RED);
g.fillPolygon(xcordinate,ycordinate,numpoints);
/*
</applet>
*/
Program 3
import java.awt.*;
import java.applet.*;
setBackground(Color.RED);
g.setColor(Color.blue);
g.fillOval(20,20,80,80);
/*
</applet>
*/
Program 4
import java.awt.*;
import java.applet.*;
/*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);
g.drawOval(180,10,80,80);
g.drawRect(192,22,55,55);
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>
*/
Program 5
import java.applet.*;
import java.awt.*;
g.setColor(Color.BLACK);
/*
</applet>
*/
Program 6
import java.applet.*;
import java.awt.*;
/*
</applet>
*/
Program 7
import java.applet.*;
import java.awt.*;
for(int i=20,j=40;i<=100;i=i+20,j=j+40)
g.setColor(Color.RED);
}
}
/*
</applet>
*/
Program 8
import java.applet.Applet;
import java.awt.Graphics;
String str=getParameter("msg");
g.drawString(str,50, 50);
/*
</applet>
*/
Summer 2022 Q6 c
values
import java.applet.*;
import java.awt.*;
/*
</applet>
*/
int n;
String year[];
int value[];
public void init()
n = 4;
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"));
g.setFont(font);
g.setColor(Color.BLUE);
g.setColor(Color.darkGray);
g.setFont(font);
—------------------------------------------------------------------------------------------------------------------------
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.*;
try
String path=args[0];
System.out.println(br.readLine());
while(br.readLine()!=null);
catch(FileNotFoundException e1)
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;
//.xlsx extension
try
while (itr.hasNext())
while (cellIterator.hasNext())
switch (cell.getCellType())
break;
System.out.print(cell.getNumericCellValue() + "\t\t\t");
break;
default:
System.out.println("");
catch(Exception e)
e.printStackTrace();
—---------------------------------------------------------------------------------------------------------------------------
Program 4
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
outputStream.write(buffer, 0, length);
inputStream.close();
outputStream.close();
—----------------------------------------------------------------------------------------------------------------------------
Exception handling programs
Program 1
import java.io.*;
class ExceptionDemo1
ed1.compute();
System.out.println("enter data");
String msg=ds.readLine();
System.out.println(msg);
Program 2
import java.io.*;
class ExceptionDemo2
try
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
td1.start();
{
System.out.println("under Thread-1");
class ThreadDemo2Main
t1.start();
}
Program 1
class ExampleCode1
try
Thread.sleep(2000);
catch(InterruptedException e)
System.out.println();
}
}
Thread t1;
ExampleCode1 ec1;
String msg;
ec1=ecode1;
msg=message;
t1.start();
@Override
try
ec1.display(msg, i);
Thread.sleep(2000);
}
catch (Exception e) {
try
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 {
Program 3
public class OddEvenSeriesMT {
}
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
m.start();
}
}
try {
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()
name=scan.next();
void display()
}//Student ends
class Q3aSummer2019
System.out.println("enter no of students:");
int n=scan.nextInt();
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();
—--------------------------------------------------------------------------------------
import java.util.*;
class A
class B extends A
{
public void display()
class C extends A
class Q3bSummer2019
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();
—-----------------------------------------------------------------------------------------
/*
import java.io.*;
super(str);
}//
try
System.out.print("Enter value");
if(!value.equalsIgnoreCase("MSBTE"))
else
catch (NoMatchException a)
System.out.println(a);
}
catch(Exception e)
—----------------------------------------------------------------------------------------------------------------------------
/*Design an applet which displays three circles one below the other and fill
import java.applet.*;
import java.awt.*;
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
/*
*/
—----------------------------------------------------------------------------------------------------------------------------
/*Design an Applet program which displays a rectangle filled with red color and message as
import java.applet.*;
import java.awt.*;
g.setColor(Color.RED);
g.fillRect(20,20,80,40);
g.setColor(Color.BLUE);
/*
</applet>
*/
—----------------------------------------------------------------------------------------------------------------------------
/*Write a program to accept a number as command line argument and print the
class CommandLineQ3bS2015
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()
empid=scan.nextInt();
name=scan.next();
salary=scan.nextDouble();
void display()
System.out.println("empid:"+empid+"\tname:"+name+"\tsalary:"+salary);
class EmployeeMain
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();