CS 1401, Final Exam

Date: Wednesday, December 11, 2013
Name (please type legibly, ideally in block letters): ______________________________________________________________________

The last page is for your reference. It can be detached and does not need to be turned in.

Each problem is worth 10 points, except if indicated otherwise.

Topic: On December 11, 2008, Bernard "Bernie" Madoff was arrested for running a Ponzi scheme, when he paid high interest to investors by taking money from the new investors. To run this scheme undetected, he used a lot of bogus accounting.




Question 1:


Question 2:


Question 3:


Question 4:


Question 5:


Question 6:


Question 7:


Question 8:


Question 9:


Question 10:


Total:

1. Modern finance requires a large number of computations. In the ancient times and in the Middle Ages, rules describing these computations were described by words. For example, a textbook would say "To calculate the profit from an investment, subtract the amount of money that you originally had from the amount that you have at the end of the period." Francois Viete (also known as Francisc Vieta) came up with an invention that simplify the description of such rules -- and thus, made computations easier and faster. What did he invent? By using his invention, how will you describe the above rule?

For extra credit: Describe one more event from the history of computing, and how this event influenced the field of computing.


2. The success of an investment scheme is measured by the return-on-investment (ROI). This return-on-investment is computed by dividing the amount a year after the investment by the amount invested, multiplying by 100 (to get it by percents) and subtracting 100. For example, if you invested $200.00 and got $250.00 a year after, the resulting return-on-investment is 250.00/200.00 * 100 − 100 = 125.0 − 100 = 25.0%.

a. Write a method called invest that takes as argument the invested amount and the amount after a year, and returns the return-on-investment. Your method should throw an "IllegalArgumentException" if the amount invested is negative or zero, otherwise it should computes and return the return-on-investment.

b. Write a statement that calls your method with the example above.

c. Trace your method call step by step.

For extra credit: Use techniques that we learned in class (and when preparing for one of the labs) to return the result rounded to one digit after the decimal point. For example, the return-on-investment of 1.37% should be returned as 1.4%. (In the class, we used the method Math.floor() that cuts off everything after the decimal point; e.g., it transforms 1.99 to 1.)


3. The following method prints a recommendation on whether to invest with a given investment firm. As input, this method receives a code, where + means 'we recommend investment", 0 means "not sure", and − means "we recommend not to invest".

public static void printRecommendation(char code, String firm){
  switch (code) {
    case '+':  System.out.println("We recommend to invest in " + firm);
             break;
    case '0':  System.out.println("We are not sure whether you should invest in " + firm);
             break;
    case '−':  System.out.println("We recommend not to invest in " + firm);
             break;
    default: System.out.println("invalid code");
}
Write an equivalent method that uses if or if-else statements instead of a switch statement.

Which version of the two methods do you prefer? Explain why.


4. Suppose you have an array of Strings named firms containing the names of all investment firms, and an array of double named invest (of the same size) containing the return-on-investment gained the corresponding firm. You also have a double variable named threshold.

Write a piece of code that prints the names of all investment firms whose return-on-investment is equal to or above the threshold.


5. In finance, it is very important to get all the numbers right, so testing a code is extremely important. Describe at least five general rules for testing software (e.g., it is necessary to test all branches), and explain, on the example of one of the methods you wrote in Problem 1-4, what exactly values you should use when testing this method. (We want exact values, not just general descriptions like "boundary values".)


6. To decide into which firm to invest, an investor looks into the return-on-investment that this firm achieved this year and in the last year. Define a class Firm in which each object has three private fields: name, investCurrent, and investLast. Your code for defining the class should contain:

Write a statement that creates an object of type Firm named "Madoff Fund" with current year's return-on-investment 15% and last year's return-on-investment of 20%.


7. To guide people in investing their money, it is important to give them access to the corresponding data. Write a method named enterData that returns an array of objects of type Firm that are created by prompting the user to enter appropriate data. In particular, your method asks the user for the number of firms to be entered, creates the array, enters a loop that prompts for the firm name, return-on-investment in the current and last years, creates an object to be stored in the array. For example, if the user wants to enter 2 firms, the program creates an array of size 2, then prompts for the name of the first firm, the first firm's return-on-investment in the current year, and the first firm's return-on-investment in the last year, then creates an object of type Firm which is stored at index 0 of the array. Then it prompts for the data for the second firm, creates another object and store it at index 1 of the array.

Reminders

To create a Scanner: Scanner input = new Scanner(System.in);
To read an integer value: v = input.nextInt();
To read a String: s = input.nextLine();


8. You want to appropriately write and test your enterData method.


9.We want to improve data entry with a graphical user interface. The user should enter the firm's name and its return-on-investment in the current year and in text fields, and click on an "Enter" button. When the enter button is clicked, the following should happen:

We provided separately the LoanCalculator program from our textbook. On the next page, we have modified that program to suit our purpose. Complete the program by writing the appropriate code for the actionPerformed method.

Below we included the window created by running the LoanCalculator and our FirmEntry programs.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class FirmsEntry extends JFrame {
   // Create text fields for title, articles, amendments,
   // and number of entries
   private JTextField jtfFirmName = new JTextField();
   private JTextField jtfInvestCurrent = new JTextField();
   private JTextField jtfInvestLast = new JTextField();
   private JTextField jtfNumberOfEntries = new JTextField();
   private int nEntries = 0; // the number of entries so far

   // Create a Enter Firm button
   private JButton jbtEnterFirm = new JButton("Enter Firm");

   public FirmsEntry() {
     // Panel p1 to hold labels and text fields
     JPanel p1 = new JPanel(new GridLayout(4, 2));
     p1.add(new JLabel("Name"));
     p1.add(jtfFirmName);
     p1.add(new JLabel("Current ROI"));
     p1.add(jtfInvestCurrent);
     p1.add(new JLabel("Last Year's ROI"));
     p1.add(jtfInvestLast);
     p1.add(new JLabel("Number of Entries"));
     p1.add(jtfNumberOfEntries);
     jtfNumberOfEntries.setText("0 ");
     p1.setBorder(new
       TitledBorder("Enter name, current ROI, last year's ROI"));

     // Panel p2 to hold the button
     JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
     p2.add(jbtEnterFirm);

     // Add the panels to the frame
     add(p1, BorderLayout.CENTER);
     add(p2, BorderLayout.SOUTH);

     // Register listener
     jbtEnterFirm.addActionListener(new ButtonListener());
   }

   /** Handle the EnterFirm button */
   private class ButtonListener implements ActionListener {
     @Override
     public void actionPerformed(ActionEvent e) {



















     }
   }

   public static void main(String[] args) {
     FirmsEntry frame = new FirmsEntry();
     frame.pack();
     frame.setTitle("TreatyEntry");
     frame.setLocationRelativeTo(null); // Center the frame
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}

10. Some students in some CS classes copy labs from others.

a. Is it ethically acceptable to copy labs? Give an argument as to why some may think it is acceptable, and an argument as to why some may think otherwise.

b. Copying labs is against UTEP regulations. Both regulations and ethics are meant to describe acceptable behavior. Explain the difference between what is ethical and what is prohibited by regulations.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class LoanCalculator extends JFrame {
  // Create text fields for interest rate, years
  // loan amount, monthly payment, and total payment
  private JTextField jtfAnnualInterestRate = new JTextField();
  private JTextField jtfNumberOfYears = new JTextField();
  private JTextField jtfLoanAmount = new JTextField();
  private JTextField jtfMonthlyPayment = new JTextField();
  private JTextField jtfTotalPayment = new JTextField();

  // Create a Compute Payment button
  private JButton jbtComputeLoan = new JButton("Compute Payment");

  public LoanCalculator() {
    // Panel p1 to hold labels and text fields
    JPanel p1 = new JPanel(new GridLayout(5, 2));
    p1.add(new JLabel("Annual Interest Rate"));
    p1.add(jtfAnnualInterestRate);
    p1.add(new JLabel("Number of Years"));
    p1.add(jtfNumberOfYears);
    p1.add(new JLabel("Loan Amount"));
    p1.add(jtfLoanAmount);
    p1.add(new JLabel("Monthly Payment"));
    p1.add(jtfMonthlyPayment);
    p1.add(new JLabel("Total Payment"));
    p1.add(jtfTotalPayment);
    p1.setBorder(new
      TitledBorder("Enter loan amount, interest rate, and years"));

    // Panel p2 to hold the button
    JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    p2.add(jbtComputeLoan);

    // Add the panels to the frame
    add(p1, BorderLayout.CENTER);
    add(p2, BorderLayout.SOUTH);

    // Register listener
    jbtComputeLoan.addActionListener(new ButtonListener());
  }

  /** Handle the Compute Payment button */
  private class ButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      // Get values from text fields
      double interest =
        Double.parseDouble(jtfAnnualInterestRate.getText());
      int year = Integer.parseInt(jtfNumberOfYears.getText());
      double loanAmount =
        Double.parseDouble(jtfLoanAmount.getText());

      // Create a loan object
      Loan loan = new Loan(interest, year, loanAmount);

      // Display monthly payment and total payment
      jtfMonthlyPayment.setText(String.format("%.2f",
        loan.getMonthlyPayment()));
      jtfTotalPayment.setText(String.format("%.2f",
        loan.getTotalPayment()));
    }
  }

  public static void main(String[] args) {
    LoanCalculator frame = new LoanCalculator();
    frame.pack();
    frame.setTitle("LoanCalculator");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}