Java, Military Format using "/" and "%" Operator!!
Write a program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between two times. Supply your own test program. Here are two sample runs. User inputs are the numbers.
Please enter the first time: 0900
Please enter the second time: 1730
8 hours 30 minutes
Please enter the first time: 1730
Please enter the second time: 0900
15 hours 30 minutes
This is what i have so far...
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in) ;
TimeInterval timeInterval = new TimeInterval(time1, time2);
System.out.println("Enter two times in military format, using 4 digits.");
int time1 = scanner.nextInt() ;
int time2 = scanner.nextInt() ;
System.out.println("The number of hours is.:");
System.out.println(timeInterval.getHours(time1, time2));
System.out.println("The number of minutes is.:");
System.out.println(timeInterval.getMinutes(time1, time2));
}
}
public class TimeInterval
{
int time1 = scanner.nextInt() ;
int time2 = scanner.nextInt() ;
private int hr;
private int min;
hr = ((time2 - time1)/100)-(((time2 - time1)/100) % 100);
public int getHours(){
return hr;
}
public int getMinutes(){
return min;
}
}
The answer to your question
I made a "rough cut" program. This question is asked in java concepts book and it should be solved without an if statement because the problem was in book before we even know what an "if" statement is. The key to this is using the Math.max method call. Well here is the code below. You will have to a litle translation becuase I am to lazy to fix it. It took me a couple of days to solve becuase i had no clue how to atttack it without an if statement, but I guess the max method might actually use one. Any ways here is the answer and ive test it and it works. Good luck. Oh and you have to put in actually valid military time, i didnt put any boundary conditions
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package book;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Owner
*/
public class MilitaryFormat {
public static void main ( String[] args )
{
Logger.global.setLevel(Level.OFF);
Scanner scanner = new Scanner(System.in);
System.out.println("Enter two times in military format, using 4 digits.");
String time1 = scanner.nextLine();
String time2 = scanner.nextLine();
double x = Double.parseDouble(time1.substring(0,2));
double xx = Double.parseDouble(time1.substring(2,4))/60;
double y = Double.parseDouble(time2.substring(0,2));
double yy = Double.parseDouble(time2.substring(2,4))/60;
Logger.global.info("x: "+x);
Logger.global.info("xx: "+xx);
Logger.global.info("y: "+y);
Logger.global.info("yy: "+yy);
double xxx = x+xx;
double yyy = y + yy;
Logger.global.info("xxx: "+xxx);
Logger.global.info("yyy: "+yyy);
double z = (yyy-xxx)+((Math.max(xxx,yyy)-yyy)/(xxx-yyy))*24;
Logger.global.info(""+z);
int hours = (int)z;
int minutes = (int)Math.round(((z - (int)z)*60));
System.out.println("The difference in times is "+hours+" hours and "+minutes+" minutes");
}
}