0% found this document useful (0 votes)
146 views

Java Assignment 3

The document describes two programming assignments. The first involves creating a Library class with instance variables to store a book's accession number, title, and author. The class contains methods to input data, calculate late fees, and display book details. A test program demonstrates class usage. The second assignment involves creating a FixedInvestment class to model an investment with fields for deposit amount, interest rate, and years. Methods include getting total return calculated as the initial deposit multiplied by monthly compound interest over the specified number of years. A test program demonstrates class usage by modeling a $10,000 investment at 4.5% annually for 3 years.

Uploaded by

Jaskirat Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

Java Assignment 3

The document describes two programming assignments. The first involves creating a Library class with instance variables to store a book's accession number, title, and author. The class contains methods to input data, calculate late fees, and display book details. A test program demonstrates class usage. The second assignment involves creating a FixedInvestment class to model an investment with fields for deposit amount, interest rate, and years. Methods include getting total return calculated as the initial deposit multiplied by monthly compound interest over the specified number of years. A test program demonstrates class usage by modeling a $10,000 investment at 4.5% annually for 3 years.

Uploaded by

Jaskirat Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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);
}
}

You might also like