The following questions are typical of the kinds of problems you will see on the first midterm exam. In each case I have provided a problem for you to try and suggested solution for that problem.
1. The least common multiple (LCM) of two positive integers n and m is the smallest positive integer that is a multiple of both n and m. For example, the LCM of 12 and 8 is 24. Write the code for a method
public static int LCM(int n,int m)
that computes and returns the LCM of the integers n and m. You may assume that whenever this method is called both of its parameters will be positive numbers. To do this problem you will need to use the remainder operator, %. For integers a and b, a % b computes the remainder of a upon division by b. The test
if(a%b == 0)
can be used to determine whether or not b divides evenly into a.
2. Write a program that prints out a table of LCM(n,m) for n and m ranging from 1 to 10.
3. Write the code for a method
public static int longestSequence(int A[])
that scans through the sorted array A looking for the longest run of identical numbers. For example, if the array A contains the numbers
1 1 2 3 3 3 4 5 6 7 7 7 7 8 9 9
the longestSequence method should return 4.
4. Write the code for a method
public static int[] remove(int[] A,int x)
that returns a new array with all occurances of x removed from the array A. For example, if A contains the numbers
2 8 6 5 3 3 9 4 2 1 6
and x is 6, then remove should return a smaller array containing the numbers
2 8 5 3 3 9 4 2 1