CS 160, Spring 2015
Programming Assignment P2
Numbers and Strings
Programming due Monday, Feb. 2 at 6:00pm; late deadline Feb. 2 at 11:59pm.
Using Numbers and Strings in Java
This programming assignment has four objectives:
- to use Java variables and operators to write expressions,
- to understand how to manipulate Java strings and characters,
- to print formatted output, and
- to see if you can follow a specification exactly!
Description
The program 1) declares a set of variables of several different data types, 2)
uses the variables to construct expressions to do some simple math, 3) manipulates
strings and characters, and 4) outputs the results.
Instructions
For this assignment, you must follow directions exactly. Create a P2 project in Eclipse
then write a class P2 with a main method, and put all of the following code into the main method:
- Declare an integer variable of type byte, short, int, and long.
- Declare a floating-point variable of type float, and double.
- Declare three variables of type char.
- Declare three variables of the class String.
- Initialize the variables to the values shown below (using an initializer or assignment statement):
- byte: 32, short: 15477, int: 664422, long: 3984759871
- float: 8.112, double: 56.4444
- char: '&', 'S', '5'
- String: "Computer", "Science", "Excellent"
- NOTE: The long integer value exceeds 32-bits, and therefore requires special syntax.
- NOTE: The float value similarly requires special syntax, otherwise it will default to type double.
- Find the syntax referenced above for long and float literals on the web and use them.
- (Line 1) Print the four integer values in the following order (byte,short,int,long) separated by colons (:).
- (Line 2) Print the two floating-point values in the following order (float, double) separated by a comma (,).
- (Line 3) Print the sum of all the integer variables divided by 99999.
- (Line 4) Use Math.sqrt() to compute and print the square root of the sum of the float and double.
- NOTE: Use System.out.printf with a specified to ensure exactly 3 digits after the decimal point.
- (Line 5) Print the quotient of the byte variable divided by the float variable.
- NOTE: Use System.out.printf with a specified to ensure exactly 5 digits after the decimal point.
- (Line 6) Print the three characters, separated by equals signs (=).
- (Line 7) Subtract 3 from all three character values and print them again, separated by tildes (~).
- (Line 8) Using the three String variables and string constants, print Computer Science is Excellent!.
- NOTE: The following items require a String method call inside a print statement.
- (Line 9) Print the length of all three String variables, separated by commas.
- (Line 10) Print the third String variable in uppercase letters.
- (Line 11) Print the third through seventh characters of the first String variable.
- (Line 12) Print the index of the character 'e' in the second String variable.
- (Line 13) Print the fourth character of the first String variable.
Sample output
Your program should match the output shown below, with no mispellings, wrong characters,
incorrect case, or extra white space! A missing line will cause many errors, so make
sure you have all of the lines below in the correct order. See the grading criteria below.
32:15477:664422:3984759871
8.112,56.4444
39854
8.035
3.94477
&=S=5
#~P~2
Computer Science is Excellent!
8,7,9
EXCELLENT
mpute
3
p
Specifications
Your program must meet the following specifications:
- Work on your own, as always.
- The name of the source code file must be exactly P2.java.
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown in P1.
- Assignments should be implemented using Eclipse and Java 1.5 or 1.6 or 1.7.
- Make sure your code runs on machines in the COMSC 120 lab.
- Submit your program to the Checkin tab as you were shown in the recitation.
- Read the syllabus for the late policy.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
- testCompile: checks that program compiles. (10 points)
- testComment: checks the comment block at top of program. (10 points)
- test1: checks the first line of output. (5 points)
- test2: checks the second line of output. (5 points)
- test3: checks the third line of output. (5 points)
- test4: checks the fourth line of output. (5 points)
- test5: checks the fifth line of output. (5 points)
- test6: checks the sixth line of output. (5 points)
- test7: checks the seventh line of output. (5 points)
- test8: checks the eighth line of output. (5 points)
- Final Tests
- test9: checks the ninth line of output. (8 points)
- test10: checks the tenth line of output. (8 points)
- test11: checks the eleventh line of output. (8 points)
- test12: checks the twelfth line of output. (8 points)
- test13: checks the thirteenth line of output. (8 points)
- Preliminary testing only checks the first through eighth lines
- Final grading checks the remaining lines, and includes the preliminary tests.
Submit your program to the Checkin tab on the course website, as you were shown in
the recitation, and read the syllabus for the late policy (if necessary).
© 2015 CS160 Colorado State University. All Rights Reserved.