Java Programming

 

     

 

Thursday, March 26th

 


Matrix 3 - Fill Array

You will now use your printArray(int[][] array) method to create a fillArray method! You can now make the first two choices of your menu work. Notice that for the fillArray(Scanner input, int[][] array) you are providing two parameters, a Scanner object which you declared earlier with your other variables and an array object. For menu item one you want to fill array1. For menu item two you want to fill array2. So if I were calling this method in the main method for item one it would look like this: fillArray(input, array1).

What do you need to change in your printArray code? Instead of printing a value you will be asking the user for a value and then taking that value and placing it into the array. Remember to assign a value to an array you indicate both the row and the column index values so it looks like this array1[row][column] = input.nextInt();
The row variable comes from the outer for loop and the column variable comes from the inner for loop. Each of these variables start at 0 and increment as you go through the loops so the very first array value that you apply is place in row 0 and column 0.

Right after you call the fillArray method you will want to call your printArray method in order to print out the array. See the sample output below.

You should be half way through the Matrix program at this point!

Run your code in Codiva. Copy the code and then take a screen shot of the output. Submit to Canvas for Matrix3 Fill Array. You should be filling array1 and array2. Use the values shown below to fill array 1 and array 2. Here is what your output should look like:

1 
Please enter a value for the array
3  
Please enter a value for the array
4  
Please enter a value for the array
5  
Please enter a value for the array
6  
Here is your array:
3 4
5 6 For array2 use the following values, 1, 0, -2, 3. Your output should look like this:
1 0
-2 3

Write a program that take in two matrices and then allow the user to choose to add, subtract, or multiply them and display the answer. The program will display the following menu:
1. Enter Matrix A - display the Matrix so the output look like the actual matrix, not just a list of numbers.
2. Enter Matrix B - display the Matrix so the output look like the actual matrix, not just a list of numbers.
3. Display A + B - display the output so it is in the form of the actual matrix answer, not just a list of numbers.
4. Display A - B
5. Display A * B
6. Exit

The program should loop and allow the user to continue to choose different options until they choose quit. The well written program will modularize the process into different methods.
Suggested methods:
fillArray(Scanner input, int[][] array)
printArray(int [][] array)
addArray(int[][]array1, int[][]array2, int[][] answerArray)
subtractArray(int[][]array1, int[][]array2, int[][] answerArray)
multiplyArray(int[][]array1, int[][]array2, int[][] answerArray)

If you have finished the Matrix problem here is what you should work on next -

How to Crash Proof Your Code - Exception Handling Demonstration

Three Types of Errors

Complete Section 6 Lesson 3: Handling Errors slides
Complete Section 6 Lesson 3: Handling Errors quiz and show your results for teacher check
Complete work on the Section 6 Lesson 3: Handling Errors practice on school loop.

Section 6.3 Program Problem:
Create new class called Crash and create an array of five names and then prompt the user for a number of the name they want to see. Loop the program until the user enters 99 to exit the program. Once the program is working crash it by entering a number that is not in the array. Then crash the program again by entering a letter instead of a number. Pay attention to the errors that are produced.

Create a new class called CrashProof and copy the code from the Crash class into this new class. Now add a try/catch block to handle the two errors that were used to crash the previous program. You will need two catch blocks since there are two errors. You will also need to import the appropriate error classes. Each error should provide an appropriate message to the user. Include in the fix for the letter a .nextLine(); statement to clear the input buffer of the letter the user entered other wise you will end up in an infinite loop.

 

Begin Section 7 Lesson 1: Passing Objects and Overloading Methods slides
Complete7 Lesson 1: Passing Objects and Overloading Methods quiz and show your results for teacher check
Complete work on the7 Lesson 1: Passing Objects and Overloading Methods practice on school loop.


 

Homework

Install Eclipse on your home computer if you would like to be able to work on your java at home.

 

What

In this lesson, you will learn how to:
•Recognize the correct general form of a class
• Create an object of a class
• Describe object references
• Create methods that compile with no errors
• Return a value from a method
• Use parameters in a method

 

Why

Class templates are the foundation of the Java language. Understanding how to create a class with various methods will be essential to becoming a good Java programmer. Knowing how to instantiate an object and compare it to other objects is critical to learning to use and work with Java data types.

 

How

By completing the slides, quiz and practice exercises.