Question:
Java programming question about Time program?
Yash
2013-01-10 10:32:28 UTC
Hi everyone! I need help writing a program to get a starting time from the user, getting a period from the user, getting the number of points from the user, and outputting the number of points the user wants with their period as the interval starting from their start time. Here is what it should look like:
Enter start time as hours minutes in 24-hour format
12 34
Enter period amount in minutes
15
Enter number of time points requested
10
Time points starting at 12:34 and every 15 minutes:
12:34
12:49
13:04
13:19
13:34
213:49
14:04
14:19
14:34
14:49
15:04

I have written the test code but I am having a lot of trouble writing the actual code. If somebody could please help me out it would be much appreciated! I am fairly new java so this is pretty difficult for me. Here is my test program:

import java.util.Scanner;

public class TestTime {

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter start time as hours minutes in 24-hour format");
int startHour = input.nextInt();
int startMinute = input.nextInt();
System.out.println("Enter period amount in minutes");
int period = input.nextInt();
System.out.println("Enter number of time points requested");
int numTimePoints = input.nextInt();
Time startTime = new Time(startHour, startMinute);
System.out.println("Time points starting at " + startTime + " and every " + period + " minutes:");
Time currentTime = startTime;
System.out.println(currentTime);
for(int i=0; i {
currentTime = currentTime.advanceMinutes(period);
System.out.println(currentTime);
}
}
}

And here is my actual program:


public class Time {
private int hours;
private int minutes;
private int time;
TestTime tt = new TestTime();

//Sets time to 0:00 (12 midnight)
public Time() {
tt.startHour = 0;
tt.startMinute = 00;
}

//Initializes this to the given time
public Time (int hours, int minutes) {
hours = tt.startHour;
minutes = tt.startMinute;
time = (hours * 60) + minutes;
}

//Set this to the given time
public void set(int hours, int minutes) {

}

//Get the hour
public int getHours() {
return hours;
}

//Get the minutes
public int getMinutes() {
return minutes;
}

//Return a Time object that is minutes ahead of this time
public Time advanceMinutes(int minutes) {
return minutes = time + tt.period;
}

//Print this time in hours:minutes format. Appends a 0 if hours/minutes is a single digit
public String toString() {
int hours1 = minutes / 60;
int remainder = minutes % 60;
}

}

Thank you in advance!
Four answers:
?
2013-01-10 12:04:28 UTC
Hi there. I rewrote your test class a bit and completed your time class! They are too long to put in here so please PM me and I will send it to you with some tips!
Kaydell
2013-01-11 03:32:31 UTC
In your Time class, you don't need to use your TestTime class at all. You're trying to create an object of the class TimeTest and then references it's variables, which you don't need, just do away with all of that. You don't need a starting time because each time object has a time that you can start from to calculate the next time in the series.



In your Time class, you have the time stored two different ways. This is just asking for trouble because when you store the same data in two different ways, it just makes it possible for the data to become inconsistent. Either use hours and minutes, or use the time in minutes, and do away with the other representation. I would keep hours and minutes and do away with the instance variable time (in minutes);



Your method advanceMinutes() could be like the following method:





//Return a Time object that is minutes ahead of this time

public Time advanceMinutes(int minutes) {

return new Time(hours, this.minutes + minutes);

}



The return type for this method is a Time object, so you create a new Time object using the values of the current Time object and adding the number of minutes specified by the parameters.



Anyway, here is my solution for the Time class. I don't believe that I made any changes to the TestTime class:



http://ideone.com/BtzAzg
anonymous
2013-01-10 21:52:12 UTC
import java.util.Scanner;



public class TestTime

{



public static void main(String[] args)

{

new TestTime ().run ();



}



public void run ()

{

Scanner input = new Scanner(System.in);



System.out.println

("Enter start time as hours minutes in 24-hour format");

int startHour = input.nextInt();

int startMinute = input.nextInt();



System.out.println

("Enter period amount in minutes");

int period = input.nextInt();



System.out.println

("Enter number of time points requested");

int numTimePoints = input.nextInt();





Time startTime = new Time(startHour, startMinute);





System.out.println

(

"Time points starting at " +

startTime +

" and every " +

period +

" minutes:"

);



Time currentTime = startTime;

System.out.println(currentTime);



for(int i=0; i < numTimePoints; i++)

{

currentTime = currentTime.advanceMinutes(period);

System.out.println(currentTime);

}

}





public class Time

{

private int hours;

private int minutes;





//Sets time to 0:00 (12 midnight)

public Time()

{

hours = 0;

minutes = 0;

}



//Initializes this to the given time

public Time (int hours_, int minutes_)

{

hours = hours_;

minutes = minutes_;



}



//Return a Time object that is minutes ahead of this time

public Time advanceMinutes ( int minutes_ )

{

minutes = minutes + minutes_;

if ( minutes > 59 )

{

minutes = minutes - 60;

hours++;

}



if ( hours > 23 )

{

hours = hours - 24;

}

return new Time ( hours, minutes );

}



//Print this time in hours:minutes format. Appends a 0 if hours/minutes is a single digit

public String toString()

{

String h = null;

String m = null;



if (hours < 10)

h = "0" + hours;

else

h = "" + hours;



if (minutes < 10)

m = "0" + minutes;

else

m = "" + minutes;



StringBuilder s = new StringBuilder ();

s.append ( h ).append ( ":" ).append ( m );

return s.toString ();

}



}

}
deonejuan
2013-01-10 19:21:41 UTC
Good effort. For time Java already has Calendar API. From that we can get just about any interval you want. We capture a snapshot of a time_request as milliseconds from the epic: Jan-1-1970. The milliseconds are a huge number that belongs in type long.



So, can you use the Java API? or where you supposed to create your own class for Time?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...