A Time class

Design a class named Time

Page 1 of 1

11 Replies - 1226 Views - Last Post: 05 October 2009 - 07:51 PM Rate Topic: -----

#1 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

A Time class

Posted 05 October 2009 - 03:41 PM

Hello all, how is everyone doing on this lovely Monday evening? I am working on this problem, and I am running into a few issues I can't figure out.
-I need data fields for hour, minute, second that represent a time.
-A no-arg constructor that creates a 'Time' object for the current time(The data fields value represents the current time).
-A constructor that constructs a Time object with a specified elapsed time since midnight Jan 1, 1970, in milliseconds(The data fields represent this time)
-Three 'get' methods for the data fields hour, minute, second.

Write a test program that creates two 'Time' objects(using 'new Time()' and new Time(555550000)), and display their hour, minute, and second.

This is my code for my classes, I haven't started the test program yet, I can't figure this out, I keep getting errors when I am trying to use defined variables, but the compiler isn't seeing them correctly...Probably it is me not using them correctly, but that is what I hope to correct...ok.

import java.util.*;

public class Time
{
	int hour;
	int minute;
	int second;
	
	//my no arg constructor for current time
	public int currentTime()
	{
		long totalMilliseconds = System.currentTimeMillis();
	}
	
		long totalSeconds = totalMilliseconds / 1000;
	
	//my get method for seconds
	static int getSecond()
	{
		int currentSecond = (int)(totalSeconds % 60);
	}
	
		long totalMinutes = totalSeconds / 60;
	
	//my get method for minutes
	static int getMinute()
	{
		int currentMinute = (int)(totalMinutes % 60);
	}
	
		long totalHours = totalMinutes / 60;
	
	//my get method for hours
	static int getHour()
	{
		int currentHour = (int)(totalHours % 24);
	}
}	


And these are my errors.....


----jGRASP exec: javac -g C:\Users\Christopher\Documents\Time.java

Time.java:23: cannot find symbol
symbol : variable totalMilliseconds
location: class Time
long totalSeconds = totalMilliseconds / 1000;
^
Time.java:27: non-static variable totalSeconds cannot be referenced from a static context
int currentSecond = (int)(totalSeconds % 60);
^
Time.java:34: non-static variable totalMinutes cannot be referenced from a static context
int currentMinute = (int)(totalMinutes % 60);
^
Time.java:41: non-static variable totalHours cannot be referenced from a static context
int currentHour = (int)(totalHours % 24);
^
4 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Please help me figure this out...I know it is simple, but I'm just not getting it....TY.

Is This A Good Question/Topic? 0
  • +

Replies To: A Time class

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,792
  • Joined: 27-December 08

Re: A Time class

Posted 05 October 2009 - 04:46 PM

Let's redo your code a little bit. The reason you were getting these errors is due to them being referenced beyond their scope. In other words, you were trying to use them in ways they couldn't be used or seen by the variable or method making the reference.

//Note: you don't need any imports
public class Time{
	private int hour, minute, second;

	public Time(){
		   second = System.currentTimeMillis()/1000;
		   minute = second/60; //fill number of minutes
		   second %= 60;  //complete transfer from seconds to minutes

		   hour = minute/60; //fill number of hours
		   minute %= 60; //complete transfer from seconds to minutes

		   hour %= 24; //sets hour to one of the 24 hours in a day 
			
		   }
	public Time(){
			//In here, convert Jan 1, 1970 to seconds
		   //Next, call the System.currentTimeMillis() method
		   //And subtract the differences
		   //Then, convert to hours, mins, and seconds
			}
	 public int getMinutes(){
		 return (hour*60)+minute;
		  }
}



Given this template, try filling in the elapsed time constructor as well as your other getter methods. Feel free to post with any more questions.
Was This Post Helpful? 1
  • +
  • -

#3 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: A Time class

Posted 05 October 2009 - 04:50 PM

View Postmacosxnerd101, on 5 Oct, 2009 - 03:46 PM, said:

Let's redo your code a little bit. The reason you were getting these errors is due to them being referenced beyond their scope. In other words, you were trying to use them in ways they couldn't be used or seen by the variable or method making the reference.

//Note: you don't need any imports
public class Time{
	private int hour, minute, second;

	public Time(){
		   second = System.currentTimeMillis()/1000;
		   minute = second/60; //fill number of minutes
		   second %= 60;  //complete transfer from seconds to minutes

		   hour = minute/60; //fill number of hours
		   minute %= 60; //complete transfer from seconds to minutes

		   hour %= 24; //sets hour to one of the 24 hours in a day 
			
		   }
	public Time(){
			//In here, convert Jan 1, 1970 to seconds
		   //Next, call the System.currentTimeMillis() method
		   //And subtract the differences
		   //Then, convert to hours, mins, and seconds
			}
	 public int getMinutes(){
		 return (hour*60)+minute;
		  }
}



Given this template, try filling in the elapsed time constructor as well as your other getter methods. Feel free to post with any more questions.


Thanks mac, I'll give it a whirl, and see what I come up with.
Was This Post Helpful? 0
  • +
  • -

#4 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: A Time class

Posted 05 October 2009 - 06:00 PM

View Postmacosxnerd101, on 5 Oct, 2009 - 03:46 PM, said:

Let's redo your code a little bit. The reason you were getting these errors is due to them being referenced beyond their scope. In other words, you were trying to use them in ways they couldn't be used or seen by the variable or method making the reference.

//Note: you don't need any imports
public class Time{
	private int hour, minute, second;

	public Time(){
		   second = System.currentTimeMillis()/1000;
		   minute = second/60; //fill number of minutes
		   second %= 60;  //complete transfer from seconds to minutes

		   hour = minute/60; //fill number of hours
		   minute %= 60; //complete transfer from seconds to minutes

		   hour %= 24; //sets hour to one of the 24 hours in a day 
			
		   }
	public Time(){
			//In here, convert Jan 1, 1970 to seconds
		   //Next, call the System.currentTimeMillis() method
		   //And subtract the differences
		   //Then, convert to hours, mins, and seconds
			}
	 public int getMinutes(){
		 return (hour*60)+minute;
		  }
}



Given this template, try filling in the elapsed time constructor as well as your other getter methods. Feel free to post with any more questions.


This is what I have for now, I have been fighting it, and I can't figure it out...


public class Time
{
	private int hour;
	private int minute;
	private int second;

	public Time()
	{
		second = System.currentTimeMillis()/1000;
		minute = second / 60; //fill number of minutes
		second %= 60; //complete transfer from seconds to minutes

		hour = minute/60; //fill number of hours
		minute %= 60; //complete transfer from seconds to minutes

		hour %= 24; //sets hour to one of the 24 hours in a day 

	}
	
	public Time()
	{
		//total milliseconds since midnight Jan 1, 1970
		long totalMilliSeconds = System.currentTimeMillis();
		
		//converting to hours, min, and seconds
		long totalSeconds = totalMilliseconds / 1000;
		long totalMinutes = totalSeconds / 60;
		long totalHours = totalMinutes / 60;
		
		
	}

	public int getMinutes()
	{
		return ((hour * 60) + minute);
	}
	
	public int getSeconds()
	{
		return ((minute * 60) + second);
	}
	
	public int getHours()
	{
		return ((second * 60) + hour);
	}
}



and these are my errors


----jGRASP exec: javac -g C:\Users\Christopher\Documents\Time.java

Time.java:27: Time() is already defined in Time
public Time()
^
Time.java:16: possible loss of precision
found : long
required: int
second = System.currentTimeMillis()/1000;
^
Time.java:33: cannot find symbol
symbol : variable totalMilliseconds
location: class Time
long totalSeconds = totalMilliseconds / 1000;
^
3 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: A Time class

Posted 05 October 2009 - 06:10 PM

you have two constructors for the Time class without parameter

Time()
and
Time()

System.currentTimeMillis() returns a long not an int
Was This Post Helpful? 1
  • +
  • -

#6 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: A Time class

Posted 05 October 2009 - 06:41 PM

View Postpbl, on 5 Oct, 2009 - 05:10 PM, said:

you have two constructors for the Time class without parameter

Time()
and
Time()

System.currentTimeMillis() returns a long not an int


Okie Dokey....this is what I have now... and it is throwing an error..

public class Time
{
	private int hour;
	private int minute;
	private int second;

	public Time()
	{
		second = (int)System.currentTimeMillis() / 1000;
		minute = second / 60; //fill number of minutes
		second %= 60; //complete transfer from seconds to minutes

		hour = minute/60; //fill number of hours
		minute %= 60; //complete transfer from seconds to minutes

		hour %= 24; //sets hour to one of the 24 hours in a day 

	}
	
	public long currentTime()
	{
		//total milliseconds since midnight Jan 1, 1970
		long totalMilliSeconds = System.currentTimeMillis();
		
		//converting to hours, min, and seconds
		long totalSeconds = totalMilliSeconds / 1000;
		long totalMinutes = totalSeconds / 60;
		long totalHours = totalMinutes / 60;
		
		int currentSecond = (int)(totalSeconds % 60);
		int currentMinute = (int)(totalMinutes % 60);
		int currentHour = (int)(totalHours % 24);
		
		return "Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + "GMT";
		
	}

	public int getMinutes()
	{
		return ((hour * 60) + minute);
	}
	
	public int getSeconds()
	{
		return ((minute * 60) + second);
	}
	
	public int getHours()
	{
		return ((second * 60) + hour);
	}
}



this is the error... why?

----jGRASP exec: javac -g C:\Users\Christopher\Documents\Time.java

Time.java:41: incompatible types
found : java.lang.String
required: long
return "Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + "GMT";
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: A Time class

Posted 05 October 2009 - 06:49 PM

first you are playing with fire here

second = (int)System.currentTimeMillis() / 1000;

replace it by

second = (int) (System.currentTimeMillis() / 1000L);

convert it to int AFTER dividing by 1000L but no reason to not make second a long.... it may crash in a few years... and I am sure you want your code to last at least 20 years :D right ?

the other one

public long currentTime()
....
return "Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + "GMT";

you declare that currenTime() returns a long.... but you return a String
Was This Post Helpful? 1
  • +
  • -

#8 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: A Time class

Posted 05 October 2009 - 07:18 PM

PBL is the man!!!! Thanks so much. I now have to write my test program for this...
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: A Time class

Posted 05 October 2009 - 07:25 PM

View Posttheautokustomizer, on 5 Oct, 2009 - 06:18 PM, said:

PBL is the man!!!! Thanks so much. I now have to write my test program for this...

I hope that out of 8,680 posts I have a few exact and relevant :D
Thanks for the thank :^:
Was This Post Helpful? 1
  • +
  • -

#10 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: A Time class

Posted 05 October 2009 - 07:34 PM

I can't forget about mac, thank you as well, without you, I would have been fighting this longer, you gave me a great start!!! You are the man too!!!!!
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: A Time class

Posted 05 October 2009 - 07:40 PM

View Posttheautokustomizer, on 5 Oct, 2009 - 06:34 PM, said:

I can't forget about mac, thank you as well, without you, I would have been fighting this longer, you gave me a great start!!! You are the man too!!!!!

So click on Macosxnerd "this post was helful" too
I am sure he is more anxious to boost his postWasUseful/numberPost ratio than I am

You do not receive many thanks for:

[rules][/rules]

or

:code:

or

duplicated posts merged :angry:

or

Respect rule #5 Give a meaningfull title to your topic
"Java" is kind of useless in a forum dedicated to help people with Java problem

which represent 5% to 10% of my posts
That is why I stopped to worry about my numberOfThanks/numberOfPosts ratio and I added to my signature that I don't really care about them
But a "thank this post was Helpful" is always appreciated

This post has been edited by pbl: 05 October 2009 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

#12 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: A Time class

Posted 05 October 2009 - 07:51 PM

View Postpbl, on 5 Oct, 2009 - 06:40 PM, said:

View Posttheautokustomizer, on 5 Oct, 2009 - 06:34 PM, said:

I can't forget about mac, thank you as well, without you, I would have been fighting this longer, you gave me a great start!!! You are the man too!!!!!

So click on Macosxnerd "this post was helful" too
I am sure he is more anxious to boost his postWasUseful/numberPost ratio than I am

You do not receive many thanks for:

[rules][/rules]

or

:code:

or

duplicated posts merged :angry:

or

Respect rule #5 Give a meaningfull title to your topic
"Java" is kind of useless in a forum dedicated to help people with Java problem

which represent 5% to 10% of my posts


Already been done when I first recieved the help;) :D (I just wish that some people would thank me....ahhhh someday I hope have the abilities of greatness that exist on this site) :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1