fork download
  1. import java.text.NumberFormat ; // format numbers involved
  2. class FutureValue {
  3. public static void main (String[] args) {
  4. // Students substitute your name for mine in the line below.
  5. System.out.println("This code is written by Danny Chen");
  6.  
  7. // We need a container to hold the employees we are about to create.
  8. int size = 10 ;
  9. AllEmployees allEmployees = new AllEmployees (size) ;
  10.  
  11. /* Create some employees whose future value you wish to calculate.
  12. * Call the Employee class’s parameterized constructor, creating an
  13. * instance of/object of the Employee class.
  14. * Each employee will need an index, which will be the next one after the last
  15. * one constructed. We use the index in two ways, one as an identifier of the
  16. * employee and one as an index into an array.
  17. */
  18. int index = 0 ; // initialize data to 0, as per cs.umd.edu
  19. // The following holds the current id (index) of the next employee.
  20. EmployeeIndex employeeIndex = new EmployeeIndex() ;
  21. Employee employee = null; // just creating the variable; no storage
  22. // Now use the current Index; initially set to 0 by the constructor above.
  23.  
  24. // Creating employee at the current index.
  25. index = employeeIndex.getIndex(); // get the current index (id)
  26. employee = new Employee (index, "Prof. Taylor", 250, 200.00, 350.00,
  27. 3.25, 20 ) ;
  28. employeeIndex.incrementIndex () ; // set the index for the next employee
  29. // Place the employee in the array of all employees.
  30. allEmployees.addEmployee (employee) ;
  31.  
  32. // Creating employee at the current index.
  33. index = employeeIndex.getIndex(); // get the current index (id)
  34. employee = new Employee (index, "Prof. Saito", 150, 500.00, 500.00,
  35. 3.25, 40 ) ;
  36. employeeIndex.incrementIndex () ; // set the index for the next employee
  37. // Place the employee in the array of all employees.
  38. allEmployees.addEmployee (employee) ;
  39.  
  40. // Students you create yourself as the next employee and place yourself in
  41. // the array copying the code above. Of course, you need to set your own
  42. // individual parameters for your constructor.
  43.  
  44. index = employeeIndex.getIndex();
  45. employee = new Employee (index, "Danny Chen", 200, 600.00, 800.00, 3.25, 60);
  46. allEmployees.addEmployee(employee);
  47.  
  48. // Calculate and place the futureValue for the employees in the container.
  49. allEmployees.calculateFutureValueAllEmployees() ;
  50. // Display the information for all the employees in the container.
  51. allEmployees.printAllEmployees();
  52. // Now print the information for an employee just given their name
  53. allEmployees.printEmployee ("Prof. Saito") ;
  54. } // end main()
  55. } // end class FutureValue
  56. /*
  57.  * In this program, indices replace ids, as ids don’t normally start at 0, while indices
  58.  * always do, and we mean to use the variable index as an id to tie employees to an
  59.  *array.
  60.  */
  61.  
  62.  
  63. class EmployeeIndex {
  64. private int currentIndex = 0; // always initialize data members
  65. EmployeeIndex () { } // Our no argument constructor
  66. public void setIndex (int currentIndex) {
  67. this.currentIndex = currentIndex;
  68. }
  69. public int getIndex () {
  70. return currentIndex;
  71. }
  72. public void incrementIndex() {
  73. ++ currentIndex ;
  74. }
  75. } // end class EmployeeIndex
  76.  
  77. class Employee {
  78. private int index = 0; // will be used to place an employee into an array
  79. private String name = null ; // always initialize data members
  80. private int graduationPresent = 0; // always initialize data members
  81. private double holidayBonus = 0.0; // always initialize data members
  82. private double monthlyIRA_Deduction = 0.0; // always initialize data members
  83. private double yearlyInterestRate = 0.0; // always initialize data members
  84. private int numberOfYears = 0; // always initialize data member
  85. private double futureValue = 0.0; // always initialize data members
  86. /* Below are our constructors (private is not a restriction to
  87. * access inside the class).
  88. * N.B. this below refers to the current instance or object of the class
  89. * Employee. The keyword this is a nice frill, since it doesn’t
  90. * require renaming a parameter/argument of the constructor.
  91. */
  92. // An Employee parameterized constructor
  93. Employee (int index, String name, int graduationPresent,
  94. double holidayBonus, double monthlyIRA_Deduction,
  95. double yearlyInterestRate, int numberOfYears ) {
  96. this.index = index ;
  97. this.name = name ;
  98. this.graduationPresent = graduationPresent ;
  99. this.holidayBonus = holidayBonus;
  100. this.monthlyIRA_Deduction = monthlyIRA_Deduction;
  101. this.yearlyInterestRate = yearlyInterestRate;
  102. this.numberOfYears = numberOfYears;
  103. futureValue = 0.0 ; // can only be changed later by a set() method
  104. } // end Employee()
  105.  
  106.  
  107.  
  108. public void printEmployee () {
  109. System.out.println ("Index = " + index ) ;
  110. System.out.println ("Name = " + name ) ;
  111. System.out.println ("Graduation Present = " +
  112. NumberFormat.getCurrencyInstance().format(graduationPresent));
  113. System.out.println ("Holiday Bonus = " +
  114. NumberFormat.getCurrencyInstance().format(holidayBonus));
  115. System.out.println ("Monthly IRA deduction = " +
  116. NumberFormat.getCurrencyInstance().format(monthlyIRA_Deduction));
  117. System.out.println ("Yearly Interest Rate = " + yearlyInterestRate + "%");
  118. System.out.println ("Number Of Years = " + numberOfYears ) ;
  119. System.out.println ("Future Value = " +
  120. NumberFormat.getCurrencyInstance().format(futureValue));
  121. } // end printEmployee()
  122.  
  123. /* The set() methods place the necessary information into the
  124. * Employee class. These methods are required when placing this
  125. * information outside the class definition as the data members of the
  126. * Employee classes are private.
  127. */
  128. public void setIndex (int index) {this.index = index;}
  129. public void setName (String name) {this.name = name;}
  130. public void setGraduationPresent (int graduationPresent) {
  131. this.graduationPresent = graduationPresent;}
  132. public void setHolidayBonus (double holidayBonus) {
  133. this.holidayBonus = holidayBonus; }
  134. public void setYearlyInterestRate (double yearlyInterestRate) {
  135. /* Note the interest rate will be entered as a percent e.g., 3.25, but
  136. * without a % sign, while the monthly interest rate (created later)
  137. * will be the decimal value, e.g., 3.25%/12 = 0.0325/12= 0.0002257,
  138.   * which is the interest rate used for monthly calculations.
  139. */
  140. this.yearlyInterestRate = yearlyInterestRate ;}
  141. public void setMonthlyIRA_Deduction (double monthlyIRA_Deduction) {
  142. this.monthlyIRA_Deduction = monthlyIRA_Deduction;}
  143. public void setNumberOfYears (int numberOfYears ) {
  144. this.numberOfYears = numberOfYears; }
  145. public void setFutureValue (double futureValue ) {this.futureValue =
  146. futureValue;}
  147.  
  148. /* The get() methods retrieve the necessary information from the EmployeeData
  149. * class. These methods are required when retrieving this information outside the
  150. * class definition. since the data members of the EmployeeData class are private.
  151. */
  152.  
  153. public int getIndex () { return index ;}
  154. public String getName () {return name ;}
  155. public int getGraduationPresent () {return graduationPresent ;}
  156. public double getHolidayBonus () {return holidayBonus ;}
  157. public double getYearlyInterestRate () {return yearlyInterestRate ;}
  158. public double getMonthlyIRA_Deduction () {return monthlyIRA_Deduction ;}
  159. public int getNumberOfYears () {return numberOfYears ;}
  160. public double getFutureValue () {return futureValue ;}
  161. } // end class Employee
  162.  
  163. class AllEmployees {
  164. int size = 10 ; // maximum number of Employees
  165. Employee[] allEmployeesArray = null ;
  166. AllEmployees (int size) { // one-argument constructor.
  167. this.size = size ;
  168. allEmployeesArray = new Employee[size];
  169. } // end one-argument constructor
  170.  
  171. public void addEmployee ( Employee employee) {
  172. int index = employee.getIndex() ;
  173. allEmployeesArray [index] = employee;
  174. } // end addEmployee()
  175.  
  176. public void printEmployee ( String name) {
  177. int invalidIndex = -1; // we use -1 to indicate an invalid index
  178. int index = findEmployeeIndex (name);
  179. if ( index == invalidIndex) {
  180. // Next line is debug code; ultimately to be removed.
  181. System.out.println (name + "is not in the array of Employees.") ;
  182. return ;
  183. }
  184. Employee employee = getEmployee (index) ;
  185. employee.printEmployee () ;
  186. return ;
  187. } // end printEmployee()
  188.  
  189. public int findEmployeeIndex (String searchName) {
  190. Employee employee = null;
  191. String name = null ;
  192. int index = -1 ; // initialize to an invalid index
  193. for ( int i = 0; i < allEmployeesArray.length; i++ ) {
  194. employee = allEmployeesArray[ i ] ;
  195. if (employee == null) {
  196. break ; // from for loop
  197. } // end if
  198. name = employee.getName() ;
  199. if (name.equals (searchName)) {
  200. index = i ;
  201. break ; // from for loop
  202. } // end if
  203. }// end for
  204. return index ;
  205. } // end findEmployeeIndex()
  206.  
  207. public Employee getEmployee (int index) {
  208. Employee employee = null ;
  209. // Safety code to avoid exceptions being thrown
  210. if ( index > -1 && index < allEmployeesArray.length ) {
  211. employee = allEmployeesArray[index] ;
  212. }
  213. return employee ;
  214. } // end getEmployee()
  215.  
  216. public void calculateFutureValueAllEmployees () {
  217. FutureValueCalculations futureValueCalculations =
  218. new FutureValueCalculations(); // Java supplied constructor
  219. Employee employee = null;
  220. double futureValue = 0.0;
  221. for ( int i = 0; i < allEmployeesArray.length; i++ ) {
  222. employee = allEmployeesArray[i] ;
  223. if (employee == null) {
  224. break ; // from for loop
  225. } // end if
  226. // Don’t calculate future value if already calculated.
  227. futureValue = employee.getFutureValue() ;
  228. if ( futureValue == 0 ) {
  229. futureValue =
  230. futureValueCalculations.calculateFutureValue (employee);
  231. employee.setFutureValue (futureValue);
  232. } // end if
  233. } // end for
  234. } // end calculateFutureValueAllEmployees()
  235.  
  236. public void printAllEmployees() {
  237. Employee employee = null;
  238. for ( int i = 0; i < allEmployeesArray.length; i++ ) {
  239. employee = allEmployeesArray[i] ;
  240. if (employee == null) {
  241. break ; // from for loop
  242. }
  243. employee.printEmployee () ;
  244. } // for
  245. }
  246. } // end class AllEmployees
  247.  
  248. class FutureValueCalculations {
  249. public double calculateFutureValue (Employee employee ) {
  250. int graduationPresent = employee.getGraduationPresent() ;
  251. double holidayBonus = employee.getHolidayBonus() ;
  252. double monthlyIRA_Deduction =employee.getMonthlyIRA_Deduction() ;
  253. double yearlyInterestRate = employee.getYearlyInterestRate () ;
  254. int numberOfYears = employee.getNumberOfYears() ;
  255. double futureValue = calculateFutureValue (graduationPresent,
  256. holidayBonus, monthlyIRA_Deduction, yearlyInterestRate,
  257. numberOfYears );
  258. return futureValue ;
  259. } // end calculateFutureValue()
  260.  
  261. // Polymorphism - 2 methods called calculateFutureValue; this one we sell.
  262. public double calculateFutureValue (int graduationPresent,
  263. double holidayBonus, double monthlyIRA_Deduction,
  264. double yearlyInterestRate, int numberOfYears ) {
  265. double futureValue = 0.0 ; // initialization
  266. /* Transform the arguments/parameters into the variables we need to use.
  267. * First transform percent to its double, and then yearly to monthly.
  268. */
  269. double monthlyInterestRate = yearlyInterestRate / 100 / 12 ;
  270. int numberOfMonths = 12 * numberOfYears ;
  271. double monthlyInterestAmount = 0; // Initialization
  272. futureValue += (double) graduationPresent; // typecasting
  273. // Now calculate the future value.
  274. int i = 1; // loop iterator
  275. while ( i <= numberOfMonths) { // loop conditional
  276. // Add a holiday bonus every 12th month.
  277. if ( (i % 12) == 0 ) { // Example 12 % 7 = 5
  278. futureValue += holidayBonus ;
  279. }
  280. futureValue += monthlyIRA_Deduction;
  281. monthlyInterestAmount = futureValue * monthlyInterestRate;
  282. futureValue += monthlyInterestAmount;
  283. i++; // increment the loop iterator
  284. } // end while loop
  285. return futureValue;
  286. } // end calculateFutureValue()
  287. } // end class FutureValueCalculations
  288.  
Success #stdin #stdout 0.24s 54088KB
stdin
Standard input is empty
stdout
This code is written by Danny Chen
Index = 0
Name = Prof. Taylor
Graduation Present = $250.00
Holiday Bonus = $200.00
Monthly IRA deduction = $350.00
Yearly Interest Rate = 3.25%
Number Of Years = 20
Future Value = $124,452.43
Index = 1
Name = Prof. Saito
Graduation Present = $150.00
Holiday Bonus = $500.00
Monthly IRA deduction = $500.00
Yearly Interest Rate = 3.25%
Number Of Years = 40
Future Value = $533,954.40
Index = 2
Name = Danny Chen
Graduation Present = $200.00
Holiday Bonus = $600.00
Monthly IRA deduction = $800.00
Yearly Interest Rate = 3.25%
Number Of Years = 60
Future Value = $1,891,136.67
Index = 1
Name = Prof. Saito
Graduation Present = $150.00
Holiday Bonus = $500.00
Monthly IRA deduction = $500.00
Yearly Interest Rate = 3.25%
Number Of Years = 40
Future Value = $533,954.40