Java Programming

 

     

 

Friday, March 27th

 

Creating an addArray( ) method

Today use your printArray(int[][] array) method to create the add and subtract Array methods! Modify the name of the method and the method'ss parameters as shown below.
addArray(int[][]array1, int[][]array2, int[][] array3)

Since the printArray already has the required nested looping you will just need to change what happens in the inner loop. In the inner loop you will want to add the values from array1 and array2 and place them in array3. Since you will be taking the first value from both array1 and array2 and placing it in the first value of array3 all three indexes will be the same thus you can simply use the [row][column] values for all three arrays. The loop will increment through all the values in the arrays to complete this task. Use the values shown below to test your code:
Matrix Examples

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

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[][] array3)
subtractArray(int[][]array1, int[][]array2, int[][] array3)
multiplyArray(int[][]array1, int[][]array2, int[][] array3)

 

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

Have a wonderful spring break!

 

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.