Mastering the Fundamentals of Object-Oriented Programming in Java
1) Define a class Candidate with the following description:
Members are : RNo of int type, Name of type String, Score of type float, Remarks of type String.
Member functions : AssignRem() to assign Remarks as per the Score obtained by a candidate.
Score range are given below:
Score Remarks
>=50 Selected
<50 Not Selected
*A function ENTER() to allow user to enter values for RNo, Name, Score and call function
AssignRem() to assign the remarks.
*A function DISPLAY() to allow user to view the content of all the data members.
Answer:
import java.util.*;
class CANDIDATE
{
int RNo;
String Name;
int Score;
String Remarks;
public void AssignRem(){
if(Score>=50)
Remarks = "Selected";
else
Remarks="Not selected";
}
public void Enter(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Roll Number : ");
RNo=sc.nextInt();
System.out.println("Enter Name : ");
Name=sc.next();
System.out.println("Enter Score : ");
Score=sc.nextInt();
AssignRem();
}
public void Display(){
System.out.println("Result ");
System.out.println("RNo : "+RNo);
System.out.println("Name : "+Name);
System.out.println("Score : "+Score);
System.out.println("Remarks : "+Remarks);
}
}
class Score
{
public static void main(String args[])
{
CANDIDATE cand=new CANDIDATE();
cand.Enter();
cand.Display();
}
}
-------------------------------------------------------------------------------------------------------------------------
2) Define a class Resort with the following description:
Members are : RNo to store Room Number, Name store customer name, Charges to store per
day charges, Days to store number of days of stay.
Member functions :
Compute() to calculate and return Amount as Days * Charges and if the values of Days*Charges
is more than 11000 then as 1.02*Days*Charges
Getinfo() A function to enter the content Rno, Name, Charges and Days.
DispInfo() A function to enter the content Rno, Name, Charges, Days and Amount by calling
function Compute().
Answer:
import java.util.*;
class RESORT
{
int RNo;
String Name;
int Charges;
int Days;
public double Compute(){
double amt;
amt=Charges*Days;
if(amt>=11000)
amt=amt*1.02;
return amt;
}
public void GetInfo(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Room Number : ");
RNo=sc.nextInt();
System.out.println("Enter Name : ");
Name=sc.next();
System.out.println("Enter Charges : ");
Charges=sc.nextInt();
System.out.println("Enter Days : ");
Days=sc.nextInt();
}
public void DispInfo(){
System.out.println("Room No : "+RNo);
System.out.println("Name : "+Name);
System.out.println("Charges : "+Charges);
System.out.println("Days : "+Days);
System.out.println("Total Amount : "+Compute());
}
}
class ResortData
{
public static void main(String args[])
{
RESORT res=new RESORT();
res.GetInfo();
res.DispInfo();
}
}
Create a class Bank with the following Menu :
1. Display all account details
2. Search by account number
3. Deposit the amount
4. Withdraw the amount
5. Exit
import java.util.Scanner;
class BankOperations {
String accno;
String name;
String type;
long balance;
Scanner sc = new Scanner(System.in);
//method to open new account
public void openAccount() {
System.out.print("Enter Account No: ");
accno = sc.next();
System.out.print("Enter Account type: ");
type = sc.next();
System.out.print("Enter Name: ");
name = sc.next();
System.out.print("Enter Balance: ");
balance = sc.nextLong();
}
//method to display account details
public void showAccount() {
System.out.println("Name of account holder: " + name);
System.out.println("Account no.: " + accno);
System.out.println("Account type: " + type);
System.out.println("Balance: " + balance);
}
//method to deposit money
public void deposit() {
long amt;
System.out.println("Enter the amount you want to deposit: ");
amt = sc.nextLong();
balance = balance + amt;
}
//method to withdraw money
public void withdrawal() {
long amt;
System.out.println("Enter the amount you want to withdraw: ");
amt = sc.nextLong();
if (balance >= amt) {
balance = balance - amt;
System.out.println("Balance after withdrawal: " + balance);
} else {
System.out.println("Your balance is less than " + amt + "\tTransaction failed...!!" );
}
}
//method to search an account number
public boolean search(String ac_no) {
if (accno.equals(ac_no)) {
showAccount();
return (true);
}
return (false);
}
}
public class Bank {
public static void main(String arg[]) {
Scanner sc = new Scanner(System.in);
System.out.print("How many number of customers do you want to input? ");
int n = sc.nextInt();
BankOperations Cust[] = new BankOperations[n];
for (int i = 0; i < Cust.length; i++) {
Cust[i] = new BankOperations();
Cust[i].openAccount();
}
int ch;
do {
System.out.println("\n ***Banking System Application***");
System.out.println("1. Display all account details \n 2. Search by Account number");
System.out.println("\n 3. Deposit the amount \n 4. Withdraw the amount \n 5.Exit ");
System.out.println("Enter your choice: ");
ch = sc.nextInt();
switch (ch) {
case 1:
for (int i = 0; i < Cust.length; i++) {
Cust[i].showAccount();
}
break;
case 2:
System.out.print("Enter account no. you want to search: ");
String ac_no = sc.next();
boolean found = false;
for (int i = 0; i < Cust.length; i++) {
found = Cust[i].search(ac_no);
if (found) {
break;
}
}
if (!found) {
System.out.println("Search failed! Account doesn't exist..!!");
}
break;
case 3:
System.out.print("Enter Account no. : ");
ac_no = sc.next();
found = false;
for (int i = 0; i < Cust.length; i++) {
found = Cust[i].search(ac_no);
if (found) {
Cust[i].deposit();
break;
}
}
if (!found) {
System.out.println("Search failed! Account doesn't exist..!!");
}
break;
case 4:
System.out.print("Enter Account No : ");
ac_no = sc.next();
found = false;
for (int i = 0; i < Cust.length; i++) {
found = Cust[i].search(ac_no);
if (found) {
Cust[i].withdrawal();
break;
}
}
if (!found) {
System.out.println("Search failed! Account doesn't exist..!!");
}
break;
case 5:
System.out.println("Thank You for using !!!");
break;
}
}
while (ch != 5);
}
}