8 Replies - 1087 Views - Last Post: 19 January 2009 - 10:43 PM Rate Topic: -----

#1 hello1212  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 29-December 08

Constructors and Methods for a Time class

Posted 19 January 2009 - 07:58 PM

i am writing a time class but this is 12 hour clock class.I mainly have two classes one Time and other TestTime(which has a main method in it). My goal is to take in the time in hours and minutes in 12 hour and ask whether it is A.m or P.m. How do i do it?? i am stuck

Here is the code for the time class
// Represents the time of the day in hours 
// and minutes using the 24-hour clock.

public class Time
{
	private int hours; 
	private int mins;
	
	public Time(int hours,int mins)
	{
		this.hours=hours;
		this.mins=mins;
		
		if (hours>0&&hours<=12&&mins>=0&&mins<=60 )
		{
			  hours=hours;
			  mins=mins;
		}
		else
		{
			  throw new IllegalArgumentException("\nINVALID INPUT!!");
		}
	}
	
	//This method returns the time in minutes
	private int toMins()
	{
		return (hours*60)+mins;
	}
	
	// Returns true if this time is earlier,
	// false if a time passed as an argument or the times are the same
	public boolean lessThan(Time t)
	{
		return toMins()<t.toMins();
	}
	
	public String toString()
	{
		return hours+":"+mins;
	}
	
	// Calculates the number of minutes that have passed by 
	// between the argument Time t and this time object.
	public int elapsedSince(Time t)
	{ 
		 if(t.toMins()<toMins())
		 	return toMins()-t.toMins();
		 else
		 	return (24*60)-(t.toMins()-toMins());
		 
	}
	

}


Here is the TestTime class which has the main method in it and i am not sure what to write in this> It is in this that i take in hours and minutes and create a 12 hour clock object!!
import java.util.Scanner;

public class TestTime
{
  public static void main(String[] args)
  {
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Time 1" );
	System.out.print("Enter hours: " );
	int h1 = keyboard.nextInt();
	System.out.print("Enter mins: " );
	int m1 = keyboard.nextInt();
	System.out.println("Enter your choice 1)A.M 2)P.M");
	keyboard.nextLine();
	int choice=keyboard.nextInt();
	convert(choice);
	Time t1 = new Time(h1, m1);
	   
	System.out.println("Time 2" );
	System.out.print("Enter hours: " );
	int h2 = keyboard.nextInt();
	System.out.print("Enter mins: " );
	int m2 = keyboard.nextInt();
	Time t2 = new Time(h2, m2);

	System.out.println(t1 + " <= " + t2 + "? " + t1.lessThan(t2));
	System.out.println(t2.elapsedSince(t1) + " minutes elapsed from t1 to t2.");
  }
  
  public static void convert(int choice,Time t)
	{
		if(choice==1)
		///	??????????

							  else//add 12 hours and call the toMins() method og the time class
							  ///????
	
	} 
}



*Edited for a more descriptive title

This post has been edited by pbl: 19 January 2009 - 10:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Constructors and Methods for a Time class

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Constructors and Methods for a Time class

Posted 19 January 2009 - 08:07 PM

Well you have kinda a random keyboard.nextLine() there. You should really take that line out...
	System.out.println("Enter your choice 1)A.M 2)P.M");
	keyboard.nextLine();



And then do you actually want to program to start the time? If so you will need some for loops to count the hours minutes and seconds up
Was This Post Helpful? 0
  • +
  • -

#3 hello1212  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 29-December 08

Re: Constructors and Methods for a Time class

Posted 19 January 2009 - 08:13 PM

so basically i fthe user enters 2 30 p.m the computer converts it to 24 hour clock since we want the total mins but it just calcualtes using the 24 hour clock but the output will be in the format 12 hour clock

For example,
TIME 1
Enter hours: 2
Enter miutes: 30
Enter 1)A.m or 2)P.m: 2
//Converts 2.30 to 14:30 but does not display it ,we need it because in the toMins() of the time class we want to calculate and compare .
TIME 2
Enter hour:2
ENter min:30
Enter 1)a.m 2)P.m:1
//Remains 02:30
2:30 a.m<2:30pm? false
720 minutes:total elapsed time

This post has been edited by hello1212: 19 January 2009 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Constructors and Methods for a Time class

Posted 19 January 2009 - 08:17 PM

Ok, so basically then what you will have to do is create for loops to count the time, one for loop for hours, another inside that one for minutes and another inside that one for seconds... To pass the information into the object you will just need to create a constructor with hours, minutes and seconds and that will store it in there... as for the AM or PM all you will need to do is have an if statement that says... if(time == 12 Hours, 0 minutes and 0 seconds) then turn the AM to PM
Was This Post Helpful? 0
  • +
  • -

#5 hello1212  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 29-December 08

Re: Constructors and Methods for a Time class

Posted 19 January 2009 - 08:26 PM

oh so this is this NESTED LOOPS?????
i didnt understand why do you need to change A.m to P.m
Was This Post Helpful? 0
  • +
  • -

#6 hello1212  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 29-December 08

Re: Constructors and Methods for a Time class

Posted 19 January 2009 - 08:31 PM

so, when i am coding this up do i need to change the Time class or the TestTime class??what is the header for this constructor?/

anyway here is what you said
public Time(Time t,int hours,int min,int seconds)
{
for(int i=0;i<hours;i++)
{
if(hours==12)
//??????
for(int j=0;j<mins;j++)
{
for(int k=0;k<seconds;k++)
{
???
}
Was This Post Helpful? 0
  • +
  • -

#7 hello1212  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 29-December 08

Re: Constructors and Methods for a Time class

Posted 19 January 2009 - 09:31 PM

but why would u need nested loops????
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8021
  • View blog
  • Posts: 31,132
  • Joined: 06-March 08

Re: Constructors and Methods for a Time class

Posted 19 January 2009 - 09:48 PM

View Posthello1212, on 19 Jan, 2009 - 08:31 PM, said:

but why would u need nested loops????

You are the one who mentioned nested loops

class Clock {
   int hh, mm, ss;

   public String toString() {
	   String hourStr, amPm;
	   if(hh > 12) {					   // greater than 12 it is PM 
		  if(hh == 24) {				 // but for 24 which is 12 AM
			  hourStr = "12";
			  amPm = "AM"
		  }
		  else {
		  hourStr = "" + (hh - 12);
		  amPm = "AM";
		  }
	   }
	   else {
		  if(hh == 0) {					// 00 hour
			hourStr = "12";			  // is 12 PM
			amPm = "PM";
		  }
		  else {
			 hourStr = "" + hh;
			 amPm = "AM";
	   }
	   
	   return hourStr + ":" + mm + ":" + ss + " " + amPm;
   }
}


Was This Post Helpful? 0
  • +
  • -

#9 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Constructors and Methods for a Time class

Posted 19 January 2009 - 10:43 PM

The reason you need nested loops is that you have 12 hours, 60 minutes, 60 seconds right? So you will need the first for loop to be less or equal to 12 hours, then the next for loop will be less than or equal to 60(inside the first for loop) and then the third one will be(again inside the minutes loop) for less than or equal to 60. This will count the seconds 60 times for each minute, 60 minutes for each hour and 12 hours for each Am/PM. Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1