You are on page 1of 6

Submitted To : Mam Samia Riaz

Submitted By : Danish Ejaz


Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

Question no 1:
Develop a java program in which data is stored in class’s attributes by using function and also use array object
concept. Also perform following tasks.
• Insert Record
• Display Record
• Search Record
• Modify Record
• Delete Record
Answer:
package October_03_2022;
import java.util.*;
class student
{
String name, fname, regNo;
student() {}
public void add()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Student name = ");
String n = in.nextLine();
System.out.print("Enter Student father name = ");
String fn = in.nextLine();
System.out.print("Enter Student Registration no = ");
String regno = in.nextLine();
System.out.println("");
this.name = n;
this.fname = fn;
this.regNo = regno;
}
public void Display()
{
System.out.println("==================================================\n");
System.out.println("name = " + name);
System.out.println("father name = " + fname);
System.out.println("Registration no = " + regNo);
System.out.println("==================================================\n");
}
public void search (String sname, student array)
{
if(this.name.equals(sname))
{
array.Display();
}
}
public void update(String name1, student array)
{
if(this.name.equals(name1))
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Student name = ");
String nam = in.nextLine();
System.out.print("Enter Student father name = ");
String fnam = in.nextLine();
System.out.print("Enter Student Registration no = ");
String Regno = in.nextLine();
System.out.println("");
this.name = nam;
this.fname = fnam;
this.regNo = Regno;
System.out.println("Data update Successfully");
}
}
public void delete (String name2, student array)
{
if(name2.equals(this.name))
{
Submitted To : Mam Samia Riaz
Submitted By : Danish Ejaz
Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

this.name = null;
this.fname = null;
this.regNo = null;
System.out.println("");
}
}
}
public class ArrayObject
{
public static void main(String args [])
{
Scanner in = new Scanner(System.in);
student [] arr = new student[5];
System.out.println("Select a Choice\n1. Insert data\n2. Display data\n3. Search data\n4. Modifey data\n5. Remove data\n0. Exit");
System.out.println("");
int choice;
do
{
System.out.print("Enter your Choice = ");
choice = in.nextInt();
System.out.println("");
switch(choice)
{
case 1:
{
for(int i = 0; i< arr.length; i++)
{
arr[i] = new student();
arr[i].add();
}
}
break;
case 2:
{
for(int i = 0; i < arr.length; i++)
{
arr[i].Display();
}
}
break;
case 3:
{
Scanner n = new Scanner(System.in);
System.out.print("Enter name you went to search = ");
String nam = n.nextLine();
for(int i = 0; i < arr.length; i++)
{
arr[i].search(nam, arr[i]);
}
}
break;
case 4:
{
Scanner n = new Scanner(System.in);
System.out.print("Enter name you went to Modify = ");
String nam = n.nextLine();
for(int i = 0; i < arr.length; i++)
{
arr[i].update(nam, arr[i]);
}
}
break;
case 5:
{
Scanner n = new Scanner(System.in);
System.out.print("Enter name you went to Delete = ");
String nam = n.nextLine();
for(int i = 0; i < arr.length; i++)
{
Submitted To : Mam Samia Riaz
Submitted By : Danish Ejaz
Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

arr[i].delete(nam, arr[i]);
}
}
break;
default:
System.out.println("program Finished");
}
}
while(choice !=0);
}
}

Question no 2.(a):
Create an Encapsulated class Marks with three data members to store three marks. Create set and get methods for
all data members. Test the class in runner.
Answer:
package October_03_2022;
import java.util.Scanner;
class Marks
{
private int maths, physics, computer;
public void set ()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Marks of Mathematics = ");
int m = in.nextInt();
System.out.print("Enter Marks of Physics = ");
int p = in.nextInt();
System.out.print("Enter Marks of Computer = ");
int c = in.nextInt();
this.physics = p;
this.maths = m;
this.computer = c;
}
public void get()

System.out.println("========================================");
System.out.println("Maths = " + maths);
System.out.println("Physics = " + physics);
System.out.println("Computer = " + computer);
System.out.println("========================================");
}
}
public class Encapsulation
{
public static void main(String[] args)
{
Marks m = new Marks();
m.set();
m.get();
}
}

Question no 2.(b):
Create an Encapsulation class Account Class with balance as data member. Create two constructors and methods
to withdraw and deposit balance. In the main create two Accounts. The second account should be created with the same
balance as first account.
Answer:
package October_03_2022;
import java.util.Scanner;
class Accounts
{
private static int balance;
Accounts(Accounts obj )
{
this.balance = obj.balance;
}
Submitted To : Mam Samia Riaz
Submitted By : Danish Ejaz
Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

Accounts(int balance)
{
this.balance = balance;
}
public void deposit()
{
Scanner ins = new Scanner(System.in);
System.out.print("Enter Amount you went to Deposit = ");
int Deposit = ins.nextInt();
this.balance = this.balance + Deposit;
}
public void withdraw()
{
Scanner ins = new Scanner(System.in);
System.out.print("Enter Amount you went to Whitdraw = ");
int Whitdraw = ins.nextInt();
if(Whitdraw > balance)
{
System.out.println("you have no sufficient balance....!");
}
else
{
this.balance = this.balance - Whitdraw;
}
}
void display()
{
System.out.println("your current balance is = " + this.balance);
}
}
public class Account
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter your Balance = ");
int Balance = in.nextInt();
Accounts A1 = new Accounts(Balance);
Accounts A2 = new Accounts(A1);
System.out.println("=========================================================");
System.out.println(" Choice 1 for Deposit \n Choice 2 for Withdraw \n Choice 3 for check balance");
System.out,println("=========================================================");
int c = 0;
do
{
System.out.println("");
System.out.print("Enter your Choice = ");
c = in.nextInt();
switch(c)
{
case 1:
{
A1.deposit();
}
break;
case 2:
{
A1.withdraw();
}
break;
case 3:
{
A1.display();
}
break;
default:
System.out.println(“Invalid Choice”);
}
Submitted To : Mam Samia Riaz
Submitted By : Danish Ejaz
Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

}
while(c!=0);
System.out.println("=========================================================");
System.out.println(" Choice 1 for Deposit \n Choice 2 for Withdraw \n Choice 3 for check balance");
System.out.println("=========================================================");
do
{
System.out.println("");
System.out.print("Enter your Choice = ");
c = in.nextInt();
switch(c)
{
case 1:
{
A2.deposit();
}
break;
case 2:
{
A2.withdraw();
}
break;
case 3:
{
A2.display();
}
break;
default:
System.out.println("invalid Choice")
}
while(c!=0);
}
}

Question no 3:
Create a saving bank account.
Answer:
package October_03_2022;
import java.text.NumberFormat;
class SavingsAccount
{
static float annualInterestRate;
private float savingsBalance;
public void calculateMonthlyInterest()
{
savingsBalance += savingsBalance * annualInterestRate / 12;
}
public void modifyInterestRate(float newRate)
{
annualInterestRate = newRate;
}
public float getSavingsBalance()
{
return this.savingsBalance;
}
SavingsAccount(float startingBalance)
{
savingsBalance = startingBalance;
}
}
public class saving_Accounts
{
public static void main(String args [])
{
SavingsAccount saver1 = new SavingsAccount(2000.0f);
SavingsAccount saver2 = new SavingsAccount(3000.0f);
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
saver1.modifyInterestRate(0.03f);
Submitted To : Mam Samia Riaz
Submitted By : Danish Ejaz
Registration no : FA21-BSE-074
Section : BSE-3B
Department : Software Engineering

saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("saver1 balance = " + currencyFormat.format(saver1.getSavingsBalance()));
System.out.println("saver2 balance = " + currencyFormat.format(saver2.getSavingsBalance()));
System.out.println();
saver2.modifyInterestRate(0.04f);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("saver1 balance = " + currencyFormat.format(saver1.getSavingsBalance()));
System.out.println("saver2 balance = " + currencyFormat.format(saver2.getSavingsBalance()));
}
}

You might also like