Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ASSIGNMENT 1

First Java Assignment

COMP-202A, Fall 2012, All Sections

Due: September 24th, 2012 (23:30)

Please read the entire pdf before starting.


You must do this assignment individually and, unless otherwise specified, you must follow all the general
instructions and regulations for assignments. Graders have the discretion to deduct up to 10% of the value
of this assignment for deviations from the general instructions and regulations. These regulations are posted
on the course website. Be sure to read them before starting.
Part 1: 0 points
Part 2, Question 1: 20 points
Part 2, Question 2: 50 points
Part 2, Question 3: 30 points
100 points total
Before starting on the assignment, you should download from the course website the following three files.
Make sure to save them in the same folder in a place that you can find.
• Question2Testfile.txt
• TypeQuestions.java
• typequestions.txt
These files will both be used for questions 2 and 3.
It is very important that you follow the directions as closely as possible. The directions, while
perhaps tedious, are designed to make it as easy as possible for the TAs to mark the assignments by letting
them run your assignment through automated tests. While these tests will not determine your entire grade,
it will speed up the process significantly, which will allow the TAs to provide better feedback and not waste
time on administrative details. Plus, if the TA is in a good mood while he or she is grading, then that
increases the chance of them giving out partial marks :)

1
Part 1 (0 points): Warm-up
Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the
second part of the assignment, which will be graded. If you have difficulties with the questions of Part 1, then
we suggest that you consult the TAs during their office hours; they can help you and work with you through
the warm-up questions.

Warm-up Question 1 (0 points)


Create a file called HelloWorld.java, and in this file, declare a class called HelloWorld. This class
should define only one method called main(). In the body of this method, use System.out.println()
to display “Hello world!”. You can find such a class in the lecture slides; make sure you can compile
and run it properly.
Warm-up Question 2 (0 points)
Create a file called A.java, and in this file, declare a class called A. This class should define only one
method called main(). In the body of this method, use System.out.println() to display the following
pattern:
A
A A
AAAAA
A A
A A

Warm-up Question 3 (0 points)


Consider the following 2-d matrix:
 
a b
c d

Write a Java program that first reads 4 doubles, representing a,b,c, and d from the keyboard. It then
outputs to the screen the determinant of the matrix.
For a 2x2 matrix, the determinant is always equal to a ∗ d − b ∗ c
Warm-up Question 4 (0 points)
Now consider the same question except on a 3x3 matrix:
 
a b c
d e f
g h i

Write a Java program that first reads 9 doubles, representing the 9 letters above from the keyboard. It
then outputs to the screen the determinant of the matrix.
For a 3x3 matrix, the determinant is always equal to

a ∗ (i ∗ e − f ∗ h) − b ∗ (d ∗ i − f ∗ g) + c ∗ (h ∗ d − e ∗ g)

Warm-up Question 5 (0 points)


For further practice, you can look at the old assignment ones: http://www.cs.mcgill.ca/~cs202/
2012-01/web/assignments.html and http://www.cs.mcgill.ca/~cs202/2011-09/assignments.html

Page 2
Part 2
The questions in this part of the assignment will be graded.

Question 1: Giving a computer instructions! (20 points)


The purpose of this question is to get into the mindset of giving instructions to a computer. You should
put your answers into a file RigorousThinking.txt.
Here is a description for how to call someone on a cell phone. List 5 ways that the instructions
are erroneous, ambiguous, or incomplete. Put each of your answers on a separate line in the file
RigorousThinking.txt and clearly number them.
1. Figure out the number you want to dial.
2. Type the numbers on the touch screen
3. Press the green button
Note that you don’t have to propose a better way to write the instruction. Just list the ambiguity. For
example, one possible answer (which you can’t count as one of your five) is There could be two green
buttons.
Question 2: Keeping in Radio Contact (50 points)
The purpose of this question is to practice writing a full Java program from start to finish. You will
practice reading and writing to and from the screen and keyboard, respectively. It is important to follow
carefully the instructions related to running your program through the test file. This test file will not
guaranteed your program is correct in all cases but it will show that you are on the right track at least.
The following code should be put into a class RobotRadioChecker and thus a file RobotRadioChecker.java
Two robots, Jack and Jill, are planning an excursion. They will start at the same place, from which
Jack heads due north at Vn kilometers per hour. Jill gets delayed before leaving, and thus leaves slightly
later than Jack. Call this time n which is always greater than or equal to 0. She heads due east at Ve
kilometers per hour. We assume that both robots move at a constant speed.
Both robots have radio transmitters that can be used to send information between the two of them. The
radios work as long as the robots are at most a distance of D apart.
As a computer programmer, you are assigned the task of writing a computer program to determine,
given their speeds and radio information, when the robots are going to be so far apart that they no
longer are in contact with each other. You will do this using the following approach:
Let t denote the amount of time (in hours) that has passed since Jack left. In this case Jack is always
t × Vn kilometers north from his starting position because the distance one travels is equal to the average
speed times the time traveling at that speed.. If t > n, meaning that Jill has already left, then Jill is
(t − n) × Ve kilometers east of her starting position.
Using Pythagorean theorem (see figure), we can determine the distance that Jack and Jill are apart from
each other.
Since the length of the hypotenuse of the triangle represents the distance between the two robots, we can
solve for when this distance is equal to D, the maximum distance the radios can communicate across.
After this point, the robots will no longer be in communication with each other.
Performing a little algebra, we thus have the equation:

D2 = (Vn ∗ t)2 + (Ve (t − n))2

which simplifies to:

Page 3
Figure 1: A way to model where the robots Jack and Jill are at time t. Notice that the distance between them
is discovered by the Pythagorean theorem. When the hypotenuse length is larger than D, the maximum
distance the radios can stay in contact, the robots will not be able to communicate any longer.

D2 = Vn2 t2 + Ve2 t2 − 2Ve2 nt + Ve2 n2

and finally becomes:

0 = (Vn2 + Ve2 )t2 − (2Ve2 n)t + Ve2 n2 − D2

Since all terms are fixed other than than t, we can now view this as a quadratic equation with the
variable being t. That is, you will be given the values Vn , Ve , D, and n and so you can substitute them
into the equation. One you do this, you will have an equation of the form at2 + bt + c = 0, which can be
solved using the quadratic equation.
To solve a quadratic equation, we can use the quadratic formula of:

−b + b2 − 4ac
t=
2a
In this case, we have:
1. a is the quadratic term before the t2 (i.e. Vn2 + Ve2 )
2. b is the linear term before the t (i.e. −2Ve2 n
3. c is the constant term that does not involve t at all (i.e. Ve2 n2 − D2 ).

Page 4
Note that normally the quadratic equation gives two roots (with a plus or minus) but in this case, we will
only keep one. (Why is it sufficient to only consider this one for this problem? What is the significance
of the root we are ignoring? See brief explanation after the specification if curious)
Your mission, if you choose to accept it, is to write a computer program that does the following:
1. Read the values Vn , Ve , D, and n from the user. These should all be doubles as they may contain
fractions. You may assume all these numbers are positive, except for n which you may only assume
is greater than or equal to 0. This means if the user types a number less than or equal to 0, your
program may do whatever it wants and still receive full credit. (Ideally, your program should not
install a virus on the user’s computer, but for the purpose of this assignment it is not specified to
do anything in particular in these cases. :) )
2. Ask the user to enter a specific number of hours for them to check. Your program will then check
whether the robots are in contact after this number of hours.
3. Using the formulas above, calculate the amount of time it will take for the robots to be out of reach,
print this result, and store this into a variable. Hint: To avoid mistakes, it will be useful to first
calculate a, b, and c and then store each result into a different variable.
4. If the amount of time is greater than q then print a message such as the following:
The robots will be able to communicate at the time you entered.
5. On the other hand, if the amount of time is less than q, then print a message such as the following:
The robots will be out of touch! If Jack falls down and breaks his crown then
Jill can’t do anything!
You do not need to consider the case where they are exactly equal. You may assume the user enters
a time that is greater than n.
In order to accomplish the last two steps, you will need to use something called an if statement which
we have not talked about yet in class. You are encouraged to read ahead in the notes for information on
if statements, but the idea is outlined below. You want to execute a certain block of code in some cases
and other code in other cases. In it’s simplest form, you can write the following:
if (expression1 > expression2)
{
//the code in between the { and subsequent
// } will only happen if expression1 is
//larger than expression2
}
expression1 and expression2 can be any numerical expression. That is to say, they could be numerical
literals, variables, calls to methods that return values, or other expressions combined using operators
such as the plus, minus, or times operators. You are encouraged although not required to read ahead
and to use more advanced syntax such as using an else clause as well, but this is not required.
A sample run of your program should look as follows:
~dans computer$ java RobotRadioChecker
Enter a speed for Jack to move north
100.0 <---user entered
Enter a speed for Jill to move east
200 <--- user entered
Over what distance can the radios communicate?
1000 <-- user entered
How much later did Jill start than Jack?
1 <----user entered
What time would you like to see if the robots are in touch at.

Page 5
Only enter positive numbers greater than n
2 <--- user entered
The two robots will be out of radio contact at time 5.2542114902640185
The robots will be able to communicate at the time you entered
Your goal is to make your program as close to the above as possible. In addition, you must run your
code with the test program we have provided. This is to make sure your program is compatible with the
method the TAs will use to grade your assignment. You must verify this output by doing the following:
1. Find the file Question2TestFile.txt from the downloaded zip and move it to the same folder as the
RobotRadioChecker.class file. (This will normally be the same folder as the RobotRadioChecker.java
file.)
2. From the command prompt, navigate to the directory of your Java program and type
java RobotRadioChecker < Question2TestFile.txt
The output should be identical to what is shown above. (with the exception of the number of decimal
points or a rounding error) Depending on your operating system the test file may not “echo” the input.
For example the output when running on the test file may not include the name numbers 5 or 3.This is
fine as well. Your program may look like the following instead:
dans computer:~$ java RobotRadioChecker < Question2Testfile.txt
Enter a speed for Jack to move north
Enter a speed for Jill to move east
Over what distance can the radios communicate?
How much later did Jill start than Jack?
What time would you like to see if the robots are in touch at.
Only enter positive numbers greater than n
The two robots will be out of radio contact at time 5.2542114902640185
The robots will be able to communicate at the time you entered.
Hint: If your program works when you run it normally but has an error when you run the program
with the test file, it may be because you created two Scanners as opposed to reusing the same one. In
order to run properly with the test file, you will have to make sure that you only create ONE scanner
throughout your program. You can do something like the following:
Scanner reader = new Scanner(System.in);
double value1 = reader.nextDouble();
double value2 = reader.nextDouble();
(value1 and value2 should be replaced by better variable names depending on the context).
It is important to keep in mind that just because your output matches on one particular run of the
program DOES NOT mean that your program works perfectly. You should still check your program for
other cases to make sure it has the correct output.

Page 6
Question 3: Types and Expressions (30 points)
In the following, you will run a Java program to answer several questions about types. Some of these
may require you to do some investigation on your own. Of course, you are welcome to run the code in a
Java program that you write to confirm the answers but you need to be careful as each expression has
only 1 type, but it may be possible to store it into more than one variable.
Make sure that the file typequestions.txt and TypeQuestions.java are in the same folder. Compile
the program TypeQuestions.java as you normally would. It should produce a file TypeQuestions.class.
Once again, verify that typeexpressions.txt is in the same folder as this.
Next, run the class TypeQuestions by typing java TypeQuestions. This will open up a form once
again (see figure). After the header where you can fill in your name and student ID, there are 4 columns
in the window:

Figure 2: The program where you can enter your answers for question 3.

1. The question number


2. A piece of Java code or an expressions
3. A blank value where you will write the type of the expression
4. A blank value where you will write the value of the expression
In each case, the final line of the Java code will “declare” a variable x, but it’s type will be “invisible”.
Your task is to fill in both the variable’s type and value in the two columns.
For example, if the code shows as:
___ x = 3;
you should write under “Type” int and under “Value” 3. For full marks on the question, you should
write the closest type to the expression. For example, although the literal 3 can be converted to a
double implicitly, you should write int since the literal 3 is an int. On the other hand the literal 3.0
should be considered a double since although it could be explicitly cast to an int by writing (int) 3.0
, the literal 3.0 is itself a double.
After you finish, hit the submit button. The program will verify that you have entered valid types in
all of the type fields (int, double, short, long, boolean, char, byte, float, or String) and that you

Page 7
have not left any values blank. It will not check for correctness, only that your answers are all filled in.
If there is an error, it will be displayed with a short description of the problem. If you want to fix it,
you may. If not, you may proceed.
At this point, a file will be created in your folder called TypeExpressionResults.txt. It will contain
your name, student id, as well as your answers. It is your responsibility to verify that this file looks like
it contains all your answers. Before exiting the program, you should verify this file exists and
that you can find it in order to avoid potentially losing your work. Then submit this file along
with the rest of the assignment.
Some of these may require a little research into the Math and String libraries. For a list of the method
inputs and return values, you should consult http://docs.oracle.com/javase/6/docs/api/java/
lang/Math.html and http://docs.oracle.com/javase/6/docs/api/java/lang/String.html There
you can find a description of the methods which includes what they take as input as well as the type of
the expression. You may also find it helpful to write a full Java program to evaluate these expressions. In
this case, you should be careful as, for example double x = 3; is perfectly valid Java code, but double
would not be the correct answer to the above example.
If, after trying to enter this on the computer, you have not been able to generate a file using the computer
program, you may, as a last resort, create the file manually. If you do this, you should put one question
per line and the format of your line should be the type followed by the value. You also must include
your Name and StudentId at the top of this file and should mention the problems you encountered in
the Confessions.txt file (see description below).

What To Submit

You should submit your assignment on WebCT. In order to do this, you will need to make a zip of the
file. You can do this on windows by following the instructions at this link: http://condor.depaul.edu/
slytinen/instructions/zip.html. On a mac or linux, you can find instructions at http://osxdaily.
com/2012/01/10/how-to-zip-files-in-mac-os-x/
Your zip should be called Assignment1.zip and should contain the following
RigorousThinking.txt
RobotRadioChecker.java
TypeExpressionResults.txt
Confession.txt (optional) In this file, you can tell the TA about any issues you ran into doing
this assignment. If you point out an error that you know occurs in your problem, it may lead
the TA to give you more partial credit. On the other hand, it also may lead the TA to notice
something that otherwise he or she would not.

Answer to “food for thought question”

The root we are ignoring is actually the root that would occur at a negative time in our model. If you start
from the point where the robots are D distance away and “rewind” their paths, so that the robots move
in reverse, eventually they will intersect again at the initial starting point. If you continue to rewind this,
they will eventually reach D away again in the other direction (Jack will be south of where he started and
Jill will be west of where she started.) The second root is essentially when they would reach a maximum
distance of D if the direction of time were flipped. (It is slightly different because Jack and Jill don’t start
at exactly the same time.)

Page 8

You might also like