Lab Assignments

### Lab 4

(1.2%) 6.12* (Reversing an array) The reverse method in ยง6.5 reverses an array by copying it to a new array. Rewrite the method without creating new arrays.

(1.0%) 6.13* (Increasing array size) Once an array is created, its size is fixed. Occasionally, you need to add more values to an array, but it is full. In such cases, you can create a new, larger array to replace the existing array. Write a method with the following header:

` public static int[] doubleCapacity(int[] list)`

(1.0%) 6.24 (Adding two matrices) Write a method to add two matrices. The header of the method is as follows:

`public static int[][] addMatrix(int[][] a, int[][] b) `

In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. As shown below, two matrices are added by adding the two elements of the arrays with the same index:

(2.5%) 6.25** (Multiplying two matrices) Write a method to multiply two matrices. The header of the method is as follows:

`public static int[][] multiplyMatrix(int[][] a, int[][] b)`

To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication, and a, b, and c are denoted as follows:

where cij = ai1 x b1j x + ai2 x b2j x + ai3 x b3j x + ai4 x b4j x + ai5 x b5j.

Total: 5.7%

December 2, 2009 - 11:40am

### Lab 3

In this assignment you are to write the following programs:

1. (1.0%) Write a program that prompts the user to enter the month and year, and displays the number of days in the month. For example, if the user entered month 2 and year 2000, the program should display that February 2000 has 29 days. Note that the user enters in the numeric representation for the month. Use the JOptionPane class for all input and output.

2. (1.0%) Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither of them, or just one of them. Here are some sample outputs for inputs 10, 30, and 23.

• 10 is divisible by 5 or 6, but not both
• 30 is divisible by both 5 and 6
• 23 is not divisible by either 5 or 6

3. (1.0%) Write a program that displays a random uppercase letter.

4. (2.0%) An ISBN (International Standard Book Number) consists of 10 digits d1d2d3d4d5d6d7d8d9d10. The last digit d10 is a checksum, which is calculated from the other nine digits using the following formula:

( d1 x 1 + d2 x 2 + d3 x 3 + d4 x 4 + d5 x 5 + d6 x 6 + d7 x 7 + d8 x 8 + d9 x 9 ) % 11

If the checksum is 10, the last digit is denoted X according to the ISBN convention. Write a program that prompts the user to enter the first 9 digits and displays the 10 digit ISBN (including leading zeros). Your program should read the input as a single number.

Total: 5%

October 24, 2009 - 11:59pm

### Lab 2

In this assignment you are to write the following programs:

1. (1.5%) Write a program that sorts three integers. The integers are entered using input dialogs and stored in variables num1, num2, and num3, respectively. The program sorts the numbers so that num1 <= num2 <= num3. All three numbers should be displayed in ascending order in a message dialog.

2. (1.0%) Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of any two edges is greater than the third edge. The console should be used for all input and output.

October 23, 2009 - 10:30am

### Lab 1

1.) Write a program that creates a variable named Celsius and assigns it the value 38.2. Your program should then calculate the corresponding temperature in Fahrenheit and assign the calculated value to a second variable named Fahrenheit. Finally, your program should output a statement to the console that reads:

`38.2 degrees Celsius is ___ Fahrenheit.`

Note that your print statement should not use any literal values. Instead, it should make use of the variables Celsius and Fahrenheit. Also, the underscores should be replaced with the actual calculated value.

2.) Programming Exercise 1.3 (Page 24)

3.) Write a program that displays the following questions and their corresponding answers:

If a NoClassDefFoundError occurs when you run a program, what is the cause of the error?

If a NoSuchMethodError occurs when you run a program, what is the cause of the error?

Why does the System class not need to be imported?

Q1: 15pts
Q2: 10pts
Q3: 15pts

Total: 40pts

October 2, 2009 - 1:00pm

### Lab 0

Throughout the quarter I may need to contact you regarding assignment updates, class meetings, and other important subject matter. To do so I will send out e-mails as well as post updates to our Class Website. To facilitate the e-mail contact you are required to submit a valid e-mail address to the Class Roster Website. This should be an e-mail address that you check regularly.

The Class Roster Website also allows you to specify whether you would like to be included in a distributed class roster. If so, you have the option of including your e-mail address and/or telephone number. Note that only the students who opt to include their information on the list will receive a copy of the course roster. Students who do not wish to include their information will not receive a copy. The copy will not be available for download via the Course Website. Instead, it will be distributed via e-mail. You are not required to participate in the class roster, but you are required to completely fill out the form. All fields are required unless otherwise specified.

If you have not already done so, you must register on the Computer Science Network Services (CSNS) Website. Every assignment, including a portion of your midterm and final will be submitted online using this system.