Friday 01/23/2015

Time problem

Textbook

1. Introduction

2. Data and operators

3. Using objects

4. Defining classes (This is where we start for real.)

https://www.cs.indiana.edu/classes/c212-dgerman/spr2015/hwOne.html

http://www.cs.indiana.edu/classes/c211/assn3.html

prompt> java One
First time: 09:30
Second time: 11:12
1 hour and 42 minutes
prompt> java One
First time: 21:45
Second time: 22:08
0 hours and 23 minutes
prompt> java One
First time: 13:29
Second time: 08:28
18 hours and 59 minutes.
prompt> 

import java.util.Scanner; // where I can find Scanner objects 

class Program { // Program.java 
  public static void main(String[] args) {
    Scanner in; // I plan on getting a Scanner object 
    in = new Scanner(System.in); // reading from the keyboard
         // going to the store and buying an actual Scanner object 
    System.out.print("Enter data: "); 
    String line;
    line = in.nextLine(); // pretend the input is just digits
    System.out.println("You have typed:" + line); 
  } 
}

import java.util.Scanner; // where I can find Scanner objects 

class Program { // Program.java 
  public static void main(String[] args) {
    Scanner in; // I plan on getting a Scanner object 
    in = new Scanner(System.in); // reading from the keyboard
         // going to the store and buying an actual Scanner object 
    System.out.print("Enter data: "); 
    String line;
    line = in.nextLine(); // pretend the input is just digits
    System.out.println("You have typed:" + line); 
    String part;
    part = line.substring(3, 6); // a substring of three characters 
    int number;
    number = Integer.parseInt( part ); 
    System.out.println( number + 4 ); 
  } 
}

This is how to extract substrings: 

import java.util.Scanner; // where I can find Scanner objects 

class Program { // Program.java 
  public static void main(String[] args) {
    Scanner in; // I plan on getting a Scanner object 
    in = new Scanner(System.in); // reading from the keyboard
         // going to the store and buying an actual Scanner object 
    System.out.print("Enter data: "); 
    String line;
    line = in.nextLine(); // pretend the input is just digits
    System.out.println("You have typed:" + line); 
    String part;
    part = line.substring(3, 6); // a substring of three characters 
    int number;
    number = Integer.parseInt( part ); 
    System.out.println( number + 4 ); 
  } 
}

Let's work out a plan for how to calculate the difference of time:

Suppose the user enters "13:29" as the first time. 

I extract 13 and 29. 

I will convert this to 809 minutes. 

Suppose the second time entered is "08:28" by the user.

From it I extract 8 and 28. 

I will convert this to 508 minutes.

My question becomes: how much time between 809 and 508? 

The difference of time is: 

   (508 + 24 * 60 - 809) % (24 * 60) 

The answer here: 1139 minutes. 

> 1139 / 60
18 (hours)
> 1139 % 60
59 (minutes)


Suppose we calculate "09:30" to "11:12" then the difference is: 

   (672 + 24 * 60 - 570) % (24 * 60) 

That's 102 mminutes. 

> 102 / 60
1 (hours)
> 102 % 60
42 (minutes)


Now it's clear that I am using one and the same formula. 

Reminder: 

   13 % 18 == 13

   21 % 18 ==  3