Computer Science
The manager of a company wants to analyze 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 logoff 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 hours.
Design a program:
(a) To find the duration for which each user 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 and some random data.
Sample Data
Input:
Number of users: 3
User Identification
Login Time and Date | Logout Time and Date | ||
---|---|---|---|
20:10 | 20-12 | 2:50 | 21-12 |
12:30 | 20-12 | 12:30 | 21-12 |
16:20 | 20-12 | 16:30 | 20-12 |
Output:
User Identification | Login Time and Date | Logout Time and Date | Duration Hours:Minutes | ||
---|---|---|---|---|---|
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 longest duration:
173 | 12:30 | 20-12 | 12:30 | 21-12 | 24:00 |
Java
Java Arrays
8 Likes
Answer
import java.util.Scanner;
public class KboatMachineUtilization
{
public static void main(String args[]) {
final int MINS_IN_DAY = 1440;
final int MINS_IN_HOUR = 60;
Scanner in = new Scanner(System.in);
System.out.print("Enter Number of Users: ");
int n = in.nextInt();
in.nextLine();
if (n > 100 || n < 1) {
System.out.println("Invalid Input!");
System.out.println("No of users must be between 1 and 100");
return;
}
String records[][] = new String[n][6];
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i = 0; i < n; i++) {
System.out.println("Enter record of user " + (i+1) + ": ");
System.out.print("Enter User Identification: ");
records[i][0] = in.nextLine();
System.out.print("Enter Login Time(hh:mm): ");
records[i][1] = in.nextLine();
System.out.print("Enter Login Date(dd-mm): ");
records[i][2] = in.nextLine();
System.out.print("Enter Logout Time(hh:mm): ");
records[i][3] = in.nextLine();
System.out.print("Enter Logout Date(dd-mm): ");
records[i][4] = in.nextLine();
}
System.out.println();
int longIdx = 0;
int longDuration = 0;
for (int i = 0; i < n; i++) {
int duration = 0;
int tempIdx = records[i][1].indexOf(':');
int loginHr = Integer.parseInt(records[i][1].substring(0, tempIdx));
int loginMin = Integer.parseInt(records[i][1].substring(tempIdx + 1));
tempIdx = records[i][3].indexOf(':');
int logoutHr = Integer.parseInt(records[i][3].substring(0, tempIdx));
int logoutMin = Integer.parseInt(records[i][3].substring(tempIdx + 1));
int m1 = loginHr * MINS_IN_HOUR + loginMin;
int m2 = logoutHr * MINS_IN_HOUR + logoutMin;
//If login & logout is on the same day
if (records[i][2].equals(records[i][4])) {
duration = m2 - m1;
}
else {
int daysDiff = 0;
tempIdx = records[i][2].indexOf('-');
int loginDay = Integer.parseInt(records[i][2].substring(0, tempIdx));
int loginMonth = Integer.parseInt(records[i][2].substring(tempIdx + 1));
tempIdx = records[i][4].indexOf('-');
int logoutDay = Integer.parseInt(records[i][4].substring(0, tempIdx));
int logoutMonth = Integer.parseInt(records[i][4].substring(tempIdx + 1));
//If login & logout is in the same month
if (loginMonth == logoutMonth) {
daysDiff = logoutDay - loginDay - 1;
}
else {
daysDiff = monthDays[loginMonth - 1] - loginDay + logoutDay - 1;
}
duration = (MINS_IN_DAY - m1) + m2 + daysDiff * MINS_IN_DAY;
}
if (duration > longDuration) {
longDuration = duration;
longIdx = i;
}
int durHr = duration / 60;
int durMin = duration % 60;
records[i][5] = (durHr == 0 ? "00" : durHr)
+ ":"
+ (durMin == 0 ? "00" : durMin);
}
System.out.println("User\t\tLogin\t\tLogout\t\tDuration");
System.out.println("Identification\tTime & Date\tTime & Date\tHours:Minutes");
for (int i = 0; i < n; i++) {
for (int j = 0; j < 6; j++) {
System.out.print(records[i][j] + "\t");
}
System.out.println();
}
System.out.println();
System.out.println("The user who logged in for longest duration:");
for (int j = 0; j < 6; j++) {
System.out.print(records[longIdx][j] + "\t");
}
}
}
Output
Answered By
3 Likes
Related Questions
Numbers have different representations depending on the bases on which they are expressed. For example, in base 3, the number 12 is written as 110 (1 x 32 + 1 x 31 + 0 x 30) but base 8 it is written as 14 (1 x 81 + 4 x 80).
Consider for example, the integers 12 and 5. Certainly these are not equal if base 10 is used for each. But suppose, 12 was a base 3 number and 5 was a base 6 number then, 12 base 3 = 1 x 31 + 2 x 30, or 5 base 6 or base 10 (5 in any base is equal to 5 base 10). So, 12 and 5 can be equal if you select the right bases for each of them.
Write a program to input two integers x and y and calculate the smallest base for x and smallest base for y (likely different from x) so that x and y represent the same value. The base associated with x and y will be between 1 and 20 (both inclusive). In representing these numbers, the digits 0 to 9 have their usual decimal interpretations. The upper case letters from A to J represent digits 10 to 19 respectively.
Test your program for the following data and some random data.
Sample Data
Input:
x=12, y=5Output:
12 (base 3)=5 (base 6)Input:
x=10, y=AOutput:
10 (base 10)=A (base 11)Input:
x=12, y=34Output:
~~12 (base 8) = 34 (base 2)~~
12 (base 17) = 34 (base 5)
[∵ 34 (base 2) is not valid as only 0 & 1 are allowed in base 2]Input:
x=123, y=456Output:
123 is not equal to 456 in any base between 2 to 20Input:
x=42, y=36Output:
42 (base 7) = 36 (base 8)Write a program to accept a date in the string format dd/mm/yyyy and accept the name of the day on 1st of January of the corresponding year. Find the day for the given date.
Example:
Input:
Date: 5/7/2001
Day on 1st January : MONDAYOutput:
Day on 5/7/2001 : THURSDAYRun the program on the following inputs:
Input Date Day on 1st January Output day for 04/9/1998 THURSDAY FRIDAY 31/8/1999 FRIDAY TUESDAY 06/12/2000 SATURDAY WEDNESDAY The program should include the part for validating the inputs namely the date and day on 1st January of that year.
A wondrous square is an n by n grid which fulfils the following conditions:
- It contains integers from 1 to n2, where each integer appears only once.
- The sum of integers in any row or column must add up to 0.5 x n x (n2 + 1).
For example, the following grid is a wondrous square where the sum of each row or column is 65 when n=5.
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 Write a program to read n (2 <= n <= 10) and the values stored in these n by n cells and output if the grid represents a wondrous square.
Also output all the prime numbers in the grid along with their row index and column index as shown in the output. A natural number is said to be prime if it has exactly two divisors. For example, 2, 3, 5, 7, 11 The first element of the given grid i.e. 17 is stored at row index 0 and column index 0 and the next element in the row i.e. 24 is stored at row index 0 and column index 1.
Test your program for the following data and some random data:
Input:
n = 416 15 1 2 6 4 10 14 9 8 12 5 3 7 11 13 Output:
Yes, it represents a wondrous squarePrime Row Index Column Index 2 0 3 3 3 0 5 2 3 7 3 1 11 3 2 13 3 3 15 0 1 Input:
n = 31 2 4 3 7 5 8 9 6 Output:
Not a wondrous squarePrime Row Index Column Index 2 0 1 3 1 0 5 1 2 7 1 1 Input:
n = 22 3 3 2 Output: Not a wondrous square
Prime Row Index Column Index 2 0 0 2 1 1 3 0 1 3 1 0 Write a program to input N and M number of names in two different single dimensional arrays A and B respectively, such that none of them have duplicate names. Merge the arrays A and B into a single array C, such that the resulting array is sorted alphabetically. Display all the three arrays.
Test your program for the following data and some random data:
Sample data:
Input:
Enter the names in array A, N = 2
Enter the names in array B, M = 3
First array: A
Suman
Anil
Second array: B
Usha
Sachin
JohnOutput:
Sorted Merged array: C
Anil
John
Sachin
Suman
Usha
Sorted First array: A
Anil
Suman
Sorted Second array: B
John
Sachin
Usha