- Write a method drawLine that accepts a single integer parameter n
and prints n minus signs (i.e. '-') followed by a newline. For example, drawLine (5) should print "-----" and a newline.
Solution:
void drawLine (int n) {
for (int i=0; i<n; i++) System.out.print (" ");
System.out.println ();
}
Write a code segment that will output a right triangle of asterisks as in
the example below. The first row contains 1 asterisk, the second contains 2,
and third contains 3, etc. The variable rows contains the number of
rows of asterisks (you may assume that rows is greater than 0).
For example, if rows has the value 4, the output should look
like this:
*
**
***
****
Hint: write nested for loops, with the outer loop counting from 1 to
rows and the inner loop printing the asterisks.
Solution:
for (int i=0; i<rows; i++) {
for (int j=0; j<=i; j++)
System.out.print ("*");
System.out.println ();
}
- The following method accepts an array of integers as a parameter and returns
the maximum value in the array:
int findMax (int v[]) {
int maxIndex = 0;
for (int j=1; j < v.length; j++)
if (v[j] > v[maxIndex]) maxIndex = j;
return v[maxIndex];
}
Using this code as a starting point, write another method that accepts
an array of doubles and returns the minimum value in the array.
Use meaningful identifier names.
Solution:
double findMin (double v[]) {
int minIndex = 0;
for (int j=1; j < v.length; j++)
if (v[j] < v[minIndex]) minIndex = j;
return v[minIndex];
}
-
Suppose we have the following declaration:
int numbers[] = { 1, 2, 3, 4, 5 };
What will be the result of the following code segment?
System.out.println (numbers.length);
System.out.println (numbers[2]);
for (int i=2; i < 5; i++) System.out.print (numbers[i] + " ");
int x = numbers[0];
for (int i=1; i < numbers.length; i++) if (x < numbers[i]) x = numbers[i];
System.out.println (x);
System.out.println (numbers[5]);
Solution:
The program will print the following:
5
3
3 4 5 5
Then when trying to print numbers[5], the program will halt with an array
of of bounds exception.
Write a code segment that prompts the user to enter two integers, then prints the
minimum of the two.
Solution:
int a, b, c;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter two integers: ");
a = scan.nextInt ();
b = scan.nextInt ();
if (a < b)
c = a;
else
c = b;
System.out.println ("The minimum number is " + c);
- Write a method that prompts the user to enter an integer value, then returns that value.
Solution:
int getUserInt () {
Scanner scan = new Scanner (System.in);
System.out.print ("Enter an integer: ");
int a = scan.nextInt ();
return a;
}
-
Write a code segment using a do/while loop that continues prompting the user to
enter a positive integer until a positive integer is entered.
Solution:
Scanner scan = new Scanner (System.in);
int a;
do {
System.out.print ("Enter a positive integer: ");
a = scan.nextInt ();
} while (a < 0);
- Write a code segment that prompts the user to enter a positive
integer, then prints "prime" if the number is prime, "not prime" otherwise.
Hint: A prime number is an integer that is divisible only by itself
and 1. Hint 2: So if a number n is not divisible
by any integer between 2 and n-1, inclusive, then it is prime.
Hint 3: a is divisible by b if and only if the
remainder when dividing a by b is 0.
Solution:
This is the nice way to do it, by dividing it into methods. You could also
do it as one big code segment but it is harder to read.
static int getUserInt () {
Scanner scan = new Scanner (System.in);
int a;
do {
System.out.print ("Enter a positive integer: ");
a = scan.nextInt ();
} while (a < 0);
return a;
}
static boolean prime (int n) {
for (int i=2; i<n; i++) if (n % i == 0) return false;
return true;
}
public static void main (String args[]) {
int a, b, c;
a = getUserInt ();
if (prime (a))
System.out.println ("prime");
else
System.out.println ("not prime");
}
- Write a method that accepts an integer parameter n and returns
the factorial of n. Hint: the factorial of n
is 1 × 2 × 3 × ... × n, i.e., the product
of all integers from 1 to n.
Solution:
int factorial (int n) {
int product = 1;
for (int i=1; i<=n; i++) product *= i;
return product;
}
- Consider the following code:
int findAvg (int v[]) {
int sum, average, count;
for (int i=0; i<=v.length; v++)
sum += v[i];
count++;
average = sum / count;
}
This method is supposed to return the average of the values in an integer
array. However, there are several errors that will prevent the code from
compiling and/or working properly. List at least five errors. (You may list
more than five errors, but for every wrong response we will take off points.)
Solution:
1. "sum" is not initialized to 0.
2. "count" is not initialized to 0.
3. It should be "< v.length", not "<= v.length"
4. If "count" is 0, then there could be a division by 0.
5. There should be curly braces around the indented statements after "for."
6. There is no "return" statement.