KEMBAR78
Coding Session Week 1-7 | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
11 views20 pages

Coding Session Week 1-7

The document contains programming problems discussed in a live session over seven weeks, focusing on Java concepts such as classes, inheritance, interfaces, and exception handling. Each problem includes code examples demonstrating object-oriented programming principles, including the creation of classes, methods, and data manipulation. The problems cover various topics including student and employee management, billing systems, and discount handling, providing practical applications of Java programming.

Uploaded by

Rituparna Das
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)
11 views20 pages

Coding Session Week 1-7

The document contains programming problems discussed in a live session over seven weeks, focusing on Java concepts such as classes, inheritance, interfaces, and exception handling. Each problem includes code examples demonstrating object-oriented programming principles, including the creation of classes, methods, and data manipulation. The problems cover various topics including student and employee management, billing systems, and discount handling, providing practical applications of Java programming.

Uploaded by

Rituparna Das
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
You are on page 1/ 20

BSCCS2005: Programs discussed in the live session

Week {1-7}

1. Week 2 Problem 1

import java.util.*;
class Student {
private String name;
private double marks [];
public Student(String name, double[] marks) {
this.name = name;
this.marks = marks;
}

public String getName() {


return (this.name);
}

public double findTotal() {


double total = 0.0;
for (double i : this.marks) {
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 = i.findTotal();
if (total_marks > max_marks) {
max_student = i.getName();
max_marks = total_marks;
}
}
return max_student;
}

public static void main(String[] args) {


Student[] arr = new Student[3];
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 3; i++){
String name = sc.next();
double[] m = {sc.nextDouble(), sc.nextDouble(),sc.nextDouble()};
arr[i] = new Student(name, m);
}
System.out.println(getMax(arr));
}
}

Page 2
2. Week 2 Problem 2

import java.util.*;
class Employee{
String name;
String[] projects;
public Employee(String n, String[] proj){
name = n;
projects = proj;
}

public Employee(Employee e){


this.name = e.name;
int l = e.projects.length;
this.projects = new String[l];
for(int i = 0; i < l; i++){
this.projects[i] = e.projects[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(System.in);
String[] proj = {"PJ1","PJ2","PJ3"};
Employee e1 = new Employee("Surya", proj);
Employee e2 = new Employee(e1);

Page 3
e2.setName(sc.next());
e2.setProject(0, sc.next());
System.out.println(e1.getName() + ": " + e1.getProject(0));
System.out.println(e2.getName() + ": " + e2.getProject(0));
}
}

Page 4
3. Week 3 Problem 1

class Employee{
private String name;
private double salary;

public Employee(String n , double s){


this.name = n;
this.salary = s;
}
public void setName(String n){
this.name = n;
}
public void setSalary(double s){
this.salary = s;
}
public String getName(){
return this.name;
}
public double getSalary(){
return this.salary;
}
public double bonus(float percent){
return (percent/100.0)*this.salary;
}
}

class Manager extends Employee{


private String secretary;

public Manager(String n , double s, String se){


super(n,s);
this.secretary = se;
}

public void setSecretary(String se){


this.secretary = se;
}

public String getSecretary(){


return this.secretary;
}

public double bonus(float percent){

Page 5
double b = super.bonus(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");
System.out.println("Employee name:" + obj1.getName() + ", Salary:" + obj1.getS
obj1.setName("jin");
obj1.setSalary(2000.00);
Manager obj2 = new Manager("rick", 12000.00 , "meera");
obj2.setSecretary("elena");
System.out.println("Employee name:" + obj1.getName() + ", Salary:" + obj1.getS
}
}

Page 6
4. Week 3 Problem 2

class Employee {
private String name;
private double salary;

public Employee(String name, double salary) {


this.name = name;
this.salary = salary;
}

public String getName() {


return name;
}

public double getSalary() {


return salary;
}

public void print() {


System.out.println("Name: " + name + ", Salary: " + salary);
}

public void print(float percent) {


double bonus = bonus(percent);
System.out.println("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);
this.secretary = secretary;
}

public void print() {


System.out.println("Name: " + getName() + ", Salary: " + getSalary() + ",
}

Page 7
public void print(float percent) {
double bonus = bonus(percent);
System.out.println("Name: " + getName() + ", Salary: " + getSalary() +
", Secretary: " + secretary + ", Bonus: " + bonus);
}

public double bonus(float percent) {


return super.bonus(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");
obj1.print();
obj1.print(10.0f);
System.out.println();
obj2.print();
obj2.print(15.0f);
}
}

Page 8
5. Week 4 Problem 1

abstract class Shopping{


abstract void offers();
}

class Amazon extends Shopping{


void offers(){
System.out.println("Great Indian festival sale from 6th-8th Oct 2023");
}
}

class Flipkart extends Shopping{


void offers(){
System.out.println("Big Billion Days from 5th-9th Oct 2023");
}
}

class User{
public void getOffers(Shopping obj){
obj.offers();
}
}

public class Main{


public static void main(String[] args){
User u = new User();
u.getOffers(new Amazon());
u.getOffers(new Flipkart());
}
}

Page 9
6. Week 4 Problem 2

interface Appraisable {
default void increment(Employee e) {
e.updateSalary(5000);
}
void checkAndUpdateSalary();
}

interface SpecialAppraisable extends Appraisable {


default void spIncrement(Employee e) {
e.updateSalary(7000);
}
}

class Employee implements SpecialAppraisable {


private String name;
private double salary;
private int numLeaves;

public Employee(String name, double salary, int numLeaves) {


this.name = name;
this.salary = salary;
this.numLeaves = 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) {
employee.checkAndUpdateSalary();
}

for (Employee employee : eList) {


System.out.println(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 (type.equalsIgnoreCase("home")) {
double bill = powerBill.getDomesticBill().billGenerate(units);
return "Final power bill: $" + bill;
} else if (type.equalsIgnoreCase("shop")) {
double bill = powerBill.getCommercialBill().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);


System.out.println(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){
this.first = first;
this.second = second;
}
public T getFirst(){
return first;
}
public T getSecond(){
return second;
}
public Pair<T> add(Pair<T> p,Adder<T> addFunction){
T firstValue = addFunction.add(p.getFirst(),this.first);
T secondValue = addFunction.add(p.getSecond(),this.second);
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 = p1.add(p2,(a,b) -> a + b);
Pair<Integer> p5 = p3.add(p3,(a,b) -> a + b);
System.out.println(p4.getFirst() + " " +p4.getSecond());
System.out.println(p5.getFirst() + " " +p5.getSecond());
}
}

Page 14
9. Week 5 Problem 2

import java.util.*;
class Operations<T extends Number> {
private T one;
private T two;
public Operations(T one, T two) {
this.one = one;
this.two = two;
}
public T getOne() {
return one;
}
public T getTwo() {
return two;
}
public void setOne(T one) {
this.one = one;
}
public void setTwo(T two) {
this.two = two;
}
public double multiply(){
return one.doubleValue() * two.doubleValue();
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Operations<Integer> o1 = new Operations<Integer>(sc.nextInt(),sc.nextInt());
Operations<Double> o2 = new Operations<Double>(sc.nextDouble(),sc.nextDouble()
System.out.println(o1.multiply() +","+o2.multiply());
}
}

Page 15
10. Week 6 Problem 1

import java.util.*;
class Employee{
private String empID;
private String empName;
public Employee(String empID, String empName){
this.empID = empID;
this.empName = 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(System.in);
LinkedHashMap<String,Employee> lhm = new LinkedHashMap<>();
for(int i = 0;i<3;i++){
String token = sc.next();
String empID = sc.next();
String empName = sc.next();
lhm.put(token,new Employee(empID,empName));
}
Map<String,Employee> sm = getSortedEmplist(lhm);
for(Map.Entry<String,Employee> e:sm.entrySet()){
System.out.println(e.getKey()+":"+e.getValue());
}
}
}

Page 16
11. Week 6 Problem 2

import java.util.*;
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 = t.getPlayers();
for(String p: players.keySet()){
boolean flag = true;
for(Integer i : players.get(p)){
if(i < 80){
flag = false;
break;
}
}
if(flag)
pList.add(p);
}
return pList;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Map<String,ArrayList<Integer>> playermap = new LinkedHashMap<String,ArrayList<
for(int i=0;i<3;i++){
ArrayList<Integer> arr = new ArrayList<Integer>();
String name = sc.next();
for(int j=0;j<3;j++){
arr.add(sc.nextInt());
}
playermap.put(name,arr);
}
Team t = new Team(playermap);
System.out.println(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) {


this.name = name;
this.expense = expense;
}

public String getName() {


return name;
}

public void checkDiscount() throws DiscountException {


if (expense >= 1000) {
double discountAmount = expense * discountrate;
System.out.printf("Customer %s: %.2f\n", name, discountAmount);
} else {
throw new DiscountException();
}
}
}

public class ExceptionTest {


public static void displayCustomers(Customer[] customers) {
for (Customer customer : customers) {
try {
customer.checkDiscount();
} catch (DiscountException e) {
System.out.println(e.getMessage());
}
}
}

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

You might also like