Java Programming

 

      

 

Tuesday, March 17th

 

Introduction to Arrays - Stop! If you did not complete yesterdays assignment go back to Monday and complete that first before starting on Arrays.

Why use Arrays? - in your 5.2 Project you keep adding new products and each time you need to create a new variable to hold that product. If you have 100 products you will need create 100 variables. Arrays solve this problem, you create ONE array to hold many values of the same type. Anytime you see [ ] you will know you are dealing with an array. So if you create a Product[ ] myProducts array you can store all of your products in this one variable myProducts.

W3Schools Intro to Arrays - Array structure - values and indices, declaring and initializing arrays
Another Array tutorial - Arrays

Array Practice - Create a new project called 6-1 Arrays in Codiva. Now create a new java class called ArrayPractice and copy and paste this code there.
Read through the comments in the Array Practice file and look at the results of the code. Use this sample code as you begin work in section 6.

Declaring an Array Variable:
double[] myList; // preferred way - type [ ] arrayName;
double myList2[]; // works but not preferred way.
int[] myList3; // an array of integers

Creating an Array:
myList3 = new int[5]; // actually creates an array of 5 integers - sets all values to 0

If you know the values that should go into an array you can initialize the array when you create it:
int[] myList5 = {1, 2, 3, 4, 5, 6};

Assigning Values after the array has already been declared:
String[] teamMembers2 = new String[5]; // - declare and initialize the array with null values.
// Now assign values to the array:
teamMembers2[0] = "Stein";
teamMembers2[1] = "Henderson";
teamMembers2[2] = "Canning";
teamMembers2[3] = "Newman";
teamMembers2[4] = "Dodge";

System.out.println("The first team member is " + teamMembers2[0]);

System.out.println("The last team member is " + teamMembers2[4]);

To iterate through an entire array use a loop - what type of loop structure should you use?
What condition should you use?
What happens if you get the condition incorrect? This is a common error when using arrays.

What to work on Today

Finish up Section 5-2 Project - after you complete this project copy the code for both classes into the same Google document and then paste a screenshot of the output and submit to Canvas.

Due Today! - Complete Section 6 Lesson 1: Arrays Vocabulary - copy and past to Canvas
- The act of progressing through an array
- A structure that stores multiple values of the same data type
- A two-dimensional array.
- An integer that identifies the location of a value in an array
- The ability to pass data into the main function and access it as an element of an array.
- A logical computational procedure that if correctly applied ensures the solution of a problem
- An array of arrays, similar to a table, matrix, or spreadsheet.
- A for loop inside of a for loop
- A named object used to store more than one value.

Begin Section 6 Lesson 1: Arrays slides
Complete Section 6 Lesson 1: Arrays questions 1-6.
Complete Section 6 Lesson 1: Arrays programs - ExamAverage and Matrix

Hints for the ExamAverage program.
In a certain class, there are 5 tests worth 100 points each. Write a program that will take in the 5 tests scores for the user, store the tests scores in an array, and then calculate the students average.

Step 1 - Create an int array (refer to ArrayPractice) that will hold 5 values.
Step 2 - Create a for loop that asks the user to enter five exam scores, one at a time.
Each time the user enters a score assign it to the array variable you created in step 1.
So for the first time through the loop it would look something like this myScores[i] = input.nextInt(); where i is the counter in the loop.
Step 3 - Iterate through the array to pull out each value one at a time and add it to a variable called sum.
You need to add the new value to sum so it will look something like this sum = sum + next array value.
Step 4 - Divide the sum by the number of values to get the average and output the results.
To test your program enter the following scores when the program prompts you:
95, 85, 75, 65, 100 - the average should be 84.

 

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.