Koustubh Javapraticalassignment1
Koustubh Javapraticalassignment1
Roll No: 41
1 Create class Student with datamembers rollno, marks of 3 subjects and methods
2 Create class Bank with datamembers accountno, balance and methods withdraw() and deposit(). Use
constructor to set account 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
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;
return actualSalary;
}
}
o/p=
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();
}
int getMarks() {
return m1 + m2 + m3;
}
}
int getTestMarks() {
return super.getMarks();
}
}
Result(int rollno, int m1, int m2, int m3, int sportsMarks) {
super(rollno, m1, m2, m3);
this.sportsMarks = sportsMarks;
}
int getTotalMarks() {
return getTestMarks() + getSportsMarks();
}
}
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;
}
}