• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Jave help! (Working with 3 class)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

phoenixofwater

Registered
Joined
Nov 15, 2002
Location
Ipswich MA USA
I have be working on the code from my class for weeks and I can't get it to work fully...

I need to have a Displayer class that calls classes named clock and date

What I can't get to work is when the clock go on to the next day (past 24 hours) I can't get the days to add one....


Well here the code

import java.awt.*;
import java.awt.event.*;


public class ASClockDisplayer extends Frame implements ActionListener
{
ASClock c1 = new ASClock(19, 30, 0);
ASDate c2 = new ASDate();

//Buttons for GUI

private Button buttonHour;
private Button buttonMinute;
private Button buttonSecond;
private Button buttonJday;
private Button buttonYear;

//Text Fields for GUI

private TextField tfMil = new TextField (5);
private TextField tfSta = new TextField (8);
private TextField tfDate = new TextField (8);
private TextField tfJday = new TextField (3);
private TextField tfAdv = new TextField (3);


public ASClockDisplayer()
{

tfMil.setText(c1.getMilTime());
tfSta.setText(c1.getStaTime());
tfDate.setText(c2.getDayMonth()+ " " + c2.getYear());
tfJday.setText(String.valueOf(c2.getJday()));




//Set Title and Layout of GUI

setTitle("Clock Displayer by Anthony");
setLayout(new FlowLayout());

//Adding Lables and TestFields to GUI

add (new Label ("Military Time"));
add (tfMil);
add (new Label ("Standard Time"));
add (tfSta);
add (new Label ("Date"));
add (tfDate);
add (new Label ("Jday"));
add (tfJday);
add (new Label ("Amount to Advance"));
add (tfAdv);


//Adding Buttons to GUI

buttonYear = new Button("Year");
add (buttonYear);
buttonYear.addActionListener(new YearHandler(this));

buttonJday = new Button("Jday");
add (buttonJday);
buttonJday.addActionListener(new JdayHandler(this));

buttonHour = new Button("Hour");
add (buttonHour);
buttonHour.addActionListener(new HourHandler(this));

buttonMinute = new Button("Minute");
add (buttonMinute);
buttonMinute.addActionListener(new MinuteHandler(this));

buttonSecond = new Button("Second");
add (buttonSecond);
buttonSecond.addActionListener(new SecondHandler(this));

addWindowListener (new CloseWindows());

}





class YearHandler implements ActionListener
{
ASClockDisplayer myFrame;
YearHandler (ASClockDisplayer pFrame)
{
myFrame = pFrame;
}

public void actionPerformed (ActionEvent event)
{
myFrame.YearStuff();
myFrame.repaint();
}
}

class JdayHandler implements ActionListener
{
ASClockDisplayer myFrame;
JdayHandler (ASClockDisplayer pFrame)
{
myFrame = pFrame;
}

public void actionPerformed (ActionEvent event)
{
myFrame.JdayStuff();
myFrame.repaint();
}
}

class HourHandler implements ActionListener
{
ASClockDisplayer myFrame;
HourHandler (ASClockDisplayer pFrame)
{
myFrame = pFrame;
}

public void actionPerformed (ActionEvent event)
{
myFrame.HourStuff();
myFrame.repaint();
}
}

class MinuteHandler implements ActionListener
{
ASClockDisplayer myFrame;
MinuteHandler (ASClockDisplayer pFrame)
{
myFrame = pFrame;
}

public void actionPerformed (ActionEvent event)
{
myFrame.MinuteStuff();
myFrame.repaint();
}
}

class SecondHandler implements ActionListener
{
ASClockDisplayer myFrame;
SecondHandler (ASClockDisplayer pFrame)
{
myFrame = pFrame;
}

public void actionPerformed (ActionEvent event)
{
myFrame.SecondStuff();
myFrame.repaint();
}
}



public void IfNewDay()
{
if (c1.getDay()>0)
{
c2.advanceJday(c1.getDay());
c1.setDay(0);
}
tfMil.setText(c1.getMilTime());
tfSta.setText(c1.getStaTime());
tfDate.setText(c2.getDayMonth()+ " " + c2.getYear());
tfJday.setText(String.valueOf(c2.getJday()));
}




public void SecondStuff()
{
int advance;
advance = Integer.parseInt(tfAdv.getText());
IfNewDay();
c1.advanceSecond(advance);
tfMil.setText(c1.getMilTime());
tfSta.setText(c1.getStaTime());
tfDate.setText(c2.getDayMonth()+ " " + c2.getYear());
tfJday.setText(String.valueOf(c2.getJday()));
}

public void MinuteStuff()
{
int advance;
advance = Integer.parseInt(tfAdv.getText());
IfNewDay();
c1.advanceMinute(advance);
tfMil.setText(c1.getMilTime());
tfSta.setText(c1.getStaTime());
tfDate.setText(c2.getDayMonth()+ " " + c2.getYear());
tfJday.setText(String.valueOf(c2.getJday()));
}

public void HourStuff()
{
int advance;
advance = Integer.parseInt(tfAdv.getText());
IfNewDay();
c1.advanceHour(advance);
tfMil.setText(c1.getMilTime());
tfSta.setText(c1.getStaTime());
tfDate.setText(c2.getDayMonth()+ " " + c2.getYear());
tfJday.setText(String.valueOf(c2.getJday()));
}

public void JdayStuff()
{
int advance;
advance = Integer.parseInt(tfAdv.getText());
IfNewDay();
c2.advanceJday(advance);
tfMil.setText(c1.getMilTime());
tfSta.setText(c1.getStaTime());
tfDate.setText(c2.getDayMonth()+ " " + c2.getYear());
tfJday.setText(String.valueOf(c2.getJday()));
}

public void YearStuff()
{
int advance;
advance = Integer.parseInt(tfAdv.getText());
IfNewDay();
c2.advanceYear(advance);
tfMil.setText(c1.getMilTime());
tfSta.setText(c1.getStaTime());
tfDate.setText(c2.getDayMonth()+ " " + c2.getYear());
tfJday.setText(String.valueOf(c2.getJday()));
}

public void actionPerformed (ActionEvent event)
{
repaint();
}

public static void main (String [] args)
{
Frame InputTestWindow = new ASClockDisplayer();
InputTestWindow.setSize (350,350);
InputTestWindow.setBackground(Color.gray);
InputTestWindow.show();
}

public class CloseWindows extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
}
}

public class ASClock
{

private int Hour, Minute , Second;
private int Day;

public ASClock() // Default
{
setHour(12);
setMinute(0);
setSecond(0);
}

public ASClock(int pHour, int pMinute, int pSecond) // 3 arg
{
setHour(pHour);
setMinute(pMinute);
setSecond(pSecond);
}

public void setHour(int pHour)
{
if ((pHour<0 || pHour>23))
{

if (pHour<0)
pHour = 0;
else
pHour = 23;
}
Hour = pHour;
}

public void setMinute(int pMinute)
{
if ((pMinute<0 || pMinute>59))
{
if (pMinute<0)
pMinute = 0;
else
pMinute = 59;
}
Minute = pMinute;
}

public void setSecond(int pSecond)
{
if ((pSecond<0 || pSecond>59))
{
if (pSecond<0)
pSecond = 0;
else
pSecond = 59;
}

Second = pSecond;
}


public int getHour()
{
return Hour;
}

public int getMinute()
{
return Minute;
}

public int getSecond()
{
return Second;
}

public void setDay(int pDay)
{
Day = pDay;
}
public int getDay()
{
return Day;
}



public String getMeridian()
{
String temp;

if(getHour()<=11)
temp = "AM";
else
temp = "PM";

return temp;
}

public int standardHour()
{
int StaHour;

if (getHour() >= 13)
StaHour = getHour() - 12;

else
StaHour = getHour();

if (getHour() == 0)
StaHour = 12;


return StaHour;
}




public void advanceSecond(int pAdvSecond)
{
int NewSecond;
NewSecond = getSecond() + pAdvSecond;
if (NewSecond>=60)
{
int NewMinute;
NewMinute = NewSecond/60;
advanceMinute(NewMinute);
NewSecond=NewSecond%60;
}
setSecond(NewSecond);
}

public void advanceMinute(int pAdvMinute)
{
int NewMinute;
NewMinute = getMinute() + pAdvMinute;
if (NewMinute>=60)
{
int NewHour;
NewHour = NewMinute/60;
advanceHour(NewHour);
NewMinute=NewMinute%60;
}
setMinute(NewMinute);
}

public void advanceHour(int pAdvHour)
{
int NewHour;
NewHour = getHour() + pAdvHour;

if (NewHour>=24)
{
System.out.println("Bigger then 24");

Day=NewHour/24;

NewHour=NewHour%24;
}


setHour(NewHour);

}



public String getMilTime()
{
int h = getHour();
int m = getMinute();
int s = getSecond();
String milTime = "";
if (h < 10)
{
milTime = milTime + "0" + h + ":";
}
else
{
milTime = milTime + h + ":";
}
if (m < 10)
{
milTime = milTime + "0" + m + ":";
}
else
{
milTime = milTime + m + ":";
}
if (s < 10)
{
milTime = milTime + "0" + s;
}
else
{
milTime = milTime + s;
}
return milTime;
}

public String getStaTime()
{
int h = getHour();
int m = getMinute();
int s = getSecond();
String StaTime = "";
if (h < 10)
StaTime = standardHour() + ":";
else
StaTime = standardHour() + ":";
if (m < 10)
StaTime = StaTime + "0" + m + ":";
else
StaTime = StaTime + m + ":";
if (s < 10)
StaTime = StaTime + "0" + s;
else
StaTime = StaTime + s;

return StaTime + " " + getMeridian();
}


public void display()
{
System.out.println("The time is " + getMilTime());
System.out.println("The time is " + getStaTime());
System.out.println("-----------------------------");
}


public static void main (String[]args)
{
ASClock c1 = new ASClock();
c1.display();
ASClock c2 = new ASClock(0, 0, 0);
c2.display();
//c2.advanceSecond(9965);
//c2.advanceMinute();
//c2.advanceHour();
//c2.display();

}
}

public class ASDate
{
private int Jday,Year;

public ASDate() // Default
{
setJday(34);
setYear(2002);
}

public ASDate(int pJday, int pYear) // 2 arg
{
setJday(pJday);
setYear(pYear);
}

public void setJday(int pJday)
{
Jday = pJday;
}

public void setYear(int pYear)
{
Year = pYear;
}

public int getJday()
{
return Jday;
}

public int getYear()
{
return Year;
}

public boolean leapYr()
{
boolean leapYr;
if ((getYear()%4==0) && (getYear()%100==0) && (getYear()%1000==0))
leapYr = true;
else
if ((getYear()%4==0) && (getYear()%100!=0) && (getYear()%1000!=0))
leapYr = true;
else
leapYr = false;
return leapYr;
}


public String getDayMonth()
{
int [] dim = new int[12];
dim [0] = 31; //Jan
if (leapYr())
dim [1] = 29; //Feb
else
dim[1] = 28;//Feb
dim [2] = 31; //Mar
dim [3] = 30; //Apr
dim [4] = 31; //May
dim [5] = 30; //Jun
dim [6] = 31; //Jul
dim [7] = 31; //Aug
dim [8] = 30; //Sep
dim [9] = 31; //Oct
dim [10] = 30; //Nov
dim [11] = 31; //Dec

int dayNum = getJday();
int sub = 0;
while (dayNum > dim)
{
dayNum = dayNum - dim;
sub++;
}

String [] Month = new String[12];
Month [0] = "Jan";
Month [1] = "Feb";
Month [2] = "Mar";
Month [3] = "Apr";
Month [4] = "May";
Month [5] = "Jun";
Month [6] = "Jul";
Month [7] = "Aug";
Month [8] = "Sep";
Month [9] = "Oct";
Month [10] = "Nov";
Month [11] = "Dec";

return (Month + " " + dayNum);
}



public void advanceJday(int pAdvJday)
{

int NewJday;
int NewYear;
NewJday = getJday() + pAdvJday;

if (NewJday>365 && leapYr()== false)
{

NewYear = NewJday/365;
advanceYear(NewYear);
NewJday=NewJday%365;
}

if (NewJday>366 && leapYr()== true)
{

NewYear = NewJday/366;
advanceYear(NewYear);
NewJday=NewJday%366;
}
System.out.println("Hello " + NewJday + " " + Jday);
setJday(NewJday);
}

public void advanceYear(int pAdvYear)
{
int NewYear;
NewYear = getYear() + pAdvYear;
setYear(NewYear);
}

public void display()
{
System.out.println(getDayMonth() + " " + getYear());
System.out.println("-----------------------------");
}

public static void main (String[]args)
{
ASDate c1 = new ASDate();
c1.display();
ASDate c2 = new ASDate(364, 1903 );
c2.display();
c2.advanceJday(1);
c2.display();
c2.advanceYear(12);
c2.display();
}
}
 
In advanceHour(int pAdvHour) in the ASClock class shouldn't:
Code:
Day= NewHour/24;
be:
Code:
Day += NewHour/24;
?

In addition, don't you need a reference to whatever ASDate object you using to advance that a day aswell. Knowing me you've already got that and I'm too dumb to spot it!

Out of interest, how come your using the standard AWT rather than swing?
 
In my experience of clock/date classes, I've found its much better to have the time and date as one class containing all the information about 'when'. Also makes it easier to ensure that all relevent attributes are updated as a value rolls over.
 
flux said:
In advanceHour(int pAdvHour) in the ASClock class shouldn't:
Code:
Day= NewHour/24;
be:
Code:
Day += NewHour/24;
?

In addition, don't you need a reference to whatever ASDate object you using to advance that a day aswell. Knowing me you've already got that and I'm too dumb to spot it!

Out of interest, how come your using the standard AWT rather than swing?

AWT was what I was told to use... This is the first time i ever hear of swing... what is swing?

Code:
Day += NewHour/24;

I don't think the "+" is really needed evertime I run this Day will be something new
 
Code:
Day += NewHour/24;

I don't think the "+" is really needed evertime I run this Day will be something new
If you don't have the plus, everytime the hour changes from 25 to 0 the day will be set to 1. With it the day is incremented by the correct number of days (suggested this way instead of day++ to cater for the possibility of incrementing by >24hrs at a time.)
 
Back