Week 03: Lab 2
Objectives
- Write a few small Java programs which require conditional structures, operators and loops.
Exercise One
Write a program which reads in an integer from the user and then prints out the last digit of the number that is seven times the value which the user entered.
For example, if a user enters the number 53
, your program will compute the
number 7*53=371
and print out the last digit which is 1
.
(Note that you will need to use the modulo division operator (%
) in this
program.)
Exercise Two
-
An examiner is using a grading scheme whereby the grade depends on the marks like this:
0-50 F 51-60 E 61-70 D 71-80 C 81-90 B 91-100 A
Write a program that asks for a score
(0-100)
and reports the grade(F-A)
. -
Refine the previous program by making it to add a
+
or-
after the gradesE
and above according to last digit as follows:1-3 - 4-7 8-0 +
This means that a mark of
93
isA-
;88
isB+
;65
isD
and so on.
Exercise Three
-
Write a program that asks the user for a positive integer n and prints out an ASCII representation of an n×n grid like this (when
n
is4
):+--+--+--+ | | | | +--+--+--+ | | | | +--+--+--+ | | | | +--+--+--+
You should have one method to print the even rows and one method to print the odd rows.
-
Write a similar program which prints out an ASCII triangle of a given height
n
, wheren
is an integer provided by the user. For example, forn == 3
:+ /| / | / | +---+
Further Programming Ideas
Different functions of your programs (like calculating a desired number, or determining a grade, or printing a result etc.), should be implemented in different methods: this is called modularisation — one of the basic principles of programming. If you did not use methods in your solutions, but lumped everything into one main method, try extracting the different functions of your program into separate methods.