Skip navigation

Introductory Programming In Java

Lab 1 Lab 2 Lab 3 Lab 4 Lab 5 Lab 6 Lab 7

Lab 2

Mastering Java control structures

Objectives

Write a few small Java programs which require to use conditional structures, operators and loops.

Exercise One

Write a program to print out the last digit of an integer seven times the value of an integer which the user enters.

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 operator (%) in this program.)

Exercise Two

  • An examiner using a grading scheme whereby the grade depends on the marks thus:
        
    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 grades E and above according to last digit as follows:

    1-3   -
    4-7    
    8-0   +
    

    This means that a mark of 93 is A-; 88 is B+; 65 is D and so on.

Exercise Three

  • Write a program that asks the user for a positive integer n and prints out the n×n grid like this (when n is 4):

    +--+--+--+
    |  |  |  |
    +--+--+--+
    |  |  |  |
    +--+--+--+
    |  |  |  |
    +--+--+--+
    

    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 a triangle:
        +
       /|
      / |
     /  |
    +---+
    

Further programming ideas

Different functionalities of your programs (like calculating a desired number, or determining a grade, or printing a result etc), should be implemented with the help of different methods: this is called modularization — one of the basic principles of programming. If you did not use methods in our coding solutions, but lumped everything into one main method, then re-implement everything using modularization.

Lab 1 Lab 2 Lab 3 Lab 4 Lab 5 Lab 6 Lab 7

 

Updated:  Tue 14 May 2013 01:03:20 EST Responsible Officer:   JavaScript must be enabled to display this email address. Page Contact:   Course Webmaster