In this lab, we will practice decisions and loops in Java. The tutorials in the lab will be presented by the TA step-by-step. However, you are free to work at your own pace on the lab. The lab assignment questions will not be presented by the TA.
This tutorial will demonstrate how to make decisions using the if/else statement and loops using a for loop.
We will make a Java program that will draw a box like this:
**** * * * * ****
Create a new project called Lab6 and a new class inside the project called Tutorial1. Import the Scanner class and create a Scanner object as shown below.
Create a statement to ask the user for an integer to represent the length (size) of the box to draw. Then we will draw a solid box of that many stars. To do so, we need two for loops (one nested in another). The first loop (with i as variable counter) draws a row every loop iteration. The second loop (with j as variable counter) draws a cell (column in a row) every loop iteration.
One way to approach drawing a hollow box is to use the same loop structure that we used to draw a solid box, but decide for each cell if it should be a star or a space. Let's think about the first row. It should all be stars. Since the first row has i=0 we should always draw a star if i==0. Similarly, the last row always needs stars (when i == size-1). Before looking at the code below, can you think about what condition we would test to make sure there are stars in the first and last columns? Once we have all these tests if any one is true (using OR - || ) we draw a star, otherwise we draw a space. The final code is Tutorial1.java.
In this tutorial, we will learn the switch statement and how to test our programs with JUnit.
The switch statement has the general form:
int num; switch (num) { case 1: // Do something break; case 2: // Do something break; default: // Do something break; }
The switch statement makes it simpler to chose between multiple choices based on a number value. The alternative would be to use a lot of nested if/else statements.
Our program in this tutorial will prompt for a date in the format dd/MM/yyyy and print the date in the form Month day, Year. For example, 17/05/2001 should print May 17, 2001. The program will verify that the day is between 1 and 31 (do not worry about months with only 28, 29, or 30 days), the month is between 1 and 12, and the year is between 1000 and 3000. We will generate errors messages if the input is invalid.
Put the starter program, Tutorial2.java, in your Lab6 project.
The next step is to complete the switch statement to contain all months. The switch statement starts building our date string by putting the month first.
It is very important to check (validate) data that your program reads. In this case, we will use two if statements. The first will check if the day is between 1 and 31. The second will check if the year is between 1000 and 3000. If either the day or year is invalid, we will return "INVALID".
Try running your code and entering some dates to see if it is correct. It is very common to need to check if your code is correct. It is quite tedious to have to test code by hand. Test suites, such as JUnit, automate the testing process. You write code that does the testing for you. For this class, you are not responsible for writing the tests, but for many assignments we will provide a test file that you can use to test your code.
To run the JUnit test file for the tutorial, open Test_Tutorial2.java. When you click the run button, it will run it as a JUnit test. After test is done, it will show JUnit view and show you which of the tests passed.
The final program is Tutorial2Final.java. You will have to rename it to Tutorial2.java to make it work.
Draw a "X" of stars. You must prompt for the width of the X in stars.
Most Canadian citizens have a Social Insurance Number (SIN). The "format" of a SIN is as follows:
Every SIN consists of nine digits
First Digit - a code indicating the region of origin
1 - Issued in Atlantic Provinces
2 or 3 - Issued in Quebec
4 or 5 - Issued in Ontario
6 - Issued in the Prairie Provinces
7 - Issued in the Pacific
Region
Last Digit - a check digit
Based on other 8 digits
to indicate whether the other 8 digits are (probably) correct. SINs are
long and often recorded incorrectly. Check digits can
spot incorrect SIN quickly
(tax programs for example, will not let you enter an invalid SIN). This
check digit also (in a limited manner) makes forging
SINs more difficult. This
check digit is described below.
Consider for example the SIN 430 837 013
consider the first 8 digits
multiply every second digit by 2:
2 x 3 = 6
2 x 8 = 16
2 x 7 = 14
2 x 1 = 2
add each digit of the above together:
6 + 1 + 6 + 1 + 4 + 2 =
20
go back to original number and add remaining alternate
digits together:
4 + 0 + 3 + 0 = 7
add these previous two totals together:
20 + 7 = 27
subtract this total from the nearest (highest) multiple
of 10, which in this case is 30:
30 - 27 = 3
Therefore 3 should be the final, so-called check digit, of this number. Hence it is a (potentially) valid S.I.N.
Write a complete Java program that will determine if a given SIN is valid or not. A starter program is here. Bonus: Three bonus marks if you can create a method that given a starting SIN (valid or not) and a count of how many to generate, will return the SIN that is in that position. For example, starting at SIN 633 333 333 the 500,000th SIN from that point is 638 333 336. Return -1 if the number given is above the SIN range. JUnit test files for the basic and bonus versions are available.
Create a Java program that:
For this lab assignment, each group should submit using Connect the Java files for the questions.