0% found this document useful (0 votes)
169 views

Chap 3 Worksheet

The document contains a lab handout on parameter mystery programs with multiple examples that print output based on parameters passed to methods. It also contains exercises to write methods that take parameters like printing columns with a number and spaces, returning the average of three integers, mapping a grade percentage to a 4.0 scale, prompting for a grade and returning the mapped grade, and prompting for integers and returning the sum and maximum.

Uploaded by

rajeswarikannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views

Chap 3 Worksheet

The document contains a lab handout on parameter mystery programs with multiple examples that print output based on parameters passed to methods. It also contains exercises to write methods that take parameters like printing columns with a number and spaces, returning the average of three integers, mapping a grade percentage to a 4.0 scale, prompting for a grade and returning the mapped grade, and prompting for integers and returning the sum and maximum.

Uploaded by

rajeswarikannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Building Java Programs

Chapter 3 Lab Handout

Parameter Mystery
1. What output is produced by the following program?
public class ParameterMystery1 {
public static void main(String[] args) {
int a = 4, b = 7, c = -2;
mystery(a, b, c);
mystery(c, 3, a);
mystery(a + b, b + c, c + a);
}

public static void mystery(int c, int a, int b) {


b -= 2;
c = a + 5;
a = a - b;
System.out.println(b + " + " + c + " = " + a);
}
}

2. What output is produced by the following program?


public class ParameterMystery2 {
public static void main(String[] args) {
String major = "fred";
String fred = "computer";
String computer = "department";
String department = "student";
String student = "major";

sentence(major, fred, department);


sentence(student, computer, fred);
sentence("fred", "honor", computer);
sentence("foo", "bar", "baz");
sentence(fred, computer, student);
}

public static void sentence(String major, String fred, String foo) {


System.out.println("Many a " + foo + " in the " + fred + " of " + major);
}
}

1 of 6
3. What output is produced by the following program?
public class ParameterMystery3 {
public static void main(String[] args) {
String a = "king";
String b = "two";
String c = "queen";
String two = "five";

sentence(a, b, c);
sentence("b", c, c);
sentence(two, "two", a);
sentence(c, a, b);
sentence(two, "queen", b);
}

public static void sentence(String b, String c, String a) {


System.out.println("a " + c + " and a " + a + " beats a " + b);
}
}

Parameters
4. Write a method named printColumns that accepts two parameters: a maximum number and a number of
spaces. The method should print that many numbers starting at 1, with each number separated by the given
number of spaces. For example, the call printColumns(8, 5) should produce the following output:
1 2 3 4 5 6 7 8

2 of 6
Return
5. Write a method named average3 that accepts three integers as parameters and returns the average of the
three values. For example, the call average3(15, 22, -1) should return 12.0.

6. Write a method named grade that accepts an integer parameter representing a student's course grade from 0
to 100, and returns the student's course grade on the 4.0 scale. Use the following mapping: (Note: This
mapping is fictional and does not represent how we will assign actual grades in this course!)
Score 60 61 62 63 ... 98 99 100
Grade 0.0 0.1 0.2 0.3 ... 3.8 3.9 4.0

3 of 6
Scanner
7. Write code to prompt a student for his/her grade using a Scanner, and use the grade method from the
previous question to report the student's grade:
What percent did you earn? 84
Your grade is 2.4

8. Write code to prompt the user for many integers and print the total sum and maximum of the numbers.
How many integers? 5
Next integer? 2
Next integer? 9
Next integer? 17
Next integer? 4
Next integer? 6

Sum = 38
Max = 17

You may assume that the user types at least one integer and that at least one integer will be non-negative.

4 of 6

You might also like