COMP 110-001: Introduction to Programming, SSI'15
Lab 2
25 points
Assigned: Wednesday, May 20, 2015
Due: Friday, May 22, 2015 by 11:59pm (EDT)
Description
Lab 2 will introduce you to string manipulation and type casting. You have been given a skeleton program StringFun.java that can be found on the course webpage. Copy the program into Eclipse (You will need to create a new Java Project called Lab2 and a new Java Class called StringFun), and compile and run the program. (Don't forget to add the header!)
Part 1
Characters are stored as numbers in a computer. In this part, we convert the input integer to the unique Unicode character stored as that number. Appendix 7 has a subset of the Unicode character set. Note: the input integer is converted to a character via type casting.
(char)inputInt
- In main() function, comment out all codes before the second JOptionPane.showMessageDialog line and uncomment the second JOptionPane.showMessageDialog line. Recompile and run the program.
- The display box should now say Rocks!
- Add your favorite band's name to the display box by type casting Unicode characters to make it show myfavoriteband Rocks! In your code, start adding the name of your band after the "". Use Appendix 7 for a reference. (choose a band that has more that 5 characters in the name.)
Type casting is more commonly done between integers and floating-point numbers when performing arithmetic operations between the two types.
Following is a subset of the Unicode character set known as the ASCII character set.
(Character number 32 is the blank). You can also find it in Appendix 7.
Part 2
For this part of the lab you will modify the code StringFun.java to prompt the user for a string (of characters) and output information about the string as well as manipulate the input string.You will be referencing pages 83-89 of the textbook.
- Declare a variable of Type String to be your input string with identifier inputString.
- Prompt the user to input a string.
inputString = JOptionPane.showInputDialog("Please Enter a string:");
This line of code will prompt the user and store the input string in inputString
- Manipulate the string stored in inputString - reference pages 83-89. For each step, print out a new dialog box using
JOptionPane.showMessageDialog(null, your output goes here);
- Output the length of inputString. E.g., "The length of the input string is 6"
- Print out the characters at position 5. Check the length of inputString first, if the assessed position exceeds the length, then output "No character at position 5"; otherwise, output the character at that position
- Display the substring starting at position 0 and ending at position 5 (i.e., including the character at position 5). Also, check the length of inputString first, if the length is less than 6, output the whole string
- Convert inputString to all uppercase letters
- Display the inputString without the first word. (Hint: find the index of the first space in the string, check Java API and find the method indexOf. Then display the substring starting at the next index after the first space. Think about how to deal with the case when there is only one word)
Additional Questions (in project Lab2 in Eclipse, create a new File called yourlastname_lab2.txt and answer questions in the file)
-
What was the most helpful part of this lab? least helpful?
- Has any part of previous lectures been confusing that you would like to be covered again? Which part?
- Modular Arithmetic (Solve these problems, Remember you are calculating the remainder)
Grading
- 5 points Part 1
- 17 points Part 2
- 2 points Declaring variables and display prompt
- 15 points String Manipulation
- 3 points Additional Questions
How to hand in the assignment
- A file named yourlastname_lab2.txt, where yourlastname
is your last name, that provides answers to the Additional Questions.
-
A file named yourlastname_lab2.jar, where yourlastname is your last name.
Follow these instructions to create yourlastname_lab2.jar.
The .jar file should include StringFun.java, StringFun.class, MANIFEST.MF (under META-INF folder), and yourlastname_lab2.txt.
Your java file should have the appropriate header.
- Submit your jar file on Sakai by 11:59pm, Friday May 22.
-
If you do not follow these instructions you will not get credit for the lab. Please let
me know if you are having any problems with the lab and/or the assignment submission process.