CS 160

Exam I

Spring 2005

 

 

Question

Number

Point

Value

Points Awarded

1

15

 

2

20

 

3

14

 

4

16

 

5

15

 

6

20

 

Total

100

 

 

 

Name _________________________

Student Id _______________________

 

  • Six questions, make sure you answer all of them
  • 50 Minutes
  • If I can’t read your handwriting I have to count it wrong.
  • Short answer means short answer.
  • Any curve would be done after all tests are graded
  • Good Luck
  1.  (15 points)In the Internet Explorer graphic on  the next page sketch the results of the following HTML code.

 

<html>

<head>

     <title>Spring 2005</title>

</head>

<body bgcolor="ffffff">

<center>

    <h1>Midterm Test 1</h1>

</center>

 

<center>

<table width="75%" border="5">

<tr>

   <td align="right">

     ADA

   </td>

   <td>

      PERL

   </td>

</tr>

<tr>

  <td colspan="2" bgcolor="000000" align="center">

    Python

   </td>

</tr>

<tr>

  <td colspan="2" align="center">

    <table width="50%">

       <tr>

          <td align="Center">Oberon</td>

          <td align="center">Logo</td>

       </tr>

     </table>

  </td>

</tr>

</table>

</body>

</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


  1. (2 points each)Short answer, keep your answers SHORT and make sure I can read them.

 

    1. How many constructors can a class have?

 

 

    1. How many return values can a constructor have?

 

 

 

    1. How many return values can a method have?

 

 

    1. How many methods can a class have?

 

 

 

    1. How many in-parameters can a method have?

 

 

    1. What would the following lines of code print:

int a = 5;

System.out.println(++a);

 

 

    1. If the next line of code after the two lines above executed was the following line what would print at this line?

System.out.println(a++);

           

 

    1. And then if the next line was the following two lines what would print?

A *= 2;

System.out.println(a+5);

 

 

    1. Declare a variable that can hold a true/false value and initialize it to true.

 

 

    1. What is the keyword for a variable that is not modifiable after it is initialized?

 

 

 

 

 

 

 

 

  1.  Answer the following questions give the following class definition:

 

public class DisneyLand

{

                private int price;

                private String ride;

               

                public DisneyLand(String r)

                {

                                ride = r;

                                price = 10;

                }

            public DisneyLand(String r, int p)

                {

                                price = p;

                                ride = r;

                }

                public DisneyLand()

                {

                                price = 5;

                                ride = "Zipper";

                }

               

                public String getName()

                {

                     return ride;

                }

               

                public String getName(String r)

                {

                    DisneyLand d1 = new DisneyLand("Spider");

                    return d1.getName();

                }

                public int takeRide(int amnt)

                {

                    int cost;

                    cost = price * amnt;

                    return cost;

                 }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(2 points each)

    1. In the code on the preceding page are there any constructors, and if so how many?

 

 

    1. In the code on the preceding page are there any methods, and if so how many?

 

 

    1. In the code on the preceding page are there any instance fields, and if so how many?

 

 

    1. In the code on the preceding page are there any API calls, and if so how many?

 

 

    1. In the code on the preceding page are there any local variables, and if so how many?

 

 

    1. In the code on the preceding page are there any examples of overloading, and if so what are the examples?

 

 

    1. In the code on the preceding page are there any examples of return values, and if so what are the return values used?

 

 

 

 


  1. (16 points)Using the DisneyLand class on the previous page give the output of the following code (4 output lines):

 

 

public class Fun

{

                public static void main(String [] args)

                {

                    DisneyLand d1 = new DisneyLand();

                    String name = d1.getName();

                   

                    DisneyLand d2 = new DisneyLand("Ferris Wheel");

                   

                    System.out.println(name + " and " + d2.getName());

                   

                    DisneyLand d3 = new DisneyLand();

                    name = d3.getName("It's a Small World");

                   

                    System.out.println(name + " and " + d3.takeRide(2));

 

                   System.out.println(d3.getName() + " and " + d3.takeRide(3));

       

                   System.out.println(d1.takeRide(1) + ", " + d2.takeRide(2) + d3.takeRide(1));

       

                }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


  1. (15 total points)Given the following cut of the String API answer the following questions:

 

 

 

char

charAt(int index)
          Returns the character at the specified index.

boolean

equals(Object anObject)
          Compares this string to the specified object.

 int

indexOf(int ch)
          Returns the index within this string of the first occurrence of the specified character.

 int

length()
          Returns the length of this string.

String

replace(char oldChar, char newChar)
          Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

 String

substring(int beginIndex)
          Returns a new string that is a substring of this string.

 

 

 

 

    1. (3 points)What is the return value of the equals method?

 

 

    1. (6 points)If you have the following String:

String answer = “hamburger”;

 

Use the API above and write the code to print out the amount of characters in the String answer.  (Do not hardcode the 9, use the API and find it).

 

 

 

 

    1. (6 points)Write a method that receives a String (any String) and finds out where the letter ‘x’ is located in the string and returns that location.

 


  1. (20 points)Implement a class Employee with the following properties. An employee has a certain wage per hour and a timecard that holds the hours the employee has worked since the last payday. The employee also has a counter that keeps track of the sales since the last pay period.  The wage per hour is specified in the constructor by the calling class, and the timecard’s initial value is 0, as well as the sales amount. Supply a method work that will be called after a day of work, the method will get the amount of hours worked and the amount of items sold. Also you will need provide the methods for returning the timecard current value, and a pay method that will calculate the employees current pay which is calculated by multiplying the amount of hours worked times the wage per hour plus $5 for every sale made. Be sure to reset the hours worked and sales made values after the pay has been calculated.

 

  Here is a sample usage of creating an instance of the Employee class and calling the methods, this is what I would write if I was writing a program that uses the class you write from the paragraph above. (sample):

 

                 Employee e1 = new Employee(5.75);

                   e1.work(5.25, 3);

                   e1.work(3.5, 1);

                   System.out.println(e1.getHours());

                   double cash = e1.pay();

                   System.out.println("paid: " + cash);

 

Write your class here: