Computer Science 202
Introduction to Programming
Fall 2012, The College of Saint Rose
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
//Your name //Partner's name, if applicable //CSC202 Section 02 (for the 11:15 class) or E1 (for the 4:10 class) //Lab 2 //Today's date /** * Practice with I/O and math */ public class Lab2Prog { public static void main(String[] args) { } }
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.
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)
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.
We will next add some input to your program and do a bit of math.
The average of 10, 2.5, and 17.5 is 10.0.
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:
> Feature | Value | Score |
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 | |