0% found this document useful (0 votes)
703 views29 pages

Final Slips

The document contains code snippets and questions from programming assignments. 1) The first code snippet defines classes Staff and OfficeStaff to create objects representing employees with id, name, and department. It creates n OfficeStaff objects and displays their details. 2) The second snippet defines a CricketPlayer class to store player details and calculate batting average. It creates an array of players, calculates averages, sorts by average, and displays sorted player details. 3) The document contains programming assignments on prime number generation, BMI calculation from arguments, sorting city names, handling exceptions for patient oxygen and HRCT levels, changing 2D array dimensions, and designing an AWT login screen.

Uploaded by

shubham wadekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
703 views29 pages

Final Slips

The document contains code snippets and questions from programming assignments. 1) The first code snippet defines classes Staff and OfficeStaff to create objects representing employees with id, name, and department. It creates n OfficeStaff objects and displays their details. 2) The second snippet defines a CricketPlayer class to store player details and calculate batting average. It creates an array of players, calculates averages, sorts by average, and displays sorted player details. 3) The document contains programming assignments on prime number generation, BMI calculation from arguments, sorting city names, handling exceptions for patient oxygen and HRCT levels, changing 2D array dimensions, and designing an AWT login screen.

Uploaded by

shubham wadekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Slip 1:

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;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

num=Integer.parseInt(args[0]);

int a[]=new int[num];


System.out.println("enter "+ num+" numbers");
for(i=0;i<num;i++)
a[i]=Integer.parseInt(br.readLine());

System.out.println("Entered Array is");


for(i=0;i<num;i++)
System.out.print(" "+a[i]);

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;

Staff(int id,String nm)


{
this.id=id;
this.name=nm;
}
}

class OfficeStaff extends Staff


{
String department;
OfficeStaff(int id,String nm,String dept)
{
super(id,nm);
this.department=dept;
}

public void Display()


{
System.out.println("Employee Id="+id);
System.out.println("Employee Name="+name);
System.out.println("Employee Department="+department);
}
}

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());

OfficeStaff os[]=new OfficeStaff[n];


for(i=0;i<n;i++)
{
System.out.println("enter id,name and department");
id=Integer.parseInt(br.readLine());
nm=br.readLine();
dept=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]);

double BMI = weight / (height * height);


System.out.print("The Body Mass Index (BMI) is " + BMI + " kg/m2");
}
}

-----------------------------------------------------------------------------
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;
}
}
}
}
}

public class Slip2_2


{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the limit:");
int n=Integer.parseInt(br.readLine());
Cricket c[]=new Cricket[n];
for(int i=0;i<n;i++){
c[i]=new Cricket();
c[i].get();
}
Cricket.avg(n,c);
Cricket.sort(n, c);
for(int i=0;i<n;i++)
{
c[i].put();
}

Output:

Enter the limit:


2

Enter the name, no of innings, no of times not out, total runs: 


suresh
1
1
100

Enter the name, no of innings, no of times not out, total runs: 


rusher
2
2
150
Name=suresh
no of innings=1
no times notout=1
total runs=100
bat avg=100.0

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));

System.out.print("Enter the value of N: ");


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

String names[] = new String[n];

System.out.println("Enter names: ");


for(int i=0; i<n; i++)
{
System.out.print("Enter name [ " + (i+1) +" ]: ");
names[i] = br.readLine();
}

//sorting strings

for(int i=0; i<n; i++)


{
for(int j=1; j<n; j++)
{
if(names[j-1].compareTo(names[j])>0) //for ascending >0
{
temp=names[j-1];
names[j-1]=names[j];
names[j]=temp;
}
}
}
System.out.println("\nSorted names are in Descending Order: ");
for(int i=0;i<n;i++)
{
System.out.println(names[i]);
}
}
}

-----------------------------------------------------------------------------
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 MyException extends Exception


{
MyException(String s)
{
super(s);
}
}

class Patient
{
String patient_name;
int patient_age,patient_oxy_level,patient_HRCT_level;

Patient(String nm,int age,int oxy,int hrct)


{
this.patient_name=nm;
this.patient_age=age;
this.patient_oxy_level=oxy;
this.patient_HRCT_level=hrct;
}

public void Display()


{
System.out.println("Patient Name ="+patient_name);
System.out.println("Patient Age ="+patient_age);
System.out.println("Patient Oxygen level ="+patient_oxy_level);
System.out.println("Patient HRCT report ="+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));

System.out.println("enter patient name,age,oxygen level and hrct report");


nm=br.readLine();
age=Integer.parseInt(br.readLine());
oxy=Integer.parseInt(br.readLine());
hrct=Integer.parseInt(br.readLine());

Patient pobj=new Patient(nm,age,oxy,hrct);


try
{
if(pobj.getOxylevel()<95 && pobj.getHrctreport()>10)
throw new MyException("Patient is Covid Positive(+) and need to be Hospitalized");

}
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(" ");
}
}

System.out.println("The above matrix value before Changing ");


for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix value after changing ");
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}

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;

class InvalidPasswordException extends Exception


{
InvalidPasswordException()
{
System.out.println("Invalid Name and Password!!!");
}

public class Email extends Frame implements ActionListener


{
Label userNameLabel, userPasswordLabel;
Button Login, Clear;
TextField username, password, message;
Panel p;
char ch = '*';
int attempt = 0;

void setMethod()
{
p = new Panel();

setTitle("Email");
setSize(350, 300);
setVisible(true);
setResizable(false);

userNameLabel = new Label("Enter the UserName:");


userPasswordLabel = new Label("Enter the UserPassword:");

username = new TextField(20);


password = new TextField(20);
password.setEchoChar(ch);
message = new TextField(20);
message.setEnabled(false);

Login = new Button("Login");


Clear = new Button("Clear");

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);

public static void main(String[] args)


{
Email obj = new Email();
obj.setMethod();
}

public void actionPerformed(ActionEvent ae)


{
try
{
if(attempt < 3)
{
if (ae.getSource() == Clear)
{
username.setText("");
password.setText("");
username.requestFocus();
}

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();
}
}

class Country extends Continent


{
String country;
public void couinput()
{
System.out.println("Enter the Country: ");
country = sc.next();
}
}

class State extends Country


{
String state;
public void stateinput()
{
System.out.println("Enter the State: ");
state = sc.next();
}
}

class Multilevel_Inheritance extends State


{
String area;

public void AreaInput()


{
System.out.println("Enter the Area: ");
area = sc.next();
}
}

public class Slip5_1


{
public static void main(String[] args)
{
Multilevel_Inheritance obj = new Multilevel_Inheritance();
obj.coninput();
obj.couinput();
obj.stateinput();
obj.AreaInput();

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();

System.out.println("How many number of columns in matrix:");


b = sc.nextInt();

int m1[][] = new int[a][b];


int m2[][] = new int[a][b];
System.out.println("Enter the 1st matrix:");
for(int i=0; i<m1.length; i++)
{
for(int j=0; j<m1.length; j++)
{
m1[i][j] = sc.nextInt();
}
}

System.out.println("1st matrix is:");


for(int i=0; i<m1.length; i++)
{
for(int j=0; j<m1.length; j++)
{
System.out.print(m1[i][j]+" ");
}
System.out.println();
}

System.out.println("Enter the 2st matrix:");


for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
m2[i][j] = sc.nextInt();
}
}
System.out.println("2st matrix is:");
for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
System.out.print(m2[i][j]+" ");
}
System.out.println();
}

do
{
System.out.println("1.Addition:");
System.out.println("2.Multiplication:");

System.out.println("3.Exit:");

System.out.println("Plz Enter ur choice:");


ch = sc.nextInt();

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];
}

System.out.println("Addition of m1 and m2:");


for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
break;
case 2:
int k=0;
int mul[][] = new int[a][b];
for(int i=0; i<m2.length; i++)
{

for(int j=0; j<m2.length; j++)


{
for(k=0;k<m2.length;k++)
mul[i][j] =mul[i][j]+ m1[i][j] * m2[k][j];
}

System.out.println("Multiplication of m1 and m2:");


for(int i=0; i<m2.length; i++)
{
for(int j=0; j<m2.length; j++)
{
System.out.print(mul[i][j]+" ");
}
System.out.println();
}
break;

}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;

Employee(int Empid,String Empname,String Empdesignation,float Empsal)


{
this.Empid = Empid;
this.Empname = Empname;
this.Empdesignation = Empdesignation;
this.Empsal = Empsal;
}
public String toString()
{
return "\nEmployee id :"+Empid+" \nEmployee Name :"+Empname+"\n Employee
Designation :"+Empdesignation+"\n Employee Salary :"+Empsal;
}

public static void main(String[] args)


{
Employee obj = new Employee(100,"Swapnil","London",50000);
Employee obj1 = new Employee(101,"Saurabh","London",55000);
Employee obj2 = new Employee(102,"Omkar","France",10000);
Employee obj3 = new Employee(103,"Abhi","America",44000);
System.out.println(obj);
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
}

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();
}

class purchase_order extends order


{
String cname;
public void accept()
{
System.out.println("Enter Customer Name :");
String n = sc.nextLine();
cname=n;
}

public void display()


{
System.out.println("\t"+id +"\t"+descp+"\t\t"+cname);
}
}

class sales_order extends order


{
String vname;
public void accept()
{
System.out.println("Enter Vendor Name :");
String n = sc.nextLine();
vname=n;
}

public void display()


{
System.out.println("\t"+id +"\t"+descp+"\t\t"+vname);
}
}

public class Slip6_2


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

purchase_order p[] = new purchase_order[3];


for (int i = 0; i < 3; i++)
{
p[i] = new purchase_order();

System.out.println("\nEnter "+(i+1)+" Customer Data :");

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();
}

sales_order s[] = new sales_order[3];


for (int i = 0; i < 3; i++)
{
s[i] = new sales_order();

System.out.println("\nEnter "+(i+1)+" Vendor Data :");

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;

public class Bank


{
String name;
int money;
Scanner sc = new Scanner(System.in);
void createAccount()
{
System.out.println("Enter the name of the new Account Holder:");
name = sc.next();
System.out.println("Enter the money u want to Deposit");
money = sc.nextInt();
}

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);
}

public static void main(String[] args)


{

Scanner sc = new Scanner(System.in);


System.out.println("Create ur Bank Account:");
Bank obj = new Bank();
obj.createAccount();
int ch;

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;

public class SphereVolume


{

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the radius of the sphere:");
float r = sc.nextFloat();

double v = (4/3)*3.14*(r*r*r);
double sa = 4*3.14*(r*r);

System.out.println("The volume of the Sphere is: "+v);


System.out.println("The surface area of the Sphere is: "+sa);
}
}

************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;

public class Clock


{
int hour,min,sec;
String mode;

Scanner sc = new Scanner(System.in);


Clock()
{
System.out.println("Enter the Hour, Minutes & Seconds:");
hour = sc.nextInt();
min = sc.nextInt();
sec = sc.nextInt();
}
void checkValid()
{
if(hour>12 && hour<1)
System.out.println("Ur input is wrong..");
if(min>60 && min<0)
System.out.println("Ur input is wrong..");
if(sec>60 && sec<0)
System.out.println("Ur input is wrong..");
}
void setTime()
{
System.out.println("Set the mode of time AM/Pm");
mode = sc.next();
}

void display()
{
System.out.println(hour+":"+min+":"+sec+"|"+mode);
}

public static void main(String[] args)


{
Clock obj = new Clock();
obj.checkValid();
obj.setTime();
obj.display();
}
}

/* 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

Enter Product data


Product Id
1
Product name
q
Product cost
21
Product qantity
1

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 FindCube implements Cube


{

public void getCube(int n)


{
System.out.println("The area of the rectangle is " + (n * n * 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.

You might also like