Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Applied OOP Principles public class MyClass { private String firstName; private

ID: 3903512 • Letter: A

Question

Applied OOP Principles

public class MyClass {

private String firstName;

private String lastName;

private int idNumber;

private double balance;

public MyClass() {

this.firstName = "";

this.lastName = "";

this.idNumber = -1;

this.balance = 0.0; }

public MyClass(String firstName, String lastName, int id) {

this.firstName = firstName;

this.lastName = lastName;

this.idNumber = id;

this.balance = 0.0; }

public void makeDeposit(int id, double amount) {

if(this.confirm(id)) {

this.balance += amount; } }

public void withdraw(int id, double amount) {

if(this.confirm(id)) {

if(amount > this.balance)

System.out.println("Not enough in account");

else this.balance -= amount; } }

private boolean confirm(int id) {

if(this.idNumber == id)

return true;

else { System.out.println("INVALID"); return false; } } }

A) Describe what this class represents and what it does: [3]

B) What methods make up the class’s public interface? [3]

C) If a programmer wanted to inherit from this class and make a more specialized version, what problems might the programmer face and how could they be corrected? [4]?

Explanation / Answer

The above given program:-

public class MyClass {

private String firstName;

private String lastName;

private int idNumber;

private double balance;

public MyClass() {

this.firstName = "";

this.lastName = "";

this.idNumber = -1;

this.balance = 0.0; }

public MyClass(String firstName, String lastName, int id) {

this.firstName = firstName;

this.lastName = lastName;

this.idNumber = id;

this.balance = 0.0; }

public void makeDeposit(int id, double amount) {

if(this.confirm(id)) {

this.balance += amount; } }

public void withdraw(int id, double amount) {

if(this.confirm(id)) {

if(amount > this.balance)

System.out.println("Not enough in account");

else this.balance -= amount; } }

private boolean confirm(int id) {

if(this.idNumber == id)

return true;

else { System.out.println("INVALID"); return false; } } }

--------------------------------------------------------------------------------------------------------------------------------------------------------

A) The class basically represents a bank procedures i.e. deposit and withdraw.

=>The class has deposit method which first validate the accountId and then adds up deposit money in current balance.

=>The withdraw method first validate the account id of customer and then checks for the available balance. If balance is more than required withdrawal amount then balance is deducted otherwise it sends message of "Not enough in account".

--------------------------------------------------------------------------------------------------------------------------------------------------------

B) The below methods make up the class's public interface:-

1) public makeDeposit(int id, double amount)

2) public void withdraw(int id, double amount)

---------------------------------------------------------------------------------------------------------------------------------------------------------

C) If a programmer wanted to inherit from this class and make a more specialized version then he/she must need to make a confirm(int id) method to impliment makeDeposit(int id, double amount) and withdraw(int id, double amount) . Since confirm method in MyClass is declared as private it can't be used in the child class.

So to correct the problem we can declare the confirm() method in MyClass as public and then we can inherit it in child class.

---------------------------------------------------------------------------------------------------------------------------------------------------------

THANKS FOR ASKING....Keep on Chegging... :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote