Final Slips
Final Slips
1. Write a Program to print all Prime Numbers in an array of n elements (use command line
argument)
import java.io.*;
class Slip1_1
{
public static void main(String args[]) throws IOException
{
int i,num, count;
num=Integer.parseInt(args[0]);
System.out.println();
System.out.println("The Prime Numbers are");
for (i = 0; i <a.length; i++)
{
count = 0;
for (int j = 2; j <a[i]; j++)
{
if (a[i] % j == 0)
{
count++;
break;
}
}
if (count == 0)
{
System.out.print(a[i]+" ");
}
}
}
}
--------------------------------------------------------------------------
Slip1_2
2. Define an abstract class Staff with protected members id and name. Define a parameterized
constructors.Define a subclass OfficeStaff with member department. Create n object of OfficeStaff
and display all details.
import java.io.*;
abstract class Staff
{
protected int id;
protected String name;
class Slip1_2
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int id,n,i;
String nm,dept;
System.out.println("enter how many records");
n=Integer.parseInt(br.readLine());
os[i]=new OfficeStaff(id,nm,dept);
}
System.out.println("The data is ");
for(i=0;i<n;i++)
os[i].Display();
}
}
-----------------------------------------------------------------------------
Slip2_1: Write a program to read FirstName and LastName of a person, his weight and height using
command line argument.Calculate the BMI Index which is defined as the individual's body mass
divided by the square of the height.
(Hint: BMI=Wts. in Kg /(ht2)
class Slip2_1
{
public static void main(String args[])
{
String fname=args[0];
String lname=args[1];
double weight =Double.parseDouble(args[2]);
double height =Double.parseDouble(args[3]);
-----------------------------------------------------------------------------
Slip2_2 : Define class CricketPlayer (name,no_of_innings,no_of_times_notout,totalruns,bat_avg).
Create an array of n player objects. Calculate the batting average for each player using static
method avg(). Define a static sort method which sorts the array on the basis of average. Display
the player details in sorted order.
import java.io.*;
class Cricket
{
String name;
int inning, tofnotout, totalruns;
float batavg;
public Cricket()
{
name=null;
inning=0;
tofnotout=0;
totalruns=0;
batavg=0;
}
public void get() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name, no of innings, no of times not out, total runs: ");
name=br.readLine();
inning=Integer.parseInt(br.readLine());
tofnotout=Integer.parseInt(br.readLine());
totalruns=Integer.parseInt(br.readLine());
}
public void put()
{
System.out.println("Name="+name);
System.out.println("no of innings="+inning);
System.out.println("no times notout="+tofnotout);
System.out.println("total runs="+totalruns);
System.out.println("bat avg="+batavg);
}
static void avg(int n, Cricket c[])
{
try
{
for(int i=0;i<n;i++)
{
c[i].batavg=c[i].totalruns/c[i].inning;
}
}catch(ArithmeticException e){
System.out.println("Invalid arg");
}
}
static void sort(int n, Cricket c[])
{
String temp1;
int temp2,temp3,temp4;
float temp5;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(c[i].batavg<c[j].batavg)
{
temp1=c[i].name;
c[i].name=c[j].name;
c[j].name=temp1;
temp2=c[i].inning;
c[i].inning=c[j].inning;
c[j].inning=temp2;
temp3=c[i].tofnotout;
c[i].tofnotout=c[j].tofnotout;
c[j].tofnotout=temp3;
temp4=c[i].totalruns;
c[i].totalruns=c[j].totalruns;
c[j].totalruns=temp4;
temp5=c[i].batavg;
c[i].batavg=c[j].batavg;
c[j].batavg=temp5;
}
}
}
}
}
Output:
Name=rusher
no of innings=2
no times notout=2
total runs=150
bat avg=75.0
---------------------------------------------------------------------------
Slip3_1: Write a program to accept 'n' anmes of cities from the user and sort them in ascending
order.
import java.io.*;
class Slip3_1
{
public static void main(String args[])throws IOException
{
String temp;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//sorting strings
-----------------------------------------------------------------------------
Slip3_2: Define a class patient (patient_name, patient_age, patient_oxy_level,patient_HRCT_report).
Create an object of patient. Handle appropriate exception while patient oxygen level less than 95%
and HRCT scan report greater than 10, then throw user defined Exception “Patient is Covid
Positive(+) and Need to Hospitalized” otherwise display its information
import java.io.*;
class Patient
{
String patient_name;
int patient_age,patient_oxy_level,patient_HRCT_level;
int getOxylevel()
{
return patient_oxy_level;
}
int getHrctreport()
{
return patient_HRCT_level;
}
}
class ExceptionEg1
{
public static void main(String args[]) throws IOException
{
String nm;
int age,oxy,hrct;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
}
catch(MyException me)
{
System.out.println("Error ="+me.getMessage());
System.out.println("Oxygen level ="+pobj.getOxylevel());
System.out.println("HRCT report ="+pobj.getHrctreport());
}
finally
{
System.out.println("Entered Data is ");
pobj.Display();
}
}
}
Slip4_1
Q1) Write a program to print an array after changing the rows and columns of a given two-
dimensional array.
public class Slip3_1
{
public static void main(String arg[])
{
int i, j;
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix Value:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
Output:
Enter total rows and columns:
23
Enter matrix Value:
10 20 30 40 50 60
The above matrix value before Changing
10 20 30
40 50 60
The above matrix value after changing
10 40
20 50
30 60
Slip4_2:
Write a program to design a screen using Awt that will take a user name and password. If the user
name and password are not same, raise an Exception with appropriate message. User can have 3
login chances only. Use clear button to clear the TextFields.
import javax.naming.InvalidNameException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
void setMethod()
{
p = new Panel();
setTitle("Email");
setSize(350, 300);
setVisible(true);
setResizable(false);
p.add(userNameLabel);
p.add(username);
p.add(userPasswordLabel);
p.add(password);
p.add(Login);
p.add(Clear);
p.add(message);
add(p);
Login.addActionListener(this);
Clear.addActionListener(this);
if (ae.getSource() == Login)
{
String uname = username.getText();
String pname = password.getText();
if (uname.compareTo(pname) == 0)
{
message.setText("Valid");
System.out.println("Email is Valid..");
}
else
{
message.setText("Invalid");
throw new InvalidPasswordException();
}
}
attempt++;
}
else
{
System.out.println("Attempt is greater than 3!!!");
}
}
catch(Exception e)
{
System.out.println(e);
Slip5_1:
Q1) Write a program for multilevel inheritance such that Country is inherited from Continent. State
is inherited from Country. Display the place, State, Country and Continent.
import java.util.Scanner;
class Continent
{
String continent;
Scanner sc = new Scanner(System.in);
public void coninput()
{
System.out.println("Enter the Continent: ");
continent =sc.next();
}
}
System.out.println("Continent : "+obj.continent);
System.out.println("Country : "+obj.country);
System.out.println("State : "+obj.state);
System.out.println("Area : "+obj.area);
}
}
Slip5_2:
Q2) Write a menu driven program to perform the following operations on multidimensional array ie
matrices :
Addition
Multiplication
Exit
import java.util.Scanner;
class Slip5_2
{
public static void main(String arr[])
{
int a,b,n,ch;
Scanner sc = new Scanner(System.in);
System.out.println("How many number of rows in matrix:");
a = sc.nextInt();
do
{
System.out.println("1.Addition:");
System.out.println("2.Multiplication:");
System.out.println("3.Exit:");
switch(ch)
{
case 1:
int sum[][] = new int[a][b];
for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
sum[i][j] = m1[i][j] + m2[i][j];
}
}while(ch!=3);
}
}
Slip6_1
Q1) Write a program to display the Employee(Empid, Empname, Empdesignation, Empsal)
information using toString().
class Employee
{
int Empid;
String Empname, Empdesignation;
float Empsal;
Slip6_2:
Q2) Create an abstract class “order” having members id, description. Create two subclasses
“PurchaseOrder” and “Sales Order” having members customer name and Vendor name respectively.
Definemethods accept and display in all cases. Create 3 objects each of Purchase Order and Sales
Order and accept and display details.
import java.util.*;
abstract class order
{
int id;
String descp;
Scanner sc = new Scanner(System.in);
public void setData(int id, String descp)
{
this.id=id;
this.descp=descp;
}
abstract public void accept();
abstract public void display();
}
System.out.println("Enter ID :");
int cid = sc.nextInt();
sc.nextLine();
System.out.println("Enter Description :");
String desc = sc.nextLine();
p[i].setData(cid, desc);
p[i].accept();
}
System.out.println("\n\t\tPurchased Details.\n");
System.out.println("\tID\tDescription\tCname");
for (int i = 0; i < 3; i++)
{
p[i].display();
}
System.out.println("Enter ID :");
int cid = sc.nextInt();
sc.nextLine();
System.out.println("Enter Description :");
String desc = sc.nextLine();
s[i].setData(cid, desc);
s[i].accept();
}
System.out.println("\n\t\tSales Details.\n");
System.out.println("\tID\tDescription\tVname");
for (int i = 0; i < 3; i++)
{
s[i].display();
}
}
}
Slip7_1
Q1) Design a class for Bank. Bank Class should support following operations; a. Deposit a certain
amount into an account b. Withdraw a certain amount from an account c. Return a Balance value
specifying the amount with details
import java.util.Scanner;
void deposit()
{
System.out.println("Enter the money u want to deposit:");
int deposit_money = sc.nextInt();
money = money + deposit_money;
System.out.println("Successfully Deposit");
System.out.println("Account Balance is:"+money);
}
void withdraw()
{
System.out.println("Enter the how much money u want to withdraw:");
int withdraw_money = sc.nextInt();
if(money>=withdraw_money)
{
money = money - withdraw_money;
System.out.println("Successfully Withdraw");
System.out.println("Account Balance is:"+money);
}
}
void accountDetail()
{
System.out.println("Account Holder:"+name);
System.out.println("Account Balnce:"+ money);
}
do {
System.out.println("\n Enter ur choice:");
System.out.println("1.Deposit\n2.Withdraw\n3.Amount Detail");
System.out.println("Enter ur choice enter 0 to end:");
ch = sc.nextInt();
switch(ch)
{
case 1: obj.deposit();
break;
case 2: obj.withdraw();
break;
case 3: obj.accountDetail();
break;
default:
System.out.println("Invalid Case!!!");
}
}while(ch!=0);
}
}
D:\TYBsc2022\Java_Slips>java Bank
Create ur Bank Account:
Enter the name of the new Account Holder:
aa
Enter the money u want to Deposit
23000
Enter ur choice:
1.Deposit
2.Withdraw
3.Amount Detail
Enter ur choice enter 0 to end:
3
Account Holder:aa
Account Balnce:23000
Enter ur choice:
1.Deposit
2.Withdraw
3.Amount Detail
Enter ur choice enter 0 to end:
1
Enter the money u want to deposit:
200
Successfully Deposit
Account Balance is:23200
Enter ur choice:
1.Deposit
2.Withdraw
3.Amount Detail
Enter ur choice enter 0 to end:
3
Account Holder:aa
Account Balnce:23200
Enter ur choice:
1.Deposit
2.Withdraw
3.Amount Detail
Enter ur choice enter 0 to end:
2
Enter the how much money u want to withdraw:
10000
Successfully Withdraw
Account Balance is:13200
Enter ur choice:
1.Deposit
2.Withdraw
3.Amount Detail
Enter ur choice enter 0 to end:
3
Account Holder:aa
Account Balnce:13200
Enter ur choice:
1.Deposit
2.Withdraw
3.Amount Detail
Enter ur choice enter 0 to end:
0
Invalid Case!!!
D:\TYBsc2022\Java_Slips>
Slip6_2
Q2) Write a program to accept a text file from user and display the contents of a file in reverse order
and change its case.
import java.io.*;
import java.util.*;
class read
{
public static void main(String args[]) throws IOException
{
FileReader f = new FileReader("sample.txt");
Scanner sc = new Scanner(f);
String CH,CH2;
while(sc.hasNext())
{
StringBuilder CH1 = new StringBuilder();
CH = sc.next();
CH2=CH.toUpperCase();
CH1.append(CH2);
CH1.reverse();
System.out.println(CH1);
}
f.close();
}
}
Slip7_1:
Q1) Create a class Sphere, to calculate the volume and surface area of sphere. (Hint : Surface
area=4*3.14(r*r), Volume=(4/3)3.14(r*r*r))
import java.util.Scanner;
double v = (4/3)*3.14*(r*r*r);
double sa = 4*3.14*(r*r);
************Output***********************************
D:\TYBsc2022\Java_Slips>javac SphereVolume.java
D:\TYBsc2022\Java_Slips>java SphereVolume
Enter the radius of the sphere:
3
The volume of the Sphere is: 84.78
The surface area of the Sphere is: 113.04
D:\TYBsc2022\Java_Slips>
Slip7_2:
2) Design a screen to handle the Mouse Events such as MOUSE_MOVED and MOUSE_CLICKED and
display the position of the Mouse_Click in a TextField
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame
{
TextField t,t1;
Label l,l1;
int x,y;
Panel p;
MyFrame(String title)
{
super(title);
setLayout(new FlowLayout());
p=new Panel();
p.setLayout(new GridLayout(2,2,5,5));
t=new TextField(20);
l= new Label("Co-ordinates of clicking");
l1= new Label("Co-ordinates of movement");
t1=new TextField(20);
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
add(p);
addMouseListener(new MyClick());
addMouseMotionListener(new MyMove());
setSize(500,500);
setVisible(true);
}
class MyClick extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
x=me.getX();
y=me.getY();
t.setText("X="+x+" Y="+y);
}
}
class MyMove extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
x=me.getX();
y=me.getY();
t1.setText("X="+ x +" Y="+y);
}
}
}
class mouseclick
{
public static void main(String args[])
{
MyFrame f = new MyFrame("Mouse screen");
}
}
Slip8_1:
Q1) Define a “Clock” class that does the following ;
a. Accept Hours, Minutes and Seconds
b. Check the validity of numbers
c. Set the time to AM/PM mode
Use the necessary constructors and methods to do the above task.
import java.util.Scanner;
void display()
{
System.out.println(hour+":"+min+":"+sec+"|"+mode);
}
/* OUTPUT:
Enter the Hour, Minutes & Seconds:
10
50
30
Set the mode of time AM/Pm
AM
10:50:30|AM
*/
Slip8_2:
Q2) Write a program to using marker interface create a class Product (product_id, product_name,
product_cost, product_quantity) default and parameterized constructor. Create objects of class
product and display the contents of each object and Also display the object count.
import java.util.*;
class Product
{
int id;
String name;
int cost;
int quantity;
int count;
Product()
{
id=0;
name=" ";
cost=0;
quantity=0;
}
Product(int id, String name, int cost, int quantity)
{
this.id=id;
this.name=name;
this.cost=cost;
this.quantity=quantity;
this.count++;
}
public void Display()
{
System.out.println("Product Id " + id);
System.out.println("Product name " + name);
System.out.println("Product cost " + cost);
System.out.println("Product qantity " + quantity);
System.out.println("\n");
}
}
class Products
{
public static void main(String[] args)
{
int count=0;
Scanner a = new Scanner(System.in);
System.out.println("How many product ?");
int number = a.nextInt();
System.out.println("\n");
Product products[] = new Product[number];
System.out.println("Enter Product data");
for(int k=0; k<number; k++)
{
System.out.println("Product Id ");
int id =a.nextInt();
System.out.println("Product name ");
String name = a.next();
System.out.println("Product cost ");
int cost = a.nextInt();
System.out.println("Product qantity ");
int quantity = a.nextInt();
System.out.println("\n");
products[k] = new Product(id, name, cost, quantity);
count++;
}
/*
//Testing for marker interface
if(products[0] instanceof ProductMarker)
{
System.out.println("Class is using ProductMarker");
}
System.out.println(" Product details\n");
for(Product product:products)
{
System.out.println("Product Id " + product.id);
System.out.println("Product name " + product.name);
System.out.println("Product cost " + product.cost);
System.out.println("Product qantity " + product.quantity);
System.out.println("\n");
}
for(int k=0; k<number; k++)
products[k].Display();
*/
System.out.println("Total object is "+count);
}
}
D:\TYBsc2022\Java_Slips>javac Products.java
D:\TYBsc2022\Java_Slips>java Products
How many product ?
2
Product Id
2
Product name
w
Product cost
12
Product qantity
1
Product details
Product Id 1
Product name q
Product cost 21
Product qantity 1
Product Id 2
Product name w
Product cost 12
Product qantity 1
Total object is 2
D:\TYBsc2022\Java_Slips>
Slip9_1:
Q1) Write a program to find the cube of given number using functional interface.
interface Cube
{
void getCube(int n);
}
class Slip9_1
{
public static void main(String[] args)
{
FindCube c = new FindCube ();
c.getCube(3);
}
}
Slip9_2:
Q2) Write a program to create a package name student. Define class StudentInfo with method to
display information about student such as rollno, class, and percentage. Create another class
StudentPer with method to find percentage of the student. Accept student details like rollno, name,
class and marks of 6 subject from user.