APCS Summer Assignment Name_______________________
Mrs. Wright
Computer Lab 128
The purpose of the summer assignment is to make sure you have the Java Environment on your computer so you can work at home and at school year.
Install Java and jGrasp on your computer use the Java Installation instructions.
Discussion
Arrays
Arrays allow programmers to store multiple values under a common name. Of course we can always just create different variables using different names.
double number1;
double number2;
:
double
number50;
But this is terribly inconvenient. Fifty commands are required to set each of our variables to zero.
number1 = 0.0;
number2 = 0.0;
:
number50
= 0.0;
A situation such as this begs for an array. Our fifty declarations are replaced with one declaration and our fifty initialization commands are replaced with one for-loop containing only one command.
double[] array = new double[50];
for(int k = 0; k < 50; k++)
array[k] = 0.0;
Three lines, that’s it, instead of a hundred. The beauty of an array is that it distinguishes different variables by an integer value only. Since an integer value can be controlled by a for-loop, large tasks require little code. Box diagrams for the two options shown above should clarify the distinction.
|
Option Two |
||||||
|
|
|
|
[0] |
[1] |
|
[49] |
|
array |
|
|
0.0 |
0.0 |
… |
0.0 |
Option One
|
number1 |
0.0 |
|
number2 |
0.0 |
:
|
number50 |
0.0 |
An array is made up of a certain number of cells. In our example there are fifty cells. These cells exist together as one logical unit where the fifty variables in option one exist as fifty separate entities. In Java, arrays are objects requiring the keyword new and accessed through references (arrow). The integer corresponding to each cell is that cell’s index. Indices are always integers for all arrays.
Be careful! Arrays are zero-indexed so the first cell is cell number zero and the fiftieth cell is cell number forty-nine. You cannot change this fact so you must either adjust your thinking or accept frustration.
The name of an array does not have to be array, the type of data stored in an array does not have to be double, and the size of an array does not have to be fifty. Some examples:
int[] scores = new int[18];
char[] alphabet = new char[26];
String[] words = new
String[numitems]; //numitems
must be initialized
Discussion
Min and Max
The Math class defines methods min and max for both the primitive types int and double.
a = Math.min(-2, -8);
b = Math.max(37., 1.0475);
c = Math.max(a, b);
d = Math.min(a, Math.min(b, c));
The min and max methods are overloaded because the same name, min or max, is used more than once.
public static int min(int arg1, int arg2)
{
. . .
}
public static double min(double arg1, double arg2)
{
. . .
}
The machine decides which of these two methods to use based on what type of arguments you pass. In the examples shown above the value of a will be an integer because –2 and –8, the two arguments to min, are both integers. The machine will call the integer version of min because of these two integer arguments and that min method will return an integer value.
If either of the two arguments is a double then the min method for doubles is called and a double value is returned. Thus b, c, and d are all doubles. Note carefully the code for finding the min of three values.
Be careful! You can store an integer value in a double but you can’t store a double value in an integer. In the examples shown above it is necessary that b, c, and d are all doubles because they store double values. But a could be declared either as an int or a double. If a were declared as a double then it would store the “integer” value –8.0.
The machine will automatically convert integer values to doubles but it will not convert double values to integers unless you explicitly say to do so. For instance, it would be an error to declare b as an int. The machine thinks that the result of Math.max(37., 1.0475) will be a double value. If you try to store a double value in an int then the decimal part will be lost generating a possible loss of precision error.
Be careful! The error is for possible loss of precision. In our example the max value that b gets is 37.0 and there would be no harm, no loss of precision, storing 37.0 in an int. The machine doesn’t know this.
To explicitly store a double value in an int you must cast. For instance:
int n = (int)Math.max(0.25, 0.2);
Here n will get the value zero. Since one fourth is bigger than one fifth the max method return 0.25, which cannot be stored in an int. But the cast to int truncates this decimal, dropping the .25 and leaving us with only 0.
Lab01
Sum, Avg, Min, Max
Objective
Calculate using data in an array.
Background
The main method must be tagged static because the java command for running programs only knows to call a method with precisely the signature:
public static void main(String[] args)
As a matter of convention constant values are written in ALLCAPS, tagged as static, and declared with class scope. The ALLCAPS makes it easier for us to quickly read our own code. The static tag makes these constant values accessible in both static and non-static methods (in both class and instance methods). The class scope makes these constants visible from any method.
In Lab01we declare a constant:
public static final int NUMITEMS = 10;
The length of an array cannot be changed once the array is created. You can always use the public variable length to find the length of an array. For instance:
int n = (int)(Math.random() * 50 + 25);
double[] array = new double[n];
System.out.println(“The length is “ + array.length + “.”);
Be careful! The length variable is a variable not a method. We do not use parentheses on a variable.
Specification
Input ten decimal values from the user. Find the sum and average of the data. Find the smallest value and the largest value. Display these four results to standard output using System.out.
Exercises
Lab01
Answer these questions.
These code fragments all manipulate the array's values through the array's index numbers. The only way to figure out what's happening to the values and the indexes is to draw a picture of each array:
|
|
|
|
[0] |
[1] |
[2] |
|
|
[array.length - 1] |
|
array |
|
|
|
|
|
… |
|
|
1) Write the contents of circle after this code has run:
|
[0] |
[1] |
[2] |
[3] |
|
1.0 |
10.0 |
1.0 |
0.0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double [] circle = {1.0, 10.0, 1.0, 0.0};
for (int index=0; index < circle.length; index++)
circle[index]=circle[index]*circle[index]*Math.PI;
|
[0] |
[1] |
[2] |
[3] |
[4] |
|
1.0 |
5.0 |
2.0 |
3.0 |
5.0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2) Write the contents of array after this code has run:
double [] array = {1.0, 5.0, 2.0,
3.0, 5.0};
for (int pos
= array.length - 1; pos > 0; pos--)
array[pos]
= array[pos - 1];
3) Write the contents of myArray after this (useless) code has run:
|
[0] |
[1] |
[2] |
[3] |
[4] |
[5] |
[6] |
|
4 |
9 |
3 |
5 |
6 |
2 |
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
temp |
|
|
|
|
|
|
int [] myArray = {4, 9, 3, 5,
6, 2, 1};
for (int
i=0; i<= myArray.length - 1; i++)
if
(myArray[i] > myArray[i + 1])
{
int temp = myArray[i];
myArray[i] = myArray[i + 1];
myArray[i
+ 1] = temp;
}
4) What is the output of this (useless) code?
|
[0] |
[1] |
[2] |
[3] |
[4] |
|
1 |
2 |
3 |
4 |
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int [] myList = {1, 2, 3, 4, 5};
changeArray(myList);
System.out.print("The changed list is ");
for (int i=0; i< myList.length; i++)
System.out.println(mylist[i] + " ");
public static void changeArray(int[] tempList)
{
for
(int i=0; i < tempList.length; i++)
tempList[i] = tempList[i] + tempList[2];
}
Lab 02 Creating and Searching a Student array
Objective
Calculate using data in an array.
Write a program that
contains an array of Students.
First create a student class
Make a Student class that has
Test your class by
making a Student
Tester
Now make a new class
StudentArray
which creates 5 Student objects
Write a method to
Either add the main method to this class or create a StudentArrayTester
Test all the possible paths of your code.
Print out listings of
Lab01, Lab 02 and copies of the runs along with the Exercises 01. Bring them to
class the first day of school. They are
worth 25 points in the first quarter.