I'm just new in java.I am trying to convert money to time which is 1 dollar == 3 minutes but minutes-- wont let me display the exact minutes of time in the second loop
package javaapplication8;
import java.util.Scanner;
public class JavaApplication8 {
public static void main(String[] args) {
/*
1 Dollar = 3 minutes
*/
//I am trying to convert money to time which is 1 dollar == 3 minutes but minutes--
//wont let me display the exact minutes of time in the second loop
int minutes;
int amount;
int hour=0;
int time;
Scanner in = new Scanner(System.in);
System.out.println("Please Enter Amount");
amount = in.nextInt();
time = amount * 3;
for (minutes = time; minutes>=59; minutes--)
{
minutes = minutes- 59;
hour = hour+1;
System.out.println(minutes+"minutes");
System.out.println(hour+"hour");
}
}
}
So if money == 40 here is the problem:
Please enter amount:
40
61 minutes
1 hour
here lies the problem it should be 2 minutes but because of minutes-- its not,
1 minutes
2 hour
Your second loop is a weird way of implementing it you should replace minute-- by minute-=60 like so:
for (minutes = time; minutes>=60; minutes-=60) {
hour = hour+1;
}
but you could just implement it by using arithmetic:
hour = minute/60;
minute = minute % 60;
Related
I have an exercise in which I need to input hours and minutes in 24 hour format and after that the program will show me the time after 15 minutes. It doesn't work when minutes overflow after 59 minutes. When I have for example the input:
15
59
My output is:
15:74
I know that after 59 minutes at a normal clock will be 16:14. How can I handle this case correctly?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int hours = in.nextInt();
int minutes = in.nextInt();
if ( minutes == 59 ){
hours = hours + 1;
minutes = 00;
minutes = minutes + 14; //because I passed 1 minute
}
else{
minutes = minutes + 15;
}
System.out.printf("%d:%02d" , hours , minutes);
}
}
In Java, you don't need to program such basic things "by hand" like in C. Java provides a lot of APIs. For example the time API. See for example LocalTime with its method plusMinutes.
It's easy you should use Modulo operation like this :
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int hours = in.nextInt();
int minutes = in.nextInt();
minutes = minutes + 15;
hours += minutes / 60;
minutes = minutes % 60;
System.out.printf("%d:%02d", hours, minutes);
}
Do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int hours = in.nextInt();
int minutes = in.nextInt();
minutes=minutes+15;
if (minutes > 59) {
hours++;
}
minutes = minutes % 60;
hours = hours % 24;
System.out.printf("%d:%02d", hours, minutes);
}
}
A sample run:
15
59
16:14
Another sample run:
23
59
0:14
I am writing a program to convert minutes into the time of day. How can I write this to keep the hours in standard time, the hour should never exceed 12.
import java.util.*;
public class MinutesConverter {
public static void main(String[] args) {
int minutes;
int hours;
Scanner input = new Scanner(System.in);
System.out.print("Enter minutes: ");
minutes = input.nextInt();
hours = minutes / 60;
minutes %= 60;
System.out.print(hours + ":" + minutes);
}
}
You can make a little change in code, where you are assigning value to hour
here is what i can suggest:
hour = ( minutes / 60 ) % 24;
I am creating a clock in Java. I have managed to do some very basic stuff and now i want to implement a function.
If i set the time to i.e. 12:04:59 and use my timeTick Method it will increase the time with 1 second but the problem is it will say the time is 12:04:60 and it doesn't change to 12:05:00.
I've been struggling a while now and i can't really find a solution to it.
My code is below, i hope you can help me,
public class Clock{
public int seconds;
public int minutes;
public int hours;
public Clock ( int InsertSeconds, int InsertMinutes, int InsertHours){
seconds = InsertSeconds;
minutes = InsertMinutes;
hours = InsertHours;
}
public void timeTick(){
seconds = seconds + 1;
}
public String toString(){
return hours + ":" + minutes + ":" + seconds;
}
}
I am not planning to use Imports because i am a beginner it would be great if we can keep it simple.
The problem here lays in the timeTick() function. In a real clock example we have some extra rules for counting. Every time we count to 60 seconds, we add a minute. Every time we count to 60 minutes, we add an hour. So you have to implement these rules.
// lets make some simple code
public void timeTick(){
seconds = seconds + 1; // you can also use seconds++; it means exactly the same thing
if(seconds == 60){
minutes++; // we reached a minute, we need to add a minute
seconds = 0; // we restart our seconds counter
if(minutes == 60){
hours++; // we reached an hour, we need to add an hour
minutes = 0; // we restart our minutes counter
// and so on, if you want to use days (24 h a day) , months ( a bit more difficult ), ...
}
}
}
I hope this will help you, for a starter it might be a good idea to split the second part of code into a function, which deals with this situation. Good luck!
How about this:
public void timeTick () {
seconds++;
while (seconds >= 60) {
minutes++;
seconds-=60;
}
while (minutes >= 60) {
hours++;
minutes-=60;
}
}
Try this :
public void timeTick () {
seconds++;
if (seconds == 60)
{
seconds = 0;
minutes++;
if (minutes == 60)
{
minutes = 0;
hours++;
}
}
}
try this:
public class Clock{
public int seconds;
public int minutes;
public int hours;
public Clock ( int InsertSeconds, int InsertMinutes, int InsertHours){
seconds = InsertSeconds;
minutes = InsertMinutes;
hours = InsertHours;
}
public void timeTick(){
seconds = seconds + 1;
if(seconds==60){
minutes++;
seconds=0;
if(minutes==60){
hours++;
minutes=0;
}
}
}
public String toString(){
return hours + ":" + minutes + ":" + seconds;
}
}
You could check for when you have 60 seconds, then reset seconds to zero, and increase minutes by 1. e.g.
if (the condition you want to check) {
//increase the number of minutes.
//reset number of seconds.
}
If you're entering seconds values of more than 60, you'll need to work out how many minutes that equals using division, and the remaining number of seconds using the modulus operator: % e.g.
seconds = 125;
minutes = seconds / 60; // 2 minutes
remaining_seconds = seconds % 60; // 5 seconds
what is the easiest and fastest way to convert minutes (double) to default time hh:mm:ss
for example I used this code in python and it's working
time = timedelta(minutes=250.0)
print time
result:
4:10:00
is there a java library or a simple code can do it?
EDIT: To show the seconds as SS you can make an easy custom formatter variable to pass to the String.format() method
EDIT: Added logic to add one minute and recalculate seconds if the initial double value has the number value after the decimal separator greater than 59.
EDIT: Noticed loss of precision when doing math on the double (joy of working with doubles!) seconds, so every now and again it would not be the correct value. Changed code to properly calculate and round it. Also added logic to treat cases when minutes and hour overflow because of cascading from seconds.
Try this (no external libraries needed)
public static void main(String[] args) {
final double t = 1304.00d;
if (t > 1440.00d) //possible loss of precision again
return;
int hours = (int)t / 60;
int minutes = (int)t % 60;
BigDecimal secondsPrecision = new BigDecimal((t - Math.floor(t)) * 100).setScale(2, RoundingMode.HALF_UP);
int seconds = secondsPrecision.intValue();
boolean nextDay = false;
if (seconds > 59) {
minutes++; //increment minutes by one
seconds = seconds - 60; //recalculate seconds
}
if (minutes > 59) {
hours++;
minutes = minutes - 60;
}
//next day
if (hours > 23) {
hours = hours - 24;
nextDay = true;
}
//if seconds >=10 use the same format as before else pad one zero before the seconds
final String myFormat = seconds >= 10 ? "%d:%02d:%d" : "%d:%02d:0%d";
final String time = String.format(myFormat, hours, minutes, seconds);
System.out.print(time);
System.out.println(" " + (nextDay ? "The next day" : "Current day"));
}
Of course this can go on and on, expanding on this algorithm to generalize it. So far it will work until the next day but no further, so we could limit the initial double to that value.
if (t > 1440.00d)
return;
Using Joda you can do something like:
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
final Period a = Period.seconds(25635);
final PeriodFormatter hoursMinutes = new PeriodFormatterBuilder().appendHours().appendSuffix(" hour", " hours")
.appendSeparator(" and ").appendMinutes().appendSuffix(" minute", " minutes").appendSeparator(" and ")
.appendSeconds().appendSuffix(" second", " seconds").toFormatter();
System.out.println(hoursMinutes.print(a.normalizedStandard()));
//Accept minutes from user and return time in HH:MM:SS format
private String convertTime(long time)
{
String finalTime = "";
long hour = (time%(24*60)) / 60;
long minutes = (time%(24*60)) % 60;
long seconds = time / (24*3600);
finalTime = String.format("%02d:%02d:%02d",
TimeUnit.HOURS.toHours(hour) ,
TimeUnit.MINUTES.toMinutes(minutes),
TimeUnit.SECONDS.toSeconds(seconds));
return finalTime;
}
I am required to store a time value in an integer in the format of HHMMSS. this time value is incrementing every second (basically a custom clock). however, since integers are naturally 10 based, I must implement a large cumbersome logic that extracts each digits and checks for 60 seconds in a minutes, 60 minutes in an hour and 24 hours a day. I wonder if there is some clever ways to do it without a massive if/else if chunk.
You can use the modulus operator to pick out each of the components of a single seconds counter:
int totalSeconds;
...
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = (totalSeconds / 3600);
Then you can just increment a single seconds counter and extract each of the components.
My suggestion would be to implement a CustomClock class, which could look something like:
public class CustomClock {
private int hour;
private int minute;
private int second;
public CustomClock(int hour, int minute, int second) {
this.hour = hour;
// ...
}
public void increment() {
second = second + 1)%60;
if (second == 0) minute = (minute + 1)%60;
if (minute == 0) hour = (hour + 1)%24;
}
}
Thus taking advantage of the mod operator (%) to compute arbitrary base numbers.