Question 1:
Define a class called LMS with the following description:
Instance Variables/Data
Members:
int acc_num — stores the accession number of the book.
String title — stores the title of the book.
String author — stores the name of the author.
Member methods:
1. void input() — To input and
store the accession number, title and author.
2. void compute() — To accept
the number of days late, calculate and display the fine charged at the rate of
Rs. 2 per day.
3. void display() — To display
the details in the following format:
Fine is Rs
Accession
Number Title Author
import
java.util.Scanner;
class LMS
{
int acc_num;
String title, author;
void input()
{
Scanner Sr = new Scanner(System.in);
System.out.print("Enter accession
number: ");
acc_num = Sr.nextInt();
System.out.print("Enter title:
");
title = Sr.nextLine();
System.out.print("Enter author:
");
author = Sr.nextLine();
}
void compute()
{
Scanner Sr = new Scanner(System.in);
System.out.print("Enter number of
days late: ");
int daysLate = Sr.nextInt();
int fine = 2 * daysLate;
System.out.println("Fine is Rs
" + fine);
}
void display()
{
System.out.println("Accession
Number\tTitle\tAuthor");
System.out.println(acc_num + "\t\t
" + title + "\t" + author);
}
public static void main(String[] args)
{
LMS libms = new LMS();
libms.input();
libms.compute();
libms.display();
}
VARIABLE
DESCRIPTION TABLE |
||
Name
of Variable |
Data
Type |
Purpose/Description |
args |
String[] |
To accept command line arguments |
acc_num |
int |
To store the accession number of the book |
title |
String |
To store the title of the book |
author |
String |
To store the author of the book |
daysLate |
int |
To store the number of days late |
fine |
int |
To store the fine amount |
Define a class with the following specifications:
Class name: Bank
Member variables:
double p – stores the principal amount
double n – stores the time period in years
double r – stores the rate of interest
double a – stores the amount
Member methods:
void accept() – input values for p and n using Scanner class methods only.
void calculate() – calculate the amount based on the following conditions:
Time in (Years) | Rate % |
Up to 1/2 | 9 |
> 1/2 to 1 year | 10 |
> 1 to 3 years | 11 |
> 3 years | 12 |
void display() – displays the details in the given format:
Principal Time Rate Amount
xxx xxx xxx xxx
Write a main() method to create an object and call the above methods.
Solution:
class bank
{
double p,n,r,a;
Scanner sc = new Scanner(System.in);
void accept()
{
System.out.println("Enter Principal");
p = sc.nextDouble();
System.out.println("Enter number of years");
n = sc.nextDouble();
}
void calculate()
{
if(n<=0.5)
{
r = 9;
}
else if(n>0.5 && n<=1)
{
r = 10;
}
else if(n>1 && n<=3)
{
r=11;
}
else
{
r=12;
}
a = p*(Math.pow(1+r/100,n));
}
void display()
{
System.out.println("Principal\tRate\tTime\tAmount");
System.out.println(p+"\t\t"+r+"\t"+n+"\t"+a);
}
public static void main(String[]args)
{
bank bnk = new bank();
bnk.accept();
bnk.calculate();
bnk.display();
}
}
VARIABLE DESCRIPTION TABLE |
||
Name of Variable |
Data Type |
Purpose/Description |
args |
String[] |
To accept command line arguments |
p |
double |
To store the Principal amount |
n |
double |
To store the time period in years |
r |
double |
To store the rate of interest |
a |
double |
To store the amount |