Test 2 study guide
Download
Report
Transcript Test 2 study guide
Test 2: What types of questions?
• Category #1: Trace a code segment
• Category #2: Trace a complete program
• Category #3: Write a code segment
• Category #4: Write a complete program
• What should I do to prepare for the test?
Slides
Lecture recordings
Lab
Homework
Practice
1
Test 2: Sample 1
•
What is the output?
•
for (int i=10; i>0; i--)
System.out.println(i);
•
•
2
Test 2: Sample 2
•
What is the exact output of this complete program?
•
public class Sample1 {
public static void main (String[] args) {
int x = 3;
int y = Method(x);
System.out.println(y);
}
•
•
public static int Method(int n) {
int result = 0;
for (int i=0; i<=n; i++)
result += i;
return result;
•
•
•
}
}
3
Test 2: Sample 3
•
Write a Java code segment using while loop inside the main method to print out the values from 1 to
100.
4
Test 2: Sample 4
•
•
•
•
•
Design and implement a Java program (name it ComputeAreas) that defines three methods as
follows:
Method squareArea (double side) determines and returns the area of a square.
Method rectangleArea (double width, double length) determines and returns the area of a rectangle.
Method circleArea (double radius) determines and returns the area of a circle.
Method triangleArea (double base, double height) determines and returns the area of a triangle.
•
Test the methods with different input value read from the user (in the main method).
•
Design the main method of your program such that it allows the user to re-run the program with
different inputs (i.e., use a loop structure). Document your code, and organize and space the outputs
properly. Use escape characters to organize the outputs.
5