Task I – Elevator Simulation Algorithm and Program
TASK-1 (Elevator Simulator)
Algorithm
START
- Function main()
- Print menu
- Take user choice in ‘choice’ and convert to uppercase
- If choice is ‘S’ then goto next step else goto 24
- Call SelectFloor()
- If called function returns 1 goto next step else 2
- If floor value entered by user is 13 print message of ‘12a’
- If prevFloor value Greater than floor value goto next step else goto 15
- Print going down
- While prevFloor value >= floor value, else goto step 15
- Time delay of 500 milliseconds
- If prevFloor is not 13 print prevFloor else print ‘12a’
- Decrement prevFloor by 1
- Goto step 10
- Increment prevFloor by 1
- Print going up
- While prevFloor value <= floor value, else goto step 22
- Time delay of 500 milliseconds
- If prevFloor is not 13 print prevFloor else print ‘12a’
- Increment prevFloor by 1
- Goto step 17
- Decrement prevFloor by 1
- Goto step 2
- If user choice is ‘F’ Call function FireAlarm()
- Goto step 2
- If user choice is ‘Q’ print QUIT message
- Goto step 30
- Else print ERROR message
- Goto step 2
- End main() function
Function SelectFloor()
- Take user input of floor in floortemp
- If 100 < floortemp > 1, print error message
- goto step 1.
- If floortemp = prevFloor value goto next step else step 7
- print ‘same floor’ message
- return 0 to calling function
- Set floor to floortemp value
- return 1 to calling function.
- End function SelectFloor().
Function FireAlarm()
- Function FireAlarm()
- print danger message
- If prevFloor value is not 1 goto next step, else goto step 10
- Print “going down message”
- While prevFloor > or = 1, goto next step else step 9
- Delay 500 mSecs
- Print prevFloor value and decrement prevFloor by 1
- goto step 5
- Increment prevFloor value by 1
- End function FireAlarm()
STOP
Source code
import java.io.*;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
/*@author: Gurpreet Singh
*@authorID: MIT 173258
*Class purpose: The purpose of this class is to simulate an Elevator system. The system takes in user input for Floor numbers they wish to travel,
*checks for validity of inputs and travels accordingly. It also allows users to raise Fire Alarms and quit the system. The system also omits floor #13.
*/
class Elevator
{
static int floor=1;
public static void main(String []args)throws IOException
{
Scanner sc = new Scanner(System.in);
int i;
char choice=’S’;
System.out.println(“Welcome to MIT173258: Gurpreet Singh’s elevator simulator!n”);
while(choice!=’Q’)
{
System.out.print(“nPress S: Select a floornPress F: Raise fire alarmnPress Q: Quit elevator systemnPress now… “);
choice=sc.next(“.”).charAt(0);
choice=Character.toUpperCase(choice);
switch(choice)
{
case ‘S’: if(SelectFloor()==1)
{
if(floor==13)
{
System.out.println(“We do not have a 13th floor. You are being taken to floor 12A.”);
}
if(prevFloor>floor)
{
System.out.println(“Going down…”);
while(prevFloor>=floor)
justify;”> {
try
{
Thread.sleep(500);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
if(prevFloor!=13)
System.out.println(prevFloor+”…”);
else
System.out.println(“12A…”);
prevFloor–;
}
prevFloor++;
System.out.println(“Ding!!”);
}
else
{
System.out.println(“Going up…”);
while(prevFloor<=floor)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
if(prevFloor!=13)
System.out.println(prevFloor+”…”);
else
System.out.println(“12A…”);
prevFloor++;
}
prevFloor–;
System.out.println(“Ding!!”);
}
}
break;
case ‘F’: FireAlarm();
break;
case ‘Q’: System.out.println(“Thank you!!”);
break;
default: System.out.println(“Invalid Entry. Try again!”);
}
}
}
public static int SelectFloor()throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the floor that you would like to go to: “);
int floortemp=sc.nextInt();
if(floortemp<1 || floortemp>100)
{
System.out.println(“Invalid Selection. Floor value must be in the range 1-100. Try again!”);
return 0;
}
else if(floortemp==prevFloor)
{
System.out.println(“You are already on this floor. Try again!”);
return 0;
}
else
{
floor=floortemp;
return 1;
}
}
public static void FireAlarm()
{
System.out.println(“Danger!! You must exit the building now.”);
if(prevFloor!=1)
{
Task II – Game of NIM Algorithm and Program
System.out.println(“Going down..”);
while(prevFloor>=1)
{
try
{
Thread.sleep(500);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
if(prevFloor!=13)
System.out.println(prevFloor+”…”);
else
System.out.println(“12A…”);
prevFloor–;
}
prevFloor++;
System.out.println(“Ding!!”);
}
}
}
TASK-2 (NIM Game)
Algorithm
START
- Function main().
- User enters number of stones.
- If number of stones LESS or EQUALS TO 0, goto step 2, else step 4.
- Set value of remaining to number of stones
- User enters first going choice in ch.
- If choice is ‘Y’ or ‘YES’, set turn to 1 and Call user() function and goto step 7 else step 8.
- Print remaining stones.
- If user enters neither Y, YES, N or NO, goto step 5.
- Until remains value reaches 0, goto step 10.
- Delay 500millisecs, Set turn to ‘c’ and call function computer().
- Print remaining stones.
- If remaining stones is 0, goto step 17.
- Set turn to ‘u’.
- Call user()
- Print remaining stones.
- If turn is ‘c’, print ‘Congratulation’ message, else print ‘Computer Won’.
- User input choice to continue or not.
- If user enters ‘Y’ or ‘YES’ goto step 3. Else goto step 19.
- If user enters N or NO, print goodbye message and goto step 21.
- Otherwise, goto step 17.
- End of main()
Function user()
- Function user().
- User input number of stones to remove.
- If remaining stones is 1 and also user enters 2, set remove to 1 and print ‘removing 1’ message.
- If user enters 1 or 2 set remaining <= remaining-remove, goto step 6
- If user enters other than values 1 or 2, print error message and goto step 2.
- End function user().
Function computer()
- Function computer().
- If remaining’s value is divisible by 3, set remove to 2 else set remove to 1.
- Set remaining<=remaining-remove
- End function computer().
STOP
Source code
import java.io.*;import java.io.*;
import java.util.Scanner;
/**
/*@author: Gurpreet singh
*@authorID: MIT 173258
*The purpose of this class is to simulate the NIM game.
*The player can enter the number of stones to start off the game and who should start the game.
*The one to remove the stone at last loses.
*Computer uses the optimal playing strategy to play turns.
*/
public class NIM
{
static int remaining=0;
public static void user()
{
Scanner sc = new Scanner(System.in);
while(true)
{
int remove;
System.out.println(“Enter the number of stones to remove (1 or 2): “);
remove=sc.nextInt();
if(remove==2 && remaining==1)
{
System.out.println(“You do not have 2 stones left.nRemoving 1 stone.”);
remove=1;
remaining-=remove;
break;
}
if(remove==1 || remove==2)
{
remaining-=remove;
System.out.println(“Player removes “+remove+” stones.”);
break;
}
else
System.out.println(“You can remove either 1 or 2 stones only. Enter again!”);
}
}
public static void computer()
{
int remove;
if(remaining%3==0)
{
remove=2;
}
else
{
remove=1;
}
remaining-=remove;
System.out.println(“Computer removes “+remove+” stones.”);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int stones=0, flag=1;
String choice=”YES”;
char turn=’u’;
System.out.println(“!! THE GAME OF NIM !!n”);
while(flag==1)
{
stones=0;
while(stones<=0)
{
System.out.println(“Enter the number of stones: “);
stones=sc.nextInt();
if(stones<=0)
System.out.println(“Number of stones cannot be 0 or negative!! Try again.”);
else
{
remaining=stones;
break;
}
}
System.out.print(“Would you like to go first (YES,Y/NO,N)? Enter… “);
choice=sc.next().toUpperCase();
if(choice.equals(“YES”) || choice.equals(“Y”))
{
turn=’u’;
user();
System.out.println(“Number of stones left is “+remaining);
}
else if(!choice.equals(“YES”) && !choice.equals(“Y”) && !choice.equals(“NO”) && !choice.equals(“N”))
{
System.out.println(“Invalid entry. Try again!!”);
continue;
}
while(remaining!=0)
{
turn=’c’;
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
computer();
System.out.println(“Number of stones left is “+remaining);
if(remaining==0)
break;
turn=’u’;
user();
System.out.println(“Number of stones left is “+remaining);
}
if(turn==’c’)
System.out.println(“nCONGRATULATIONS!! YOU WON.n”);
else
System.out.println(“nCOMPUTER WON!!n”);
while(true)
{
System.out.print(“Would you like to go continue (YES,Y/NO,N)? Enter… “);
choice=sc.next().toUpperCase();
if(choice.equals(“YES”) || choice.equals(“Y”))
{
flag=1;
break;
}
else if(choice.equals(“NO”) || choice.equals(“N”))
{
System.out.println(“Thank You for playing NIM!! Have a nice day.”);
flag=0;
break;
}
else
{
System.out.println(“Invalid Entry!! Enter again.”);
}
}
}
}
}
Output