0% found this document useful (0 votes)
37 views14 pages

Koustubh Javapraticalassignment1

The document contains code snippets demonstrating different object-oriented programming concepts in Java including: 1. Creating a Student class with roll number and marks as attributes and methods to calculate total and average marks. 2. Creating a Bank class with account number and balance attributes and methods to deposit, withdraw and check balance. 3. Creating a Shape class with an overloaded calArea() method to calculate area of different shapes like circle, square, rectangle. 4. Creating an Employee base class and Account derived class to calculate salary with bonuses based on experience. 5. Overloading the Employee class constructor to initialize attributes with different parameters. 6. Demonstrating hybrid inheritance with a Student class, Test

Uploaded by

rdlolge
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)
37 views14 pages

Koustubh Javapraticalassignment1

The document contains code snippets demonstrating different object-oriented programming concepts in Java including: 1. Creating a Student class with roll number and marks as attributes and methods to calculate total and average marks. 2. Creating a Bank class with account number and balance attributes and methods to deposit, withdraw and check balance. 3. Creating a Shape class with an overloaded calArea() method to calculate area of different shapes like circle, square, rectangle. 4. Creating an Employee base class and Account derived class to calculate salary with bonuses based on experience. 5. Overloading the Employee class constructor to initialize attributes with different parameters. 6. Demonstrating hybrid inheritance with a Student class, Test

Uploaded by

rdlolge
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/ 14

Name: Koustubh Rajesh Sadavarte

Roll No: 41

1 Create class Student with datamembers rollno, marks of 3 subjects and methods

public class Student { private


int rollNo; private double
subject1Marks; private double
subject2Marks; private double
subject3Marks;

// Constructor to initialize the Student object


public Student(int rollNo, double subject1Marks, double subject2Marks, double subject3Marks)
{
this.rollNo = rollNo;
this.subject1Marks = subject1Marks;
this.subject2Marks = subject2Marks; this.subject3Marks
= subject3Marks;
}

// Getter method for rollNo


public int getRollNo() {
return rollNo;
}

// Setter method for rollNo


public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}

// Getter method for subject1Marks


public double getSubject1Marks() {
return subject1Marks;
}

// Setter method for subject1Marks


public void setSubject1Marks(double subject1Marks) {
this.subject1Marks = subject1Marks;
}
// Getter method for subject2Marks
public double getSubject2Marks() {
return subject2Marks;
}
// Setter method for subject2Marks
public void setSubject2Marks(double subject2Marks) {
this.subject2Marks = subject2Marks;
}

// Getter method for subject3Marks


public double getSubject3Marks() {
return subject3Marks;
}

// Setter method for subject3Marks


public void setSubject3Marks(double subject3Marks) {
this.subject3Marks = subject3Marks;
}

// Method to calculate the total marks of the student


public double calculateTotalMarks() {
return subject1Marks + subject2Marks + subject3Marks;
}

// Method to calculate the average marks of the student


public double calculateAverageMarks() {
return calculateTotalMarks() / 3;
}

// Method to display the student's information


public void displayStudentInfo() {
System.out.println("Roll No: " + rollNo);
System.out.println("Subject 1 Marks: " + subject1Marks);
System.out.println("Subject 2 Marks: " + subject2Marks);
System.out.println("Subject 3 Marks: " + subject3Marks);
System.out.println("Total Marks: " + calculateTotalMarks());
System.out.println("Average Marks: " + calculateAverageMarks());
}

public static void main(String[] args) {


Student student = new Student(101, 85.5, 92.0, 78.5);
student.displayStudentInfo();
}
}
o/p=

2 Create class Bank with datamembers accountno, balance and methods withdraw() and deposit(). Use
constructor to set account balance

public class Bank {


private int accountNo;
private double balance;

// Constructor to initialize the account balance


public Bank(int accountNo, double initialBalance) {
this.accountNo = accountNo; this.balance =
initialBalance;
}

// Method to withdraw money public void


withdraw(double amount) { if (amount >
0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawal of $" + amount + " successful.");
} else {
System.out.println("Insufficient funds or invalid amount for withdrawal.");
}
}

// Method to deposit money public


void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposit of $" + amount + " successful.");
} else {
System.out.println("Invalid amount for deposit.");
}
}

// Method to check the account balance


public double checkBalance() {
return balance;
}

public static void main(String[] args) {


Bank account = new Bank(1001, 1000.0); // Creating a bank account with account number
1001 and initial balance $1000.0
System.out.println("Initial Account Balance: $" + account.checkBalance());

account.deposit(500.0); // Depositing $500.0


System.out.println("Current Account Balance: $" + account.checkBalance());

account.withdraw(200.0); // Withdrawing $200.0


System.out.println("Current Account Balance: $" + account.checkBalance());

account.withdraw(1500.0); // Attempting to withdraw an amount that exceeds the balance


}
}

o/p=
3 Create class shape to print area of circle, square, rectangle. The class has one method calArea() with
same name but different parameters

public class Shape {


// Method to calculate and print the area of a circle
public void calArea(double radius) { double area
= Math.PI * radius * radius;
System.out.println("Area of the circle with radius " + radius + " is: " + area);
}
// Method to calculate and print the area of a square
public void calArea(double sideLength, String shapeType) {
if (shapeType.equals("square")) {
double area = sideLength * sideLength;
System.out.println("Area of the square with side length " + sideLength + " is: " + area);
} else if (shapeType.equals("rectangle")) {
System.out.println("Invalid shape type. Use 'square' for square area or 'rectangle' for
rectangle area.");
}
}
// Method to calculate and print the area of a rectangle
public void calArea(double length, double width, String shapeType) {
if (shapeType.equals("rectangle")) { double area = length * width;
System.out.println("Area of the rectangle with length " + length + " and width " + width + "
is: " + area);
} else if (shapeType.equals("square")) {
System.out.println("Invalid shape type. Use 'rectangle' for rectangle area or 'square' for
square area.");
}
}
public static void main(String[] args) {
Shape shape = new Shape();
shape.calArea(5.0); // Calculate and print the area of a circle with a radius of 5.0
shape.calArea(4.0, "square"); // Calculate and print the area of a square with a side length of 4.0
shape.calArea(3.0, 6.0, "rectangle"); // Calculate and print the area of a rectangle with
length 3.0 and width 6.0
}
}
o/p=

4 Create class Employee to calculate Salary of Employee. Base class Employee has datamembers
empid, basic_sal,joining_year. Derive Class Account has datamember present_days. Give bonus to
employee based on experience.(if exp>=10 - 10%, exp>=5 &

class Employee {
private int empid;
private double basicSal;
private int joiningYear;

public Employee(int empid, double basicSal, int joiningYear) {


this.empid = empid; this.basicSal = basicSal;
this.joiningYear = joiningYear;
}

public double calculateSalary() {


return basicSal;
}

public int getExperience() {


int currentYear = 2023; // Assuming the current year is 2023
return currentYear - joiningYear;
}
}

class Account extends Employee {


private int presentDays;

public Account(int empid, double basicSal, int joiningYear, int presentDays) {


super(empid, basicSal, joiningYear);
this.presentDays = presentDays;
}

public double calculateSalary() {


double baseSalary = super.calculateSalary();
double bonus = 0;
int experience = getExperience();

if (experience >= 10) {


bonus = 0.10 * baseSalary;
} else if (experience >= 5 && experience < 10) {
bonus = 0.05 * baseSalary; } else if
(experience < 5) { bonus = 0.025 *
baseSalary;
}

// Assuming 30 days in a month for salary calculation


double monthlySalary = baseSalary + bonus; double
dailySalary = monthlySalary / 30; double
actualSalary = dailySalary * presentDays;

return actualSalary;
}
}

public class Main {


public static void main(String[] args) {
Account account = new Account(101, 5000, 2015, 20); // Employee with empid, basic_sal,
joining_year, and presentDays
double salary = account.calculateSalary();
System.out.println("Employee ID: " + account.empid);
System.out.println("Total Salary: $" + salary);
}
}

o/p=

5 Create employee class with empid,deptid using Constructor -


Employee(),Employee(empid),Employee(empid,deptid) (Use Constructor overloading)

public class Employee {


private int empid; private
int deptid;
// Constructor with no arguments
public Employee() { empid =
0; deptid = 0;
}

// Constructor with empid


public Employee(int empid) {
this.empid = empid;
deptid = 0;
}

// Constructor with empid and deptid


public Employee(int empid, int deptid) {
this.empid = empid; this.deptid =
deptid;
}

public void displayEmployeeDetails() {


System.out.println("Employee ID: " + empid);
System.out.println("Department ID: " + deptid);
}

public static void main(String[] args) {


Employee emp1 = new Employee();
Employee emp2 = new Employee(101);
Employee emp3 = new Employee(102, 201);

System.out.println("Employee 1 Details:");
emp1.displayEmployeeDetails();
System.out.println();

System.out.println("Employee 2 Details:");
emp2.displayEmployeeDetails();
System.out.println();

System.out.println("Employee 3 Details:");
emp3.displayEmployeeDetails();
}
}
o/p=

6 Hybrid Inheritance - Class student should hold the rollno,class test should hold marks scored in two
different subjects(m1,m2,m3), interface sports should hold marks in sports and class Result should
calculate the total marks. The program must print the rollno, marks scored in individual subject marks,
marks scored in sports and the total marks.

interface Sports {
int getSportsMarks();
}

class Test { int


m1, m2, m3;

Test(int m1, int m2, int m3) {


this.m1 = m1; this.m2 =
m2; this.m3 = m3;
}

int getMarks() {
return m1 + m2 + m3;
}
}

class Student extends Test {


int rollno;
Student(int rollno, int m1, int m2, int m3) {
super(m1, m2, m3);
this.rollno = rollno;
}

int getTestMarks() {
return super.getMarks();
}
}

class Result extends Student implements Sports {


int sportsMarks;

Result(int rollno, int m1, int m2, int m3, int sportsMarks) {
super(rollno, m1, m2, m3);
this.sportsMarks = sportsMarks;
}

@Override public int


getSportsMarks() {
return sportsMarks;
}

int getTotalMarks() {
return getTestMarks() + getSportsMarks();
}
}

public class Main {


public static void main(String[] args) {
Result result = new Result(101, 85, 92, 78, 90);

System.out.println("Roll No: " + result.rollno);


System.out.println("Test Marks (M1, M2, M3): " + result.m1 + ", " + result.m2 + ", " +
result.m3);
System.out.println("Sports Marks: " + result.getSportsMarks());
System.out.println("Total Marks: " + result.getTotalMarks());
}
}
o/p=

7 Design an interface AdvancedArithmetic which contains a method signature int divisor_sum(int n).
You need to write a class called MyCalculator which implements the interface. divisor_sum(int n)
function takes an integer as input and return the sum of all its divisors. Divisors of 6 are 1, 2, 3 and 6,
so divisor_sum should return 12. (0<n

// Interface
interface AdvancedArithmetic {
int divisor_sum(int n);
}
// Class implementing the interface
class MyCalculator implements AdvancedArithmetic {
@Override
public int divisor_sum(int n) {
if (n <= 0 || n >= 100) {
throw new IllegalArgumentException("Input should be in the range 0 < n < 100");
}
int sum = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum;
}
}
public class Main {
public static void main(String[] args) {
MyCalculator calculator = new MyCalculator();
int n = 6;
int sumOfDivisors = calculator.divisor_sum(n);
System.out.println("Divisor Sum of " + n + " is: " + sumOfDivisors);
}
}
o/p=

8 Design an interface Shape which contain method input() and area() to calculate area of cirlce,square
and rectangle import java.util.Scanner;

// Interface interface
Shape { void
input(); double
area();
}
// Class for Circle
class Circle implements Shape {
double radius;
@Override public
void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
radius = scanner.nextDouble();
}
@Override public
double area() {
return Math.PI * radius * radius;
}
}
// Class for Square
class Square implements Shape {
double side;
@Override public
void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the side length of the square: ");
side = scanner.nextDouble();
}
@Override public
double area() {
return side * side;
}
}
// Class for Rectangle class
Rectangle implements Shape {
double length;
double width;

@Override
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the length of the rectangle: "); length
= scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
width = scanner.nextDouble();
}

@Override public
double area() {
return length * width;
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Create objects for Circle, Square, and Rectangle


Circle circle = new Circle();
Square square = new Square();
Rectangle rectangle = new Rectangle();

// Input data for each shape


circle.input();
square.input();
rectangle.input();

// Calculate and display areas


System.out.println("Area of the Circle: " + circle.area());
System.out.println("Area of the Square: " + square.area());
System.out.println("Area of the Rectangle: " + rectangle.area());
}
}
o/p=

You might also like