Jump to content

Shorter method of making an elapsed time calculator.

- - - - -

  • Please log in to reply
5 replies to this topic

#1
AntiSmurffette

AntiSmurffette

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts

Quote

"What comes 13 hours after 4 o'clock? Create an ElapsedTimeCalculator application that prompts the user for a starting hour, whether it is am or pm, and the number of elapsed hours. The application then displays the time after that many hours have passed." (Guide to Programming in Java, Java 2 platform standard edition 5)

Application output should look similar to:

Enter the starting hour: 7
Enter am or pm: pm
Enter the number of elapsed hours: 10
The time is: 5:00 am

I have found what seems to be a decent solution, but I'm pretty sure it could be done in less characters even though I haven't been able to find a way that works all the time. Any one else have any advice or tips that would make my future programs less bulky and more easy to read?


//elena 

import java.util.*;

public class ex11

{

public static void main (String args [])

{

Scanner input = new Scanner (System.in);

String Grr;


int time;

int elapsed = 0;

final String AfterNoon = "pm";

final String Morning = "am";


System.out.print ("Enter am or pm: ");

Grr = input.nextLine();

System.out.print ("Enter the starting hour: ");

time = input.nextInt ();


if (time <= 12)

{System.out.print ("Enter the number of elapsed hours: ");

elapsed = input.nextInt ();}

else

{System.out.println ("I don't know how to use that time keeping system!");}


//if its the afternoon it will change it to the 24 hour format

if (Grr.equalsIgnoreCase(AfterNoon))

{

time = time + 12;

time = time + elapsed;


if (time > 24)

{Grr = "am";

time = time - 24;}


else

{time = time - 12;

Grr = "pm";}

System.out.println ("The time is: " + time + ":00 " + Grr);

}


else if (Grr.equalsIgnoreCase(Morning))

{time = time + elapsed;

if (time > 11)

{Grr = "pm";

time = time - 12;}

System.out.println ("The time is: " + time + ":00 " + Grr);}


else

System.out.println ("You error-ed someplace, please read more carefully.");

}}


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
//elena 

import java.util.*;

public class ex11

{

public static void main (String args [])

{

  Scanner input = new Scanner (System.in);

  

  System.out.print ("Enter am or pm: ");

  String timeOfDay = input.nextLine();

  System.out.print ("Enter the starting hour: ");

  int time = input.nextInt ();

  System.out.print ("Enter the number of elapsed hours: ");

  time += input.nextInt ();


  if(timeOfDay.equals("pm")){

    time += 12;

  }

  String result = ((time/12)%2)==0? "am" : "pm";

  time %= 12;


  if(time==0){ //to make 0 AM and 0 PM change to 12 AM and 12 PM.

    time+=12;

  }

  System.out.println("The time is: " + time + ":00 " + result);

}}


#3
AntiSmurffette

AntiSmurffette

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
That version is shorter then mine but it has issues with am and pm.

Quote

Enter am or pm: am
Enter the starting hour: 12
Enter the number of elapsed hours: 12
The time is: 12:00 am

Quote

Enter am or pm: pm
Enter the starting hour: 12
Enter the number of elapsed hours: 12
The time is: 12:00 pm

Since I don't completely understand the whole program just yet I haven't figured out why, but I am attempting to figure it out... Will look at it again tomorrow when I don't want to crawl up in my warm bed and sleep for the rest of eternity.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
hm, i propably have to change am to pm and vica versa in this part of the code:

if(time==0){ //to make 0 AM and 0 PM change to 12 AM and 12 PM.

    time+=12;

  }

if(time==0){ //to make 0 AM and 0 PM change to 12 AM and 12 PM.

    time+=12;

    if(timeOfDay.equals("am"){

       timeOfDay = "pm";

    } else {

       timeOfDay = "am";

    }

  }

Or: shorter but less readable:
if(time==0){ //to make 0 AM and 0 PM change to 12 AM and 12 PM.

    time+=12;

    timeOfDay = timeOfDay.equals("am")? "pm" : "am";

    }

  }

Edited by wim DC, 22 November 2010 - 12:02 PM.


#5
AntiSmurffette

AntiSmurffette

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
after I changed that bit of code it still didn't work until I changed "result" to timeOfDay.
}

  System.out.println("The time is: " + time + ":00 " + result);

}}

Thanks for your help. :) The final program was:
//elena 

import java.util.*;

public class ex11ox

{

public static void main (String args [])

{

  Scanner input = new Scanner (System.in);

  

  System.out.print ("Enter am or pm: ");

  String timeOfDay = input.nextLine();

  System.out.print ("Enter the starting hour: ");

  int time = input.nextInt ();

  System.out.print ("Enter the number of elapsed hours: ");

  time += input.nextInt ();


  if(timeOfDay.equals("pm")){

    time += 12;

  }

  String result = ((time/12)%2)==0? "am" : "pm";

  time %= 12;


  if(time==0){ //to make 0 AM and 0 PM change to 12 AM and 12 PM.

    time+=12;

     if(timeOfDay.equals("am")){

       timeOfDay = "pm";

    } else {

       timeOfDay = "am";

    }

  

}

  System.out.println("The time is: " + time + ":00 " + timeOfDay);

}}

Quote

Enter am or pm: am
Enter the starting hour: 12
Enter the number of elapsed hours: 12
The time is: 12:00 pm

Enter am or pm: pm
Enter the starting hour: 12
Enter the number of elapsed hours: 12
The time is: 12:00 am



However, I will of course be presenting my own program to my teacher as it wouldn't feel right turning in your code as my own, but I did learn something from your code that I might use in the future.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
oww riiiight i kinda mixed up the 2




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users