Question

In: Computer Science

Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount)...

Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount) and double getBalance() const. The method names explain its purpose. Further suppose that child class Savings has one more attribute double interest_rate and a public method void addInterest() which will update the balance according the formula new balance = old balance * (1 + interest_rate/100.0); Implement addInterest method. c++

Solutions

Expert Solution

The main problem of this question can be faced when accessing the private variables of the parent class (in this case, Account Class). But we have methods set for that purpose, and thus the child class (Savings) will also inherit them.

class Account{
    private:
        double balance;
    public:
        void setBalance(double amount);
        const double getBalance();
};

void Account::setBalance(double amount){
    this->balance = amount;
}

const double Account::getBalance(){
    return this->balance;
}

The above code snippet just describes the structure of our parent class (from now on we can refer as Account class). The account class has the standard variables and methods, described in the question.

Adding the code of the child class(Savings Class) below:


class Savings : public Account{ // Savings Class inheriting the parent class
    private:
        double interest_rate;
    public: 
        // Instead of using constructor, we are using a method to set
        // the interest rate
        void setInterestRate(double interest_rate);
        void addInterest();
};

void Savings::setInterestRate(double interest_rate){
    this->interest_rate = interest_rate;
}

void Savings::addInterest(){
    double new_balance;
    double old_balance  = getBalance();
    new_balance = (old_balance) * (1 + (interest_rate)/100);
    setBalance(new_balance);
}
​

Just for the sake of representation, we have created two variables (old_balance and new_balance) in the addInterest() method of the Savings class. We use the setBalance() method in the savings class, as the child class cannot access the private members of the parent class, thus using the method provided by the parent class. Attaching the code of the driver class:

int main(){
    // Initialising our saving account and then adding a
    // balance of 100.00 units
    Savings *savings = new Savings();
    savings->setBalance(100.00);

    const double trial_get_balance = savings->getBalance();
    cout << "Initial Balance: "<< trial_get_balance << endl;

    // Creating a Saving (child class) and changing the value 
    // of current balance using an interest_rate
    
    // In this class, we can set the interest_rate either using a 
    // constructor or a method. I have used a method to set the 
    // interest rate.
    savings->setInterestRate(2.00);

    savings->addInterest();
    cout << "After Adding interest: "<< savings->getBalance() << endl;
    return 0;
}

As the code comments explain, we are using the driver class to create an instance of the object of the Savings class (child class of Account) and thus inheriting the methods and variables. But the private variables (or methods) would not be accessible to it. Furthermore, we are adding the setInterest() (Here adding 2% as the interest rate) method to set the interest value and updating the balance in the addInterest() method. The addInterest() method uses the setBalance() method of the Account class to set its private variable.

The output of the program would be as follows:

Thus summing up the question, the whole code is pasted below:

#include<bits/stdc++.h>

using namespace std;

class Account{
    private:
        double balance;
    public:
        void setBalance(double amount);
        const double getBalance();
};

void Account::setBalance(double amount){
    this->balance = amount;
}

const double Account::getBalance(){
    return this->balance;
}

class Savings : public Account{
    private:
        double interest_rate;
    public: 
        // Instead of using constructor, we are using a method to set
        // the interest rate
        void setInterestRate(double interest_rate);
        void addInterest();
};

void Savings::setInterestRate(double interest_rate){
    this->interest_rate = interest_rate;
}

void Savings::addInterest(){
    double new_balance;
    double old_balance  = getBalance();
    new_balance = (old_balance) * (1 + (interest_rate)/100);
    setBalance(new_balance);
}

int main(){
    // Initialising our saving account and then adding a
    // balance of 1000.00 units
    Savings *savings = new Savings();
    savings->setBalance(100.00);

    const double trial_get_balance = savings->getBalance();
    cout << "Initial Balance: "<< trial_get_balance << endl;

    // Creating a Saving (child class) and changing the value 
    // of current balance using an interest_rate
    
    // In this class, we can set the interest_rate either using a 
    // constructor or a method. I have used a method to set the 
    // interest rate.
    savings->setInterestRate(2.00);

    savings->addInterest();
    cout << "After Adding interest: "<< savings->getBalance() << endl;
    return 0;
}

In case of any doubts, please revert back.


Related Solutions

java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
Required methods:: public static void processAccount (Account[] acc, printWriter out{}. public static boolean deposit(Account a){}.
Java programming. Required methods::public static void processAccount (Account[] acc, printWriter out{}.public static boolean deposit(Account a){}.public static boolean withdrawal(Account a){}.public static int findAccount(long accountNumber, Account[] acc){}. 1/ Remove the applyRandomBonus method from the test class2/ Create a File, Printwriter for an output file yourlastnameErrorLog.txt4/ Catch InputMismatch and ArrayIndexOutOfBounds exceptions when reading data from the file:a. Skip any lines that cause an exceptionb. Write information about the exception to the log file, yourlastnameError.txtc. Include exception type and line number in exception message...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT