Exemption exam for ECS 102 - Java

The goal of this exam is to give recognition for programming you already know, so you can fulfill a requirement and move on to learning something new. If you already know Java don't plan on taking ECS102 to learn C. You already know some fundamentals of programming, and you can learn the syntax of C in a weekend if you need it.

This is a closed book exam. You are writing code without running it. Please answer the questions in this Word document, immediately below the question. Use as much space as you need. If you do not remember the exact syntax of a statement, make your best guess and continue. Send solutions to . Use "ecs102 exemption exam" as the Subject to make sure I see it and process it promptly.

Your name:______Your SU ID#:______

Campus email:______

Major:______

ECS102 section______

Have you taken the Computer Science AP exam? If so, what did you get? ______

1. Write a method, convert, that converts a length from metric to British measure or from British to metric measure. You may not assume any global variables. There should be a parameter conv_type where the user can tell the method which kind of conversion to do (British to metric or metric to British). There should be a parameter orig_length, the length in the original unit. The return value should be the length in the new units.

  • British lengths are in inches.
  • Metric lengths are in centimeters.
  • You may use the values of 2.54 cm is 1 inch and
  • 0.39 inches is 1 cm.
  • If conv_type is 'B' or 'b' then the conversion is from British to metric.
  • If conv_type is 'M' or 'm' then the conversion is from metric to British.
  • If conv_type is any other character, then Invalid Conversion is printed and -1 should be the returned function(method) value.

For example, if we have

bl = 10.0;

ml = 0.0;

Then after the call ml=convert('B', bl);

we should have

ml is 25.4

bl is 10.0

because 10 inches is equal to 25.4 cm.

2. a) I have an array with ten thousand numbers, in ascendingorder. I want to find the index (subscript) of a particular number, if it is in the array. Is it better for me to use a binary search or a linear search? Why?

b) I have an array with ten thousand numbers that is unordered. I want to find the index (subscript) of a particular number if it is in the array. Is it better for me to use a binary search or a linear search? Why?

3. Consider the following code:

public static int mystery( int n )

{

if ( n = = 1)

return 1;

else

return (2 * mystery(n-1) + n);

}

Fill in values in the table below:

n / mystery(n)
1
2
3

4. Assume you have access to the method

public static int myrand( );

that returns a random integer in the range 0 to 100,000 with uniform probability. (That is, every whole number is equally likely to come up.) You do not have to write this method, and you should not use a built in random method. Use myrand for this problem.

You are given the code segment:

char[] cards=new char[15];

int i;

for (i=0; i<15; i++)

cards[i]='Z';

Write a code segment (several lines of code) to follow this code that will change the values of two distinct,randomlychosen cards to 'A'. Make sure that the two cards are not the same card! Declare any variables that you use.

5. Write a code segment that reads an integer and prints “odd” or “even” to show that the number is odd or even.

6. Write a code segment to read a string from the keyboard and print the number of occurrences of the letter E (upper case and lower case.)

7. The class Shirt has the following fields:

int size;

String color;

If sh is a Shirt I want

System.out.print(sh)

to print

shirt size: 18

color: blue

(if sh is a blue size 18 shirt)

Write the Shirt method toString.

8. You are given the following class:

public class Station

{

private double onTheDial;

private String callLetters;

public Station(double otd, String cl)

{

onTheDial = otd;

cl = callLetters;

}

}

Write a code segment for main that creates an array of Stations with the following values:

Call LettersOn the Dial

WRVO / 89.9
WCNY / 91.3
WAER / 88.3

9. Write a method

public static int FindLargest(double[] d)

that will return the index of the largest number in the array d.