You are on page 1of 35

//slip 1

//Prime number from command line arguments.

import java.io.*;
class Prime
{
public static void main(String args[])
{
int i,j,n;

for( i=0;i<args.length;i++)
{
n=Integer.parseInt(args[i]);
}
for( i=2;i<=args.length;i++)
{
for(j=2;j<=i;j++)
{
if(i%j==0)
break;
}
if(i==j)
{
System.out.println("Prime number is:"+i);
}
}
}
}
-----------------------------------------------------------------------------------
-

--//slip 1
/*Q2
abstract class staff with protected members id and name,define parameterized
constructor.define
one officestaff subclass with member department.
*/
import java.io.*;
abstract class Staff
{
protected int id;
protected String name;
Staff(int i,String n)
{
id=i;
name=n;
}
void display()
{
System.out.println("id:"+id);
System.out.println("name:"+name);

}
}
class OfficeStaff extends Staff
{
String dept;
OfficeStaff(int i,String n,String d)
{
super(i,n);
dept=d;
}
void display()
{
super.display();
System.out.println("dept:"+dept);
System.out.println("-------------------------------");
}
}
class staffdemo
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no of staff");
int n=Integer.parseInt(br.readLine());

OfficeStaff s[]=new OfficeStaff[n];


for(int i=0;i<n;i++)
{
System.out.println("Enter the id:");
int id=Integer.parseInt(br.readLine());

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


String name=br.readLine();

System.out.println("Enter the dept:");


String dept=br.readLine();
s[i]=new OfficeStaff(id,name,dept);
}
for(int i=0;i<n;i++)
{
s[i].display();
}
}
}
-----------------------------------------------------------------------------------
//slip 2
/* calculate BMI through cammand line argument*/

import java.io.*;
class BMI
{
public static void main(String args[])throws Exception
{
String fname=args[0];
System.out.println("Enter the first name:"+fname);

String lname=args[1];
System.out.println("Enter the last name:"+lname);

float weight=Float.parseFloat(args[2]);
float height=Float.parseFloat(args[3]);

float BMI=weight/(height*height);
System.out.println("the body mass index(BMI) is"+BMI+"kg/m2");
}
}
-----------------------------------------------------------------------------------
---
//slip 2
/*
Q2
define cricketplayer (name,no_of_inning,no_of_times_notout,runs,bat_avg)
create an array of player object,cal batting avg using static avg method()
static sort method
*/
import java.io.*;

class Inning_Invalid extends Exception{}

class Cricket
{
String name;
int no_of_inning,no_of_times_notout,runs;
float bat_avg;
Cricket()
{

}
Cricket(String n,int inn,int notout,int r)
{
name=n;
no_of_inning=inn;
no_of_times_notout=notout;
runs=r;
}
void cal_avg()
{
try
{
if(no_of_inning==0)
throw new Inning_Invalid();

bat_avg=runs/no_of_inning;
}
catch(Inning_Invalid e1)
{
System.out.println("No of inning cannot be zero.");

runs=0;
bat_avg=0;
}
}
void display()
{
System.out.println("Name of player:"+name);
System.out.println("No of inning :"+no_of_inning);
System.out.println("No of times not out."+no_of_times_notout);
System.out.println("batting avg:"+bat_avg);
System.out.println("total runs:"+runs);
System.out.println("-------------");
}
float getavg()
{
return bat_avg;
}
public static void sortPlayer(Cricket c[],int n)
{
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(c[j].getavg() >c[j+1].getavg())
{
Cricket t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
}
}
for(int i=0;i<n;i++)
c[i].display();
}
}
class Cricketdemo
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter how many player:");
int n=Integer.parseInt(br.readLine());

Cricket cp[]=new Cricket[n];


for(int i=0;i<n;i++)
{
System.out.println("Enter name:");
String name=br.readLine();

System.out.println("Enter no of inning:");
int inn=Integer.parseInt(br.readLine());

System.out.println("Enter the no of times not out:");


int notout=Integer.parseInt(br.readLine());

System.out.println("Enter runs:");
int r=Integer.parseInt(br.readLine());

cp[i]=new Cricket(name,inn,notout,r);
cp[i].cal_avg();
}
for(int i=0;i<n;i++)
cp[i].display();

Cricket.sortPlayer(cp,n);
}
}
-------------------------------------------------------------------------------
//slip 3
/* accept names of cities and sort in ascending order*/

import java.io.*;
class Cities
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter how many cities:");
int n=Integer.parseInt(br.readLine());

String s[]=new String[n];


System.out.println("Enter the names of cities:");
for(int i=0;i<n;i++)
{
s[i]=br.readLine();
}

for(int i=0;i<n;i++)
{
for(int j=1;j<n;j++)
{
if(s[j-1].compareTo(s[j])>0)
{
String t=s[j-1];
s[j-1]=s[j];
s[j]=t;
}
}
}
System.out.println("Cities in ascending order :");
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}

}
}
-------------------------------------------------------------------------
//slip 3
/*patient (name,age,oxy_level,HRCT_report)
create an object of patient .oxylevel less than 95% and HRCT level greater than 10
throw exception "patient is covid + and need to hospitalized"*/

import java.util.*;

class Patient
{
String name;
int age;
int oxylevel;
int HRCTreport;

Patient(String name,int age,int oxylevel,int HRCTreport)


{
this.name=name;
this.age=age;
this.oxylevel=oxylevel;
this.HRCTreport=HRCTreport;
}
}
public class PatientTest extends Exception
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("How many patient you want to insert: ");
int n=s.nextInt();

Patient p[]=new Patient[n];


for(int i=0;i<n;i++)
{
System.out.println("Enter patient name:");
String name=s.next();

System.out.println("Enter age of patient:");


int age=s.nextInt();

System.out.println("Enter oxylevel of patient:");


int oxylevel=s.nextInt();

System.out.println("Enter HRCTreport of patient:");


int HRCTreport=s.nextInt();

p[i]=new Patient(name,age,oxylevel,HRCTreport);
}

for(int i=0;i<n;i++)
{
if(p[i].oxylevel<95 && p[i].HRCTreport>10)
try
{
throw new NullPointerException("\n");
}
catch(Exception e)
{
System.out.println("Patient is covid+ and need to
hospitalized.");
}
else
{
System.out.println("name :"+p[i].name);
System.out.println("age:"+p[i].age);
System.out.println("oxylevel :"+p[i].oxylevel);
System.out.println("HRCTreport :"+p[i].HRCTreport);
}
}
}
}
-------------------------------------------------------------------------------
//slip 4
/* changing rows and column 2d array
*/

import java.util.*;
import java.util.Arrays;
import java.util.Scanner;

class Array
{
public static void main(String args[])throws Exception
{
int a[][]={{2,3},{4,5}};
System.out.println("Original array is:");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("\t");
}

int row=a.length;
int column=a[0].length;
int temp[][]=new int[row][column];

for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
temp[i][j]=a[j][i];
}
}

System.out.println("After changing rows and column:");


for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
System.out.print(temp[i][j]+" ");
}
System.out.println("\t");
}
}
}
---------------------------------------------------------------
//slip 4
//Q2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class InvalidPasswordException extends Exception


{}

class Userpass extends JFrame implements ActionListener


{
JLabel name,pass;
JTextField nametxt;
JPasswordField passtxt;
JButton login,end;

static int cnt=0;


Userpass()
{
name=new JLabel("Name");
pass=new JLabel("Password");

nametxt=new JTextField(20);
passtxt=new JPasswordField(20);
login=new JButton("Login");
end=new JButton("End");

login.addActionListener(this);
end.addActionListener(this);

setLayout(new GridLayout(3,2));
add(name);
add(nametxt);
add(pass);
add(passtxt);
add(login);
add(end);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Login check:");
setSize(300,300);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==end)
{
System.exit(0);
}
if(e.getSource()==login)
{
try
{
String user=nametxt.getText();
String pass=new String(passtxt.getPassword());
if(user.compareTo(pass)==0)
{
JOptionPane.showMessageDialog(null,"login
successfully","login",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e1)
{
cnt++;
JOptionPane.showMessageDialog(null,"login
failed","login",JOptionPane.ERROR_MESSAGE);
nametxt.setText(" ");
passtxt.setText(" ");
nametxt.requestFocus();

if(cnt==3)
{
JOptionPane.showMessageDialog(null,"3
login","login",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}

}
}
public static void main(String args[])
{
new Userpass();
}
}
---------------------------------------------------------------------------
//slip 5 Q1
/*multilevel inheritance country ,continent,state,place*/

import java.io.*;
class Continent
{
String con;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

void con_input()throws IOException


{
System.out.println("Enter the continent name:");
con=br.readLine();
}
}
class Country extends Continent
{
String cou;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

void cou_input()throws IOException


{
System.out.println("Enter the contry name:");
cou=br.readLine();
}
}
class State extends Country
{
String stat;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

void stat_input()throws IOException


{
System.out.println("Enter the state name:");
stat=br.readLine();
}
}
class Place extends State
{
String pla;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

void pla_input()throws IOException


{
System.out.println("Enter the Place name:");
pla=br.readLine();
}
public static void main(String args[])throws IOException
{
Place p=new Place();

p.con_input();
p.cou_input();
p.stat_input();
p.pla_input();

System.out.println("\n Continent :"+p.con);


System.out.println("\n Country :"+p.cou);
System.out.println("\n state :"+p.stat);
System.out.println("\n place :"+p.pla);
}
}
-----------------------------------------------------------------------------------
-
//slip 5
//Q2 addition and mutiplication matrix

import java.util.*;
import java.util.Scanner;
import java.util.Arrays;
public class Matrix
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{9,8,7},{6,5,4},{3,2,1}};
int c[][]=new int[3][3];

System.out.println("A:"+Arrays.deepToString(a));
System.out.println("B:"+Arrays.deepToString(b));

int choice;
do
{
System.out.println("\n Choose the matrix operation:");
System.out.println("---------------------------------------");

System.out.println("1.Addition:");
System.out.println("2.Multiplication:");
System.out.println("3.Exit:");

System.out.println("Enter your choice:");


choice=sc.nextInt();

switch(choice)
{
case 1:c=add(a,b);
System.out.println("Addition of the matrix is:");
System.out.println(Arrays.deepToString(c));
break;

case 2:c=multiply(a,b);
System.out.println("Multiplication of the matrix
is:");
System.out.println(Arrays.deepToString(c));
break;

case 3:System.out.println("Thank you:");


break;
default:System.out.println("Invalid input");
break;
}

}while(true);
}
public static int[][] add(int[][] a,int[][] b)
{
int row=a.length;
int column=a[0].length;
int sum[][]=new int[row][column];

for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
return sum;
}

public static int[][] multiply(int[][] a,int[][] b)


{
int row=a.length;
int column=a[0].length;
int product[][]=new int[row][column];

for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
product[i][j]=0;
for(int k=0;k<a[0].length;k++)
{
product[i][j]=a[i][k]+b[k][j];
}
}
}
return product;
}
}
------------------------------------------------------
//slip 6
/*display Employee (id,name,designation,sal) using toString()*/

import java.io.*;
class Employee
{
String name;
int id;
float sal;
String designation;

Employee(int id,String name,String designation,float sal)


{
this.id=id;
this.name=name;
this.designation=designation;
this.sal=sal;
}
public String toString()
{
return id+" "+name+" "+designation+" "+sal;
}
public static void main(String args[])
{
Employee e1=new Employee(101,"Rakesh","Supervisor",30000);
Employee e2=new Employee(102,"mahesh","junior",20000);

System.out.println("Employee detail are:"+e1);


System.out.println("Employee detail are:"+e2);
}
}
--------------------------------------------------------------------
//slip 6

/* Q2 abstract class order id,des.create 2 subclass PurchaseOrder and SalesOrder


having
members customer name,vendor name and method accept(),display(),
create 3 obj each of class*/

import java.io.*;
abstract class Order
{
int id;
String description;
}
class PurchaseOrder extends Order
{
String cname,vname;
public void accept()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the id:");
id=Integer.parseInt(br.readLine());

System.out.println("Enter the description:");


description=br.readLine();

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


cname=br.readLine();

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


vname=br.readLine();
}
public void display()
{
System.out.println("id:"+id);
System.out.println("description:"+description);
System.out.println("customer_name:"+cname);
System.out.println("vendor name"+vname);

}
}
class SalesOrder extends Order
{
String cname,vname;
public void accept()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the id:");
id=Integer.parseInt(br.readLine());

System.out.println("Enter the description:");


description=br.readLine();

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


cname=br.readLine();

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


vname=br.readLine();

}
public void display()
{
System.out.println("id:"+id);
System.out.println("description:"+description);
System.out.println("customer_name:"+cname);
System.out.println("vendor name:"+vname);
}
}
public class OrderTest
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Select any one:");
System.out.println("1.PurchaseOrder:");
System.out.println("2.SalesOrder:");

int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:System.out.println("Enter the no of purchase order:");
int n=Integer.parseInt(br.readLine());
PurchaseOrder p[]=new PurchaseOrder[n];
for(int i=0;i<n;i++)
{
p[i]=new PurchaseOrder();
p[i].accept();
}
for(int i=0;i<n;i++)
{
p[i].display();
System.out.println("object is created.");

System.out.println("-------------------------------");
}
break;

case 2:System.out.println("Enter the no of SalesOrder:");


int m=Integer.parseInt(br.readLine());
SalesOrder s[]=new SalesOrder[m];
for(int i=0;i<m;i++)
{
s[i]=new SalesOrder();
s[i].accept();
}
for(int i=0;i<m;i++)
{
s[i].display();
System.out.println("object is created.");

System.out.println("-------------------------------");
}
break;
}
}
}
-------------------------------------------------------------------
import java.io.*;
class Slip7Bank
{
int accno;
String accname;
double balance,newbal;

Slip7Bank(int accno,String accname,double balance)


{
this.accno=accno;
this.accname=accname;
this.balance=balance;
}
void deposite(double deposite_amount)
{
balance=balance+deposite_amount;
System.out.println("your available balance is:"+balance);
}
void withdraw(double amount)
{
if(balance >amount)
{
balance=balance-amount;
System.out.println("your current available balance is:"+balance);
}
else //if(balance <amount)
{
System.out.println("cannot withdraw the amount. Insufficient
balance");
}
}

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


{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the account no.:");
int accno=Integer.parseInt(br.readLine());

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


String accname=br.readLine();

System.out.println("Enter the balance.:");


double balance=Double.parseDouble(br.readLine());
Slip7Bank s=new Slip7Bank(accno,accname,balance);

System.out.println("select option:");
System.out.println("1.deposite:");
System.out.println("2.withdraw:");
int choice=Integer.parseInt(br.readLine());

switch(choice)
{
case 1:System.out.println("Enter the amount to deposite.:");
double amount=Double.parseDouble(br.readLine());
s.deposite(amount);
break;

case 2:System.out.println("Enter the amount to withdraw.:");


double with_amount=Double.parseDouble(br.readLine());
s.withdraw(with_amount);
break;

}
}
}
------------------------------------------------------------------------
//slip 7

import java.io.*;
class FileReverse
{
public static void main(String args[])throws Exception
{
RandomAccessFile f=new RandomAccessFile("data17.txt","r");
for(long i=f.length()-1;i>=0;i--)
{
System.out.print((char)f.read());
f.seek(i);
}
f.close();
}
}
------------------------------------------------------
//slip 8
//calculate voulme and surface area of sphere
import java.io.*;
class Sphere
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

double area,volume;
System.out.println("Enter the radius of sphere:");
float r=Float.parseFloat(br.readLine());

area=4*3.14*r*r;
volume=(4/3)*3.14*r*r*r;

System.out.println("area of sphere is:"+area);


System.out.println("volume of sphere is:"+volume);
}
}
----------------------------------------------------------------------------
//slip 8
//Q2
import java.awt.*;
import java.awt.event.*;
class Mousedemo extends Frame
{
TextField statusBar;
Mousedemo()
{
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
statusBar.setText("Clicked at("+e.getX()+","+e.getY()+")");
repaint();
}
public void mouseEntered(MouseEvent e)
{
statusBar.setText("Entered at("+e.getX()+","+e.getY()+")");
repaint();
}

});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

statusBar=new TextField(20);
setTitle("Mouse clicked position");
setLayout(new FlowLayout());
setSize(275,300);

add(statusBar);
setVisible(true);
}
public static void main(String args[])
{
new Mousedemo().show();
}
}
-------------------------------------------------------------
//slip 9

import java.io.*;

class Clockdemo
{
int hours,minutes,second;
Clockdemo(int h,int m,int s)
{
hours=h;
minutes=m;
second=s;
}
void isTimeValid()
{
if(hours >=0 && hours<24 && minutes>0 &&minutes<60 && second>0
&&second<60)
{
System.out.println("time is valid");
}
else
{
System.out.println("time is not valid");
}
}
void setTimeMode()
{
if(hours <12)
{
System.out.println("time="+ hours +":"+minutes+":"+second+"AM");
}
else
{
hours=hours-12;
System.out.println("time="+ hours +":"+minutes+":"+second+"PM");
}
}
}
class Clock
{
public static void main(String args[])throws Exception
{
Clockdemo c1=new Clockdemo(16,45,23);
c1.isTimeValid();
c1.setTimeMode();
}
}
---------------------------------------------------------------------------
//marker interface slip no 9
//Q2

import java.io.*;
interface ProductMarker
{
}
class Product implements ProductMarker
{
int id,cost,quantity,count;
String name;
Product()
{
id=0;
name=" ";
cost=0;
quantity=0;
count++;
}
Product(int id,String name,int cost,int quantity)
{
this.id=id;
this.name=name;
this.cost=cost;
this.quantity=quantity;
count++;
}
public void display()
{
System.out.println("Product details are:");
System.out.println("Product id:"+id);
System.out.println("Product name:"+name);
System.out.println("Product cost:"+cost);
System.out.println("Product quantity:"+quantity);
System.out.println("---------------------------------------");
}
public void count()
{
System.out.println(count+"No of object created.");
}
}
public class Productdemo
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter how many product:");
int n=Integer.parseInt(br.readLine());

Product p[]=new Product[n];


for(int i=0;i<n;i++)
{
System.out.println("Enter the product id:");
int id=Integer.parseInt(br.readLine());

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


String name=br.readLine();

System.out.println("Enter the product cost:");


int cost=Integer.parseInt(br.readLine());

System.out.println("Enter the product quantity:");


int quantity=Integer.parseInt(br.readLine());

p[i]=new Product(id,name,cost,quantity);

p[i].count();

}
for(int i=0;i<n;i++)
{
p[i].display();
}
if(p[0] instanceof ProductMarker)
{
System.out.println("class is using ProductMarker.");
}

}
------------------------------------------------------
//slip no 10
//Q1

interface cube
{
int calculate(int x);
}
class CubeTest
{
public static void main(String args[])
{
int a=5;
cube c=(int x)->x*x*x;
int ans=c.calculate(a);
System.out.println("The cube of "+a+"is:"+ans);
}
}
-----------------------------------------------------------------
package Student;
public class Studentper
{
public void find(int ct,int et,int cn,int ds,int wt,int m)
{
double per=(ct+et+cn+ds+wt+m)/6;
System.out.println("Percentage:"+per);
}
}

package Student;
public class StudentInfo
{
public int rollno;
public String name;
public void display()
{
System.out.println("Roll no:"+rollno);
System.out.println("name:"+name);
}
}

//slip 10

import Student.StudentInfo;
import Student.Studentper;
import java.io.*;
class Studentdemo
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter student rollno:");


int rollno=Integer.parseInt(br.readLine());

System.out.println("Enter student name:");


String name=br.readLine();

System.out.println("Enter computer marks:");


int ct=Integer.parseInt(br.readLine());
System.out.println("Enter electronic marks:");
int et=Integer.parseInt(br.readLine());

System.out.println("Enter computer network marks:");


int cn=Integer.parseInt(br.readLine());

System.out.println("Enter data science marks:");


int ds=Integer.parseInt(br.readLine());

System.out.println("Enter foundation technolody marks:");


int wt=Integer.parseInt(br.readLine());

System.out.println("Enter maths marks:");


int m=Integer.parseInt(br.readLine());

StudentInfo s2=new StudentInfo();


s2.rollno=rollno;
s2.name=name;
s2.display();

Studentper s1=new Studentper();


s1.find(ct,et,cn,ds,wt,m);
}
}
------------------------------------------------------------------------------

//functional interface that contain only 1 abstract method.


//slip no 16

interface Shape
{
int calculate(int x);
}
class SquareTest
{
public static void main(String args[])
{
int a=5;
Shape s=(int x)->x*x;
int ans=s.calculate(a);
System.out.println(ans);
}
}
------------------------------------------------------
//slip 19
//Sum of diagonal
import java.util.*;
class DiagonalSum
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int i,j,row,col,sum=0;
System.out.println("Enter the number of rows:");
row=sc.nextInt();

System.out.println("Enter the number of cols:");


col=sc.nextInt();
int [][]mat=new int[row][col];

System.out.println("Enter the element of matrix:");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mat[i][j]=sc.nextInt();
}
}
System.out.println("the element of matrix:");
for( i=0;i<row;i++)
{
for( j=0;j<col;j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println(" ");
}
for( i=0;i<row;i++)
{
for( j=0;j<col;j++)
{
if(i==j)
{
sum=sum+mat[i][j];
}
}
}
System.out.println("diagonal sum of element of marks:"+sum);
}

}
------------------------------------------------
//slip 19

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Tydemo extends JFrame
{
private JLabel sub;
private JComboBox tysub;
private JTextField txt;
private Panel p;
public Tydemo()
{
p=new Panel(new FlowLayout());
sub=new JLabel("Subject");
txt=new JTextField(20);

String s1[]={"computer_network","electronic","data science","data


structire","web technology"};
tysub=new JComboBox(s1);

p.add(sub);
p.add(tysub);
p.add(txt);
add(p);

setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("TY BSC COMPUTER SCIENCE");

tysub.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==tysub)
{
txt.setText(tysub.getSelectedItem()+"selected");
}
}
});
}

public static void main(String args[])


{
new Tydemo();

}
}
-------------------------------------------------
import java.awt.*;
//slip 18

import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f=new JFrame();
JButton b1=new JButton("North");
JButton b2=new JButton("South");
JButton b3=new JButton("East");
JButton b4=new JButton("West");
JButton b5=new JButton("Center");

f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);

f.setVisible(true);
f.setSize(300,250);
f.setTitle("Border layout ");
}
public static void main(String args[])
{
new Border();
}
}
--------------------------------------------------
//slip 20

import Operation.*;
import java.io.*;

class OpDemo
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the first number:");


int n1=Integer.parseInt(br.readLine());

System.out.println("Enter the second number:");


int n2=Integer.parseInt(br.readLine());

Addition a1=new Addition();


a1.add(n1,n2);
a1.sub(n1,n2);

Maximum m1=new Maximum();


m1.max(n1,n2);
}
}
-----------------------------
//slip 20
/* write a package for operation which has two classes ,addition and maximum
addition contain two method add and sub
maximum contain max */

package Operation;
public class Addition
{
public void add(float x,float y)
{
float add= x+y;
System.out.println("The addition is :"+add);
}
public void sub(float x,float y)
{
float sub=x-y;
System.out.println("The subtraction is :"+sub);
}
}

//slip 20
/* write a package for operation which has two classes ,addition and maximum
addition contain two method add and sub
maximum contain max */

package Operation;
public class Maximum
{
public void max(float x,float y)
{
if(x>y)
{
System.out.println("The maximum no is:"+x);
}
else
{
System.out.println("The maximum no is:"+y);
}
}

-----------------------------------------------------------------------------

//slip 21
//Q2
// create an employee class .define default and parameterized constructor
//.use 'this' keyword .keep count of object created. (use static member and method)

//display the content of each object....

import java.io.*;
class Employee
{
int id;
String ename,deptname;
float sal;
static int count=0;

Employee()
{
id=0;
ename=" ";
deptname=" ";
sal=0;
count++;
}
Employee(int id,String ename,String deptname,float sal)
{
this.id=id;
this.ename=ename;
this.deptname=deptname;
this.sal=sal;
count++;
}
void display()
{
System.out.println(id+"\t" +ename+"\t" +deptname+"\t" +sal);
}
static void counter()
{
System.out.println(count+"no of object created....");
}
}
class EmpDemo
{
public static void main(String args[]) throws Exception
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Enter the no .of Employee:");


int n=Integer.parseInt(br.readLine());

Employee obj[]=new Employee[n];


for(int i=0;i<n;i++)
{
System.out.println("Enter Employee id:");
int id=Integer.parseInt(br.readLine());

System.out.println("Enter Employee name:");


String ename=br.readLine();

System.out.println("Enter Employee deptname:");


String deptname=br.readLine();

System.out.println("Enter Employee salary:");


float sal=Float.parseFloat(br.readLine());

obj[i]=new Employee(id,ename,deptname,sal);
Employee.counter();

}
for(int i=0;i<n;i++)
obj[i].display();
}

}
---------------------------------------------------------------------------
//slip 27

import java.io.*;
class Employee
{
String name;
int sal;
Employee()
{
name=" ";
sal=0;
}
Employee(String n,int s)
{
name=n;
sal=s;
}
int getSalary()
{
return sal;
}
void display()
{
System.out.println("name:"+name);
System.out.println("sal:"+sal);
}
}
class Manager extends Employee
{
int house,travel_allowance;
Manager()
{
super();
house=0;
travel_allowance=0;
}
Manager(String n,int s,int h,int t)
{
super(n,s);
house=h;
travel_allowance=t;
}
int getSalary() //method overriding
{
return super.getSalary()+house+travel_allowance;
}
void display()
{
super.display();
System.out.println("house:"+house);
System.out.println("travel_allowance:"+travel_allowance);
System.out.println("------------------------------------------");
}
}
class Slip27
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter how many employee:");
int n=Integer.parseInt(br.readLine());

Manager m1[]=new Manager[n];


for(int i=0;i<n;i++)
{
System.out.println("Enter name of employee:");
String name=br.readLine();

System.out.println("Enter salary:");
int sal=Integer.parseInt(br.readLine());

System.out.println("Enter house rent:");


int house=Integer.parseInt(br.readLine());

System.out.println("Enter travel_allowance:");
int travel_allowance=Integer.parseInt(br.readLine());

m1[i]=new Manager(name,sal,house,travel_allowance);
}
for(int i=0;i<n;i++)
{
m1[i].display();
m1[i].getSalary();
}

}
}
-----------------------------------------------------------------------------------
-------
//slip 27
import java.io.*;
class InvalidDateException extends Exception
{}
class MyDate
{
int day,mon,yr;
void accept(int d,int m,int y)
{
day=d;
mon=m;
yr=y;
}
void display()
{
System.out.println("Date is valid:"+day+"/"+mon+"/"+yr);
}
}
class Datemain
{
public static void main(String args[])throws Exception
{
System.out.println("Enter date: dd mm yyyy");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int day=Integer.parseInt(br.readLine());
int mon=Integer.parseInt(br.readLine());
int yr=Integer.parseInt(br.readLine());

int flag=0;
try
{
if(mon<=0 || mon>12)
throw new InvalidDateException();
else
{
if(mon==1|| mon==3 ||mon==5 ||mon==7||mon==8 ||mon==10||
mon==12)
{
if(day>=1 && day<=31)
flag=1;
else
throw new InvalidDateException();
}

else if(mon==2)
{
if(yr%4==0)
{
if(day>=1 &&day<=29)
flag=1;
else
throw new InvalidDateException();
}
else
{
if(day>=1 && day<=28)
flag=1;
else
throw new InvalidDateException();
}
}
else
{
if(mon==4||mon==6||mon==9||mon==11)
{
if(day>=1 && day<=30)
flag=1;
else
throw new InvalidDateException();
}
}
}
if(flag==1)
{
MyDate dt=new MyDate();
dt.accept(day,mon,yr);
dt.display();
}
}
catch(InvalidDateException e)
{
System.out.println("Invalid date");
}
}
}
-----------------------------------------------------------------------------------
--
//slip 29

// create a superclass vehicle with two members company and price.


// derive two class LightMotorVehicle(mileage) and
HeavyMotorVehicle(capacity_in_tons).
//ask user first the type of vehicle

import java.io.*;
class Vehicle
{
private String cname;
private float price;
public void accept()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter company name:");
cname=br.readLine();

System.out.println("Enter price:");
float price=Float.parseFloat(br.readLine());
}
public void display()
{
System.out.println("Company:"+cname+"\n"+"Price:"+price);
}
}

class LightMotorVehicle extends Vehicle


{
private int mileage;
public void accept()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

super.accept();
System.out.println("Enter mileage:");
mileage=Integer.parseInt(br.readLine());
}
public void display()
{
super.display();
System.out.println("mileage:"+mileage);
}
}
class HeavyMotorVehicle extends Vehicle
{
private int capacity;
public void accept()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

super.accept();
System.out.println("Enter capacity_in_tons:");
capacity=Integer.parseInt(br.readLine());
}
public void display()
{
super.display();
System.out.println("capacity_in_tons:"+capacity);
}
}
class VehicleTest
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no of vehicles:");
int n=Integer.parseInt(br.readLine());

Vehicle v[]=new Vehicle[n];


for(int i=0;i<n;i++)
{
System.out.println("LightMotorVehicle[0]/HeavyMotorVehicle[1]:");
int ch=Integer.parseInt(br.readLine());

if(ch==0) v[i]=new LightMotorVehicle();


if(ch==1) v[i]=new HeavyMotorVehicle();
v[i].accept();
}
for(int i=0;i<n;i++)
{
v[i].display();
}
}
}
-----------------------------------------------------------------------------------
-------
//slip no 15
/*define account having member custname,accno,.
define default and parameterized constructor.
create subclass savingaccount with member savingbal,minbal,
create derived class accountdetail that extends savingaccount with member
depositant and withdrawalamt.*/

import java.io.*;
class Account
{
String custname;
long accno;

Account()
{
custname=" ";
accno=0;
}

Account(String custname,long accno)


{
this.custname=custname;
this.accno=accno;
}
}

class SavingAccount extends Account


{
float minbal,savingbal;
SavingAccount()
{
savingbal=0;
minbal=0;
}
SavingAccount(String custname,long accno,float minbal,float savingbal)
{
super(custname,accno);

this.minbal=minbal;
this.savingbal=savingbal;
}

}
class AccountDetail extends SavingAccount
{
float depositant,withdraw;
AccountDetail()
{
depositant=0;
withdraw=0;
}
AccountDetail(String custname,long accno,float minbal,float savingbal,float
depositant,float withdraw)
{
super(custname,accno,minbal,savingbal);

this.depositant=depositant;
this.withdraw=withdraw;
}
void display()
{
System.out.println("------customer account detail---------------");
System.out.println("Customer name:"+custname+"\n"+"account
no:"+accno+"\n");
System.out.println("saving balance:"+savingbal+"\n"+"mininum
bal:"+minbal+"\n");
System.out.println("deposite balance:"+depositant+"\n"+"withdraw
bal:"+withdraw);
}
}
class Detail
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter how many customer detail you want to


enter:");
int n=Integer.parseInt(br.readLine());

AccountDetail c[]=new AccountDetail[n];


for(int i=0;i<n;i++)
{
System.out.println("Enter the customer name:");
String custname=br.readLine();

System.out.println("Enter the customer account no:");


long accno=Long.parseLong(br.readLine());

System.out.println("Enter the saving balance:");


float savingbal=Float.parseFloat(br.readLine());

System.out.println("Enter the minimum bal:");


float minbal=Float.parseFloat(br.readLine());

System.out.println("Enter the deposite balance:");


float depositant=Float.parseFloat(br.readLine());

System.out.println("Enter the withdraw bal:");


float withdraw=Float.parseFloat(br.readLine());

AccountDetail ac=new
AccountDetail(custname,accno,minbal,savingbal,depositant,withdraw);
ac.display();
}

}
}
-----------------------------------------------------------------------------------
-------------------------------------------
//slip 16

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;

public class menudemo extends Frame


{
menudemo()
{
MenuBar mBar=new MenuBar();
Menu file=new Menu("File");
Menu edit=new Menu("Edit");
Menu about=new Menu("About");
setMenuBar(mBar);

MenuItem new_file=new MenuItem("New CNTRL+N");


MenuItem open_file=new MenuItem("Open");
MenuItem save_file=new MenuItem("Save CNTRL+S");
MenuItem exit_file=new MenuItem("Exit");
MenuItem show=new MenuItem("show About.");

file.add(new_file);
file.add(open_file);
file.add(save_file);

file.addSeparator();
file.add(show);

file.addSeparator();
file.add(exit_file);

mBar.add(file);
mBar.add(edit);
mBar.add(about);

setSize(300,300);
setVisible(true);
setTitle("Java Awt program.");
}
public static void main(String args[])
{
new menudemo();

}
}
-----------------------------------------------------------------------------------
-------------------
//slip 11 Q1

//define an interface "operation "which has area(),volumn().


//constant pi value=3.142 create a class cylinder which implements this interface.
//(members -radius,height),create one obj and cal area and volumn

//interface has final var and abstract method.


import java.io.*;
interface Operation
{
final double pi=3.142;
double area();
double volume();
}
class Cylinder implements Operation
{
double radius,height;
Cylinder(double radius,double height)
{
this.height=height;
this.radius=radius;
}
public double area() //overridding
{
return 2*pi*radius*radius*height; //2*pi*r*r*h
}
public double volume()
{
return pi*radius*radius*height; //pi*r*r*h
}
public static void main(String args[])throws Exception
{
Operation obj;
double r,h;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter radius and height:");
r=Double.parseDouble(br.readLine());
h=Double.parseDouble(br.readLine());

obj=new Cylinder(r,h);

System.out.println("Area of cylinder is:"+obj.area());


System.out.println("Volume of cylinder is:"+obj.volume());
}
}
-----------------------------------------------------------------------------------
---------------------------------------------
//slip 27

import java.io.*;
class demofile
{
public static void main(String args[])throws IOException
{
String fname=args[0];
File f=new File(fname);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String ans;
if(f.isDirectory())
{
System.out.println(fname+"is a directory.");

String list[]=f.list();
for(int i=0;i<list.length;i++)
{
System.out.println(list[i]);
}
System.out.println("Number of files :"+list.length);

for(int i=0;i<list.length;i++)
{
if(list[i].endsWith(".txt"));
{
File f2=new File(fname+"/"+list[i]);
System.out.println("Are u sure u want to delete
file ?");
ans=br.readLine();
if(ans.equals("yes"))
{
f2.delete();
System.out.println("File deleted."+list[i]);
}
}
}
}

if(f.isFile())
{
System.out.println(fname+"is a file.");
System.out.println("Path:"+f.getAbsolutePath());
System.out.println("size"+f.length());
}
}
}
-----------------------------------------------------------------------------------
-------------------------------------------------------
//book author slip 15 Q1

import java.io.*;
class File
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the first file name:");
String f1=br.readLine();

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


String f2=br.readLine();

//FileInputStream file1=new FileInputStream(f1);


FileReader file1=new FileReader(f1);

//FileOutputStream file2=new FileOutputStream(f2);


FileWriter file2=new FileWriter(f2);

int ch1;
while((ch1=file1.read())!=-1)
{
char ch2=(char)ch1;
file2.write(ch2);
}

file2.write("end of file");
file1.close();
file2.close();
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------
//slip 11

import java.io.*;

class InvalidPasswordException extends Exception{}

public class Slip11


{
public static void main(String args[])throws IOException
{
String username,password;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter username:");
username=br.readLine();

System.out.println("Enter password:");
password=br.readLine();
try
{
if(username.equals(password))
{
System.out.println("valid username and password.");
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e)
{
System.out.println("invalid password.");
System.out.println("Enter the correct username and password");
}
}
}

You might also like