Overview | Schedule | Announcements | Resources | Assignments | Home |
Write a method named total
that has two
int
parameters named start
and
finish
. total
should add all integers
between start
and finish
(inclusive) and
return this value when it is invoked. You may assume that
start
is less than finish
.
For example, the following invocation of total
should return 15:
total(4, 6);
In the second example, calling total
returns
42:
total(9, 12);
public int total(int start, int finish) { int sum = 0; int counter = start; while (counter <= finish) { sum = sum + counter; counter = counter + 1; } return sum; }
Write a method named arithmetic
that has three
parameters:
operation
num1
num2
The method uses the first parameter to determine the arithmetic
operation to be performed on the second and third parameters. The
result is printed on the monitor when the method is invoked. The
only legal values that can be used as parameters when the method is
invoked are: '+', '-', '*', '/', '%'
. Any other
character value will cause "illegal operation" to be printed
instead.
For example, if arithmetic('+', 2, 3)
is called,
then 5 is printed.
On the other hand, if arithmetic('*', 3, 2)
is
called, then 6 is printed.
public void arithmetic(char operation, int num1, int num2) { if (operation == '+') { System.out.println(num1 + num2); } else if (operation == '-') { System.out.println(num1 - num2); } else if (operation == '*') { System.out.println(num1 * num2); } else if (operation == '/') { System.out.println(num1 / num2); } else if (operation == '%') { System.out.println(num1 % num2); } else { System.out.println("illegal operation"); } }
Write a method named sequence
, which prints the
first few numbers in a special sequence that is formed by starting
with 1, 1. The third number in the sequence is found by adding the
first two together; the fourth number in the sequence is found by
adding the second and third numbers together; all other numbers in
the sequence are also found by adding the prior two numbers
together. sequence
takes one integer parameter (called
number
): the number of numbers that will be printed
from the sequence, which starts with 1, 1. If sequence
is called with a non-positive integer, then the string, "Only
positives may be used.", is printed.
sequence(4)
prints
1 1 2 3
sequence(8)
prints
1 1 2 3 5 8 13 21
public void sequence(int number) { if (number <= 0) { System.out.println("Only positives may be used."); } else { int previous = 0; int current = 1; int counter = 0; while (counter < number) { System.out.println(current); counter = counter + 1; int next = previous + current; previous = current; current = next; } } }
Write a class called DPUprinting
that has an
int
field called totalPages
.
totalPages
is a DPU student's current total number of
pages of paper that have been printed. Write the entire source code
for the class, including the fields, the constructor and two
methods. One of the methods, getPages
, will simply
return the current value of the one field. The other,
addOnePage
, will add one to the
totalPages
field every time addOnePage
is
called/invoked.
public class DPUprinting { private int totalPages; public DPUprinting() { totalPages = 0; } public int getPages() { return totalPages; } public void addOnePage() { totalPages = totalPages + 1; } }
What is the output of the following code fragment?
x = 0; y = 1; while (y < 20) { if (x == 0) { System.out.println("Hello!"); y = y + 2; } else if (x == 1) { System.out.println(y); } else if (x == 4) { if (y > 10) { System.out.println("y is getting big!"); y = y * 2; x = 0; } else { x = 3; y = y + 5; } } x = x + 1; System.out.println("x = " + x); } System.out.println("y = " + y);
Hello! x = 1 3 x = 2 x = 3 x = 4 x = 4 x = 4 y is getting big! x = 1 y = 26
3 * 4 % 6
false
Overview | Schedule | Announcements | Resources | Assignments | Home |
DePauw University, Computer Science
Department, Spring 2005
Maintained by Brian
Howard (bhoward@depauw.edu
).
Last updated