SET A
1. Define a class CricketPlayer (name, no_of_innings, no_times_notout, total_runs, bat_avg). Create an array of n player objects. Calculate the batting average for each player using a static method avg(). Handle appropriate exception while calculating average. Define a static method “sortPlayer” which sorts the array on the basis of average. Display the player details in sorted order.
2. Define a class SavingAccount (acNo, name, balance). Define appropriate constructors and operations withdraw(), deposit() and viewBalance(). The minimum balance must be 500. Create an object and perform operations. Raise user defined InsufficientFundsException when balance is not sufficient for withdraw operation.
SET B
1. Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid. Examples of invalid dates : 12 15 2015, 31 6 1990, 29 2 2001.
1. Define a class CricketPlayer (name, no_of_innings, no_times_notout, total_runs, bat_avg). Create an array of n player objects. Calculate the batting average for each player using a static method avg(). Handle appropriate exception while calculating average. Define a static method “sortPlayer” which sorts the array on the basis of average. Display the player details in sorted order.
2. Define a class SavingAccount (acNo, name, balance). Define appropriate constructors and operations withdraw(), deposit() and viewBalance(). The minimum balance must be 500. Create an object and perform operations. Raise user defined InsufficientFundsException when balance is not sufficient for withdraw operation.
SET B
1. Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid. Examples of invalid dates : 12 15 2015, 31 6 1990, 29 2 2001.
3 Comments
Slip 1_1: Write a Program to print all Prime numbers in an array of ‘n’
ReplyDeleteelements. (use command line arguments)
Solution:
class PrNo
{
public static void main (String[] args)
{
int size = args.length;
int[] array = new int [size];
for(int i=0; i<size; i++)
{
array[i] = Integer.parseInt(args[i]);
}
for(int i=0; i<array.length; i++)
{
boolean isPrime = true;
for (int j=2; j<array[i]; j++)
{
if(array[i]%j==0)
{
isPrime = false;
break;
}
}
if(isPrime)
System.out.println(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 java.util.*;
abstract class Staff
{
protected int id;
protected String name;
public Staff(int id,String name)
{
this.id=id;
this.name=name;
}
}
class OfficeStaff extends Staff
{
String dept;
OfficeStaff(int id,String name,String dept)
{
super(id,name);
this.dept=dept;
}
public void display()
{
System.out.println("ID :"+id+" Name :"+name+" Department :"+dept);
}
public static void main(String args[])
{
int n,id;
String name,dept;
Scanner sc=new Scanner(System.in);
System.out.println("How many staff members?");
n=sc.nextInt();
OfficeStaff ob[]=new OfficeStaff[n];
System.out.println("Enter details of "+n+" staff");
for(int i=0;i<n;i++)
{
System.out.println("Enter id,name, department");
id=sc.nextInt();
name=sc.next();
dept=sc.next();
ob[i]=new OfficeStaff(id,name,dept);
}
System.out.println("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
ReplyDeleteheight using command line arguments. Calculate the BMI Index which is defined as the
individual's body mass divided by the square of their height.
(Hint : BMI = Wts. In kgs / (ht)2
// body mass index
class BM {
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.println("First name is:" +fname);
System.out.println("Last Name is:" + lname);
System.out.println("weight is:" + weight);
System.out.println("height is:"+ height);
System.out.println("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. 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;
MOBILE: 9730381255 | WWW.NRCLASSESPUNE.COM | WWW.BCSBCA.COM
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(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();
}
}
This comment has been removed by the author.
ReplyDelete