CS 1401, Exam #3, 11:30 am version

Date: Friday, November 9, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Exactly 40 years ago, on November 9, 1967, the "Rolling Stone" magazine was launched. It is still the main magazine to read about rock music, rock groups, and rock concerts. Rock music is usually not about computing, but when a group stages a concert, a lot of planning and computing is needed. Let us help them.

The profit produced by a concert can be computed as the revenue (ticket price (p) times number of seats (s)) minus expenses (e).

1a. Write a method named profit for computing the value p * s - e for given integer s and given real numbers p and e.

1b. Call (invoke) your method profit in the main method to compute the profit of a concert where each ticket costs $50.00, 10,000 seats were sold, and the expenses were $450,000.00. (You do not need to write the entire main method, just the part that assigns values to m and c and calls your method.)

1c. Trace, step by step, how the computer will perform the needed computations, and check that the result is indeed correct (should be $50,000.00).


Solution:

public double profit(double price, int seats, double expenses){
  return price * seats - expenses;}

in the method main:

double p = 50.0;
int s = 10000;
double e = 450000.0;
double profi = profit(p, s, e);

Tracing:

first,

 ---------    ---------   -----------
|   50.0  |  | 10000  |  | 450000.0  |
 ---------    ---------   -----------
    p             s           e

 --------------------------------------------
|  ---------    ----------    -----------    |
| |  50.0   |  |  10000   |  | 450000.0  |   |
|  ---------    ----------    ------------   |
|   price         seats         expenses     |
|--------------------------------------------|
|                                            |
|                                            |
|                                            |
|                                            |
 --------------------------------------------
            profit

 ---------
| 50000.0 |
 ---------
  profi

2a. Define a class Concert whose elements are concerts; each concert is characterized by its ticket price (in US dollars), the number of seats, and expenses (in US dollars). Your class should contain a constructor method, accessor methods, mutator methods, and a method for computing the concert's profit.

2b. Use your class in the main method to define a new object "elPasoConcert" of type Concert, at which each ticket costs $50, 10,000 seats were sold, and the expenses were 450,000.00. Compute and print this concert's profit; then, assume that twice as many seats were sold, and compute and print the new value of the profit.

2c. Trace your program step-by-step.

Solution:

public class Concert{
  private double price;
  private int seats;
  private double expenses;

  public Concert(double aprice, int aseats, double aexpenses){
    price = aprice, seats = aseats; expenses = aexpenses;}

  public double getPrice(){return price;}

  public int getSeats(){return seats;}

  public double getExpenses(){return expenses;}

  public void setPrice(double newPrice){price = newPrice;}

  public void setSeats(int newSeats){seats = newSeats;}

  public void setExpenses(double newExpenses){expenses = newExpenses;}

  public double profit(){return price * seats - expenses;}
}

In the main method:

Concert elPasoConcert = new Concert(50.0, 10000, 450000.0);
double profi = elPasoConcert.profit();
System.out.println(profi);

int s = elPasoConcert.getSeats();
elPasoConcert.setSeats(2 * s);
profi = elPasoConcert.profit();
System.out.println(profi);

Tracing:

on the first line, we call a constructor method:

 --------------
|  ----------  |
| |  50.0    | |
|  ----------  |
|   aprice     |
|  ----------  |
| |  10000   | |
|  ----------  |
|    aseats    |
|  ----------  |
| | 450000.0 | |
|  ----------  |
|  aexpenses   |
 --------------
|              |
|              |
|              |
|              |
|              |
|              |
 --------------
    Concert

after the first line, we get a new object

 --------------
|  ----------  |
| |  50.0    | |
|  ----------  |
|    price     |
|  ----------  |
| |  10000   | |
|  ----------  |
|     seats    |
|  ----------  |
| | 450000.0 | |
|  ----------  |
|   expenses   |
 --------------
 elPasoConcert

after the second line, we get

 ----------
| 50000.0  |
 ----------
   profi

at the third line, we print 500000.0 to the screen

then, we compute the value

 ----------
|  20000   |
 ----------
     s

and change the object to:

 --------------
|  ----------  |
| |  50.0    | |
|  ----------  |
|    price     |
|  ----------  |
| |  20000   | |
|  ----------  |
|     seats    |
|  ----------  |
| | 450000.0 | |
|  ----------  |
|   expenses   |
 --------------
 elPasoConcert


after that, we change the value of the variable profi:

 ----------
| 550000.0 |
 ----------
    profi

and we print 450000.0 to the screen

3a. Write a piece of code that, given an array p of ticket prices of different concerts, an array s containing numbers of seats, and the array e containing expenses, computes and prints the profits of different concerts.

3b. To check the correctness of the code you wrote in Part 3a, write a piece of code write a piece of code that defines arrays corresponding to 2 concerts:

3c. Trace step-by-step how the piece of code you wrote in Part 3a will compute the corresponding profits. Hint: the profits are equal to $50,000 and $550,000.


Solution:

for (int i = 0; i < d.length; i++)
  {System.out.println(p[i] * s[i] - e[i]);}

Testing:

double[] p = {50.0, 50.0};
double[] s = {10000, 20000};
double[] e = {450000.0, 450000.0};

Tracing:

The fact that we have defined an array p with 2 elements means, in
effect, that we have defined two variables p[0] and p[1]:

 ----
|  \|
 ---\
  p  \
      \|
      - -------
       | 50.0 |  p[0]
        -------
       | 50.0 |  p[1]
        -------

The fact that we have defined an array s with 2 elements means, in
effect, that we have defined two variables s[0] and s[1]:

 ----
|  \|
 ---\
  s  \
      \|
      - -------
       | 10000 |  s[0]
        -------
       | 20000 |  s[1]
        -------

The fact that we have defined an array e with 2 elements means, in
effect, that we have defined two variables e[0] and e[1]:

 ----
|  \|
 ---\
  e  \
      \|
      - ----------
       | 450000.0 |  e[0]
        ----------
       | 450000.0 |  e[1]
        ----------


Next, we get to the for-loop. First, we define a new integer
variable i and assign to it the value 0:

 -------
|   0   |
 -------
   i

We check the condition: the length of our array p is 2, so i is
smaller than this length. Thus, the condition i < p.length is
satisfied, and we implement the operation in the body of the loop.
Since i = 0, we thus perform the operation

  System.out.println(p[0] * s[0] - e[0]);

In other words, we print the value 50.0 * 1000 - 450000.0 = 50000.0.

Then, we go to the update statement i++; this statement adds 1 to i,
so we get:

 -------
|   1   |
 -------
    i

The condition i < p.length is still satisfied, so we again go inside
the loop. This time, i is 1, so the statement in the body of the
loop becomes

  System.out.println(p[1] * s[1] - e[1]);

Thus, we print the value 50.0 * 2000 - 450000.0 = 550000.0.

Then, we go to the update statement i++; this statement adds 1 to i,
so we get:

 -------
|   2   |
 -------
    i

The condition i < p.length is no longer satisfied, so we exit the
loop.

4a. Write a method that, given an array s of numbers of seats at different concerts, finds the concert with the largest attendance.

4b. To check the correctness of your method, write a piece of code that defines a new array s consisting of 3 integers 10,000, 20,000, and 5,000.

4c. Trace step-by-step how the piece of code you wrote in Part 4a will find the concert with the largest attendance. Hint: the desired largest attendance concert is element No. 1, its attendance is

max(10,000, 20,000, 5,000) = 20,000.


public int findLargest(int[] s){
  int largest = 0;
  for(int i = 1; i < m.length; i++)
    {if (s[i] > s[largest]) largest = i;}
  return largest;
}

in the main method:

int[] seats = {10000, 20000, 5000};
int bestAttended = findLargest(seats);

tracing:

First, we define an array

 ---------
|        \|
 ---------\
  seats    \
            \|
            --------
            | 10000 |  seats[0]
             -------
            | 20000 |  seats[1]
             -------
            |  5000 |  seats[2]
             -------

Then, we call a method:

 ---------------------------
|    ----                   |
|   |  \|                   |
|    ---\                   |
|     s  \                  |
|         \|                |
|         - -------         |
|          | 10000 |  s[0]  |
|           -------         |
|          | 20000 |  s[1]  |
|           -------         |
|          |  5000 |  s[2]  |
|           -------         |
|---------------------------|
|                           |
|          ...              |
 ---------------------------
       findLargest

Within this method, we define a new variable largest and assign
it a value 0:

 ---------------------
|        0            |
 ---------------------
    largest

Then, we start a loop by defining a new variable i and assigning it
a value 1:

 ---------------------      ---------
|        0            |    |    1    |
 ---------------------      ---------
     largest                    i

Since i = 1 < s.length = 3, we go inside the loop. Since i = 1, the
condition 20000 = s[1] > s[largest] = 10000 is satisfied, so we assign,
to the variable largest, a new value i = 1:

 ---------------------      ---------
|        1            |    |    1    |
 ---------------------      ---------
     largest                    i

Next, we increase i by 1:

 ---------------------      ---------
|        1            |    |    2    |
 ---------------------      ---------
     largest                    i

Since i = 2 < s.length = 3, we go inside the loop. Since i = 2, the
condition 5000 = s[2] > s[largest] = 20000 is not satisfied, so we do not
do anything. Next, we increase i by 1:

 ---------------------      ---------
|        1            |    |    3    |
 ---------------------      ---------
     largest                    i


Now, 3 = i is not smaller than s.length = 3, so we exit the loop, and
return the value largest = 1 to the line

int bestAttended = findLargest(seats);

according to which the value 1 is assigned to the new variable
bestAttended:

 -------------
|     1       |
 -------------
  bestAttended

5. Name main GUI components, and briefly explain their use.

Windows create actual windows on the screen

Labels indicate the meaning of different parts of the window

Text area is a place where the user types in the input and/or the
program  places the output

A button is what a user pressed to initiate a new process, like
"Calculate" or "Exit"