Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java help for beginners

Name: Anonymous 2009-02-08 18:18

Compute the remainder of seconds entered by the user by the number of seconds
in an hour (SECONDS_X_MINUTE).


import java.util.Scanner;

public class Time {

    public static final int SECONDS_X_HOUR = 3600;
    public static final int SECONDS_X_MINUTE = 60;
    public static final int AM_PERIOD = 43199;
    public static void main(String[] args) {
   
        int timeInSeconds;
        int timeInHours;
        int remainderHours;
        int timeInMinutes;
        int standardTimeInHours;
       
        Scanner keyboard = new Scanner(System.in);
        System.out.println ("Please Enter Time in Seconds :\t");
        timeInSeconds = keyboard.nextInt();
        timeInHours = timeInSeconds/SECONDS_X_HOUR;   
        remainderHours = timeInHours&SECONDS_X_HOUR;
        timeInMinutes = remainderHours * SECONDS_X_MINUTE;
       
       
        System.out.println (timeInSeconds + " " + timeInHours + " " + remainderHours + " " +timeInMinutes);
        System.out.println ("Please enter the time in seconds: " + timeInSeconds);
        System.out.println ("Standard Format: " + (int)timeInHours + "h. " + timeInMinutes + "m. " + timeInSeconds + "s.");
        System.out.println ("AM/PM Format: " + timeInHours + "h. " + timeInMinutes + "m. " + timeInSeconds + "s.");


    }

}

Name: Anonymous 2009-02-09 13:41

>>24
Not really -- int arithmetic is faster.

Name: Anonymous 2009-02-09 18:37

>>30
int isqrt(n){
 /* calculates integer square root.    *
  * if n is less than zero, calculates *
  * integer square root divided by i.  */
  int r = 0, i = 1;
  if(0 > n) n = -n;
  for(i = i << ((sizeof(int) * CHAR_BIT - (0 > ~i ? 2 : 1)) / 2) * 2; i; i >>=2)
    if(n >= (i | r)){
      n -= i | r;
      r = r >> 1 | i;
    }else r >>= 1;
  return r;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List