OneCompiler

uk2

129

Q1
mport java.util.Scanner;
public class Main {
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("Input First name: ");
String Fname = input.nextLine();
System.out.print("Input last name: ");
String Lname = input.nextLine();
System.out.print("Input weight in KG: ");
double weight = input.nextDouble();
System.out.print("Input height in Meters: ");
double height = input.nextDouble();
double BMI = weight / (height * height);
System.out.print("Body Mass Index is " + BMI+"\n");
}
}
Q2
mport java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class CricketPlayer {
String name;
int no_of_innings;
int no_of_times_not_out;
int totlaruns;
float bat_avg;
static int count;
CricketPlayer() {
}
CricketPlayer(String name, int no_of_innings, int no_of_times_not_out, int
totlaruns , float bat_avg) {
count++;
this.name=name;
this.no_of_innings=no_of_innings;
this.no_of_times_not_out=no_of_times_not_out;
this.totlaruns=totlaruns;
this.bat_avg=bat_avg;
}
void display() {
System.out.println("NAME: "+name+" INNINGS: "+no_of_innings+" NOT
OUT: "+no_of_times_not_out+" TOTAL RUNS: "+totlaruns+" BATTING
AVERAGE: "+bat_avg);
}
float getBatAvg() {
return bat_avg;
}
void counter() {
System.out.println(count + " object is created");
}
public static void sortStudent(CricketPlayer s[], int n) {
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (s[j].getBatAvg() > s[j + 1].getBatAvg()) {
CricketPlayer t = s[j];
s[j] = s[j + 1];
s[j + 1] = t;
}
}
}
System.out.println("-----List of Player sorted as per Batting Average-----");
for (int i = 0; i < n; i++)
s[i].display();
}
}
public class ass2_SetB_q2 {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter no. of Player:");
int n = Integer.parseInt(br.readLine());
CricketPlayer p[] = new CricketPlayer[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter Name:");
String name = br.readLine();
System.out.print("Enter Innings:");
int no_of_innings = Integer.parseInt(br.readLine());
System.out.println("Enter number of times not out");
int no_of_times_not_out = Integer.parseInt(br.readLine());
System.out.println("Enter total runs");
int totlaruns = Integer.parseInt(br.readLine());
System.out.println("Enter Batting Average");
float bat_avg = Float.parseFloat(br.readLine());
p[i] = new
CricketPlayer(name,no_of_innings,no_of_times_not_out,totlaruns,bat_avg);
p[i].counter();
}
CricketPlayer.sortStudent(p, CricketPlayer.count);
}
}