Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

Lab 2: Console I/O and Basic Arithmetic
Due: 11:59 PM, Monday, September 17, 2012

In this lab, you will practice console output and see how the computer performs arithmetic operations. You will start with the basic "skeleton" of a Java program and add statements to it as you answer each question.

Your answers to the numbered questions will be graded. You may use your notes, lecture notes, textbook, etc. to answer the question. If a question says you may do it later without the computer, you can save the question to do after you have completed writing the Java program. Place your answers to these questions in a comment at the bottom of the program you will be developing for this lab assignment.

You may work alone or with a partner on this lab.

Getting Set Up

Lab Procedure

Add a comment inside the main method that says "Two statements to print first and last names on separate lines." Follow it with 2 println statements, which will print your first and last name to the console on separate lines. For example, the output of my program would be:

Jim Teresco

Below your previous statements, add a comment that says "Letters of my first name on separate lines." Then write 1 println statement (using the new-line character, \n) to print your first name vertically on the console. For example, my output would be:

J
i
m

Below your previous statements, add a comment that says "Previous println without backslashes." Copy and paste your previous println statement and then remove the backslashes only.

Question 1: What output do you get when then backslashes are removed? (1 point)

We will now experiment with some Java arithmetic.

Consider each of the following Java arithmetic expressions:

	5 + 9
	
	2 / 3
	
	8 - 2 * 9
	
	(7 - 2) * 8
	
	4 + 5 * 17 % 2 
	
	3 * 3 * 3
	
	15.0 / 2
	
	(26 - 7 + 3) % (4 - 1)

Question 2: What do you think the answer will be when each of the above is evaluated? (2 points)

After you have answered all of them, add a comment below your previous program statements that says "Trying some Java arithmetic." Then write 1 println statement for each of the above problems to determine what answer the computer calculates for this arithmetic expression.

For example, if you wanted to check 9.0 * 3, you would write this println statement:

  System.out.println("9.0 * 3 = " + (9.0 * 3));

Important note: the parentheses around the arithmetic expression are very important to ensure that Java does not try to append the first part of your arithmetic expression to the String at the start of the println before "doing the math" to compute the desired value.

Question 3: When you run your program with all of the answers printed, did any differ from your guesses from the previous question? If so, why? (2 points)

We will next add some input to your program and do a bit of math.

Submitting Your Work

Before 11:59 PM, Monday, September 17, 2012, submit your Java program for grading. Please upload your Java source file (the one with the .java file extension, and only that file), including the comment at the end that includes your responses to the lab questions, to Blackboard using the submission procedure in the "Labs" section. Remember that your answers to the lab questions should be contained in a comment at the end of your Java program.

Grading

This assignment is worth 25 points, which are distributed as follows:

> FeatureValueScore
println practice 3
Arithmetic expression evaluations in Java 4
Reading input, calculate average, print result 8
Comments 2
Naming conventions 2
Formatting 1
Lab questions 5
Total 25