Solution: Operating Systems and Java Programming Fundamentals
VerifiedAdded on 2024/04/26
|14
|857
|463
Homework Assignment
AI Summary
This document presents a solved assignment focusing on the fundamentals of Operating Systems and Java Programming. Task 1 involves creating a menu-driven system simulating lift operations with floor selection and fire alarm functionalities, including input validation and error handling. The solution provides a detailed algorithm, Java source code, and screenshots of the program's output. Task 2 implements the NIM game, where a user plays against the computer, with the goal of forcing the opponent to take the last stone. The solution includes algorithms for both user-first and computer-first scenarios, Java code, and output screenshots, as well as added input validation to ensure robust gameplay. The document also includes references to external resources used in developing the solutions. Desklib provides this and other solved assignments to aid students in their studies.

Fundamentals of Operating Systems and
Java Programming
1
Java Programming
1
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Contents
Task 1:..............................................................................................................................................................3
Algorithm:....................................................................................................................................................3
Source code:.................................................................................................................................................4
Screenshots of the output:...........................................................................................................................6
Task 2...............................................................................................................................................................8
Algorithm:....................................................................................................................................................8
Coding screenshots:.....................................................................................................................................9
NIM game output:......................................................................................................................................12
Output after Adding input validation to your Nim program.......................................................................13
References......................................................................................................................................................14
List of Figures
Figure 1 Source code........................................................................................................................................4
Figure 2 welcome message with menu system................................................................................................6
Figure 3: error message for invalid selection....................................................................................................6
Figure 4 for fire alarm selection........................................................................................................................6
Figure 5 invalid floor selection..........................................................................................................................6
Figure 6 for floor number 15............................................................................................................................7
Figure 7 floor number 10 selection...................................................................................................................7
Figure 8 select fire alarm choice after 10-floor selection.................................................................................7
Figure 9 quit the system...................................................................................................................................7
Figure 10 source code screenshots...................................................................................................................9
Figure 11 NIM game output...........................................................................................................................12
Figure 12 Output after Adding input validation to your NIM program...........................................................13
2
Task 1:..............................................................................................................................................................3
Algorithm:....................................................................................................................................................3
Source code:.................................................................................................................................................4
Screenshots of the output:...........................................................................................................................6
Task 2...............................................................................................................................................................8
Algorithm:....................................................................................................................................................8
Coding screenshots:.....................................................................................................................................9
NIM game output:......................................................................................................................................12
Output after Adding input validation to your Nim program.......................................................................13
References......................................................................................................................................................14
List of Figures
Figure 1 Source code........................................................................................................................................4
Figure 2 welcome message with menu system................................................................................................6
Figure 3: error message for invalid selection....................................................................................................6
Figure 4 for fire alarm selection........................................................................................................................6
Figure 5 invalid floor selection..........................................................................................................................6
Figure 6 for floor number 15............................................................................................................................7
Figure 7 floor number 10 selection...................................................................................................................7
Figure 8 select fire alarm choice after 10-floor selection.................................................................................7
Figure 9 quit the system...................................................................................................................................7
Figure 10 source code screenshots...................................................................................................................9
Figure 11 NIM game output...........................................................................................................................12
Figure 12 Output after Adding input validation to your NIM program...........................................................13
2

Task 1:
Algorithm:
The algorithm describes the logic to solve a program [1]. The algorithm for the task 1 is shown below:
Step 1: Start the program
Step2: Print a welcome message
Step3: print menu system with “s/f/q” choice. ‘s' for the select floor, ‘f' for select fire alarm and ‘q' for quit
the system[3].
Step 4: Ask user to enter a choice
4.1 If user select choice‘s’ then ask user to enter the floor number
4.1.1. if floor number <1 and floor number >100 then print an error message
4.1.2 A. if current floor number is less than previously enter floor number then
Take lift in down direct up to entered floor number
B. else take lift in upward direction up to entered floor number
4.2. if user select choice ’f’ then
4.2.1 check lift is on which floor if it is not on the ground floor then take the lift down to the ground
floor and print a dangerous message.
4.2.2. else print a dangerous message.
4.3 if user select ‘q’ choice then exit the system.
Step 5: end the program
3
Algorithm:
The algorithm describes the logic to solve a program [1]. The algorithm for the task 1 is shown below:
Step 1: Start the program
Step2: Print a welcome message
Step3: print menu system with “s/f/q” choice. ‘s' for the select floor, ‘f' for select fire alarm and ‘q' for quit
the system[3].
Step 4: Ask user to enter a choice
4.1 If user select choice‘s’ then ask user to enter the floor number
4.1.1. if floor number <1 and floor number >100 then print an error message
4.1.2 A. if current floor number is less than previously enter floor number then
Take lift in down direct up to entered floor number
B. else take lift in upward direction up to entered floor number
4.2. if user select choice ’f’ then
4.2.1 check lift is on which floor if it is not on the ground floor then take the lift down to the ground
floor and print a dangerous message.
4.2.2. else print a dangerous message.
4.3 if user select ‘q’ choice then exit the system.
Step 5: end the program
3
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Source code:
Figure 1 Source code
4
Figure 1 Source code
4
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

5

Screenshots of the output:
Figure 2 welcome message with menu system
Figure 3: error message for invalid selection
Figure 4 for fire alarm selection
Figure 5 invalid floor selection
6
Figure 2 welcome message with menu system
Figure 3: error message for invalid selection
Figure 4 for fire alarm selection
Figure 5 invalid floor selection
6
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Figure 6 for floor number 15
Figure 7 floor number 10 selection
Figure 8 select fire alarm choice after 10-floor selection
Figure 9 quit the system
7
Figure 7 floor number 10 selection
Figure 8 select fire alarm choice after 10-floor selection
Figure 9 quit the system
7
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Task 2
Algorithm:
Step1: start the program
Step 2: print N I M game welcome message
Step3: Ask user to enter the number of starting stones
3.1. if stones< 1 print error message and go to step 3
Step4: (A) Ask user whether he wants to go first or not
4.1.1. if yes ask the user to enter how many stones he wants to remove 1 or 2
4.1.1.1. if choice=1 remove one stone
4.1.1.2 if choice=2 remove 2 stones
a. if remaining stone < 0 generate an error message and add 2 stones that were
removed.
b. else repeat 4.1.1
4.1.2. print number of stones left.
4.1.2.1. if stones==0 print “computer wins” message
4.1.3. ask the user whether he wants to play again or not
a. if yes go to step 3
b. exit the system
(B) 4.1.1. Calculate remainder by dividing number of stones by 3
a. if remain ==0 remove 2 stones and print a message that shows “The computer removes 2
stones”
b. else removes 1 stones and print a message that shows “The computer removes 1 stones”
4.1.2. Print number of stones left.
a. if stones==0 print computer wins message
Ask user whether he wants to play again or not if yes go to step 3 else exit the system.
Step 5: END the program
8
Algorithm:
Step1: start the program
Step 2: print N I M game welcome message
Step3: Ask user to enter the number of starting stones
3.1. if stones< 1 print error message and go to step 3
Step4: (A) Ask user whether he wants to go first or not
4.1.1. if yes ask the user to enter how many stones he wants to remove 1 or 2
4.1.1.1. if choice=1 remove one stone
4.1.1.2 if choice=2 remove 2 stones
a. if remaining stone < 0 generate an error message and add 2 stones that were
removed.
b. else repeat 4.1.1
4.1.2. print number of stones left.
4.1.2.1. if stones==0 print “computer wins” message
4.1.3. ask the user whether he wants to play again or not
a. if yes go to step 3
b. exit the system
(B) 4.1.1. Calculate remainder by dividing number of stones by 3
a. if remain ==0 remove 2 stones and print a message that shows “The computer removes 2
stones”
b. else removes 1 stones and print a message that shows “The computer removes 1 stones”
4.1.2. Print number of stones left.
a. if stones==0 print computer wins message
Ask user whether he wants to play again or not if yes go to step 3 else exit the system.
Step 5: END the program
8

Coding screenshots:
Figure 10 source code screenshots
9
Figure 10 source code screenshots
9
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

10
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

11

NIM game output:
Figure 11 NIM game output
12
Figure 11 NIM game output
12
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 14
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.