The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

toString(){.....}

Posted by Dorota Lukas on September 22, 2001 at 6:22 PM

I need some help with toString() method.I have a method Clock and what I want to do is to change toString method in order to display (11:03) NOT (11:3).I'm trying to convert int to string and than again string to int using Integer.parse().Doesn't work!!!Still (11:3) is displayed.
I'm new to JAVA, but trying to learn!!!!!
Please replay someone, please,please!!
public class Clock{
protected int minutes,hours;
/* - - - - - - - - - - - - - - - */
/** * constructs a clock with a random time. */
public Clock(){
minutes = (int)(Math.random()*60);
hours = (int)(Math.random()*24);
}
/* - - - - - - - - - - - - - - - */
/** * constructs a clock.
* @param hours the number of hours from midnight
* @param minutes the number of minutes since the last hour */
public Clock(int hours, int minutes){
this.minutes = minutes % 60;
this.hours = hours % 24;
}
/* - - - - - - - - - - - - - - - */
/**
* @return the number of complete hours past midnight */
public int getHours(){ return hours; }
/* - - - - - - - - - - - - - - - */
/**
* @return the number of complete minutes past the hour */
public int getMinutes(){ return minutes; }
/* - - - - - - - - - - - - - - - */
/**
* @param hour -- the number of complete hours past midnight */
public void setHours(int hour){ hours = hour % 24; }
/* - - - - - - - - - - - - - - - */
/**
* @param minute -- the number of complete minutes past the hour */
public void setMinutes(int minute){
minutes = minute % 60; }
/* - - - - - - - - - - - - - - - */
/**
* sets the Clock to the current time. */
public void setToCurrentTime(){
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
setMinutes((int)cal.get(Calendar.MINUTE));
setHours((int)cal.get(Calendar.HOUR_OF_DAY));
} /* - - - - - - - - - - - - - - - */
/**
* @return a string representation of Clock */
public String toString(){ return hours+":"+minutes; }



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us