ISC practical papers › ISC 2004 › Question 3
Machine Usage Problem
ISC 2004 Computer Science Practical, Question 3 · Simulation and records
The question
The manager of a company wants to analyse the machine usage from the records to find the utilization of the machine. He wants to know how long each user used the machine. When the user wants to use the machine he must login to the machine and after finishing the work he must log off the machine.
Each log record consists of: User identification number. Login time and date. Logout time and date. Time consists of: Hours Minutes
Date consists of: Day Month
You may assume all logins and logouts are in the same year and there are 100 users at the most. The time format is 24 hour machine hours and minutes.
Design a program: (a) To find the duration for which each user has logged. Output all records along with the duration in hours (format hours: minutes).
(b) Output the record of the user who logged for the
longest duration. You may assume that no user will
login for more than 48 hours.
Test your program for the following data values and some more random data.
SAMPLE DATA:
INPUT: Number of users: 3 USER LOGIN LOGOUT IDENTIFICATION TIME & DATE TIME & DATE 149 20:10 20-12 2:50 21-12 173 12:30 20-12 12:30 21-12 142 16:20 20-12 16:30 20-12
OUTPUT: USER LOGIN LOGOUT DURATION IDENTIFICATION TIME & DATE TIME & DATE HOURS:MINS 149 20:10 20-12 2:50 21-12 6:40 173 12:30 20-12 12:30 21-12 24:00 142 16:20 20-12 16:30 20-12 00:10 THE USER WHO LOGGED IN FOR THE LONGEST DURATION 173 12:30 20-12 12:30 21-12 24:00
Question: ISC 2004 Computer Science Paper 2 (Practical), Question 3, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class UserLog{
private int id[];
private String loginTime[], loginDate[], logoutTime[], logoutDate[];
int duration[], maxIndex;
private static int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
public UserLog(int id[],String loginTime[], String loginDate[], String logoutTime[],String logoutDate[]){
this.id=id;
this.loginTime=loginTime;
this.loginDate=loginDate;
this.logoutTime=logoutTime;
this.logoutDate=logoutDate;
duration = new int[id.length];
process();
}
private void process(){
String temp[];
int hr, min, dd,mm,finalMinutes, initialMinutes, delta, maxDelta=0;
for( int i=0; i<id.length; i++){
temp = loginTime[i].split(":");
hr=Integer.parseInt(temp[0]);
min=Integer.parseInt(temp[1]);
temp = loginDate[i].split("-");
dd=Integer.parseInt(temp[0]);
mm=Integer.parseInt(temp[1]);
initialMinutes = getMinutes(dd, mm, hr, min);
temp = logoutTime[i].split(":");
hr=Integer.parseInt(temp[0]);
min=Integer.parseInt(temp[1]);
temp = logoutDate[i].split("-");
dd=Integer.parseInt(temp[0]);
mm=Integer.parseInt(temp[1]);
finalMinutes = getMinutes(dd, mm, hr, min);
delta = finalMinutes-initialMinutes;
duration[i]=delta;
if(delta>maxDelta){
maxDelta=delta;
maxIndex=i;
}
}
}
private static int getMinutes(int dd, int mm, int hr, int min){
int days=dd;
for( int i=0; i<mm-1;i++){
days+=month[i];
}
return (days*24*60)+(hr*60)+min;
}
public String toString(){
String output="";
output+="USER\t\tLOGIN\t\tLOGOUT\tDURATION\n";
output+="IDENTIFICATION\tTIME & DATE\tTIME & DATE\tHOURS:MINS\n";
for( int i=0; i<id.length;i++){
output+=id[i]+"\t\t"+loginTime[i]+" "+loginDate[i]+"\t"+logoutTime[i]+" "+logoutDate[i]+"\t"+formatTime(duration[i])+"\n";
}
output+="THE USER WHO LOGGED IN FOR THE LONGEST DURATION\n";
output+=id[maxIndex]+"\t\t"+loginTime[maxIndex]+" "+loginDate[maxIndex]+"\t"+logoutTime[maxIndex]+" "+logoutDate[maxIndex]+"\t"+formatTime(duration[maxIndex])+"\n";
return output;
}
private String formatTime(int minutes){
int hr=minutes/60;
int min = minutes%60;
return hr+":"+min+(min<10?"0":"");
}
}
public class ISC2004Q3 {
public static void main(String[] args) {
Scanner in = new Scanner( System.in );
System.out.println("INPUT:");
System.out.print("Number of users: ");
int users = in.nextInt();
int id[] = new int[users];
String loginTime[] = new String[users];
String loginDate[] = new String[users];
String logoutTime[] = new String[users];
String logoutDate[] = new String[users];
System.out.println("USER\t\tLOGIN\t\tLOGOUT");
System.out.println("IDENTIFICATION\tTIME & DATE\tTIME & DATE");
for( int user=0; user<users; user++){
id[user] = in.nextInt();
loginTime[user] = in.next();
loginDate[user] = in.next();
logoutTime[user] = in.next();
logoutDate[user] = in.next();
}
UserLog log = new UserLog(id, loginTime, loginDate,logoutTime,logoutDate);
System.out.println("OUTPUT:\n"+log);
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.