CS 1401, Final Exam

Date: Friday, December 13, 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 13, 1962, the first active repeater communication satellite was launched. At present, satellites help to send information everywhere in the world.




Question 1:


Question 2:


Question 3:


Question 4:


Question 5:


Question 6:


Question 7:


Question 8:


Question 9:


Question 10:


Total:

1. Before modern times, communication was difficult and took a long time. As a result, people tried to explain their ideas in a very clear way -- since it was not possible to send an email and ask for a clarification. Explain how this necessity led to the development of what we now call algorithms, what role Al-Khowarizmi (also spelled Al-Horezmi) played in this development, and where does the word "algorithm" come from.

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


2. While communications now are faster, they are not immediate. It is desirable to know how much time it will take to send a given amount of information. This amount can be obtained by dividing the amount of information that we want to send (in MegaBytes, MB) by the communication speed of a given channel. For example, if we want to send 340.0 MB of data and the communication channel has a speed of 100.0 MB/sec, then sending this information will take 340.0/100.0 = 3.4 seconds.

a. Write a method called time that takes as argument the amount of information (in MegaBytes, MB) that we want to send and the speed (in Bytes per second) of a given communication channel, and returns the communication time. Your method should throw an "IllegalArgumentException" if any of the arguments are negative, otherwise it should computes and return the communication time.

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

c. Trace your method call step by step.

For extra credit: Use the method described in class and in one of the labs to modify your method so that it returns the result rounded up to two digits after the decimal point. For example, the time 2.137 sec should be returned as 2.14. (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 describes the quality of a communication channel. As input, this method receives a code, where 'e' means excellent, 'g' means good, "s" means satisfactory, and 'u' means unsatisfactory.

public static void printRecommendation(char code, String chan){
  switch (code) {
    case 'e':  System.out.println("The channel " + chan + " is excellent");
             break;
    case 'g':  System.out.println("The channel " + chan + " is good");
             break;
    case 's':  System.out.println("The channel " + chan + " is satisfactory");
             break;
    case 'u':  System.out.println("The channel " + chan + " is unsatisfactory");
             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 channels containing the names of all the communication channels, and an array of double named speed (of the same size) containing the communication speeds of the corresponding channels. You also have a double variable named threshold.

Write a piece of code that prints the names of all the communication channels communication speed is equal to or above the threshold.


5. In many applications of computing, 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. Different communication channels also differ by the cost of using this channel, in dollars per Megabyte. Define a class Channel in which each object has three private fields: name, speed, and cost. Your code for defining the class should contain:

Write a statement that creates an object of type Channel names "satellite" with a speed of 10,000 MBytes/sec and a cost of $0.02/MByte.


7. To make computers select the most appropriate communication channel, it is important to give them access to the corresponding data. Write a method named enterData that returns an array of objects of type Channel that are created by prompting the user to enter appropriate data. In particular, your method asks the user for the number of channels to be entered, creates the array, enters a loop that prompts for the channel name, its speed and cost, and creates an object to be stored in the array. For example, if the user wants to enter 2 channels, the program creates an array of size 2, then prompts for the name of the first channel and the first channel's speed and cost, then creates an object of type Channel which is stored at index 0 of the array. Then it prompts for the data for the second channel, 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 ChannelEntry extends JFrame {
  // Create text fields for title, articles, amendments,
  // and number of entries
  private JTextField jtfChannelName = new JTextField();
  private JTextField jtfSpeed = new JTextField();
  private JTextField jtfCost = new JTextField();
  private JTextField jtfNumberOfEntries = new JTextField();
  private int nEntries = 0; // the number of entries so far

  // Create a Enter Channel button
  private JButton jbtEnterChannel = new JButton("Enter Channel");

  public ChannelEntry() {
    // Panel p1 to hold labels and text fields
    JPanel p1 = new JPanel(new GridLayout(4, 2));
    p1.add(new JLabel("Name"));
    p1.add(jtfChannelName);
    p1.add(new JLabel("Speed"));
    p1.add(jtfSpeed);
    p1.add(new JLabel("Cost"));
    p1.add(jtfCost);
    p1.add(new JLabel("Number of Entries"));
    p1.add(jtfNumberOfEntries);
    jtfNumberOfEntries.setText("0 ");
    p1.setBorder(new
      TitledBorder("Enter channel name, speed, and cost"));

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

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

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

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



















    }
  }

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

10. While the syllabi of most CS classes prohibit this, some students in some of these classes use their cellphones during the tests to look for possible solutions to the test problems.

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

b. Using, on the test, sources of information which are not allowed by the syllabus, 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);
  }
}