COMP 110-001 - Introduction to Programming (Spring 2009)

Lab 2

25 points

Assigned: January 30rd

Due: February 5th by midnight

Description

Part 1

On the course webpage, you will find TypeCasting.java. Copy and paste the program into a new Java file in jGrasp, and edit the Program Header to reflect your name and assignment information. The program as given has a type error and will not compile. Use type casting to fix the error. You will not receive credit for changing the types of the declared variables, you must use type casting. Once you have fixed the error, run the program and observe that it produces an output of "0.0%" when the user enters 3 for the days per week. Use type casting to fix the code so that the program produces the correct output when the user enters a number of days from 1-7.

Part 2

On the course webpage, you will find StringFun.java. Copy and paste the program into a new Java file in jGrasp, and edit the Program Header to reflect your name and assignment information. This program asks the user to input a line of text such as,

Hi there, how are you?

which is stored in the program as a String variable called "inputString". You must modify the program to output the following things using the String input by the user. Add your code for each item where you see the comments "//your code here". Consult the textbook page 78 for a listing of the methods of the class String you will need.

Part 3

On the course webpage, you will find YearInSchool.java. Copy and paste the program into a new Java file in jGrasp, and edit the Program Header to reflect your name and assignment information. The program as given asks the user for his/her year in school and reads the year in from the keyboard. You must modify the program to output whether the user is a "Freshman", "Sophomore", "Junior", or "Senior", based on the year that the user enters using If-statements. For example, if the user enters 1, your program should output, "You are a Freshman". If an invalid year is entered, your program should output an error message. The table below gives the output your program should produce for all possible inputs.

Input

Output

≤ 0
That is not a valid year
1
You are a Freshman
2
You are a Sophomore
3
You are a Junior
≥ 4
You are a Senior

Submission

For this assignment you will need to create a Jar file that contains the source code for TypeCasting.java, StringFun.java AND YearInSchool.java that you edited as part of this assignment. Follow these instructions for creating a Jar file.
The Jar file you submit must be named yourlastname_lab2.jar, where yourlastname is your last name

Submit yourlastname_lab2.jar via the UNC Blackboard System under "Assignments → Lab 2" before the deadline.

Grading