Lab6: Nested Loops Solution

$30.00

Category:

Description

Objectives:

 

  • Solving problems and writing algorithms
  • Implementing algorithms with nested loop statements

 

Problem [40 marks]

Instructions:

 

  1. Create a folder named Lab6 to store all the files from this lab
  2. Create an external documentation file (filename: docx) to store the summary, algorithm(s), and sample input and output for each problem.
  3. All your programs must have good internal and external documentations

 

Problem 1: [10 marks] Printing patterns using loops (filename: DisplayPatterns.java)

 

Design an algorithm and then write a program that prints the following two patterns in a single program. Note: The patterns do not need to be perfectly aligned. Use nested for loops

 

Pattern I

 

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

 

Pattern II

 

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

 

Problem 2: [10 marks] Display Pyramid (filename: DisplayPyramid.java)

 

Design an algorithm and then write a program that prompts the user to enter an integer from 1 to 15 (assume that the user always enters a valid integer) and displays a pyramid, as shown in the following sample run:

 

Enter a number between 1 and 15: 12

1

2  1  2

3  2  1  2  3

4  3  2  1  2  3  4

5  4  3  2  1  2  3  4  5

6  5  4  3  2  1  2  3  4  5  6

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

8  7  6  5  4  3  2  1  2  3  4  5  6  7  8

9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9

10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10

11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11

12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12

 

Problem 3: [20 marks] Simulate the movement of an elevator (filename: Elevator.java)

 

Design an algorithm and then write a program called Elevator, which models a simplified elevator.

 

  1. The elevator operates in a building with 9 floors. The labelling of the floors begins at 1.
  2. The user will request a floor by typing in an integer in the appropriate range. The elevator

you are modelling needs to “move” to the requested floor, displaying floor numbers and a

picture of an up/down arrow as it goes.

  1. The program should continue moving to new floors and prompting for a floor selection until the user enters a sentinel value of 0 instead of a floor number. When the sentinel value is entered, the program should exit.
  2. If the entered floor number is not valid, the user should be warned, and the user is asked to enter a floor number again.

 

A sample runs is given below:

 

o——-o

|      1  |

o——-o

 

The elevator is at Floor 1 now

 

Enter a floor number between 1 and 9, or enter 0 to quit: 5

 

The elevator is moving up from Floor 1 to 5

 

o——-o     /\

|      5   |    //\\

o——-o  //   \\

o——-o     /\

|      4  |     //\\

o——-o  //   \\

o——-o     /\

|      3   |    //\\

o——-o  //   \\

o——-o     /\

|      2   |    //\\

o——-o  //   \\

o——-o     /\

|      1   |    //\\

o——-o  //   \\

 

The elevator is at Floor 5 now

 

Enter a floor number between 1 and 9, or enter 0 to quit: 12

Invalid floor number. Try again.

 

Enter a floor number between 1 and 9, or enter 0 to quit: 3

 

The elevator is moving down from Floor 5 to 3

 

o——-o  \\    //

|      5   |    \\ //

o——-o     \/

o——-o  \\    //

|      4   |    \\ //

o——-o     \/

o——-o  \\    //

|      3   |    \\ //

o——-o     \/

 

The elevator is at Floor 3 now

 

Enter a floor number between 1 and 9, or enter 0 to quit: 0

 

Thank you for using the elevator program. Goodbye!

 

 

What to hand in

 

Zip the folder which contains the document file, the Java source files, and the Java class files from this lab and submit the zipped folder to D2L.

 

 


error: Content is protected !!