Java programming fundamentals 1a)Define a class BOOK with the following members:
ID: 3906349 • Letter: J
Question
Java programming fundamentals
1a)Define a class BOOK with the following members:
Private members
Bno of type int
Bname of type String
Author of type String
Price of type float
Public members
void Enter() member function to enter the values of data members
void Display() member function to display the values of data members
int RBno() member function that returns the value of Bno
String RBname() member function that returns the value of Bname
Write a program which creates an object BOOK.
Takes the input of all fields using Enter() method
Displays all the fields using Display() method
Display <<Bname “ is a new Book”>> if Bno>1000, else displays <<Bname “is an Old Book”>>
1b)Define a class CANDIDATE with the following members:
Private members
Cno of type int
Cname of type String
Score of type int
Public members
void Enter() method to enter the values of data members
void Display() method to display the values of data members
void CheckScore() method that displays “Excellent” if the value of Score>90, “Good” if score>80, else displays “Average”
Write a program which uses the above three methods to create an object, display the
details of the candidate and uses CheckScore() method to display the kind of score.
1c)
Define a class ACCOUNT with the following members:
Private members
Acno of type int
Name of String
Balance of type float
Public members
void Init() method to enter the values of data members
void Show() method to display the values of data members
void Deposit(int Amt) method to increase Balance by Amt
void Withdraw(int Amt) method to reduce Balance by Amt
float RBalance() method that returns the value of Balance
Write a program which uses the above methods to create an object, display the
details of the candidate and uses menu driven code to Deposit or Withdraw amount and then displays the balance in the account using RBalance().
Explanation / Answer
ScreenShot
1a)Define a class BOOK with the following members:
//Package for I/O
import java.util.*;
//Create class Book
class Book{
//Private member variables
private int Bno;
private String Bname,Author;
private float Price;
//Public member functions
public void Enter() {
Scanner sc=new Scanner(System.in);
//member function to enter the values of data members
System.out.println("Please enter Book Number:");
Bno=sc.nextInt();
sc.nextLine();
System.out.println("Please enter Book Name:");
Bname=sc.nextLine();
System.out.println("Please enter Price of the book:");
Price=sc.nextFloat();
sc.nextLine();
System.out.println("Please enter Authork Name:");
Author=sc.nextLine();
}
public int RBno() {
//member function that returns the value of Bno
return Bno;
}
public String RBname() {
//member function that returns the value of Bname
return Bname;
}
public void Display() {
//member function to display the values of data members
if(Bno>1000) {
System.out.println("Book Number="+Bno+" Book Name="+Bname+" is a new book. "+"Author="+Author+" Price="+Price);
}
else {
System.out.println("Book Number="+Bno+" Book Name="+Bname+" is an old book. "+"Author="+Author+" Price="+Price);
}
}
}
//Test Class
public class Testing {
public static void main(String[] args) {
//Create object of the class book
Book b=new Book();
//Call member functions
b.Enter();
b.Display();
}
}
Output:-
12
Please enter Book Name:
Robin hood
Please enter Price of the book:
150
Please enter Authork Name:
Dominic Minghella
Book Number=12
Book Name=Robin hood is an old book.
Author=Dominic Minghella
Price=150.0
---------------------------------------------------------------------------
1b)Define a class CANDIDATE with the following members:
//Package for I/O
import java.util.*;
//Create a class Candidate
class CANDIDATE{
//Member variables
private int Cno,Score;
private String Cname;
//Member functions
public void Enter() {
//method to enter the values of data members
Scanner sc=new Scanner(System.in);
System.out.println("Enter candidate number:");
Cno=sc.nextInt();
sc.nextLine();
System.out.println("Enter candidate name:");
Cname=sc.nextLine();
System.out.println("Enter candidateScore:");
Score=sc.nextInt();
}
public void Display() {
//method to display the values of data members
System.out.println("Candidate Number="+Cno+" Candidate Name="+Cname+" Score="+Score);
}
public void CheckScore() {
//method that displays “Excellent” if the value of Score>90, “Good” if score>80, else displays “Average”
if(Score>90) {
System.out.println("Excellent");
}
else if(Score>80 && Score<=90) {
System.out.println("Good");
}
else {
System.out.println("Average");
}
}
}
//Test Class
public class Testing {
public static void main(String[] args) {
//Create object of the class Candidate
CANDIDATE c=new CANDIDATE();
//Call member functions
c.Enter();
c.Display();
c.CheckScore();
}
}
Output:-
Enter candidate number:
12
Enter candidate name:
Mridhul
Enter candidateScore:
70
Candidate Number=12
Candidate Name=Mridhul
Score=70
Average
----------------------------------------------------------------
1c)Define a class ACCOUNT with the following members:
//Package for I/O
import java.io.IOException;
import java.util.*;
//Create a class ACCOUNT
class ACCOUNT{
private int Acno;
private String name;
private float Balance;
public void Init() {
Scanner sc=new Scanner(System.in);
//method to enter the values of data members
System.out.println("Please enter account number:");
Acno=sc.nextInt();
sc.nextLine();
System.out.println("Please enter accountholder name:");
name=sc.nextLine();
System.out.println("Please enter account balnce:");
Balance=sc.nextFloat();
}
public void Show() {
//method to display the values of data members
System.out.println("Account Number="+Acno+" Account Holder Name="+name+" Balance="+Balance);
}
public void Deposit(int Amt) {
//method to increase Balance by Amt
Balance+=Amt;
}
void Withdraw(int Amt) {
//method to reduce Balance by Amt
Balance=Balance-Amt;
}
float RBalance() {
//method that returns the value of Balance
return Balance;
}
}
//Test Class
public class Testing {
public static void main(String[] args) throws IOException {
//Scanner for inputting choice
Scanner sc=new Scanner(System.in);
//Create object of the class Candidate
ACCOUNT a=new ACCOUNT();
//Call member functions
a.Init();
a.Show();
//menu driven deposit withdraw operations
System.out.println("Do you want to (d)eposit / (w)ithdraw:/(e)xit");
char ch = (char) System.in.read();
do {
if(ch=='d') {
System.out.println("Please enter the amount you want to deposit:");
int amt=sc.nextInt();
a.Deposit(amt);
System.out.println("Current Balance="+a.RBalance());
}
else if(ch=='w') {
System.out.println("Please enter the amount you want to withdraw:");
int amt=sc.nextInt();
a.Withdraw(amt);
System.out.println("Current Balance="+a.RBalance());
}
else if(ch=='e') {
System.out.println("Thank You!!!");
System.exit(0);
}
else {
System.out.println("Your Choice is wrong");
}
System.out.println("Do you want to (d)eposit / (w)ithdraw:/(e)xit");
ch = (char) System.in.read();
}while(ch=='d'||ch=='w'|| ch=='e');
}
}
Output
Please enter account number:
145236
Please enter accountholder name:
Maya Mills
Please enter account balnce:
1000
Account Number=145236
Account Holder Name=Maya Mills
Balance=1000.0
Do you want to (d)eposit / (w)ithdraw:/(e)xit
w
Please enter the amount you want to withdraw:
200
Current Balance=800.0
Do you want to (d)eposit / (w)ithdraw:/(e)xit
d
Please enter the amount you want to deposit:
1000
Current Balance=1800.0
Do you want to (d)eposit / (w)ithdraw:/(e)xit
e
Thank You!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.