CISC 1115
Introduction to Programming Using Java
Lab #4
Methods
Lab 4.1 — Degrees of Difficulty (DegreesToRadians)
Write a program that prompts the user for the measure of an angle in degrees and uses the
toRadians
method of
the
Math
class to print out the measure of the same angle in radians.
Sample Test Run
Enter angle in degrees: 90.0
90.0 degrees equals 1.5707963267948966
Lab 4.2 — Straight to the Diagonal (RectDiagonal)
Write a program that finds prompts the user for the height and width of a rectangle and prints the length of the diagonal.
The diagonal of a rectangle can be obtained (by virtue of the Pythagorean Theorem) by taking the square root (using the
sqrt
method of the
Math
class) of the sum of the squares.
of the two sides.
Sample Test Run
Enter the (integer) width of the rectangle: 3
Enter the (integer) height of the rectangle: 4
The length of the diagonal of a rectangle with a width of 3 and a height of 4 is 5.0
Lab 4.3 — HELP!! (Help)
Write a program containing two methods,
printS
, and
printO
, that prints the letters S O S as 'ASCII art', one
beneath the other:
SSSSSSSSSSS
SS
SS
SSSSSSSSSSS
SS
SS
SSSSSSSSSSS
OOOOOOOOOOO
O O
O O
O O
O O
O O
OOOOOOOOOOO
SSSSSSSSSSS
SS
SS
SSSSSSSSSSS
SS
SS
SSSSSSSSSSS
Lab 4.4 — Almost JAVA (AVA)
Write a program containing a method,
printAVA
, that prints the string string "AVA" as 'ASCII art':
A V V A
A A V V A A
A A V V A A
AAAAAAA V V AAAAAAA
A A V V A A
A A V V A A
A A V A A
Hint: While you can do this with 'brute force', i.e., just drawing out the figure, a little thought and
learning how to copy/paste in Dr Java should save you some effort.
Lab 4.5 — A Madness to My Methods (Box) (Approval)
This one is a bit (but not too) challenging — you are to write a program to
print out the following figure:
!----------------!
! . . . . . . . .!
!. . . . . . . . !
! . . . . . . . .!
!. . . . . . . . !
! . . . . . . . .!
!. . . . . . . . !
! . . . . . . . .!
!. . . . . . . . !
!----------------!
The program should have the following methods:
printBorder
— prints out the line
!----------------!
printInnerLine1
— prints out the line
! . . . . . . . .!
(ie., the interior line where the first position is blank)
printInnerLine2
— prints out the line
!. . . . . . . . !
(ie., the interior line where the first position is a '.')
printBox
— prints out the entire box
Some other constraints (rules):
printBox
should call the other methods to accomplish its task
- You'll notice that the inside of the box consists of 4 pair of inner line type 1 and line type 2.
You should use a for loop to print out these inner lines.
- Your
main
method should consist of nothing more than a call (invoclation) of printBox
.
Lab 4.6 — Odds and Evens (OddEven) (Approval)
Write a program that contains two methods:
- printOdds(int n) - accepts an integer argument in parameter
n
, and prints the odd numbers from 1 to n
- printEvens(int n) - accepts an integer argument in parameter
n
, and prints the even numbers from 2 to n
Your
main
should prompt the user for an integer and call
printOdds
and
printEvens
(in that order). Use a for loop (in
main
) to do this twice.
Sample Test Run
Enter an integer: 5
Odds:
1
3
5
Evens:
2
4
--- Done ---
Enter an integer: 10
Odds:
1
3
5
7
9
Evens:
2
4
6
8
10
--- Done ---
Lab 4.7 — A Second Vacation and Bonus (Approval)
Rewrite Lab 3.2 so introducing the following methods:
- int calcVacation(int years) — accepts the number of years worked, and returns the number of weeks of vacation
- int calcVacation(int years, int salary) — accepts the number of years worked, and the salary, and returns the
amount of the bonus
Enter last name: Weiss
Enter first name: Gerald
Enter years worked: 1
Enter salary: 100
Gerald Weiss gets 1 weeks vacation, and a bonus of $0
Enter last name: Arnow
Enter first name: Davis
Enter years worked: 3
Enter salary: 200
Davis Arnow gets 1 weeks vacation, and a bonus of $0
Enter last name: Tenenbaum
Enter first name: Aaron
Enter years worked: 4
Enter salary: 100
Aaron Tenenbaum gets 2 weeks vacation, and a bonus of $0
Enter last name: Thurm
Enter first name: Joe
Enter years worked: 6
Enter salary: 100
Joe Thurm gets 2 weeks vacation, and a bonus of $200
Enter last name: Whitlock
Enter first name: Paula
Enter years worked: 7
Enter salary: 400
Paula Whitlock gets 3 weeks vacation, and a bonus of $200
Enter last name: Jones
Enter first name: Jackie
Enter years worked: 9
Enter salary: 500
Jackie Jones gets 3 weeks vacation, and a bonus of $200
Enter last name: Dreizen
Enter first name: Phil
Enter years worked: 10
Enter salary: 1000
Phil Dreizen gets 3 weeks vacation, and a bonus of $50
Enter last name: Augenstein
Enter first name: Moshe
Enter years worked: 12
Enter salary: 1500
Moshe Augenstein gets 3 weeks vacation, and a bonus of $75