12 04 2024 ucd cse csci2312 sp24 pa 7 programming assignment 7 pa7 ove
Search for question
Question
12/04/2024,
ucd-cse-csci2312/sp24-pa 7-
Programming Assignment 7 (PA7)
Overview
This PA will cover inheritance, virtual functions, and file I/O basics. Instructions on how to complete
this assignment are provided below
Naming Conventions:
⚫ While implementation details are still up to you, make sure you use the coding style discussed in
class regarding functions, name, classes, and indentations unless a specific name is given to you
throughout this assignment.
•
If a specific name for something like a variable class or function is given to you, you MUST
use it exactly or the autograder won't be able to find it and the tests will fail.
。 This applies to function prototypes/signatures/definitions as well. The inputs and outputs of
the functions you implement must also match the ones specified.
。 Points will not be given for tests that fail due to incorrect naming or signature mismatch.
Variables should be localized with the correct scope, datatype (based on the data needed to be
stored), and their names must be pneumonic. I.e., avoid variables like a, b, i, j, k, x, y, z, etc.
Coding style will be reviewed and your assessment grade may be impacted due to that as
described in our syllabus.
• Remember that homework must be completed individually without help (human or Al).
Restrictions:
•
Only the libraries discussed in class can be used in homework:
о
iostream
о string
о fstream
о iomanip
о
cmath
The using namespace statement such as using namespace std; is not allowed in this homework.
Tasks & TODOS
Note:
There are no TODO comments for this assignment as you are creating your own files. You can use the
TODOS from prior PAs as a rough guide to the pieces you need. You should also refer to your class
notes and textbook for guidance as necessary.
Loan
There will be three classes developed for this assignment: Loan SimpleLoan , and Amortized Loan
is designed to be a base class and both simpleLoan and Amortized Loan will be derived classes from
that.
Loan Class
This class will be the parent class of SimpleLoan and AmortizedLoan
Member Variables
.
principal a float to store the initial loan amount
о
interest Rate the interest rate for a given loan
loan Length: the period of time in which the loan is payed
о
loan Type the type of loan, Simple or
https://github.com/ucd-cse-csci2312/sp24-pa7-
1/7 12/04/2024,
Amortized.
Constructors
ucd-cse-csci2312/sp24-pa 7-
о Loan () initialize member variables to default values (0 or "" for numbers and strings
respectively)
о
Loan (float, float, int, string) initialize member variables to given parameter
Getters/setters
。 A getter and setter must be implemented for each member
variable.
Base Class Functions
о
The
о
virtual float monthlyPayment () = 0 : A pure virtual function to calculate the monthly payment.
function here just returns 0. It will be overridden in the derived classes.
void display Loan ( ) : displays the loan information in the format below. There are 32 '='
signs on the second row, and each row after that has a padding of 18 before the values
are displayed.
Loan Overview
Loan Type:
Principal:
Interest Rate:
Length in Years:
Monthly Payment:
Simple Loan
15000
8.5%
3
522.917
01
File I/O
о
void save Loan() : To get started, you will have to make a directory named data in the root
directory (same as src and tests ). Save the loan information in a file named loans.txt in
that directory, in the format principal interest Rate loanLength, in that order, delimited with
spaces, with a newline at the end. It is important to note, that depending in what directory you
compile the code from, it will change what the file path needs to be. By default, the
autograder runs from the root directory, so it is advised to do the same while testing as to not
have a conflicting path with the autograder tests. The file will be overwritten each time
save Loan () is called.
SimpleLoan class
This class is a public derived class of Loan, it will have no member variables
of its own. Constructors
о
SimpleLoan () calls default constructor of Loan
SimpleLoan (float, float, int) : calls parameterized constructor of
Loan setting loanType as "Simple Loan"
'
Monthly Payment
where P is the
monthly Payment () : this will override the monthly payment function of the Loan class. The
formula to calculate the monthly payment of a simple loan is as follows:
principal
amount, R is the monthly interest rate, and L is the length in months. Remember, loanLength
by default is in years, and interestRate is the yearly interest rate given as a value between
0-100 instead of 0-1, so both of these values must be converted to the proper format before
the monthly interest rate is calculated.
AmortizedLoan class
•
•
This class is a public derived class of Loan it will have no member variables
of its own. Constructors
о
Amortized Loan() calls default constructor of Loan
Amortized Loan (float, float, int) : calls parameterized constructor of
Loan, setting loanType as "Amortized Loan"
Monthly Payment
о
monthlyPayment () : this will override the monthly payment function of the Loan class. The
P*R*(1+R)¹
formula to calculate the monthly payment of an amortized loan is as follows: (1+ R)² - 1
where P is the principal amount, R is the monthly interest rate, and L is the length in months.
https://github.com/ucd-cse-csci2312/sp24-pa7-
2/7 12/04/2024,
ucd-cse-csci2312/sp24-pa7-
Remember, loanLength by default is in years, and interest Rate is the yearly interest rate
given as a value between 0-100 instead of 0-1, so both of these values must be converted to
the proper format before the monthly interest rate is calculated.
Below is the UML representation of the class structure of Loan , SimpleLoan and Amortized Loan .
%%{
init: {
'themeVariables': {
'fontFamily': 'monospace'
}
}
}%%
classDiagram
Loan |-- SimpleLoan
Loan <|-- AmortizedLoan
class Loan {
# principal: float
# interestRate: float
# loanLength : int
# loanType: string
+ Loan()
+ Loan(float, float, int, string)
+ getPrincipal() float
+ getinterestRate() float
+ getLoanLength() float
+ getLoanType() string
+ setPrincipal(float) void
+ setInterestRate(float) void
+ setLoanLength(int) void
+ setLoanType(string) void
+ monthlyPayment() float: virtual
+ displayLoan() void
+ saveLoan() void
}
class SimpleLoan {
+ SimpleLoan()
+ SimpleLoan(float, float, int)
+ monthlyPayment() float
}
class AmortizedLoan {
https://github.com/ucd-cse-csci2312/sp24-pa7-
3/7 12/04/2024,
+ Amortized Loan()
+ Amortized Loan(float, float, int)
+ monthlyPayment() float
}
ucd-cse-csci2312/sp24-pa7-
Note:
Make sure you put the declarations in the header file and the implementations in the .cpp file!
3. Create a main.cpp file with an int main() and test every function that isn't a getter or setter to
receive full credit
Important!
Failure to complete this step may result in a loss of points!
Testing
Your code will run against unit tests on an Autograder. The Autograder runs on Ubuntu Linux so the
unit tests are configured for that OS only. You can run the tests in a Codespace (which uses Ubuntu
as well) or on your own Ubuntu environment (if you have one) using the provided g++ instructions.
Remember tests must pass on the Autograder to receive points.
Compiling
It is expected that you are able to compile and run your own code without relying solely on other
tools. This includes the VS Code Debugger's "Play/Run" Button. You should not be using that at all for
this assignment. Here is the general format for g++ (you leave out the [ ] characters when
actually writing the commands):
g++ [relevant setting and flags. These usually start with a
[list of files to link toget
The g++ build command does use a couple extra settings when compiling with the unit tests. The
additional options and what they signify are explained here:
.
•
[.cpp files, .o files, and .a files] : Now, list off any .cpp source files, .o object files, and .a
static library files that need to be included in the build. For all of these, you must include the
relative file path for any files that aren't in your current working directory.
You will first list off any .cpp files needed by your program to run. In general, if your code uses
an #include "*.h" statement, you will put the corresponding .cpp file here.
https://github.com/ucd-cse-csci2312/sp24-pa7-
4/7 12/04/2024,
•
•
ucd-cse-csci2312/sp24-pa7-
Then, add the object file for the test(s) you wish to run. Remember you must also include the
relative file path if you are in a different working directory from the .o file (for example, ../tests/
if you are in the src folder or just tests/ if you at the root of the repo).
Lastly, include the file gtest_main.a . This file contains the main program needed to launch the
unit tests against your code.
Remember: A build must include exactly one (1) main function so you can't include your own main
when running the unit tests.
-lpthread: Links the pthread library to the resulting executable. In essence, it ensures that
threading functions are available in the resulting binary. The unit tests use multithreading which is
why we are including it.
-0 : This option lets you specify the name of the file you want g++ to output the executable code.
If a file with that name already exists, it will be overwritten. The default output file is a. out if you
don't include this option.
Here is an example of a build command that could be used for this assignment. In this example, the
working directory is repository root or top level of the repository:
g++ src/Book.cpp tests/gtest_main.a tests/displayLoanTest_test.o -lpthread -o display LoanTest
Modify it as needed to run the tests as you wish. The autograder will also still run on each push to
your repo if you prefer to test that way.
Caution
Do not attempt to force the unit tests to pass by hardcoding in responses to expected values. Attempts to
bypass or "trick" the Autograder are considered academic misconduct and may result in a 0 for the
whole assignment.
Rubric & Grading
Here is the breakdown of the tests, what they test, and how many marks they are worth.
Test Name
simpleLoanConstructorsTest_test
simpleLoan Monthly PaymentTest_test
50
Marks
Description
Tests constructors, getters, and setters for
SimpleLoan
Tests SimpleLoan's implementation of
monthly Payment
20
Tests constructors, getters, and setters for
Amortized Loan
5
amortized Loan ConstructorsTest_test
Tests Amortized Loan's implementation of
2
amortized Loan Monthly PaymentTest_test monthly Payment
displayLoanTest_test
saveLoanTest_test
Grading Breakdown & Style Points
Tests output of displayLoan for formatting
Tests loans.txt for directory location and formatting
Assignments on Canvas will be recorded out of 100 marks.
The grading of homework is a combination of completeness and correctness of the outputs and
155
programming style. Completeness and correctness (80 marks) will be assessed automatically through
testing on GitHub Classroom, while instructors and TAs will assess the programming style after the
deadline (20 marks).
Code style will be evaluated using the guidelines shown in the Modules on
https://github.com/ucd-cse-csci2312/sp24-pa7-
5/7/n