The Artima Developer Community
Sponsored Link

Java Answers Forum
New to Programming revised

1 reply. Most recent reply: Jun 21, 2002 9:14 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 1 reply on 1 page
Kymberly

Posts: 6
Nickname: kym
Registered: Jun, 2002

New to Programming revised Posted: Jun 21, 2002 2:20 PM
Reply to this message Reply
I am having a problem computing a date for Easter Sunday from 1982 to 2048 as follows (all variables are of type int):
a is year %19
b is year % 4
c is year % 7
d is (19 * a + 24) % 30
e is (2 * b + 4 * c + 6 * d + 5) % 7

Easter Sunday is March (22 + d + e)3(cubed)

I am trying to write an application that inputs the year and outputs the date (month and day) of Easter Sunday for that year using two buttons. This programming thing is new to me this year and I am so frustrated trying to figure out how to write simple applications. Can somebody give me some advice how to make this simple application work?

import java.awt.*;
import java.awt.Frame.*;
import java.awt.event.*;
import java.lang.*;

public class p287ex3
{

private static class TwoButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{



int year;
int a;
int b;
int c;
int d;
int e;

a = year % 19;
b = year % 4;
c = year % 7;
d = (19 * a + 24) % 30;
e = (2 * b + 4 * c + 6 * d + 5) % 7;

if (year >= 1982 && year <= 2048);
{
result =(22 + d + e) ;Math.sqrt(3);
}
else
{
inputFrame.dispose();
System.exit(0);
};
}

private static Frame inputFrame;
private static TextField inputField;
private static Label entryLabel;
private static Label outputLabel;
private static Button enter;
private static Button clear;



public static void main(String[] args);
{
Button clear;
Button enter;
TwoButtonListener listener;


inputFrame = new Frame ();
entryLabel = new Label ("Enter year here");
outputLabel = new Label ("", Label.CENTER);
inputField = new TextField ("",4);


{


listener = new TwoButtonListener();
enter = new Button ("Enter");
enter.setActionCommand ("Enter");
enter.addActionListener (listener);
clear = new Button ("Clear");
clear.setActionCommand ("Clear");
clear.addActionListener (listener);


inputFrame.setLayout(new GridLayout (3,3));
inputFrame.add(entryLabel);
inputFrame.add(inputField);
inputFrame.add(outputLabel);
inputFrame.pack();
inputFrame.show();

inputFrame.addWindowlistener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
inputFrame.dispose(0);
System.exit(0);
}
});
}
}
}
}


Re: New to Programming revised Posted: Jun 21, 2002 9:14 PM
Reply to this message Reply
Posted by: Charles Bell    Posts: 519 / Nickname: charles / Registered: Feb, 2002


import javax.swing.*;

public class EasterSundayCalculator{

public EasterSundayCalculator(){
init();
}

public static void main(String[] args){
new EasterSundayCalculator();
}

public void init(){
boolean done = false;
while (!done){
int year = getYear();
JOptionPane.showMessageDialog(null,calculateEasterSunday(year));
done = (JOptionPane.showConfirmDialog(null,"Would you like to calculate another?") != JOptionPane.YES_OPTION);
}
System.exit(0);
}

public int getYear(){
int year = -1;
while (year < 0){
String message = "Enter a 4 digit year between 1982 and 2048";
String input = JOptionPane.showInputDialog(null,message);
try{
year = Integer.parseInt(input);
}catch(NumberFormatException nfe){
System.err.println("NumberFormatException: " + nfe.getMessage());
year = -1;
}
System.err.println("year: " + year);
if ((year < 1982)|| (year > 2048)) {
year = -1;
JOptionPane.showMessageDialog(null,"That was not a valid year input. Try again!","Invalid year", JOptionPane.ERROR_MESSAGE);
}
}
return year;
}


/** a is year %19
* b is year % 4
* c is year % 7
* d is (19 * a + 24) % 30
* e is (2 * b + 4 * c + 6 * d + 5) % 7
* Easter Sunday is March (22 + d + e)3(cubed)
*/
public String calculateEasterSunday(int year){

int a= year %19;
int b = year % 4;
int c = year % 7;
int d = (19 * a + 24) % 30;
int e = (2 * b + 4 * c + 6 * d + 5) % 7;
int result = (22 + d + e) * (int)Math.sqrt(3);
String month = "March";
if (result > 31){
month = "April";
result = result - 31;
}
return "Easter Sunday is " + month + " "
+ String.valueOf (result) + " in the year "
+ String.valueOf(year);
}


}


Topic: Naming object references at compile or run time? Previous Topic   Next Topic Topic: Java Code Safety

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use