Chapter 7

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Declare an array named taxRates of five elements of type double and initialize the elements (starting with the first) to the values 0.10, 0.15, 0.21, 0.28, 0.31 respectively.

double[] taxRates = {0.10, 0.15, 0.21, 0.28, 0.31};

In a single statement: declare, create and initialize an array named a of ten elements of type int with the values of the elements (starting with the first) set of 10, 20 , ...,100 respectively.

int [] a = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

Declare and instantiate an array named scores of twenty-five elements of type int

int [] scored = new int [25];

Declare an array reference variable, week, and initialize it to an array containing the strings "mon", "tue", "wed", "thu", "fri", "sat", "sun" (in that order).

String [] week = {"mon", "tue", "wed", "thu", "fri", "sat", "sun"};

Given that the array monthSales of integers has already been declared and that its elements contain sales data for the 12 months of the year in order (i.e., January, February, etc.), write a statement that writes to standard output the element corresponding to October. Do not write anything else out to standard output.

System.out.print(monthSales [9]); System.out.println();

Given an array a, write an expression that refers to the first element of an array.

a [0]

Given that an array named a with elements of type int has been declared, assign 3 to its first element.

a [0] = 3;

Given an array a, declared to contain 34 elements, write an expression that refers to the last element of the array.

a [33]

We informally define the term corresponding element as follows: The first element in an array and the last element of the array are corresponding elements. Similarly, the second element and the element just before the last element are corresponding elements. The third element and the element just before the element just before the last element are corresponding elements -- and so on. Given an array a, write an expression for the corresponding element of a[i].

a[a.length-1-i]

Write the definition of a method, isReverse, whose two parameters are arrays of ints of equal size. The method returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.) 1

boolean isReverse(int a[], int b[]) { int k; for (k = 0; k < a.length && a[k] == b[a.length - 1 - k]; k++); return k == a.length; }

Write a statement that declares an array named streetAddress that contains exactly eighty elements of type char.

char [] streetAddress = new char [80];

You are given two int variables j and k, an int array zipcodeList that has been declared and initialized, and a boolean variable duplicates. Write some code that assigns true to duplicates if any two elements in the array have the same value, and that assigns false to duplicates otherwise. Use only j, k, zipcodeList, and duplicates.

duplicates = false; for (j = 0; !duplicates && j < zipcodeList.length - 1; j++) for (k = j + 1; !duplicates && k < zipcodeList.length; k++) if (zipcodeList[k] == zipcodeList[j]) duplicates = true;

You are given an int variable k, an int array zipcodeList that has been declared and initialized, and a boolean variable duplicates. Write some code that assigns true to duplicates if there are two adjacent elements in the array that have the same value, and that assigns false to duplicates otherwise. Use only k, zipcodeList, and duplicates.

duplicates = false; for (k = 0; !duplicates && k < zipcodeList.length - 1; k++) if (zipcodeList[k] == zipcodeList[k + 1]) duplicates = true;

Given: an int variable k, an int array currentMembers that has been declared and initialized, an int variable memberID that has been initialized, and a boolean variable isAMember, write code that assigns true to isAMember if the value of memberID can be found in currentMembers, and that assigns false to isAMember otherwise. Use only k, currentMembers, memberID, and isAMember.

for (k = 0; k < currentMembers.length && memberID != currentMembers[k]; k++); isAMember = k < currentMembers.length;

Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array . Given an array a and two other int variables , k and temp, write a loop that reverses the elements of the array . Do not use any other variables besides a, k, and temp.

for (k=0;k<a.length/2;k++) { temp = a[k]; a[k] = a[a.length-1-k]; a[a.length-1-k] = temp; }

Given an int variable k, an int array incompletes that has been declared and initialized , an int variable studentID that has been initialized , and an int variable numberOfIncompletes, write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, studentID, and numberOfIncompletes.

for (numberOfIncompletes=0,k=0; k<incompletes.length; k++) if (incompletes[k] == studentID) numberOfIncompletes++;

Given an array temps of doubles, containing temperature data, compute the average temperature. Store the average in a variable called avgTemp, which has already been declared. Besides temps and avgTemp, you may use only two other variables -- an int variable k and a double variable named total, which have been declared .

for (total=0.0, k=0; k<temps.length; k++) total += temps[k]; avgTemp = total/temps.length; Correct

Write the definition of a method named sumArray that has one parameter, an array of ints. The method returns the sum of the elements of the array as an int.

int sumArray(int a[]) { int i, total = 0; for(i = 0; i < a.length; i++) total += a[i]; return total; }

Write a statement to declare and initialize an array named denominations that contains exactly six elements of type of int. Your declaration statement should initialize the elements of the array to the following values : 1, 5, 10, 25, 50, 100. (The value 1 goes into the first element, the value 100 to the last.)

int[] denominations = {1, 5, 10, 25, 50, 100};

printArray is a method that accepts one argument, an array of ints. The method prints the contents of the array; it does not return a value. inventory is an array of ints that has been already declared and filled with values. Write a statement that prints the contents of the array inventory by calling the method printArray.

printArray(inventory);

Write the definition of a method printArray, which has one parameter, an array of ints. The method does not return a value. The method prints out each element of the array, on a line by itself, in the order the elements appear in the array, and does not print anything else.

void printArray(int a[]) { int i; for (i = 0; i < a.length; i++) { System.out.print(a[i]); System.out.println(); } }

Write the definition of a method reverse, whose parameter is an array of ints. The method reverses the elements of the array. The method does not return a value.

void reverse(int a[]) { int k, temp; for (k = 0; k < a.length / 2; k++) { temp = a[k]; a[k] = a[a.length - 1 - k]; a[a.length - 1 - k] = temp; } }

Assume that the array arr has been declared. Write a statement that assigns the next to last element of the array to the variable x, which has already been declared.

x = arr [arr.length - 2];


Kaugnay na mga set ng pag-aaral

Algebra 1 Semester 2 Unit Test Review

View Set

Chapter 5: Ethics, Corporate Social Responsibility, and Sustainability

View Set

Periodic Table Elements 1-56, 72-88

View Set

IL CHAPTER 2 - TYPES OF LIFE POLICIES

View Set

Marketing Management Topic 3: Know Your Customer - Understanding Buyer Behaviour

View Set

Journeys L1 Word Work: short vowels

View Set

Hanyu-Lesson4 What is your name ?你叫什么名字?

View Set