Codelab Chapter 8

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Write the definition of a method , isReverse, whose two parameters are arrays of integers 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.)

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; }

Given: an int variable k, an int array currentMembers that has been declared and initialized, an int variable memberID that has been initialized, and an 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;

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 an array a, write an expression that refers to the first element of the array .

a[0]

Assume that an array of int s named a that contains exactly five elements has been declared and initialized . In addition, an int variable j has also been declared and initialized to a value somewhere between 0 and 3. Write a single statement that assigns a new value to element of the array indexed by j. This new value should be equal to twice the value stored in the next element of the array (i.e. the element after the element indexed by j.

a[j] = 2 * a[j+1] ;

Write the definition of a method , oddsMatchEvens, whose two parameters are arrays of integers of equal size. The size of each array is an even number. The method returns true if and only if the even-indexed elements of the first array equal the odd-indexed elements of the second, in sequence. That is if w is the first array and q the second array , w[0] equals q[1], and w[2] equals q[3], and so on.

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

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};

An array of Strings , names , has been declared and initialized . Write the statements needed to determine whether any of the the array elements are null or refer to the empty String . Set the variable hasEmpty to true if any elements are null or empty-- otherwise set it to false .

hasEmpty = false; for (int i = 0; i < names.length; i++) if (names[i] == null || names[i].length() == 0) hasEmpty = true;

Given an array arr, of type int , along with two int variables i and j, write some code that swaps the values of arr[i] and arr[j]. Declare any additional variables as necessary.

int temp; temp = arr[i]; arr[i] = arr[j]; arr[j] = 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];

Given an array of ints named x and an int variable named total that has already been declared , write some code that places the sum of all the elements of the array x into total. Declare any variables that you need.

total = 0; for (int i=0; i<x.length; i++) total+=x[i];

Write the definition of a method printArray, which has one parameter , an array of int s. 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(); } }

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();

Assume that an array of integers named a has been declared and initialized . Write a single statement that assigns a new value to the first element of the array . The new value should be equal to twice the value stored in the last element of the array .

a[0] = 2*a[a.length-1];

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

a[0] = 3;

Assume that an array named a containing exactly 5 integers has been declared and initialized . Write a single statement that adds 10 to the value stored in the first element of the array .

a[0]+= 10;

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

a[33]

Given that an array named a whose elements are of type int has been declared , assign the value -1 to the last element in a.

a[a.length-1] = -1;

Given that an array of int s named a with 30 elements has been declared , assign 5 to its last element .

a[a.length-1] = 5;

An array of int s named a has been declared with 12 elements . The integer variable k holds a value between 0 and 6. Assign 9 to the element just after a[k].

a[k+1] = 9;

An array of int s named a has been declared with 12 elements . The integer variable k holds a value between 2 and 8. Assign 22 to the element just before a[k].

a[k-1] = 22;

Assume that an array of int s named a has been declared with 12 elements . The integer variable k holds a value between 0 and 6. Assign 15 to the array element whose index is k.

a[k] = 15;

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 an 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 an 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 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 double s, containing temperature data, compute the average temperature. Store the average in a variable called avgTemp. 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;

Write the definition of a method max that has three int parameters and returns the largest.

int max(int a, int b, int c) { return a<b? (b<c?c:b) : (a<c?c:a); }

Write the definition of a method named sumArray that has one parameter , an array of int s. 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; }

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 to 10, 20, ..., 100 respectively.

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

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};

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

int[] scores = new int[25];

Given the integer variables x and y, write a fragment of code that assigns the larger of x and y to another integer variable max.

max = x; if (y > max) max = y;

An array of integers named parkingTickets has been declared and initialized to the number of parking tickets given out by the city police each day since the beginning of the current year. (Thus, the first element of the array contains the number of tickets given on January 1; the last element contains the number of tickets given today.) A variable named mostTickets has been declared , along with a variable k. Without using any additional variables , write some code that results in mostTickets containing the largest value found in parkingTickets.

mostTickets = parkingTickets[0]; k = 1; while (k<parkingTickets.length) { if (mostTickets<parkingTickets[k]) mostTickets = parkingTickets[k]; k++; }

printArray is a method that accepts one argument , an array of int s. The method prints the contents of the array; it does not return a value. Inventory is an array of int s 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 a 'main' method that examines its command-line arguments and calls the (static ) method displayHelp if the only argument is the class name . Note that displayHelp accepts no parameters otherwise, 'main' calls the (static ) method argsError if the number of arguments is less than four. argsError accepts the number of arguments (an integer ) as its parameter otherwise, 'main' calls the (static ) method processArgs, which accepts the command-line argument array as its parameter .

public static void main(String [] args) { if (args.length == 1) displayHelp(); else if (args.length < 4) argsError(args.length); else processArgs(args); }

Assume that an array named salarySteps whose elements are of type int and that has exactly five elements has already been declared . Write a single statement to assign the value 30000 to the first element of this array .

salarySteps[0] = 30000;

Assume that an array of integers named salarySteps that contains exactly five elements has been declared . Write a statement that assigns the value 160000 to the last element of the array salarySteps.

salarySteps[4] = 160000;


संबंधित स्टडी सेट्स

Care of the patient with a respiratory disorder

View Set

Chapter 2: Real Estate Appraisal

View Set

Computer security : security-components

View Set

emt module chapter 18: neurological emergencies

View Set