Objective: Use Java 7 or 8 (JDK 1.7 or 1.8) and arrays to take an input file, re
ID: 3597530 • Letter: O
Question
Objective: Use Java 7 or 8 (JDK 1.7 or 1.8) and arrays to take an input file, read and process the data in a specific way (outlined in the prompt) and put results in an output file.
Use of standard libraries is restricted to standard I/O calls and standard math functions, etc. In other words, you can't use ArrayList.
Use named files to handle I/O. No hardcoded file names. Enter file names as command line prompts. Do NOT use GUI/console input of data.
Output files must: Contain the input as well as contain answers to the required input. Be user friendly with additional labels, lines, and white space. Have statistical information as needed.
Prompt:
The diagram below is an example of a directed graph. Each edge has an arrow denoting its direction. Node 2 is considered adjacent to node 1 if there is a directed edge from 1 to 2.
1 will only be adjacent to 2 if there is also an edge from 2 to 1.
It is possible to associate a matrix called the adjacency matrix with a graph. Adj[i,j] = 1 if and only if there is an edge from node i to node j in the graph. If Adj[i,j] = 0 then there is no edge from node i to node j.
We will assume that our adjacency matrices contain only 1’s or 0’s. In a graph, one is usually interested in the possible paths from one node to another. The ones which are the most useful are the ones which do not contain loops (cycles), i.e.. do not visit a node more than once. The node sequence 1 2 4 is an example of a path with no loops. 1 2 2 4 1 3 is an example of a path with a cycle and a loop. An allowable exception is for the starting and ending node to be the same. 1 2 1 1 is OK but not 1 2 1 2 4 1.
Write a program to read in the number of nodes in the graph and the corresponding adjacency matrix, one row at a time. Copy the adjacency matrix to your output file. The program should use recursion to find and list all possible non-looping paths between all possible pairs of nodes. It is ok if the start and end nodes are the same but otherwise the nodes should not repeat within the path. If no path exists for a particular pair of nodes then print 'No Path Found'. Check out all possible paths in each graph. The recursion is the most important part of this assignment. Use an array to store the adjacency matrix. You may not use ArrayLists. See below for the required input.
4
0 1 1 0
1 1 1 1
1 0 0 0
1 1 0 1
6
0 1 0 1 1 0
1 0 0 1 1 0
0 0 1 0 0 1
0 0 0 0 1 0
1 0 0 0 0 0
0 0 1 0 0 1
3
0 1 1
1 0 1
1 1 0
"To Nodes" 0 110 Adjacency "From11 1 1 Matrix Nodes" 1 0 0 0 4Explanation / Answer
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class ElevatorSimulation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter name of input file: ");
String fileName=input.nextLine();
ArrayList<Passenger> people=getListOfPassenger("data.txt");
if(people!=null)
{
simulate(people);
}
else
{
System.out.println("Error occured while reading input file.");
}
}
public static ArrayList<Passenger> getListOfPassenger(String fileName)
{
ArrayList<Passenger> arr=new ArrayList<>();
try
{
BufferedReader input=new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
while(input.ready())
{
String data=input.readLine();
if(!data.startsWith("//")&&data.length()>1)
{
Scanner scr=new Scanner(data);
String name=scr.next();
name=name.trim();
int entry=scr.nextInt();
int exit=scr.nextInt();
arr.add(new Passenger(name, entry, exit));
}
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
return arr;
}
public static void simulate(ArrayList<Passenger> list)
{
int numberOfPeopleRide=0;
int numberOfPeopleDoNotRide=0;
int emptyCount=0;
int fullCount=0;
boolean direction=true; //for up true for down false
int targetFloor=-1; //target of lift
int currentFloor=1; //start the lift from here
//stack for elevator
Stack<Passenger> elevator=new Stack<Passenger>();
// temporary stack
Stack<Passenger> temp=new Stack<>();
//keep track of number of people with exitfloor
int target[]=new int[10];
while(!list.isEmpty()||!elevator.isEmpty())
{
if(elevator.isEmpty())
{
System.out.println("Elevator is empty.");
emptyCount++;
}
//first check to see if there people who want to exit at current floor
if(currentFloor==targetFloor)
{
//if someone wants to exit
//remove others in front of them
while(target[currentFloor-1]!=0)
{
//remove from elevator
Passenger r=elevator.pop();
//check if it is the passengers exit
if(r.getExitFloor()==currentFloor)
{
target[currentFloor-1]--;
System.out.println(r.getName()+" exit. His/her exit floor is--->"+r.getExitFloor()+" His/her total number of temporary exits----> "+r.getTemporaryExit());
}
else
{
temp.push(r);
r.temporaryExit();
}
}
//load the elevator with temporary exit person
while(!temp.isEmpty())
{
elevator.push(temp.pop());
}
}
//now come to those who were waiting for lift
if(elevator.size()==5)
{
System.out.println("Elevator is full.");
fullCount++;
//these people have to travel without elevator
while(!list.isEmpty()&&list.get(0).getEnteryFloor()==currentFloor)
{
System.out.println(list.get(0).getName()+" can't get into elevator so he/she take stairs. His/Her destination is floor: "+list.get(0).getExitFloor());
numberOfPeopleDoNotRide++;
list.remove(0);
}
}
else
{
//fill until elevator become full or waiting people for this floor become zero
while(elevator.size()<5&&!list.isEmpty()&&list.get(0).getEnteryFloor()==currentFloor)
{
Passenger p=list.remove(0);
elevator.push(p);
numberOfPeopleRide++;
target[p.getExitFloor()-1]++;
}
//if elevator become full and waiting people are left then they have to take stairs
if(elevator.size()==5&&!list.isEmpty()&&list.get(0).getEnteryFloor()==currentFloor)
{
fullCount++;
System.out.println("Elevator is full.");
//these people have to travel without elevator
while(!list.isEmpty()&&list.get(0).getEnteryFloor()==currentFloor)
{
System.out.println(list.get(0).getName()+" can't get into elevator so he/she takes stairs. His/Her destination is floor: "+list.get(0).getExitFloor());
numberOfPeopleDoNotRide++;
list.remove(0);
}
}
}
//find target floor
if(direction)
{
if(currentFloor==5)
{
direction=false;
for(int i=4;i>=0;i--)
{
if(target[i]!=0)
{
targetFloor=i+1;
break;
}
}
currentFloor--;
}
else
{
for(int i=currentFloor-1;i<5;i++)
{
if(target[i]!=0)
{
targetFloor=i+1;
break;
}
}
currentFloor++;
}
}
else
{
if(currentFloor==1)
{
direction=true;
for(int i=0;i<5;i++)
{
if(target[i]!=0)
{
targetFloor=i+1;
break;
}
}
currentFloor++;
}
else
{
for(int i=currentFloor-1;i>=0;i--)
{
if(target[i]!=0)
{
targetFloor=i+1;
break;
}
}
currentFloor--;
}
}
}
System.out.println(" ");
System.out.println("Total number of passenger who took elevator: "+numberOfPeopleRide);
System.out.println("Total number of passenger who don't take elevator: "+numberOfPeopleDoNotRide);
System.out.println("Number of time elevator become empty: "+emptyCount);
System.out.println("Number of time elevator become full: "+fullCount);
}
}
------------------------------------------------------------------------------------------------------
Passenger.java
------------------------------------------------------------
public class Passenger {
String name;
int enteryFloor;
int exitFloor;
int temporaryExit;
public Passenger(String name, int entryFloor, int exitFloor) {
this.name = name;
this.enteryFloor =entryFloor;
this.exitFloor = exitFloor;
this.temporaryExit=0;
}
public String getName() {
return name;
}
public int getEnteryFloor() {
return enteryFloor;
}
public int getExitFloor() {
return exitFloor;
}
public int getTemporaryExit() {
return temporaryExit;
}
public void temporaryExit()
{
temporaryExit = temporaryExit+1;
}
}
------------------------------------------------------------------------------------------------
Stack.java
-----------------------------------------------------
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Stack<Item> implements Iterable<Item> {
private int n; // size of the stack
private Node first; // top of stack
// helper linked list class
private class Node {
private Item item;
private Node next;
}
public Stack() {
first = null;
n = 0;
}
public boolean isEmpty() {
return first == null;
}
public int size() {
return n;
}
public void push(Item item) {
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
n++;
}
public Item pop() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Item item = first.item; // save item to return
first = first.next; // delete first node
n--;
return item; // return the saved item
}
public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return first.item;
}
public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}
public Iterator<Item> iterator() {
return new ListIterator();
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
}
----------------------------------------------------------------------------------
Output
------------------------------
Enter name of input file: data.txt
Elevator is empty.
Fred exit. His/her exit floor is--->2 His/her total number of temporary exits----> 0
Ian exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Babs exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Tip exit. His/her exit floor is--->4 His/her total number of temporary exits----> 1
Tom exit. His/her exit floor is--->4 His/her total number of temporary exits----> 2
Barb exit. His/her exit floor is--->5 His/her total number of temporary exits----> 1
Eli exit. His/her exit floor is--->5 His/her total number of temporary exits----> 0
Hal exit. His/her exit floor is--->2 His/her total number of temporary exits----> 0
Tina exit. His/her exit floor is--->2 His/her total number of temporary exits----> 0
Deb exit. His/her exit floor is--->1 His/her total number of temporary exits----> 0
Ted exit. His/her exit floor is--->1 His/her total number of temporary exits----> 1
Joe exit. His/her exit floor is--->1 His/her total number of temporary exits----> 1
Elevator is full.
Ada can't get into elevator so he/she takes stairs. His/Her destination is floor: 5
Elevator is full.
Dina can't get into elevator so he/she take stairs. His/Her destination is floor: 3
Bob can't get into elevator so he/she take stairs. His/Her destination is floor: 5
Sal exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Jim exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Ann exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Vic exit. His/her exit floor is--->4 His/her total number of temporary exits----> 1
Flo exit. His/her exit floor is--->5 His/her total number of temporary exits----> 2
Wil exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Lee exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Isa exit. His/her exit floor is--->1 His/her total number of temporary exits----> 0
Gia exit. His/her exit floor is--->1 His/her total number of temporary exits----> 1
Elevator is full.
Nan can't get into elevator so he/she takes stairs. His/Her destination is floor: 3
Lou exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Abe exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Elevator is full.
Moe can't get into elevator so he/she takes stairs. His/Her destination is floor: 5
Ina exit. His/her exit floor is--->4 His/her total number of temporary exits----> 0
Al exit. His/her exit floor is--->4 His/her total number of temporary exits----> 1
Yul exit. His/her exit floor is--->4 His/her total number of temporary exits----> 1
Bif exit. His/her exit floor is--->5 His/her total number of temporary exits----> 1
Lew exit. His/her exit floor is--->5 His/her total number of temporary exits----> 2
Jan exit. His/her exit floor is--->1 His/her total number of temporary exits----> 0
Mia exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Lisa exit. His/her exit floor is--->4 His/her total number of temporary exits----> 1
Pat exit. His/her exit floor is--->5 His/her total number of temporary exits----> 0
Cal exit. His/her exit floor is--->5 His/her total number of temporary exits----> 2
Tess exit. His/her exit floor is--->2 His/her total number of temporary exits----> 1
Don exit. His/her exit floor is--->1 His/her total number of temporary exits----> 0
Nora exit. His/her exit floor is--->1 His/her total number of temporary exits----> 1
Sam exit. His/her exit floor is--->5 His/her total number of temporary exits----> 0
Liv exit. His/her exit floor is--->4 His/her total number of temporary exits----> 0
Elevator is empty.
Elevator is empty.
Elevator is empty.
Nina exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Elevator is empty.
Elevator is empty.
Mary exit. His/her exit floor is--->2 His/her total number of temporary exits----> 0
Elevator is empty.
Ed exit. His/her exit floor is--->2 His/her total number of temporary exits----> 0
Tad exit. His/her exit floor is--->3 His/her total number of temporary exits----> 0
Ali exit. His/her exit floor is--->4 His/her total number of temporary exits----> 0
Sue exit. His/her exit floor is--->5 His/her total number of temporary exits----> 0
Total number of passenger who took elevator: 44
Total number of passenger who don't take elevator: 5
Number of time elevator become empty: 7
Number of time elevator become full: 4
Process finished with exit code 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.