Download CmSc150 Fundamentals of Computing I

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Name:
CmSc150 Fundamentals of Computing I
Homework 05 due 10/01 by 5 pm
1. Programming exercise
Write and application that
a. converts hours, minutes and seconds to seconds
b. converts seconds to hours, minutes and seconds
The dialog with your program should look like this:
This program converts hours, minutes and seconds to seconds and
seconds to hours, minutes and seconds
A. Conversion to seconds
How many hours? 4
How many minutes? 0
How many seconds? 3
There are 14403 seconds in 4 hour(s), 0 minute(s), and 3 second(s)
B. Conversion from seconds
How many total seconds? 2309
There are 0 hour(s), 38 minute(s), and 29 second(s) in 2309 seconds
Declare the amount of seconds in one minute and the amount of minutes in one hour as
constants (use final modifier)
Using appropriate comments in the source code, indicate which part of the program prints
the welcome message, which part converts to seconds, and which part converts from
seconds.
Test your program on various examples. Perform tests with zero values for each input item.
Do not forget to write title comments (what the class does, your name and version or date).
1
2. Questions and answers
True/False (Write T or F immediately after the number of the question)
1. In Java, the modulus operator (%) requires integer operands.
2. The statement
myVar++;
causes 1 to be subtracted from myVar.
3. In a Java expression, all additions are performed before any subtractions.
4. Execution of the statement
someInt = 3 * (int)someFloat;
does not change the contents of the variable someFloat in memory.
5. In a Java expression without parentheses, all operations are performed in order from left to right.
6. If someFloat is a variable of type float, the statement
someFloat = 395;
causes someFloat to contain an integer rather than floating-point value.
7. The value of the expression a+b * c+d is different from the value of the expression
a + b*c + d.
8. If the int variable someInt contains the value 26, the statement
System.out.println( "someInt");
outputs the value 26.
Fill-In
1. In a Java method, the statements enclosed by a { } pair are known as the
____________________ of the method.
2. A programming language is said to be ____________________ if it considers uppercase
letters to be different from lowercase letters.
3. A(n) ____________________ is a location in memory, referenced by an identifier, in
which a data value that can be changed is stored.
2
4. The set of rules that determines the meaning of instructions written in a programming
language is called ____________________.
5. A(n) ____________________ is a statement that stores the value of an expression into a
variable.
6. A(n) ____________________ is a location in memory, referenced by an identifier, where a
data value that cannot be changed is stored.
7. ____________________ is the formal rules governing how valid instructions are written in
a programming language.
8. The expression (int)someFloat is an example of a(n) ____________________ operation.
9. The Java ____________________ is a collection of prewritten classes that are available for
use by any Java programmer.
Multiple Choice (use boldface or underline to answer)
1. Which of the following statements about the Java main method are false?
a. Every program must have a method named main.
b. Program execution begins with the first executable statement in the main
method.
c. The main method must call (invoke) at least one other method.
2. Which of the following can be assigned to a char variable?
a.
b.
c.
d.
't'
'2'
'$'
All of the above
3. Which assignment statement could be used to store the letter A into the char variable someChar?
a.
b.
c.
d.
e.
someChar = "A";
someChar = A;
someChar = 'A';
a and b above
a, b, and c above
4. Among the Java operators +, -, *, /, and %, which ones have the lowest precedence?
a.
b.
c.
d.
e.
+ and * and /
*, /, and %
+, -, and %
+, -, and *
3
5. The value of the Java expression 3 / 4 * 5 is:
a. 0.0
b. 0
c. 3.75
d. 3
e. 0.15
6. Assuming all variables are of type float, the Java expression for
(a+b)c
d+e
is:
a.
b.
c.
d.
e.
a + b * c /
(a + b) * c
(a + b) * c
(a + b * c)
(a + b) c /
d + e
/ d + e
/ (d + e)
/ d + e
(d + e)
7. Given that x is a double variable and num is an int variable containing the value 5, what will
x contain after execution of the following statement:
x = num + 2;
a.
b.
c.
d.
e.
7
7.0
5
5.0
nothing; a compile-time error occurs
8. Given that x is a double variable containing the value 84.7 and num is an int variable, what
will num contain after execution of the following statement:
num = x + 2;
a.
b.
c.
d.
e.
86.7
86
87
87.0
nothing; a compile-time error occurs
9. The value of the Java expression 11 + 22 % 4 is:
a. 13
b. 1
c. 8
d. 16
e. none of the above
4
10. If the int variables int1 and int2 contain the values 4 and 5, respectively, then the value of the
expression (double)(int1 / int2) is:
a.
b.
c.
d.
e.
0.8
0
0.0
1.0
1
11. What is the output of the following program fragment?
System.out.println( "Barrel" );
System.out.print (" ");
System.out.print ( "of");
System.out.println( "Laughs" );
a.
Barrel of
Laughs
b.
Barrel
of Laughs
c.
Barrel
of
Laughs
d.
Barrel
of Laughs
e.
Barrel
ofLaughs
12. Categorize each of the following situations as a compile-time error, run-time error, or logical
error (check the appropriate box)
Compile-time
Run-time
Logical
multiplying two numbers when you meant to add them
dividing by zero
forgetting a semicolon at the end of a programming
statement
spelling a word wrong in the output
producing inaccurate results
typing a { when you should have typed (
How to turn in the assignment:
The program: Zip the project and send it as attached file by e-mail. Follow the naming
conventions.
The questions: write your answers into this document, write your name, then save and
send as attached .doc document by e-mail
5