CMPUT 102: Structural Programming and Data Structures

Exercise 1

  1. Write a program that calculates the compounding value of an investment I with a given interest rate R and a given duration in years D.
    Here is an example of input and output of this program:
    Enter investment amount: 1500
    Enter interest rate in %: 4.5
    Enter number of years: 5
    
    OUTPUT
    
    Investment=$1500.0
    Rate=4.5%
    Number of Years=5
    Your final Amount : $1869.27
         
  2. Change the previous program such that the investment is either compounded every year, every 6 months, or every month, based on a given parameter P. You can use a static method to read and validate the user's input.
    Here is an example of input and output of this program:
    Enter investment amount: 1500
    Enter interest rate in %: 4.5
    Enter number of years: 5
    Interest Compounded: (1)Yearly, (2)Every 6 months, (3)Monthly:6
    (1)Yearly, (2)Every 6 months, (3)Monthly:abc
    (1)Yearly, (2)Every 6 months, (3)Monthly:33
    (1)Yearly, (2)Every 6 months, (3)Monthly:3
    
    OUTPUT
    
    Investment=$1500.0
    Rate=4.5%
    Number of Years=5
    Period: Monthly
    Your final Amount : $1877.69     
         
  3. Change the previous program such that the processing of the investment value is done in a method of a class Investment invoked many times in the main program for different values of I, R, D, and P.
  4. Change the previous program such that it displays the increases in the investment value after each year period.
    Here is an example of input and output of this program:
    Enter amount 0 to quit.
    Enter investment amount: 1500
    Enter interest rate in %: 4.5
    Enter number of years: 5
    Interest Compounded:(1)Yearly, (2)Every 6 months, (3)Monthly:1
    
    Year	Ratio   Investment      Increase        Amount End of Year
    1       	4.5     	1500.0          	67.5             1567.5
    2       	4.5     	1567.5          	70.54           1638.04
    3       	4.5     	1638.04         	73.71           1711.75
    4       	4.5     	1711.75         	77.03           1788.78
    5       	4.5     	1788.78         	80.49           1869.27
    
    Enter amount 0 to quit.
    Enter investment amount: 1500
    Enter interest rate in %: 4.5
    Enter number of years: 5
    Interest Compounded:(1)Yearly, (2)Every 6 months, (3)Monthly:2
    
    Year	Ratio   Investment      Increase        Amount End of Year
    1 	4.5	1500.0		33.75           1533.75
    1	4.5	1533.75       	34.51           1568.26
    2	4.5	1568.26        	35.29           1603.55
    2	4.5	1603.55        	36.08           1639.63
    3	4.5	1639.63        	36.89           1676.52
    3	4.5	1676.52        	37.72           1714.24
    4	4.5	1714.24        	38.57           1752.81
    4	4.5	1752.81        	39.44           1792.25
    5	4.5	1792.25        	40.32           1832.57
    5	4.5	1832.57        	41.24           1873.81
    
  5. Can you add in the class Investment a method that given values for R, D, P, and a final sum S, will calculated the necessary initial investment I?
Solution for exercise 1

Exercise 2

A fictive company has 10 employees and manages the personnel files using java classes. An employee record includes the following information:
  • Id (9 digits stored in a string)
  • Name (string)
  • Date of birth (date)
  • Years of service (integer)
  • Days of vacation earned (integer)
  • Salary (double)
  • Days absent for illness (integer)
    1. Write the class Employee with the needed instance variables and two constructors: the first receives a values for each instance variable in the order given above, and the second receives a value for only the ID, the name and date of birth of the employee.
    2. Add in the class Employee the following methods:
      • display(): displays all information of an employee in the following format exemple:
        =======================
        ID : 123456789
        Name : Fred Flintstone
        Birth : 60/03/12
        Age : 40
        Salary : $120000
        Service: 8 years
        Abcence: 2 days
        Vacation: 13 days
        =======================
      • displayID(): displays the ID and the name of an employee on the same line (ex: 123456789: Fred Flintstone);
      • getSalary(): returns the salary of an employee;
      • getYearService(): returns the number of years in service;
      • getDaysVacation(): returns the number of days of vacation earned;
      • getDaysAbsence(): returns the number of days of absence for illness;
      • setSalary(): changes the salary with a given new value;
      • setYearService(): changes the number of years of service with a given number;
      • setDaysVacation(): changes the number of days of vacation earned with a given number;
      • setDaysAbsence(): changes the number of days of absence for illness with a given number;
      • getAge(): returns the age of the employee;
    3. Write a black box test for class Employee and incapsulate it in the class Employee as the main method.
    Solution for exercise 2

    Exercise 3

    Write a program that displays the following menu:

    1 - Add employees	
    2 - Display all employees
    3 - Display Given employee
    4 - Display employees with salary higher bound
    5 - Display employees with absence
    6 - Display employees with earned vacation
    7 - Display employees with service
    8 - Remove employees
    9 - Change employees
    0 - Quit
    

    and reads an entry from the keyboard until "0" (for Quit) is entered. Each time a entry is read, the equivalent option label is printed, and the menu is displayed again. There should be a validation for the input; only a digit for 0 to 9 should be accepted.

    Solution for exercise 3


    Exercise 4

    Write the class Staff that models the personnel (collection of employees from class Employee in exercise2) using a vector. The class should have a constructor that creates an empty vector and the following methods: display() to display all employees; display(String ID) to display the employee with the given ID; topSalary(double salary) that displays all the employees and their salaries that have a salary higher than the given salary; absence() that displays the employees with their days of absence in decreasing order of their days of absence; vacation() that displays the employees and their vacation earned in increasing order of their days of vacation earned; remove(String ID) that removes the employee with the given ID; and change(String ID, ...) that changes the state of the Employee with the given ID. The parameters should be consistent with the instance variables of an Employee object.

    Solution for exercise 4


    Exercise 5

    Update the program with the menu written for exercise 3 such that instead of printing strings, the menu would be used to create, update and display the staff by the behavior of class Staff developed in exercise 4.

    Solution for exercise 5


    Exercise 6

    Create an extention called StaffFile of class Staff, developed in exercise 4, to include methods to write and read the staff members in and from a file.

    Solution for exercise 6


    Copyright Osmar R. Zaiane, 1999