Introduction
java,java
Requirement
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 C don’t plan on taking ECS102 to learn C. You already know some fundamentals of programming, and you can learn the syntax of Java 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. If you don’t know how to do a part of a problem, do as much of it as you can, writing a note about what you don’t know how to do. 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_
How did you learn programming? _
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 ascending order. 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];