Java Programming Practical Solutions
Java Programming Practical Solutions
Java Programming - I
Solved Practical Slips
2022-23
Follow us on Instagram
@logic_overflow
Solution:
class PrNo
{
public static void main (String[] args)
{
int size = [Link];
int[] array = new int [size];
for(int i=0; i<size; i++)
{
array[i] = [Link](args[i]);
}
for(int i=0; i<[Link]; i++)
{
boolean isPrime = true;
if(array[i]%j==0)
{
isPrime = false;
break;
}
}
if(isPrime)
Slip 1_2: Define an abstract class Staff with protected members id and name. Define a parameterized
constructor. Define one subclass OfficeStaff with member department. Create n objects of
OfficeStaff and display all details.
Solution:
import [Link].*;
Slip2_1: Write a program to read the First Name and Last Name of a person, his weight and
height using command line arguments. Calculate the BMI Index which is defined as the
individual's body massdivided by the square of their height.
(Hint : BMI = Wts. In kgs / (ht)2
import [Link].*;
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{
}
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){
[Link]("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;
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;
}
}
}
}
}
class Name {
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the limit:");
int n=[Link]([Link]());
Cricket c[]=new Cricket[n];
for(int i=0;i<n;i++){
c[i]=new Cricket();
c[i].get();
}
[Link](n,c);
[Link](n, c);
for(int i=0;i<n;i++){
c[i].put();
}
import [Link];
class SortStr
{
public static void main(String args[])
{
String temp;
Scanner SC = new Scanner([Link]);
//sorting strings
import [Link].*;
class CovidException extends Exception{
public CovidException(){
[Link]("Patient is Covid Positive and needs to be hospitalized");
}
}
class Patient{
String name;
int age;
double level,hrct;
public Patient(String name,int age,double level,double hrct)
{
[Link]=name;
[Link]=age;
[Link]=level;
[Link]=hrct;
}
public static void main(String[] args)throws IOException
{
String name;
int age;
double level,hrct;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter name: ");
name=[Link]();
[Link]("Enter the age: ");
age=[Link]([Link]());
[Link]("Oxygen level: ");
level=[Link]([Link]());
[Link]("HRCT report: ");
else
[Link]("Patient Info: \n"+"Name: "+[Link]+"\nAge: "+[Link]+"\nHRCT
report: "+[Link]+"\nOxygen level:"
+[Link]);
}catch(CovidException e){
}
}
}
Slip4_1: Write a program to print an array after changing the rows and columns of a giventwo-
dimensional array.
import [Link].*;
class ArrTrans
{
public static void main(String args[])
{
[Link]("enter the row and column");
Scanner sc = new Scanner([Link]);
int r = [Link]();
int c = [Link]();
int mat[][] = new int[r][c];
[Link]("enter the array elts:");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
mat[i][j] = [Link]();
}
}
[Link]("the matrix is:");
for(int i=0;i<c;i++)
{
import [Link].*;
import [Link].*;
class InvalidPasswordException extends Exception
{
InvalidPasswordException()
{
[Link](" User name and Password is not same");
}
}
class PasswordDemo extends Frame implements ActionListener
{
Label uname,upass;
TextField nametext;
TextField passtext,msg;
Button login,Clear;
Panel p;
int attempt=0;
char c= '*' ;
public void login()
{
p=new Panel();
uname=new Label("Use Name: " ,[Link]);
upass=new Label ("Password: ",[Link]);
nametext=new TextField(20);
passtext =new TextField(20);
[Link](c);
msg=new TextField(10);
[Link](false);
login=new Button("Login");
import [Link];
import [Link];
import [Link];
class Continent{
String con;
InputStreamReader i = new InputStreamReader([Link]);
BufferedReader r = new BufferedReader(i);
void con_input() throws IOException
{
[Link]("Enter the continent name:");
con = [Link]();
}
}
String sta;
void sta_input()throws IOException
{
[Link]("Enter the state name:");
sta = [Link]();}
}
class Main extends State
{
String pla;
void pla_input()throws IOException
{
import [Link].*;
class Matrix
{
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int M[][] = new int[a][b];
void accept()
{
}
}
void display()
{
for(int i =0;i<a;i++)
{
for(int j =0;j<b;j++)
{
[Link](" "+this.M[i][j]);
}
[Link](" ");
}
}
public static void main(String a[])
{
[Link]("enter size 2*2 or 3*3 or ...");
Matrix m1 = new Matrix();
[Link]();
[Link]("values to matrix 1:");
[Link]();
int choice;
Scanner scanner = new Scanner([Link]);
while(true) {
[Link]("Press 1: Addition, 2: Multiplication, 3: Exit");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Addition is:" );
for(int i=0;i<m1.a;i++)
{
for(int j=0;j<m1.b;j++)
{
[Link](" "+ (m1.M[i][j]+m2.M[i][j]));
case 3:
[Link](0);
}
}
}
}
Slip6_2: 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 [Link];
import [Link];
import [Link];
abstract class Order{
String id,des;
}
class Porder extends Order{
String cnm, vnm;
public void accept()throws IOException{
[Link]("enter id, description,names of customers and vendors");
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
id = [Link]();
des= [Link]();
cnm = [Link]();
vnm = [Link]();
}
public void display(){
[Link]("id"+id);
[Link]("Description"+des);
[Link]("Customer Name"+cnm);
[Link]("Vendor Name"+vnm);
[Link]("-------------------");
}
}
int ch = [Link]([Link]());
switch(ch){
case 1:
[Link]("enter the no of purchas order:");
int n = [Link]([Link]());
Porder[] l = new Porder[n];
for(i=0;i<n;i++)
{
l[i] = new Porder();
l[i].accept();
}
for(i=0;i<n;i++)
{
l[i].display();
[Link]("Object is created:");
}
case 2:
[Link]("enter the no of sales order:");
int m = [Link]([Link]());
Porder[] h = new Porder[m];
for(i=0;i<m;i++)
{
h[i] = new Porder();
h[i].accept();
}
for(i=0;i<m;i++)
{
h[i].display();
[Link]("Object is created:");
}
case 3:
[Link]("exit:");
[Link](0);
}
}
}
class Bank
{
private double balance;
public Bank()
{
balance = 0;
Slip7_2: 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 [Link].*;
import [Link].*;
class ReverseFile
{
public static void main(String args[])throws IOException
{
Scanner sc = new Scanner([Link]);
[Link]("enter file name:");
String fnm = [Link]();
File f = new File(fnm);
if([Link]())
{
BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(fnm));
int size =[Link]();
for(int i = size-1;i>=0;i--)
{
[Link](i);
[Link](i);
char ch=((char)[Link]());
if([Link](ch))
ch=[Link](ch);
else if([Link](ch))
ch = [Link](ch);
[Link](ch);
[Link]();
}
[Link]();
}
else
[Link]("file not found");
Slip8_1: 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 [Link].*;
class Sphere
{
public static void main (String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the radius of the sphere: ");
double radius=[Link]();
double surface_area = (4*3.14*(radius*radius));
double volume = ((double)4/3)*3.14*(radius*radius*radius);
[Link]("The surface area of the sphere = "+surface_area);
[Link]("The volume of sphere = "+volume);
}
}
import [Link].*;
class Clock
{
int hours,minutes,seconds;
Clock()
{
[Link]("enter the time in HH MM SS format");
Scanner sc= new Scanner([Link]);
[Link] = [Link]();
Slip9_2: Write a program to using marker interface create a class Product (product_id,
product_name, product_cost, product_quantity) default and parameterized constructor. Create
objectsof classproduct and display the contents of each object and Also display the object
count.
import [Link].*;
interface MarkerInt {
product() {
pid = 1;
pcost = 10;
quantity = 1;
pname = "pencil";
cnt++;
}
// Parameterized constructor
}
}
class MarkerInterface {
[Link]();
[Link]("Enter Product Name: ");
String pn = [Link]();
}
[Link]("\n\t\t Product Details\n");
[Link]("\tId\tPname\tCost\tQuantity\n");
for (int i = 0; i < n; i++) {
pr[i].display();
}
[Link]();
}
Slip10_1: Write a program to find the cube of given number using functional interface.
import [Link].*;
interface Cube
{
float cube();
}
class Draw implements Cube
{
public float cube()
{
[Link]("enter the number");
Scanner sc= new Scanner ([Link]);
float cu = [Link]();
Slip10_2: 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.
PackageFIle
package student;
class StudentInfo
{
public int r_no;
public String name, clas;
public int a,b,c,d,e,f;
int sum=0;
double per;
Main File
import [Link];
import [Link].*;
import [Link].*;
import [Link].*;
class StudentMain
{
public static void main(String[] args)
{
String nm, clas;
int roll;
Scanner sc = new Scanner([Link]);
[Link]("Enter Roll no:= ");
roll = [Link]();
[Link]("Enter Name:= ");
nm = [Link]();
[Link]("Enter class:= ");
clas= [Link]();
int m1,m2,m3,m4,m5,m6;
[Link]("Enter 6 sub mark:= ");
m1 = [Link]();
m2 = [Link]();
m3 = [Link]();
m4 = [Link]();
[Link]();
}
}
Slip11_1: Define an interface “Operation” which has method volume( ).Define a constant PI
having a value 3.142 Create a class cylinder which implements this interface (members –
radius,height). Createone object and calculate the volume.
import [Link].*;
interface Operation
{
final static float pi=3.142f;
void area();
void volume();
}
class Cylinder implements Operation
{
double radius,height;
void input() throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("\n Enter the radius and height=");
radius=[Link]([Link]());
height=[Link]([Link]());
}
public void area()
{
double a=(2*pi*radius*height)+(2*pi*radius*radius);
[Link]("the area of cylinder is " +a);
}
public void volume()
{
double v=pi*radius*radius*height;
[Link]("the volume of cylinder is "+v);
}
}
class slipno11a
{
public static void main(String args[]) throws Exception
{
Cylinder C1=new Cylinder();
[Link]();
[Link]();
Slip11_2: Write a program to accept the username and password from user if username and
password arenot same then raise "Invalid Password" with appropriate msg.
import [Link].*;
import [Link].*;
import [Link].*;
class InvalidPasswordException extends Exception
{}
class Userpassword extends JFrame implements ActionListener
{
JLabel name, pass;
JTextField nameText;
JPasswordField passText;
JButton login, end;
static int cnt=0;
Userpassword()
{
name = new JLabel("Name : ");
pass = new JLabel("Password : ");
nameText = new JTextField(20);
passText = new JPasswordField(20);
login = new JButton("Login");
end = new JButton("End");
[Link](this);
[Link](this);
setLayout(new GridLayout(3,2));
add(name);
add(nameText);
add(pass);
add(passText);
}
public void actionPerformed (ActionEvent e)
{
if([Link]()==end)
{
[Link](0);
}
if([Link]()==login)
{
try
{
String user = [Link]();
String pass = new String([Link]());
if([Link](pass)==0)
{
[Link](null, "Login Successful",
"Login", JOptionPane. INFORMATION_MESSAGE);
[Link](0);
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e1)
}
}
}
public static void main(String args[])
{
new Userpassword();
}
}
Slip12_1: Write a program to create parent class College(cno, cname, caddr) and derived
classDepartment(dno, dname) from College. Write a necessary methods to display College
details.
import [Link].*;
class college
{
int no;
String name;
String addr;