Information Systems PDF

Title Information Systems
Author paul oyebanjo
Course Management Information Systems
Institution The University of Texas at San Antonio
Pages 5
File Size 77.3 KB
File Type PDF
Total Downloads 40
Total Views 177

Summary

Lecture Notes for information system for java 2...


Description

CodeLab: 11.13.06 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 += 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.) public boolean isReverse(int[] x, int[] y) { boolean result = true; for(int i = 0; i < x.length; i++) { if (x[i] != y[y.length - i - 1]) result = false; } return result; }

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

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 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. total = 0; for(k = 0; k < temps.length; k++) { total += temps[k]; } avgTemp = total / k;

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]

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

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. isAMember = false; for(k = 0; k < currentMembers.length; k++) { if (currentMembers[k] == memberID) isAMember = 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; k < zipcodeList.length - 1; k++) { if(zipcodeList[k] == zipcodeList[k + 1]) duplicates = true; }

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(k = 0; k < zipcodeList.length - 1; k++) { for(j = 0; j < zipcodeList.length; j++) { if(j != k && zipcodeList[j] == zipcodeList[k]) 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. numberOfIncompletes = 0; for(k = 0; k < incompletes.length; k++) { if(incompletes[k] == studentID) numberOfIncompletes++; }

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 = Math.max(x, y);

Write the definition of a method max that has three int parameters and returns the largest. public int max(int x, int y, int z) { return Math.max(Math.max(x,y), z); }

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++) { if(mostTickets < parkingTickets[k]) mostTickets = parkingTickets[k]; }

An array of Strings, names, has been declared and initialized. Write the statements needed to determine whether any of 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] == "") hasEmpty = true; }

An array of ints, arr, has been declared and initialized. Write the statements needed to reverse the elements in the array. So, if the elements were originally 5, 13, 4, 97 then after your code executes they would be 97, 4, 13, 5. int[] tempArr = new int[arr.length]; for(int i = 0; i < arr.length; i++) { tempArr[i] = arr[arr.length - i - 1]; } arr = tempArr;

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


Similar Free PDFs