Lab 1: Using BlueJ at UofW

advertisement
ACS-1903
Lab 9
switch ? output
This lab deals with
 the conditional operator
 the switch statement
 writing files
Student Identification
Student number
____________________________________________
Name
____________________________________________
1. Wikipedia describes the first digit of the SIN as:
The first digit of a SIN indicates the province in which it was registered:
1: Atlantic Provinces: Nova Scotia, New Brunswick, Prince Edward
Island, and Newfoundland and Labrador
2: Quebec
3: Quebec
4: Ontario (including overseas forces)
5: Ontario
6: Prairie Provinces (Manitoba, Saskatchewan, and Alberta), Northwest
Territories, and Nunavut
7: Pacific Region (British Columbia and Yukon)
8: Not used
9: Temporary resident
0: Not used (Canada Revenue may assign fictitious SIN numbers
beginning with zero to taxpayers who do not have SINs)
Write a program that reads SINs from the keyboard (until the user enters a zero)
and displays the province the SIN was registered in. Determine the province in the
following way:
a) Read the SIN as a character string
b) Use Character.charAt(0) to get the first character.
c) Use a java switch statement to display the province. An example of a switch from
the text can be found in PetFood.java :
Fall 2009
1/3
ACS-1903
Lab 9
public static void main(String[] args)
{
String input;
// To hold the user's input
char foodGrade;
// Grade of pet food
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Prompt the user for a grade of pet food.
System.out.println("Our pet food is available in " +
"three grades:");
System.out.print("A, B, and C. Which do you want " +
"pricing for? ");
input = keyboard.nextLine();
foodGrade = input.charAt(0);
// Display pricing for the selected grade.
switch(foodGrade)
{
case 'a':
case 'A':
System.out.println("30 cents per lb.");
break;
case 'b':
case 'B':
System.out.println("20 cents per lb.");
break;
case 'c':
case 'C':
System.out.println("15 cents per lb.");
break;
default:
System.out.println("Invalid choice.");
}
Fall 2009
2/3
ACS-1903
Lab 9
2. The conditional operator ? can be used to express an if-else. For example:
z = X < 0 ? 10 : 20;
System.out.println( X < 0 ? 10 : 20);
The text gives an example
X < 0 ? y = 10 : z = 20;
But this does not work – its not proper java.
Wikipedia specifies that 7 indicates British Columbia and Yukon:
7: Pacific Region (British Columbia and Yukon)
Let’s make an assumption that the second digit differentiates these two. Let’s
assume that if the second digit is a ‘1’ then the region/province is Yukon and
British Columbia otherwise. Use a conditional operator to set this value for
province in the case ‘7’ clause.
3. Now modify your program above to write the SINs and provinces out to a file as
well as displaying them. You can view slides at
http://ion.uwinnipeg.ca/~rmcfadye/1903/writing to a file nov 2009.ppt. An
example of how to create a file :
public static void main(String[] args) throws IOException
{
FileWriter fw = new FileWriter("SinAndProvince.txt");
PrintWriter of = new PrintWriter(fw);
of.println( "287654321"+" "+"Quebec" );
of.close();
}
Note that if a file is created within your BlueJ project, then you can navigate to
that directory and list the file later to ensure your program created the file.
Demonstrate your work to the lab demonstrator
Upload your program(s) to Assignment 9 in PASS. Note that
there is no assignment 9 … we have run out of time.
Fall 2009
3/3
Download