BSCCS2005: Programs discussed in the live session
Week {1-7}
1. Week 2 Problem 1
import [Link].*;
class Student {
private String name;
private double marks [];
public Student(String name, double[] marks) {
[Link] = name;
[Link] = marks;
}
public String getName() {
return ([Link]);
}
public double findTotal() {
double total = 0.0;
for (double i : [Link]) {
total = total + i;
}
return (total);
}
}
class Main {
public static String getMax(Student[] student) {
double max_marks = 0.0;
String max_student = "";
for (Student i : student) {
double total_marks = [Link]();
if (total_marks > max_marks) {
max_student = [Link]();
max_marks = total_marks;
}
}
return max_student;
}
public static void main(String[] args) {
Student[] arr = new Student[3];
Scanner sc = new Scanner([Link]);
for(int i = 0; i < 3; i++){
String name = [Link]();
double[] m = {[Link](), [Link](),[Link]()};
arr[i] = new Student(name, m);
}
[Link](getMax(arr));
}
}
Page 2
2. Week 2 Problem 2
import [Link].*;
class Employee{
String name;
String[] projects;
public Employee(String n, String[] proj){
name = n;
projects = proj;
}
public Employee(Employee e){
[Link] = [Link];
int l = [Link];
[Link] = new String[l];
for(int i = 0; i < l; i++){
[Link][i] = [Link][i];
}
}
public void setName(String n) {
name = n;
}
public void setProject(int index, String proj) {
projects[index] = proj;
}
public String getName() {
return name;
}
public String getProject(int indx) {
return projects[indx];
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] proj = {"PJ1","PJ2","PJ3"};
Employee e1 = new Employee("Surya", proj);
Employee e2 = new Employee(e1);
Page 3
[Link]([Link]());
[Link](0, [Link]());
[Link]([Link]() + ": " + [Link](0));
[Link]([Link]() + ": " + [Link](0));
}
}
Page 4
3. Week 3 Problem 1
class Employee{
private String name;
private double salary;
public Employee(String n , double s){
[Link] = n;
[Link] = s;
}
public void setName(String n){
[Link] = n;
}
public void setSalary(double s){
[Link] = s;
}
public String getName(){
return [Link];
}
public double getSalary(){
return [Link];
}
public double bonus(float percent){
return (percent/100.0)*[Link];
}
}
class Manager extends Employee{
private String secretary;
public Manager(String n , double s, String se){
super(n,s);
[Link] = se;
}
public void setSecretary(String se){
[Link] = se;
}
public String getSecretary(){
return [Link];
}
public double bonus(float percent){
Page 5
double b = [Link](percent);
return (50.0/100.0 * b) + b;
}
public class Main {
public static void main(String[] args) {
Employee obj1 = new Manager("jack", 1000.00 , "adam");
[Link]("Employee name:" + [Link]() + ", Salary:" + [Link]
[Link]("jin");
[Link](2000.00);
Manager obj2 = new Manager("rick", 12000.00 , "meera");
[Link]("elena");
[Link]("Employee name:" + [Link]() + ", Salary:" + [Link]
}
}
Page 6
4. Week 3 Problem 2
class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
[Link] = name;
[Link] = salary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void print() {
[Link]("Name: " + name + ", Salary: " + salary);
}
public void print(float percent) {
double bonus = bonus(percent);
[Link]("Name: " + name + ", Salary: " + salary + ", Bonus: " +
}
public double bonus(float percent) {
return (percent / 100.0) * salary;
}
}
class Manager extends Employee {
private String secretary;
public Manager(String name, double salary, String secretary) {
super(name, salary);
[Link] = secretary;
}
public void print() {
[Link]("Name: " + getName() + ", Salary: " + getSalary() + ",
}
Page 7
public void print(float percent) {
double bonus = bonus(percent);
[Link]("Name: " + getName() + ", Salary: " + getSalary() +
", Secretary: " + secretary + ", Bonus: " + bonus);
}
public double bonus(float percent) {
return [Link](percent) * 1.5;
}
}
public class InheritanceTest {
public static void main(String[] args) {
Employee obj1 = new Employee("John", 50000.0);
Manager obj2 = new Manager("Alice", 70000.0, "Bob");
[Link]();
[Link](10.0f);
[Link]();
[Link]();
[Link](15.0f);
}
}
Page 8
5. Week 4 Problem 1
abstract class Shopping{
abstract void offers();
}
class Amazon extends Shopping{
void offers(){
[Link]("Great Indian festival sale from 6th-8th Oct 2023");
}
}
class Flipkart extends Shopping{
void offers(){
[Link]("Big Billion Days from 5th-9th Oct 2023");
}
}
class User{
public void getOffers(Shopping obj){
[Link]();
}
}
public class Main{
public static void main(String[] args){
User u = new User();
[Link](new Amazon());
[Link](new Flipkart());
}
}
Page 9
6. Week 4 Problem 2
interface Appraisable {
default void increment(Employee e) {
[Link](5000);
}
void checkAndUpdateSalary();
}
interface SpecialAppraisable extends Appraisable {
default void spIncrement(Employee e) {
[Link](7000);
}
}
class Employee implements SpecialAppraisable {
private String name;
private double salary;
private int numLeaves;
public Employee(String name, double salary, int numLeaves) {
[Link] = name;
[Link] = salary;
[Link] = numLeaves;
}
public void updateSalary(double amount) {
salary += amount;
}
public double getSalary() {
return salary;
}
public void checkAndUpdateSalary() {
if (salary >= 50000 && numLeaves <= 6) {
increment(this);
} else if (salary < 50000 && numLeaves <= 6) {
spIncrement(this);
} else if (salary < 50000 && numLeaves > 6) {
increment(this);
}
}
Page 10
public String toString() {
return name + " " + salary + " " + numLeaves;
}
}
public class InterfaceTest {
public static void printUpdatedEmpList(Employee[] eList) {
for (Employee employee : eList) {
[Link]();
}
for (Employee employee : eList) {
[Link](employee);
}
}
public static void main(String[] args) {
Employee[] eArr = {
new Employee("John", 52000, 5),
new Employee("Alice", 48000, 7),
new Employee("Bob", 55000, 3)
};
printUpdatedEmpList(eArr);
}
}
Page 11
7. Week 4 Problem 3
interface Generatable {
double billGenerate(int units);
}
class PowerBill {
private double totalBill;
private class Domestic implements Generatable {
public double billGenerate(int units) {
if (units > 0 && units <= 100) {
totalBill = units * 1.6;
} else if (units > 100 && units <= 200) {
totalBill = units * 2.5;
} else if (units > 200) {
totalBill = units * 5.6;
}
return totalBill;
}
}
private class Commercial implements Generatable {
public double billGenerate(int units) {
if (units > 0 && units <= 100) {
totalBill = units * 5.6;
} else if (units > 100 && units <= 200) {
totalBill = units * 7.5;
} else if (units > 200) {
totalBill = units * 20.00;
}
return totalBill;
}
}
public Generatable getDomesticBill() {
return new Domestic();
}
public Generatable getCommercialBill() {
return new Commercial();
}
Page 12
}
public class BillingTest {
public static String getBill(int units, String type) {
PowerBill powerBill = new PowerBill();
if (units < 0) {
return "Invalid input for units";
} else if ([Link]("home")) {
double bill = [Link]().billGenerate(units);
return "Final power bill: $" + bill;
} else if ([Link]("shop")) {
double bill = [Link]().billGenerate(units);
return "Final power bill: $" + bill;
} else {
return "Invalid valid bill type";
}
}
public static void main(String[] args) {
int units = 150;
String type = "home";
String result = getBill(units, type);
[Link](result);
}
}
Page 13
8. Week 5 Problem 1
interface Adder<T> {
T add(T a,T b);
}
class Pair<T>{
private T first;
private T second;
public Pair(T first, T second){
[Link] = first;
[Link] = second;
}
public T getFirst(){
return first;
}
public T getSecond(){
return second;
}
public Pair<T> add(Pair<T> p,Adder<T> addFunction){
T firstValue = [Link]([Link](),[Link]);
T secondValue = [Link]([Link](),[Link]);
return new Pair<T>(firstValue,secondValue);
}
}
public class Main {
public static void main(String[] args) {
Pair<Double> p1 = new Pair<Double>(2.0,3.0);
Pair<Double> p2 = new Pair<Double>(4.0,5.0);
Pair<Integer> p3 = new Pair<Integer>(5,7);
Pair<Double> p4 = [Link](p2,(a,b) -> a + b);
Pair<Integer> p5 = [Link](p3,(a,b) -> a + b);
[Link]([Link]() + " " +[Link]());
[Link]([Link]() + " " +[Link]());
}
}
Page 14
9. Week 5 Problem 2
import [Link].*;
class Operations<T extends Number> {
private T one;
private T two;
public Operations(T one, T two) {
[Link] = one;
[Link] = two;
}
public T getOne() {
return one;
}
public T getTwo() {
return two;
}
public void setOne(T one) {
[Link] = one;
}
public void setTwo(T two) {
[Link] = two;
}
public double multiply(){
return [Link]() * [Link]();
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
Operations<Integer> o1 = new Operations<Integer>([Link](),[Link]());
Operations<Double> o2 = new Operations<Double>([Link](),[Link]()
[Link]([Link]() +","+[Link]());
}
}
Page 15
10. Week 6 Problem 1
import [Link].*;
class Employee{
private String empID;
private String empName;
public Employee(String empID, String empName){
[Link] = empID;
[Link] = empName;
}
public String toString(){
return empID+" "+empName;
}
}
public class Main{
public static Map<String, Employee> getSortedEmplist(LinkedHashMap<String,Employ
TreeMap<String,Employee> tm = new TreeMap<String,Employee>(hm);
return tm;
}
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
LinkedHashMap<String,Employee> lhm = new LinkedHashMap<>();
for(int i = 0;i<3;i++){
String token = [Link]();
String empID = [Link]();
String empName = [Link]();
[Link](token,new Employee(empID,empName));
}
Map<String,Employee> sm = getSortedEmplist(lhm);
for([Link]<String,Employee> e:[Link]()){
[Link]([Link]()+":"+[Link]());
}
}
}
Page 16
11. Week 6 Problem 2
import [Link].*;
class Team{
private Map<String,ArrayList<Integer>> players;
public Team(Map<String,ArrayList<Integer>> mp){
players = mp;
}
public Map<String,ArrayList<Integer>> getPlayers(){
return players;
}
}
public class Main{
public static ArrayList<String> getFinalList(Team t){
ArrayList<String> pList = new ArrayList<String>();
Map<String,ArrayList<Integer>> players = [Link]();
for(String p: [Link]()){
boolean flag = true;
for(Integer i : [Link](p)){
if(i < 80){
flag = false;
break;
}
}
if(flag)
[Link](p);
}
return pList;
}
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
Map<String,ArrayList<Integer>> playermap = new LinkedHashMap<String,ArrayList<
for(int i=0;i<3;i++){
ArrayList<Integer> arr = new ArrayList<Integer>();
String name = [Link]();
for(int j=0;j<3;j++){
[Link]([Link]());
}
[Link](name,arr);
}
Team t = new Team(playermap);
[Link](getFinalList(t));
}
Page 17
}
Page 18
12. Week 7 Problem 1
class DiscountException extends Exception {
public String getMessage() {
return "Nondiscountable expense";
}
}
class Customer {
private String name;
private double expense;
private final double discountrate = 0.05;
public Customer(String name, double expense) {
[Link] = name;
[Link] = expense;
}
public String getName() {
return name;
}
public void checkDiscount() throws DiscountException {
if (expense >= 1000) {
double discountAmount = expense * discountrate;
[Link]("Customer %s: %.2f\n", name, discountAmount);
} else {
throw new DiscountException();
}
}
}
public class ExceptionTest {
public static void displayCustomers(Customer[] customers) {
for (Customer customer : customers) {
try {
[Link]();
} catch (DiscountException e) {
[Link]([Link]());
}
}
}
public static void main(String[] args) {
Page 19
Customer[] customers = new Customer[4];
customers[0] = new Customer("Customer1", 1200.00);
customers[1] = new Customer("Customer2", 800.00);
customers[2] = new Customer("Customer3", 1500.00);
customers[3] = new Customer("Customer4", 900.00);
displayCustomers(customers);
}
}
Page 20