Question: This program is Binary Object Files example problem. This is a pretty beginner class so we havent discussed any major topics. We are using abstract
This program is Binary Object Files example problem. This is a pretty beginner class so we havent discussed any major topics. We are using abstract methods and exception handling to deal with this problem/ arrays and other basic java topics.I will include an example of a person Class as reference to how much we have used. Essentially this is an excersie on Binary Object Files.
This this lab, youll read and write files containing objects of the Loan class. Here are the details of that class:
Instance variables: customer name (String)
annual interest percentage (double)
number of years (int)
loan amount (double)
loan date (String)
------------------
monthly payment (double)
total payments (double)
Methods:
getters for all instance variables
setters for all instance variables except monthly payment and total payment
calculateMonthlyPayment and calculateTotalPayments
The setters for the annual interest percentage, number of years, and loan amount invoke both calculate methods
The calculate methods calculate the named amount, then store that amount in the associated instance variable. They are private (helper) methods.
Constructors:
1. A no-arg constructor which sets the customer name to a default value, the loan date to no date, and all numeric variables to zero. This method invokes the full constructor
2. A full constructor which takes the customer name, annual interest percentage, number of years, loan amount, and loan date. It invokes the appropriate setters, but doesnt need to invoke the calculate methods (why?)
Calculations:
monthly interest rate = annual interest percentage / 1200
* monthly payment = loan amount * monthly interest rate /
(Math.pow(1. 0 / (1 + monthly interest rate), number of years * 12))
* total loan payments = monthly payment * number of years * 12
* Note: round all dollar amounts to 2 decimal places
Heres what you should do in main():
Use the following data:
Annual Interest Number of Loan Loan
Customer Name Percentage Years Amount Date
Bob Smith 6.5% 20 318,000 Sep 1, 2015
Alicia Herman 4.2% 15 248,000 Oct 15, 2013
Julie Franciosa 8.5% 10 30,000 Apr 14, 2010
Julio Quiros 15.0% 3 50,000 June 23, 2017
Frank Larsen 8.9% 5 23,000 Mar 8, 2016
Use this summary algorithm for your program:
1. Make the Loan class Serializable
2. Instantiate Loan objects representing the first three loans above using the full constructor
3. Create an output stream and store the three objects into a binary file using the writeObject method
4. Instantiate two Loan objects representing the last two loans listed above using the no-arg constructor, then using setters to update information about those loans
5. Append those objects to the output stream created above in step 2
6. Close the output stream
7. Create an input stream and read the objects from the binary file above and display in a nicely formatted columnar report.
a. Write your code to handle any number of loan objects (i.e., do not assume that there are 5 loan objects in the file
b. Include in your report the monthly loan payment and total loan payment amounts
c. Display totals for the amount of the loan amounts, monthly payments, and total loan payments.
8. Close the input stream
After writing the first three Loan objects in step 2, close the output stream.
Re-open the output stream before step 4 appending the last 2 Loan objects to the existing file
Here is an example of the output from your program:
Annual No. of Monthly Total
Customer Name Percent Years Loan-Amount Payment Loan Payments Loan-Date
------------------ ------- ------- ------------- ----------- -------------- -------------
Bob Smith 6.5 20 318,000.00 6,298.23 1,511,575.20 Sep 1, 2015
Alicia Herman 4.2 15 248,000.00 1,627.97 293,034.60 Oct 15, 2013
Julie Franciosa 8.5 10 30,000.00 495.69 59,482.80 Apr 14, 2010
Julio Quiros 15.0 3 50,000.00 977.46 35,188.56 June 23, 2017
Frank Larsen 8.9 5 23,000.00 265.76 15,945.60 Mar 8, 2016
============= =========== =============
669,000.00 9,665.11 1,915,226.76
EXAMPLE CODE NOT REALTED TO THE QUESTION BEING ASKED
* Project: Demonstration * Problem Statement: Demonstrate some of the features of the File class * * Algorithm: * 1. Delete a file named "demo.txt" in the classes folder (if it exists) * and a folder named "temporary" (if it exists) * 2. Create a file named "demo.txt" and show that it is writeable * 3. Make the file read-only * 4. Make the file writeable * 5. Create a directory named "temporary" * 8. List all of the files in the classes folder */ import java.io.File ; import java.io.IOException ; import java.util.Scanner ; public class FileClassDemo { // The names of the file and folder we will be manipulating private static String fileName = "demo.txt" ; private static String folderName = "temporary" ; public static void main(String[] args) { // Create a keyboard so we can monitor the results Scanner keyboard = new Scanner(System.in) ; // Get the entire path name of the file we're manipulating, so that we // can get it's parent directory's name. (Note that this code will work // whether the file actually exists or not.) File demoFile = new File(fileName) ; String absolutePath = demoFile.getAbsolutePath() ; System.out.println("Using file '" + fileName + "' to be located at " + absolutePath) ; // Reassign demoFile to use absolute path, not relative path demoFile = new File(demoFile.getAbsolutePath()) ; // reassign demoFile // If the file is left over from the last demo, then delete it if (demoFile.exists()) { demoFile.delete() ; System.out.println("Deleting old demo file...") ; } // Do the same thing for the temporary folder File folder = new File(folderName) ; absolutePath = folder.getAbsolutePath() ; folder = new File(absolutePath) ; if (folder.exists()) { folder.delete() ; System.out.println("Deleting old folder...") ; } System.out.print("The demo.txt file and temporary directory should be gone...") ; keyboard.nextLine() ; // Create a file with the file name (using the absolute path) System.out.println(" Create a new file named " + fileName) ; try { demoFile.createNewFile() ; } catch (IOException e) { System.out.println("Couldn't create the demo file -- aborting...") ; System.exit(0) ; } System.out.println("File created successfully with length " + demoFile.length() + "... ") ; // Test if the file is writable System.out.print("Is the file writable? " + demoFile.canWrite() + "...") ; keyboard.nextLine() ; // Now set to read-only System.out.println("Changing to read-only") ; demoFile.setReadOnly() ; System.out.print("Is the file writable now? " + demoFile.canWrite() + "...") ; keyboard.nextLine() ; // Set back to writable System.out.println("Changing back to writable") ; demoFile.setWritable(true) ; System.out.println("Is the file writable now? " + demoFile.canWrite()) ; // Make a folder named "temporary" in the parent folder System.out.print("Creating a \'temporary\' folder...") ; folder.mkdir() ; keyboard.nextLine() ; // List all of the files in the parent directory. First, get the // String of the parent directory to demo.txt. Create a new File // object of the parent directory, then list and print the files. System.out.println(" Here are all of the files in the parent folder") ; File parent = new File(demoFile.getParent()) ; String[] fileNames = parent.list() ; for (String f : fileNames) { System.out.println(" " + f) ; } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
