### Examples of Integer arithmetic

• Example using integers: convert seconds to (hours, minutes, seconds)

• Problem description:

• Given n seconds

 Compute the number of hours, number of minutes and number of seconds in n seconds

• Example:

 ``` n = 15432 seconds 15432 = 4*(60*60) + 17*(60) + 12 = 4 hours + 17 minutes + 12 seconds ```

• Use a concrete example to discover the steps that need to be performed to solve the problem.

• Determine the steps that you must do to complete the task (solve the problem)

• Write down the steps in "pseudo code" (see below)

• Verify that the algorithm in pseudo code is correct,

 You can verify an algorithm by running the algorithm by hand with a small example

• After you have Verify that the algorithm in pseudo code is correct,

• Pseudo code:

• Pseudo code is a compact and informal high-level description of an algorithm

• Pseudo code uses the structural conventions of a programming language, but is intended for human reading (rather than machine reading).

• Pseudo code omits details that are not essential for human understanding of the algorithm, such as variable declarations

In other words:

 Pseudo code are commands that are easy for humans to follow (no complex descriptions) Pseudo code are not Java statements !!

• Pseudo code is made up by yourself

There is no "standard pseudo code"

• Requirement for pseudo code:

 An algorithm in pseudo code must be easily translated into any programming language

• Purpose: rapid prototyping algorithms

 An algorithm written in pseudo code is very easy to change You can correct errors easily in an algorithm written in pseudo code (Trust me, correcting errors in a large computer program is a pain - talk to someone in Microsoft....)

• Use a concrete example to determine the steps used to find (hours, minutes, seconds):

 ``` Let n = 15432 seconds 1. We can find the # seconds as follows: 257 <---- # minutes ------------- 60 / 15432 15420 -------- 12 <--- # seconds So: 15432 seconds = 257 minutes + 12 seconds Or: 15432 seconds = 15432 / 60 minutes + 15432 % 60 seconds 2. Next we convert the remaining 257 minutes into hours: 4 <--- # hours -------- 60 / 257 240 ---- 17 <--- # minutes Therefore: 257 minutes = 257 / 60 hours + 257 % 60 minutes ```

• Pseudo code:

 ``` input n; // n = total number of seconds seconds = n % 60; // computes seconds n = n / 60; // n is now = remaining minutes minutes = n % 60; // computes minutes n = n / 60; // n is now = remaining hours hours = n; ```

• Verify the algorithm (in pseudo code): (use a small example, e.g. n = 15432)

 ``` n = 15432; hours = 15432 / 3600 = 4 r = 15432 % 3600 = 1032 minutes = 1032 / 60 = 17 seconds = 1032 % 60 = 12 ```

• Write the algorithm in Java (translate from pseudo code):

 ``` import java.util.Scanner; public class Hours { public static void main(String[] args) { int n, r, hours, minutes, seconds; Scanner in = new Scanner(System.in); System.out.print("Enter # seconds: "); n = in.nextInt(); // in.nextInt() reads in an integer value seconds = n % 60; // computes seconds n = n / 60; // n is now = remaining minutes minutes = n % 60; // computes minutes n = n / 60; // n is now = remaining hours hours = n; System.out.print(n); System.out.print(" seconds = "); System.out.print(hours); System.out.print(" hours + "); System.out.print(minutes); System.out.print(" minutes+ "); System.out.print(seconds); System.out.println(" seconds."); } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Hours.java To run:          java Hours

• Programming example: tell the time of the day

• Preliminary:

• The method System.currentTimeMillis() will return the following value:

 System.currentTimeMillis() returns the number of milli seconds (1/1000 second) since midnight, Jan 1 1970 in GMT

• Problem description:

 Print the current time as: HH:MM:SS GMT

• Use a concrete example to determine the steps used to find (hours, minutes, seconds):

 ``` Let n = 100000000 millisec 1. Find total number of seconds since midnight, Jan 1 1970: n = n / 1000 = 100000000 / 1000 = 100000 (total # seconds) 2. Find seconds in current time: seconds = n % 60 = 100000000 % 1000 = 40 *** part of answer n = n / 60 = 100000 / 60 = 1666 (total # minutes) 3. Find minutes in current time: minutes = n % 60 = 1666 / 60 = 46 *** part of answer n = n / 60 = 1666 % 60 = 27 (total # hours) 3. Find hours in current time: hours = n % 24 = 27 % 24 = 3 *** part of answer ```

• Algorithm in pseudo code:

 ``` n = System.currentTimeMillis(); // n = total # milli sec n = n / 1000; // n is now = total # sec seconds = n % 60; // Compute seconds in current time n = n / 60; // n is now = total # minutes minutes = n % 60; // Compute minutes in current time n = n / 60; // n is now = total # hours hours = n % 24; // Compute hours in current time ```

• Java program:

 ``` public class ShowCurrentTime { public static void main(String[] args) { long nMillis; // We need long for accuracy !!! long n, hours, minutes, seconds; nMillis = System.currentTimeMillis(); n = nMillis/1000; // Total # Second; seconds = n % 60; // Seconds n = n / 60; // Total # minutes; minutes = n % 60; // Minutes n = n / 60; // Total # hours; hours = n % 24; // Hours System.out.print(hours); System.out.print(":"); System.out.print(minutes); System.out.print(":"); System.out.print(seconds); System.out.println(" GMT"); } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac ShowCurrentTime.java To run:          java ShowCurrentTime