0% found this document useful (0 votes)
5 views31 pages

Java Programming Practical Solutions

This document contains solved practical slips for a Java Programming course for the academic year 2022-23. It includes various programming exercises such as printing prime numbers, defining classes with inheritance, calculating BMI, sorting city names, and handling exceptions. Additionally, it provides solutions for matrix operations and GUI programming using AWT.

Uploaded by

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

Java Programming Practical Solutions

This document contains solved practical slips for a Java Programming course for the academic year 2022-23. It includes various programming exercises such as printing prime numbers, defining classes with inheritance, calculating BMI, sorting city names, and handling exceptions. Additionally, it provides solutions for matrix operations and GUI programming using AWT.

Uploaded by

jayware
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TY BCS

Java Programming - I
Solved Practical Slips
2022-23

Solution Credit goes to


Akshay, Deep, Atharv, Maithili, Sakshi, Shreya, Prajakta
[Link] | [Link]
Subscribe to our YouTube Channel for BCS Study videos
[Link]
Click on the below link to join our Free Study Material WhatsApp Group
[Link]

Follow us on Instagram
@logic_overflow

MOBILE: 9730381255 | [Link] | [Link]


Slip 1_1: Write a Program to print all Prime numbers in an array of ‘n’
elements.(use command line arguments)

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;

for (int j=2; j<array[i]; j++)


{

if(array[i]%j==0)
{
isPrime = false;
break;
}
}

if(isPrime)

[Link](array[i] + " are the prime numbers in the array ");


}
}
}

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

abstract class Staff


{
protected int id;
protected String name;

MOBILE: 9730381255 | [Link] | [Link]


public Staff(int id,String name)
{
[Link]=id;
[Link]=name;
}
}
class OfficeStaff extends Staff
{
String dept;
OfficeStaff(int id,String name,String dept)
{
super(id,name);
[Link]=dept;
}
public void display()
{
[Link]("ID :"+id+" Name :"+name+" Department :"+dept);
}
public static void main(String args[])
{
int n,id;
String name,dept;
Scanner sc=new Scanner([Link]);
[Link]("How many staff members?");
n=[Link]();
OfficeStaff ob[]=new OfficeStaff[n];
[Link]("Enter details of "+n+" staff");
for(int i=0;i<n;i++)
{

[Link]("Enter id,name, department");


id=[Link]();
name=[Link]();
dept=[Link]();
ob[i]=new OfficeStaff(id,name,dept);
}
[Link]("Entered Details are\n");
for(int i=0;i<n;i++)
{
ob[i].display();
}
}
}

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

MOBILE: 9730381255 | [Link] | [Link]


// body mass index
class BM {
public static void main(String args[]) {
String fname = args[0];
String lname = args[1];
double weight = [Link](args[2]);
double height = [Link](args[3]);
double BMI = weight / (height * height);
[Link]("First name is:" +fname);
[Link]("Last Name is:" + lname);
[Link]("weight is:" + weight);
[Link]("height is:"+ height);
[Link]("The Body Mass Index (BMI) is " + BMI + " kg/m2");
}
}

Slip2_2: Define a class CricketPlayer (name,no_of_innings,no_of_times_notout, totatruns,


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. Displaythe player details in sorted order.

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{

MOBILE: 9730381255 | [Link] | [Link]


BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the name, no of innings, no of times not out, total runs: ");
name=[Link]();
inning=[Link]([Link]());
tofnotout=[Link]([Link]());
totalruns=[Link]([Link]());
}
public void put(){
[Link]("Name="+name);
[Link]("no of innings="+inning);
[Link]("no times notout="+tofnotout);
[Link]("total runs="+totalruns);
[Link]("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){
[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;

MOBILE: 9730381255 | [Link] | [Link]


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

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

MOBILE: 9730381255 | [Link] | [Link]


}
Slip3_1: Write a program to accept ‘n’ name of cities from the user and sort them in
ascendingorder.

import [Link];

class SortStr
{
public static void main(String args[])
{
String temp;
Scanner SC = new Scanner([Link]);

[Link]("Enter the value of N: ");


int N= [Link]();
[Link](); //ignore next line character

String names[] = new String[N];

[Link]("Enter names: ");


for(int i=0; i<N; i++)
{
[Link]("Enter name [ " + (i+1) +" ]: ");
names[i] = [Link]();
}

//sorting strings

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


{
for(int j=1; j<N; j++)
{
if(names[j-1].compareTo(names[j])>0)
{
temp=names[j-1];
names[j-1]=names[j];
names[j]=temp;
}
}
}
[Link]("\nSorted names are in Ascending Order: ");
for(int i=0;i<N;i++)
{
[Link](names[i]);
}

MOBILE: 9730381255 | [Link] | [Link]


}
}

Slip 3_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 CovidPositive(+)
and Need to Hospitalized” otherwise display its information.

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

MOBILE: 9730381255 | [Link] | [Link]


hrct=[Link]([Link]());
Patient ob=new Patient(name,age,level,hrct);
try{
if([Link]<95 && [Link]>10)

throw new CovidException();

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++)
{

MOBILE: 9730381255 | [Link] | [Link]


for(int j=0;j<r;j++)
{
[Link](" " +mat[j][i]);
}
[Link](" ");
}
}
}
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 [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");

MOBILE: 9730381255 | [Link] | [Link]


Clear=new Button("Clear");
[Link](this);
[Link](this);
[Link](uname);
[Link](nametext);
[Link](upass);
[Link](passtext);
[Link](login);
[Link](Clear);
[Link](msg);
add(p);
setTitle("Login ");
setSize(290,200);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Button btn=(Button)([Link]());
if(attempt<3)
{
if(([Link]())=="Clear")
{
[Link]("");
[Link]("");
}
if(([Link]()).equals("Login"))
{
try
{
String user=[Link]();
String upass=[Link]();
if([Link](upass)==0)
{
[Link]("Valid");
[Link]("Username is valid");
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e)

MOBILE: 9730381255 | [Link] | [Link]


{
[Link]("Error");
}
attempt++;
}
}
else
{
[Link]("you are using 3 attempt");
[Link](0);
}
}
public static void main(String args[])
{
PasswordDemo pd=new PasswordDemo();
[Link]();
}
}
Slip5_1: Write a program for multilevel inheritance such that Country is inherited from
[Link] is inherited from Country. Display the place, State, Country and Continent.

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

class Country extends Continent


{
String cou;
void cou_input()throws IOException
{
[Link]("Enter the country name:");
cou = [Link]();}
}

MOBILE: 9730381255 | [Link] | [Link]


class State extends Country
{

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
{

[Link]("Enter the place name:");


pla = [Link]();}

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


{
Main s = new Main();
s.con_input();
s.cou_input();
s.sta_input();
s.pla_input();
[Link]("place is:"+[Link]);
[Link]("state is:"+[Link]);
[Link]("country is:"+[Link]);
[Link]("continent is:"+[Link]);
}
}

Slip5_2: Write a menu driven program to perform the following operations on


multidimensional arrayie matrices :
 Addition
Multiplication

import [Link].*;
class Matrix
{
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int M[][] = new int[a][b];
void accept()
{

MOBILE: 9730381255 | [Link] | [Link]


int a = this.a;
int b = this.b;
[Link]("enter the "+(a*b)+ " values to matrix:");
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
this.M[i][j] = [Link]();
}

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

[Link]("enter the size:");


Matrix m2 = new Matrix();
[Link]();
[Link]("values to matrix 2:");
[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]));

MOBILE: 9730381255 | [Link] | [Link]


}
[Link](" ");
}
break;
case 2:
[Link]("Multiplication is:");
for(int i=0;i<m2.a;i++)
{
for(int j=0;j<m2.b;j++)
{
[Link](" "+
(m1.M[i][j]*m2.M[i][j]));
}
[Link](" ");
}
break;

case 3:
[Link](0);
}
}
}
}

Slip6_1: Write a program to display the Employee(Empid, Empname,


mpdesignation, Empsal)information using toString().
.
class Emp
{
int id,salary;
String name;
String desig;
Emp(int id, String name, int salary ,String desig)
{
[Link]=id;
[Link]=name;
[Link]=salary;
[Link]=desig;
}
public String toString() // overrides toString() method
{
return id+" "+name+" "+salary+" "+desig;
}
public static void main(String args[])
{
Emp E1=new Emp(111,"Rakesh",50000,"bsc cs");
Emp E2=new Emp(112,"Suresh",25000,"msc cs");
[Link]("Employee details: "+E1);
[Link]("Employee details: "+E2);

MOBILE: 9730381255 | [Link] | [Link]


}
}

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

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

MOBILE: 9730381255 | [Link] | [Link]


[Link]("-------------------");
}
}
class Main{
public static void main(String args[])throws IOException{
int i;
[Link]("Select any one:");
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("[Link] order:");
[Link]("[Link] order:");
[Link]("[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);
}
}
}

MOBILE: 9730381255 | [Link] | [Link]


Slip7_1: 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

class Bank
{
private double balance;

public Bank()
{
balance = 0;

public Bank(double initialBalance)


{
balance = initialBalance;
}

public void deposit(double amount)


{
balance = balance + amount;
}

public void withdraw(double amount)


{
balance = balance - amount;
}

public double getBalance()


{
return balance;
}

public static void main(String[] args)


{
Bank b = new Bank(1000);
[Link](250);
[Link]("the withdraw is:"+ [Link]);
[Link](400);
[Link]("the deposit is:"+ [Link]);
[Link]("the balance is:"+ [Link]());
}

MOBILE: 9730381255 | [Link] | [Link]


}

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

MOBILE: 9730381255 | [Link] | [Link]


}
}

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

Slip8_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 [Link].*;
import [Link].*;
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();
[Link](new GridLayout(2,2,5,5));
t=new TextField(20);
l= new Label("Co-ordinates of mouse clicking");
l1= new Label("Co-ordinates of mouse movement");
t1=new TextField(20);
[Link](l);
[Link](t);
[Link](l1);
[Link](t1);

MOBILE: 9730381255 | [Link] | [Link]


add(p);
addMouseListener(new MyClick());
addMouseMotionListener(new MyMove());
setSize(500,500);
setVisible(true);
}
class MyClick extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
x=[Link]();
y=[Link]();
[Link]("X="+x+" Y="+y);
}
}
class MyMove extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
x=[Link]();
y=[Link]();
[Link]("X="+ x +" Y="+y);
}
}
}
class frame1
{
public static void main(String args[])
{
MyFrame f = new MyFrame("Set A-2");
}
}
Slip9_1: 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 [Link].*;
class Clock
{
int hours,minutes,seconds;
Clock()
{
[Link]("enter the time in HH MM SS format");
Scanner sc= new Scanner([Link]);
[Link] = [Link]();

MOBILE: 9730381255 | [Link] | [Link]


[Link] = [Link]();
[Link] = [Link]();
}
void isTimeValid()
{
if(hours>=0 && hours<24 && minutes>0 &&minutes<60
&&seconds>0 && seconds<60)
[Link]("time is valid");
else
[Link]("time is not valid");
}
void setTimeMode()
{
if(hours<12)
{
[Link]("time ="
+hours+":"+minutes+":"+seconds +" AM");
}
else
hours = hours-12;
[Link]("time ="
+hours+":"+minutes+":"+seconds +" PM");
}
public static void main(String args[])
{

Clock c = new Clock();


[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 {

class product implements MarkerInt {


int pid, pcost, quantity;

MOBILE: 9730381255 | [Link] | [Link]


String pname;
static int cnt;
// Default constructor

product() {
pid = 1;
pcost = 10;
quantity = 1;
pname = "pencil";
cnt++;
}

// Parameterized constructor

product(int id, String n, int c, int q) {


pid = id;
pname = n;
pcost = c;
quantity = q;
cnt++;
[Link]("\nCOUNT OF OBJECT IS : " + cnt + "\n");
}

public void display() {

[Link]("\t" +pid + "\t" + pname + "\t" + pcost + "\t" + quantity);

}
}

class MarkerInterface {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number of Product : ");


int n = [Link]();

product pr[] = new product[n];


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

[Link]("\nEnter " + (i + 1) + " Product Details :\n");

MOBILE: 9730381255 | [Link] | [Link]


[Link]("Enter Product ID: ");
int pid = [Link]();

[Link]();
[Link]("Enter Product Name: ");
String pn = [Link]();

[Link]("Enter Product Cost:");


int pc = [Link]();

[Link]("Enter Product Quantity:");


int pq = [Link]();

pr[i] = new product(pid, pn, pc, pq);

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

MOBILE: 9730381255 | [Link] | [Link]


double cue = cu*cu*cu;
[Link]("cube of no is:"+cue);
return 0;
}
public static void main(String args[])
{
Draw d = new Draw();
[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;

public void display()


{
[Link]("Roll_no : "+r_no);
[Link]("Name : "+name);
[Link]("class :"+clas);
[Link]("-----MARKS-------");
[Link]("Sub 1 : "+a);
[Link]("Sub 2 : "+b);
[Link]("Sub 3 : "+c);
[Link]("Sub 4 : "+d);
[Link]("Sub 5 : "+e);
[Link]("Sub 6 : "+f);
[Link]("Total : "+sum);
[Link]("percentage: "+per);
[Link]("------------------");

MOBILE: 9730381255 | [Link] | [Link]


}
}

public class StudentPer extends StudentInfo {


public StudentPer(int roll, String nm, String cla,int m1,int m2,int m3,int m4, int
m5,int m6)
{
r_no = roll;
clas = cla;
name = nm;
a = m1;
b = m2;
c = m3;
d = m4;
e = m5;
f = m6;
sum = a+b+c+d+e+f;
per = sum/6;
}
}

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

MOBILE: 9730381255 | [Link] | [Link]


m5 = [Link]();
m6 = [Link]();

StudentPer s = new StudentPer(roll,nm,clas,m1,m2,m3,m4,m5,m6);

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

MOBILE: 9730381255 | [Link] | [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);

MOBILE: 9730381255 | [Link] | [Link]


add(login);
add(end);
setTitle("Login Check");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

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

MOBILE: 9730381255 | [Link] | [Link]


{
cnt++;
[Link](null, "Login Failed", "Login",
JOptionPane.ERROR_MESSAGE);
[Link]("");
[Link]("");
[Link]();
if(cnt==3)
{
[Link](null,"3 Attempts Over",
"Login", JOptionPane.ERROR_MESSAGE);
[Link](0);
}

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

MOBILE: 9730381255 | [Link] | [Link]


}
class Dept extends college
{
int dno;
String dname;
Scanner sc = new Scanner([Link]);
public void accept()
{
[Link]("enter no");
no=[Link]();
[Link]("enter name");
name=[Link]();
[Link]("enter college address");
addr=[Link]();
[Link]("enter depatrment no");
dno=[Link]();
[Link]("enter department name");
dname=[Link]();
}
public void display()
{
[Link]("college no"+no);
[Link]("college name"+name);
[Link]("college address"+addr);
[Link]("department number"+dno);
[Link]("department number"+dname);
}
public static void main(String a[])
{
Dept ob=new Dept();
[Link]();
[Link]();

MOBILE: 9730381255 | [Link] | [Link]

You might also like