CS 1401, Final Exam

Date: Tuesday, December 10, 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 10, 1898, the Treaty of Paris ended the Spanish-American War. As a result of the treaty, Puerto Rico, Guam, and the Philippines were ceded to the United States, and Cuba gained independence from Spain.




Question 1:


Question 2:


Question 3:


Question 4:


Question 5:


Question 6:


Question 7:


Question 8:


Question 9:


Question 10:


Total:

1. Following its inclusion in the United States, Puerto Rico had a first census in 1899, and in that year, its estimated population was 953,243. At that time, Herman Hollerith, was a U.S. Census Bureau employee, and one of his inventions was very useful for the census. What did he invent? Also, which company did he found, following up on his invention?

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


2. Nowadays, international treaties are registered with the United Nations, so the UN must store all those treaties, which are usually lengthy. Before printing a treaty, we would like to know how many pages it will take.

a. Write a method called nPages that takes as argument the number of words in a treaty, and the number of words per page desired. Your method throws an "IllegalArgumentException" if any of the arguments is negative, otherwise it computes and returns the number of pages to print the treaty. For example, if a treaty has 2410 words, and you want 200 words per page, the method should return 12.5.

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

c. Trace your method call step by step.

For extra credit: Instead of returning the number of pages as a double, return the number of pages as an integer. If the treaty had 2401 words, printing 200 words per page would require 13 pages, as the last word would require a new page. You may want to call the method Math.ceil() that rounds a number up to the next integer.


3. The following method prints a part of a treaty. It receives a code, where 0 means an article, 1 means an amendment, and 2 means a reservation.

public static void printDescription(int code, String description){
  switch (code) {
    case 0:  System.out.println("Article: "+description);
             break;
    case 1:  System.out.println("Amendment: "+description);
             break;
    case 2:  System.out.println("Reservation: "+description);
             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 titles containing the title of all treaties, and an array of integers named articleCounts of the same size containing the number of articles in the corresponding treaties. You also have an integer variable named threshold.

Write a piece of code that prints the title of each treaty for which the article count is equal or greater than the threshold.


5. The United Nation's software development unit is in charge of writing software to handle all the treaties. To be effective, it uses the software development process that includes 7 stages: (in alphabetic order) deployment, implementation, maintenance, requirement specification, system analysis, system design, and testing. Put these stages in order from first stage to last stage, and describe each stage with one sentence.


6. Usually, a treaty has several articles and amendments. Define a class Treaty in which each object has three private fields: title, numberOfArticles, and numberOfAmendments. Your code for defining the class should contain:

Write a statement that creates a Treaty object titled "Treaty of Paris" with 10 original articles and one amendment (related to U.S. involvement in Cuban affairs.)


7. As part of the United Nations software development unit team, your task is to write a method that is used to enter data. Write a method named enterTreaties that returns an array of Treaty objects that are created by prompting the user to enter appropriate data. In particular, your method asks the user for the number of treaties to be entered, creates the array, enters a loop that prompts for the treaty title, number of articles, number of amendments, and creates an object to be stored in the array. For example, if the user wants to enter 2 treaties, the program creates an array of size 2, then prompts for the title of the first treaty, the number of articles in the first treaty, and the number of amendments in the first treaty, then creates a Treaty object which is stored at index 0 of the array. Then it prompts for the data for the second treaty, creates another object and store it at index 1 of the array.

Reminders

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


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


9.We want to improve data entry with a graphical user interface. The user should enter the treaty title, the number of articles and the number of amendments 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 TreatyEntry programs.


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

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

  // Create a Enter Treaty button
  private JButton jbtEnterTreaty = new JButton("Enter Treaty");

  public TreatyEntry() {
    // Panel p1 to hold labels and text fields
    JPanel p1 = new JPanel(new GridLayout(4, 2));
    p1.add(new JLabel("Title"));
    p1.add(jtfTreatyTitle);
    p1.add(new JLabel("Number of Articles"));
    p1.add(jtfNumberOfArticles);
    p1.add(new JLabel("Number of Amendments"));
    p1.add(jtfNumberOfAmendments);
    p1.add(new JLabel("Number of Entries"));
    p1.add(jtfNumberOfEntries);
    jtfNumberOfEntries.setText("0 ");
    p1.setBorder(new
      TitledBorder("Enter title, #articles, #amendments"));

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

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

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

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



















    }
  }

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

10. Both laws and rules of ethics are meant to describe acceptable behavior. What is the difference between a law and a rule of ethics?











The World Intellectual Property Organization Copyright (WIPO) Treaty is an international treaty on copyright law. Article 11 of this treaty prohibits circumvention of technological measures for the protection of works. Is it ethically acceptable to circumvent technological protection measures in the pursuit of gathering evidence in criminal cases? Give an argument as to why some may think it is acceptable, and an argument as to why some may think otherwise.


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);
  }
}