Professional Documents
Culture Documents
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());
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 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());
System.out.println("Enter no of inning:");
int inn=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());
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;
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];
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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));
p.con_input();
p.cou_input();
p.stat_input();
p.pla_input();
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:");
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;
}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;
}
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;
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());
}
}
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());
}
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;
System.out.println("-------------------------------");
}
break;
}
}
}
-------------------------------------------------------------------
import java.io.*;
class Slip7Bank
{
int accno;
String accname;
double balance,newbal;
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;
}
}
}
------------------------------------------------------------------------
//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;
});
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());
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));
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();
}
------------------------------------------------
//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);
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");
}
}
});
}
}
}
-------------------------------------------------
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));
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)
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));
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());
System.out.println("Enter salary:");
int sal=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
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);
}
}
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());
import java.io.*;
class Account
{
String custname;
long accno;
Account()
{
custname=" ";
accno=0;
}
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));
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;
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
obj=new Cylinder(r,h);
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();
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.*;
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");
}
}
}