Assignment
Q1. Develop a class called Library with the following description by applying
the concept of class and object:
Instance Variable/Data Members
int acc_num - to store the accession number of the book.
String title - to store the title of the book
String author - to store the name of the author
Member methods
void input() - to input and store the detail of the book
void compute() - to accept the number of days late, calculate and display the
fine charged at the rate of Rs. 2 per day.
void display() - to display the all the details of book
Write a program for the above mentioned class and call the method
accordingly.
class Assignment3Question1 {
public static void main(String[] args) {
Library book = new Library();
book.input(001, "harry puttar", "joking rolling");
book.compute(10);
book.display();
}
}
class Library {
int acc_num;
String title;
String author;
void input(int acc_num, String title, String author)
{
this.acc_num = acc_num;
this.title = title;
this.author = author;
}
void compute(int days_late) {
System.out.println("Your late fine is ₹" + 2*days_late);
}
void display()
{
System.out.println("accession number:\t" + this.acc_num);
System.out.println("title:\t\t\t" + this.title);
System.out.println("author:\t\t\t" + this.author);
}
}
2. Build a class named FixedInvestment that contains:
A double data field named depositAmount that specifies the investment
amount (default 1000).
A double data field named annualInterestRate that specifies the fixed
interest rate (default 5.0%)
An int data field named numberOfYears that specifies the investment
duration (default 1).
A no-arg constructor that create a default instance.
A constructor that creates an instance with the specified
depositAmount, annualInterestRate and numberOfYears.
The accessor methods for depositAmount, numberOfYears and
annualInterestRate.
A method named getTotalReturn() that return the investment return
after the specified number of years.
The total return can be computed using the following formula:
totalReturn = investmentAmount x (1 +
monthlyInterestRate)numberOfYears*12
Write a test program that creates a FixedInvestment object with a deposit
amount of 10000 and an annual interest rate of 4.5% for three years. Display
the total return after three years.
import java.lang.Math;
class Assignment3Q2 {
public static void main(String[] args) {
FixedInvestment invest = new FixedInvestment(10000, 4.5, 3);
System.out.println("Returns value of default investment is: " +
invest.getTotalReturn());
}
}
class FixedInvestment {
double depositAmout;
double annualInterestRate;
int numberOfYears;
FixedInvestment() {
this.depositAmout = 1000.0;
this.annualInterestRate = 5.0;
this.numberOfYears = 1;
}
FixedInvestment(double depositAmout, double annualInterestRate, int
numberOfYears) {
this.depositAmout = depositAmout;
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
}
public double getDepositAmout() {
return depositAmout;
}
public void setDepositAmout(double depositAmout) {
this.depositAmout = depositAmout;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public int getNumberOfYears() {
return numberOfYears;
}
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
double getTotalReturn() {
return depositAmout * Math.pow((1 + this.annualInterestRate / 100),
this.numberOfYears * 12);
}
}