Week 1
1) What best defines a "programming language"?
- It allows us to control a computer.
- It allows us to make a calculation.
- It allows to execute a program.
- It allows us to express an algorithm.
2) Select the valid identifiers from the following list. State why each of the others is not valid.
- 123
- $50
- 50dollars
- abs
- hunter
- @60cents
- a_b
- #define
3) We learned about basic programs such as Hello World!
How can we modify this to print a whole message?
Let's print two lines:
- First: Hello, my name is [YOUR NAME HERE].
- Second: How are you?
4) Write a Java program that creates TWO integer variables to the values of your choice (e.g. 4 and 5, 12 and 10, etc.) and computes and prints the following:
- the sum of the two numbers
- the difference between the two numbers
- the product of the two numbers
- the quotient of the two numbers
5) Answer the following:
- What is the result of the following Java expression: 5 + 6 * 3 ?
- How does the order of operations affect the evaluation of the following expression: 4 + 5 * 2 - 3 ?
- Evaluate the following Java expression: 10 / 2 + 3 * 5 - 4
-
What is the output of the following code snippet?
int a = 2 + 3 * 4 - 1; System.out.println("Result: " + a);
- Rewrite the following expression using parentheses to explicitly define the order of operations: 8 - 2 * 4 / 2 + 3
-
What is the value of the variable 'b' after executing the following
code snippet?
int b = 10; b += 2 * 3 - 1;
6) Trace the following program:
public static void main(String [] args) {
int a, b, c, d, e, f, g;
a = 10;
b = 27;
System.out.println("a is " + a + ", b is " + b);
c = a + b;
System.out.println("a+b is " + c);
d = a - b;
System.out.println("a-b is " + d);
e = a+b*3;
System.out.println("a+b*3 is " + e);
f = b / 2;
System.out.println("b/2 is " + f);
g = b % 10;
System.out.println("b%10 is " + g);
}
7) Write a Java program to print the area and perimeter of a rectangle.
- initialize two integer variables:
- one for length
- one for width
- compute the formula for area: a = wl
- compute the formula for perimeter: p = 2(l+w)
- print and label the results
8) Write a Java program that initializes the radius of a circle and computes and prints the circle's area and circumference (perimeter). (Limit each result to a precision of 3 decimal places)
- Formula for area: πr^2
- Formula for circumference (perimeter): 2πr
Review
a) Write a Java program to convert temperature from Fahrenheit to Celsius degrees.
b) What will be printed by the following program?
public static void main(String [] args) {
int w, x;
double y;
w = -9;
x = Math.abs(w + 5);
y = Math.sqrt(x);
System.out.printf("%d %d %.2f%n", w,x,y);
}
c) Assuming that the variable x has already been declared and assigned a value, is the following assignment legal? If not why?
d) Write a Java expression that would compute the following:

1) Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable. Assume also that stdin is a variable that references a Scanner object associated with standard input.
2) Assume the availability of a class named Arithmetic that provides a static method,
add, that accepts two int arguments and returns their sum.
Two int variables, euroSales and asiaSales, have already been declared
and initialized. Another int variable, eurasiaSales, has already been declared.
Write a statement that calls add to compute the sum of euroSales and asiaSales
and that stores this value in eurasiaSales.
3) Given the variable pricePerCase, write an expression corresponding to the price of a dozen cases.
4) Write a Java expression that would compute the following:

5) Write a Java program that computes the cost of traveling between two cities.
Prompt for and read in the name of each city (two separate prompts),
the distance (in miles) between the two cities, and the cost of travel per mile.
Run the program with the following data:
- Cincinnati, Columbus, 115 and 0.50
Your output should look like:
- The cost of traveling from Cincinnati to Columbus is $57.50
Lab 1: One Guess Game
The goal of this exercise is to program a Guess My Number game. When it's finished, it should work like this:

- First, use Math.random() to generate a number between 1-100 (inclusive)
- Prompt the user to enter their guess and use a Scanner to read in user input
- Display the user input and the random number
- Finally, compute and display the difference between the user's guess and the number that was generated (hint: use absolute value!)
- Write all output (not prompts!) to a plain text file called: "oneGuessGame_Results.txt"
Submit both the .java and .txt file to EC Lab Submissions under Class Labs on Blackboard before next class. (This lab will count as part of your CodeLab grade)
6) Three business partners are forming a company whose name will be of the form "Name1, Name2, and Name3".
However, they can't agree whose name should be first, second or last.
Help them out by writing code that
reads in their
three names and prints each possible combination exactly once, on a line by itself (that is, each possible
combination is
terminated with a newline character).
Assume that name1, name2, and name3 have already been declared
and use them in your code.
Assume also that stdin is a variable that references a Scanner object associated with standard input.
For example, if your code read
in Larry, Curly, and Moe it would print out "Larry, Curly, and Moe", "Curly, Larry, and Moe", etc., each on a
separate line.
(CodeLab: Chapter 3: Input and output > 3.2: The Scanner class > #20966)
7) Write a Java program that converts a total number of seconds to hours, minutes, and seconds.
It should:
- prompt the user for input
- read an integer from the keyboard
- calculate the result
- and use printf to display the output
For example:
"5000 seconds = 1 hours, 23 minutes,
and 20 seconds".
(Hint: Use the modulus operator)
8) Write a Java program to prompt the user to enter their first and last name, then generate a random integer
between 1000 - 9999 (inclusive)
and write it to a file as follows:
'[last name]_[first_name]_Info.txt'
Then read from the newly created file and print out the contents (it should be the same as you just entered,
plus the ID number)