Declare and instantiate an array named scores of twenty-five elements of type int .
int [] scores = new int [25];
*********************************
Write a statement that declares an array named streetAddress that contains exactly eighty elements of type char .
char[] streetAddress = new char [80];
*********************************
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. More...
System.out.println(monthSales[9]);
*********************************
Given an array a , write an expression that refers to the first element of the array. More...
a [0]
********************************
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 with elements of type int has been declared, assign 3 to its first element. More...
a [0] = 3;
********************************
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;
***********************************
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] = a [0]+10;
**********************************
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;
*********************************
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 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] ;
**********************************
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 = arr[i];
arr[i] = arr[j];
arr[j] = temp;
***********************************
Given that an array of int s named a with 30 elements has been declared, assign 5 to its last element.
a[29] = 5 ;
************************************
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 ;
************************************
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] ;
**********************************
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 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];
}
**********************************
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. More...
total = 0 ;
for( k = 0 ; k<temps.length ; k++ )
{
total = total + temps[k] ;
}
avgTemp = total/temps.length ;
************************************
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]. More...
a [ a . length - i - 1 ]
***********************************
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 - k - 1] ;
a[a.length - k - 1] = temp ;
}
*********************************
Declare an array named a of ten elements of type int and initialize the elements (starting with the first) to the values 10 , 20 , ..., 100 respectively.
int a [] ={ 10,20,30,40,50,60,70,80,90,100};
********************************
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};
**********************************
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 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" };
*********************************
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 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. More...
public void printArray( int[] s )
{
for ( int i = 0 ; i < s.length ; i++ )
{
System.out.println(s[i]) ;
}
}
**********************************
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 .
public int sumArray ( int[] s )
{
int sum = 0 ;
for ( int i = 0 ; i < s.length ; i++ )
{
sum = sum + s[i] ;
}
return sum ;
}
************************************
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.) More...
public boolean isReverse ( int[] a , int[] b )
{
if (a.length != b.length)
{
return false ;
}
for (int i = 0 ; i < a.length ; i++)
{
if (a[i] != b[b.length-1-i])
{
return false ;
}
}
return true ;
}
*************************************
Write the definition of a method reverse , whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value. More...
public void reverse ( int[] a )
{
for ( int i = 0 ; i < a.length/2 ; i++ )
{
int temp = a[i] ;
a[i] = a[a.length-i-1] ;
a[a.length-i-1] = temp ;
}
}
*************************************
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);
}
}
*********************************
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 ; k++ )
{
if (memberID==currentMembers[k])
{
isAMember = true ;
break ;
}
else
{
isAMember = false ;
}
}
************************************
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;
***********************************
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 . More...
duplicates = false ;
for( k=0 ; k<zipcodeList.length-1 ; k++ )
{
if (zipcodeList[k]==zipcodeList[k+1])
{
duplicates=true;
break ;
}
else
{
duplicates=false;
}
}
***********************************
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 . More...
duplicates = false ;
for( k=0 ; k<zipcodeList.length ; k++ )
{
for( j=k+1 ; j<zipcodeList.length ; j++ )
{
if (zipcodeList[k]==zipcodeList[j])
{
duplicates=true ;
}
}
}
************************************
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=0;
for(k=0;k<parkingTickets.length;k=k+1)
{
if (k==0)
{
mostTickets=parkingTickets[k];
}
if (parkingTickets[k]>mostTickets)
{
mostTickets=parkingTickets[k];
}
}
***********************************
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 .
numberOfIncompletes=0;
for(k=0;k<incompletes.length;k++)
{
if (studentID==incompletes[k])
{
numberOfIncompletes++ ;
}
}
*************************************
Declare a two-dimensional array of strings named chessboard.
String [][]chessboard;
****************************
Declare a two-dimensional array of integers named tictactoe.
int [][]tictactoe;
******************************
A 2-dimensional array of ints with 4 rows, has been created and assigned to a2d. Write an expression whose value is the total number of ints that could be stored in the entire array.
a2d[0].length + a2d[1].length + a2d[2].length + a2d[3].length
*******************************
A 2-dimensional array of ints, has been created and assigned to a2d. Write an expression whose value is the number of rows in this array.
a2d.length
*********************************
A 2-dimensional array of ints, has been created and assigned to a2d. Write an expression whose value is the number of elements in the last row. (Assume the array is not empty.)
a2d[a2d.length-1].length
***************************
A 2-dimensional array of ints, has been created and assigned to a2d. Write an expression whose value is the number of elements in the first row. (Assume the array is not empty.)
a2d[0].length
**************************
A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of any row or column or diagonal are equal.
tictactoe[0][0] == tictactoe[0][1] && tictactoe[0][0] == tictactoe[0][2]||tictactoe[1][0] == tictactoe[1][1] && tictactoe[1][0] == tictactoe[1][2]||tictactoe[2][0] == tictactoe[2][1] && tictactoe[2][0] == tictactoe[2][2]||tictactoe[0][0] == tictactoe[1][0] && tictactoe[0][0] == tictactoe[2][0]||tictactoe[0][1] == tictactoe[1][1] && tictactoe[0][1] == tictactoe[2][1]||tictactoe[0][2] == tictactoe[1][2] && tictactoe[0][2] == tictactoe[2][2]||tictactoe[0][0] == tictactoe[1][1] && tictactoe[0][0] == tictactoe[2][2]||tictactoe[0][2] == tictactoe[1][1] && tictactoe[0][2] == tictactoe[2][0]
***************************
A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of the diagonal that does NOT include the first element of the first row are all equal.
tictactoe[0][2] == tictactoe[1][1] && tictactoe[0][2] == tictactoe[2][0]
******************************
Assume you are given a boolean variable named isSquare and a 2-dimensional array that has has been created and assigned to a2d. Write some statements that assign true to isSquare if the entire 2-dimensional array is square meaning every row and column has the same number of elements as every other row and column.
isSquare = true ;
for (int i = 0 ; i < a2d.length; i++ ) {
if (a2d.length != a2d[i].length ) {isSquare = false ;}
}
*****************************
Assume you are given an int variable named sum and a 2-dimensional array of ints that has been created and assigned to a2d. Write some statements that compute the sum of all the elements in the entire 2-dimensional array and assign the value to sum.
sum=0;
for (int i=0; i<a2d.length; i++)for (int j=0; j<a2d[i].length; j++)sum += a2d[i][j];
No comments:
Post a Comment