Solved

Jave Error incompatible types

Posted on 2008-10-15
15
859 Views
Last Modified: 2010-04-21
I compiled it with no errors but when I run it I get oen error - incompatible types on this line
int day = dateFinder.getEasterSundayDay();

Any help would greatly be appreciated!



*********************************
package eastertester;
public class Easter
{
public Easter(int y)
{
   
    int a = y % 19;
    int b = y / 100;
    int c = y % 100;
    int d = b  / 4;
    int e = b % 4;
    int g = (8 * b + 13) / 25;
     h = (19 * a + b -d - g + 15) % 30;
    int j = c / 4;
    int k = c % 4;
   
    m = (a + 11 * h) / 319;
    r = (2 * e + 2 * j - k - h + m + 32) % 7;        
}
public int getEasterSundayMonth()
{
 int n;  
 return n = (h - m + r + 90) / 25;
}
public int getEasterSundayDay(int n)
{
    int p;  
    return p = (h - m + r + n + 19) % 32;
}

private int h;
private int m;
private int r;
}

******************************
package eastertester;
import java.util.Scanner;

public class EasterTester
{  
    public static void main(String[] args)
    {
       Scanner myScanner  = new Scanner(System.in);
       int year;
       
       System.out.println("Please enter a year of your choice: ");
       year = myScanner.nextInt();
       
       Easter dateFinder = new Easter(year);
       
       int month = dateFinder.getEasterSundayMonth();
       int day = dateFinder.getEasterSundayDay();
                 
       System.out.print("In " + year);
       System.out.println(", Easter is in month " + month);
       System.out.println(" and on day " + day);
       
    }

}
0
Comment
Question by:ca1358
  • 9
  • 4
  • 2
15 Comments
 
LVL 6

Expert Comment

by:javaexperto
ID: 22727113
when you run, get and Exception? incompatible types is a compiler time error, please tell me what Exception do you get.
on the other hand the code you pasted wont compile because you dont pass a int to the getEasterSundayDay() method.
0
 

Author Comment

by:ca1358
ID: 22727135
Error reads
getEasterSunday(int) in eastertester.Easter cannot be applied to ()

incompatible types
found : eastertester.Easter.getEasterSundayDay
required int


What do you mean by

 because you dont pass a int to the getEasterSundayDay() method.

Thank you for you time.
0
 
LVL 6

Expert Comment

by:javaexperto
ID: 22727166
yes, your method getEasterSundayDay() was declared to receive an int so when you make this declaration:
int day = dateFinder.getEasterSundayDay();
the compiler founds that you must pass an int to this method, for example:
 int day = dateFinder.getEasterSundayDay(5);

0
 
LVL 2

Expert Comment

by:jaydm
ID: 22727177
Where did you get your algorithm for calculating the date of Easter?

I found the algorithm by Knuth on a web page (originally found in his book: "Fundamental Algorithms: The Art of Computer Programming, Vol. I" by Donald Knuth).  And adjusted your Easter class based on it.

The problem is that you need to typecast the intermediate portions of the calculations to (int) otherwise you can either get inconsistent results or exceptions.
package eastertester;
 

public class Easter {

	private int month;

	private int day;
 

	public Easter() {

		month = 0;

		day = 0;

	}

	

	public Easter(int y) {

		month = 0;

		day = 0;

		

		calculateEaster(y);

	}
 

	public void calculateEaster(int y) { 

		int golden = (y % 19) + 1;

		int century = (y / 100) + 1;
 

		int solar = ((int) ((3 * century) / 4)) - 12;

		int lunar = ((int) ((8 * century) + 5) / 25) - 5;
 

		int sunday = ((int) ((5 * y) / 4)) + solar - 10;
 

		int epact = (11 * golden + 20 + lunar - solar) % 30;
 

		if ((epact == 24) || (epact == 25) && (golden > 11)) {

			epact ++;

		}
 

		int fullMoon = 44 - epact;
 

		if (fullMoon < 21) {

			fullMoon = fullMoon + 30;

		}
 

		int easter = fullMoon + 6 - ((sunday + fullMoon) % 7);
 

		if (easter > 31) {

			month = 4; // april

			day = easter - 31;

		} else {

			month = 3; // march

			day = easter;

		}        

	}
 

	public int getEasterSundayMonth() {

		return month;

	}
 

	public int getEasterSundayDay() {

		return day;

	}

}

Open in new window

0
 
LVL 2

Expert Comment

by:jaydm
ID: 22727181
I forgot that I also changed your EasterTester class
package eastertester;
 

import java.io.BufferedReader;

import java.io.InputStreamReader;
 

public class EasterTester {  

	public static void main(String[] args) {

		String year;

		InputStreamReader converter = new InputStreamReader(System.in);

		BufferedReader in = new BufferedReader(converter);
 

		System.out.println("Please enter a year of your choice: ");
 

		try {

			year = in.readLine();
 

			Easter dateFinder = new Easter(Integer.valueOf(year));
 

			int month = dateFinder.getEasterSundayMonth();

			int day = dateFinder.getEasterSundayDay();
 

			System.out.print("In " + year);

			System.out.print(", Easter is in month " + month);

			System.out.println(" and on day " + day);

		} catch (Exception e) {
 

		}

	}

}

Open in new window

0
 

Author Comment

by:ca1358
ID: 22727229
Thank you for explaining.  
I am supposed to have the program find the day and month when I put in a year.

So I change this:
public int getEasterSundayDay(int n)
if I take it out and replace it with this
public int getEasterSundayDay()

and now I getting error on this line
 return p = (h - m + r + n + 19) % 32;

operator + cannot be applied to int, n
cammpt fomd symbol
symbol : variable n
location: class eastertester.Easter

operator % cannot be applied to <nulltype>,int

I appreciated the other code, however, this is an assignment and I have to do it this way.  
I am a beginner and do not understand the error messages and only beginning to grasp the methods.

All the help is greatly appreciated.
0
 
LVL 2

Expert Comment

by:jaydm
ID: 22727266
The reason that you are getting an error on the line:

return p = (h - m + r + n + 19) % 32;

Because you are no longer sending in the variable "n" as a parameter to the getEasterSundayDay method.

But, I am still curious where found the algorithm in the first place?

I tried to make those same program changes that you tried regarding the removal of the "n" parameter.

But, the result that came out of the program were incorrect.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:ca1358
ID: 22727320
The text book Cay S. Horstmann "Big Java".

All that was given was
Public int getEasterSundayDay()
{
      Int p;
//Using step 10, compute and return the value of p. Note : This value (p) is the day that Easter falls on.
}

In the book it states
Divide h-m+r+n+19 by 32 to get remainder p.  Ignore the quotient.



0
 

Author Comment

by:ca1358
ID: 22727335
Thank you for explaining.  

I am supposed to have the program find the day and month when I put in a year.

So I change this:
public int getEasterSundayDay(int n)
if I take it out and replace it with this
public int getEasterSundayDay()

and now I getting error on this line
 return p = (h - m + r + n + 19) % 32;

operator + cannot be applied to int, n
cammpt fomd symbol
symbol : variable n
location: class eastertester.Easter

operator % cannot be applied to <nulltype>,int

I appreciated the other code, however, this is an assignment and I have to do it this way.  
I am a beginner and do not understand the error messages and only beginning to grasp the methods.

0
 

Author Comment

by:ca1358
ID: 22727411
The algorithm invented by mathematician Carl Friedrich Gauss in 1800.
0
 

Author Comment

by:ca1358
ID: 22727561
I found this on the net. It explains the exercise.  

If I take out "n" and run it
if complies
put the year of 1999
and give me answer of
In 1999, Easter is in month 4 and on day 0

So it has something to do with this line and the "n"
   return p = (h - m + r + n + 19) % 32;

I hope I have not board you all
************************************
Exercise P3.17. Write a program to compute the date of Easter Sunday. Easter Sunday is the first Sunday after the first full moon of Spring. Use this algorithm, invented by the mathematician Carl Friedrich Gauss in 1800: 1.
 Let y be the year (such as 1800 or 2001)
 
2.
 Divide y by 19 and call the remainder a. Ignore the quotient.
 
3.
 Divide y by 100 to get a quotient b and a remainder c
 
4.
 Divide b by 4 to get a quotient d and a remainder e
 
5.
 Divide 8 * b + 13 by 25 to get a quotient g. Ignore the remainder.
 
6.
 Divide 19 * a + b - d - g + 15 by 30 to get a remainder h. Ignore the quotient.
 
7.
 Divide c by 4 to get a quotient j and a remainder k
 
8.
 Divide a + 11 * h by 319 to get a quotient m. Ignore the remainder.
 
9.
 Divide 2 * e + 2 * j - k - h + m + 32 by 7 to get a remainder r. Ignore the quotient.
 
10.
 Divide h - m + r + 90 by 25 to get a quotient n. Ignore the remainder.
 
11.
 Divide h - m + r + n + 19 by 32 to get a remainder p. Ignore the quotient.
 
Then Easter falls on day p of month n. For example, if y is 2001:
a = 6
b = 20
c = 1
d = 5, e = 0
g = 6
h = 18
j = 0, k = 1
m = 0
r = 6
n = 4
p = 15
Therefore, in 2001, Easter Sunday fell on April 15. Write a class Year with methods getEasterSundayMonth and getEasterSundayDay.

0
 

Author Comment

by:ca1358
ID: 22727710
Found this, I am bound to determine what is wrong.  Would appreciated any help.

Gauss's algorithm
This algorithm for calculating the date of Easter Sunday was first presented by the mathematician Carl Friedrich Gauss:

The number of the year is denoted by Y; mod denotes the remainder of integer division (e.g., 13 mod 5 a 3; see modular arithmetic). Calculate first a, b, and c:

a = Y mod 19
b = Y mod 4
c = Y mod 7
Then calculate

d = (19a + M) mod 30
e = (2b + 4c + 6d + N) mod 7
For the Julian calendar (used in Eastern churches), M = 15 and N = 6, and for the Gregorian calendar (used in Western churches), M and N are from the following table:

Years M N
15831699 22 2
17001799 23 3
18001899 23 4
19002099 24 5
21002199 24 6
22002299 25 0

If d + e < 10 then Easter is on the (d + e + 22)th of March, and is otherwise on the (d + e  9)th of April.

The following exceptions must be taken into account:

If the date given by the formula is 26 April, Easter is on 19 April.
If the date given by the formula is 25 April, with d = 28, e = 6, and a > 10, Easter is on 18 April.

[edit] Meeus/Jones/Butcher Gregorian algorithm
This algorithm for calculating the date of Easter Sunday is given by Jean Meeus in his book Astronomical Algorithms (1991), which in turn cites Spencer Jones in his book General Astronomy (1922) and also the Journal of the British Astronomical Association (1977). This algorithm also appears in The Old Farmer's Almanac (1977), p. 69. The JBAA cites Butcher's Ecclesiastical Calendar (1876).

The method is valid for all Gregorian years and has no exceptions and requires no tables.

Notation is as for the Gauss Algorithm above: all quotients are truncated to integers, thus 7 / 3 = floor(7 / 3) = 2 (not 2 1/3), and 7 mod 3 = 1.

                         Worked example
                         Year(Y) = 1961                   Worked example
                                                                      Year(Y) = 2008
a = Y mod 19       1961 mod 19 = 4                2008 mod 19 = 13
b = Y / 100           1961 / 100 = 19                  2008 / 100 = 20
c = Y mod 100     1961 mod 100 = 61             2008 mod 100 = 8
d = b / 4                19 / 4 = 4                             20 / 4 = 5
e = b mod 4          19 mod 4 = 3                       20 mod 4 = 0
f = (b + 8) / 25      (19 + 8) / 25 = 1                  (20 + 8) / 25 = 1
g = (b - f + 1) / 3    (19 - 1 + 1) / 3 = 6            (20 - 1 + 1) / 3 = 6
h = (19 × a + b - d - g + 15) mod 30        (19 × 4 + 19 - 4 - 6 + 15) mod 30 = 10        (19 × 13 + 20 - 5 - 6 + 15) mod 30 = 1
i = c / 4                    6x1 / 4 = 15                      8 / 4 = 2
k = c mod 4              61 mod 4 = 1                     8 mod 4 = 0
L = (32 + 2 × e + 2 × i - h - k) mod 7             (32 + 2 × 3 + 2 × 15 - 10 - 1) mod 7 = 1      (32 + 2 × 0 + 2 × 2 - 1 - 0) mod 7 = 0
m = (a + 11 × h + 22 × L) / 451                   (4 + 11 × 10 + 22 × 1) / 451 = 0             (13 + 11 × 1 + 22 × 0) / 451 = 0
month = (h + L - 7 × m + 114) / 31            (10 + 1 - 7 × 0 + 114) / 31 = 4 (April)       (1 + 0 - 7 × 0 + 114) / 31 = 3 (March)
day = ((h + L - 7 × m + 114) mod 31) + 1      (10 + 1 - 7 × 0 + 114) mod 31 + 1 = 2     (1 + 0 - 7 × 0 + 114) mod 31 + 1 = 23
                                                                          2 April 1961                                              23 March 2008
0
 
LVL 2

Accepted Solution

by:
jaydm earned 500 total points
ID: 22730949
I just figured out what is missing.  You need to send the month as a parameter to the getEasterSundayDay method.  That is what the variable 'n' was there for.

Put that parameter back into the getEasterSundayDay method and in the Easter class, call it with:

int day = dateFinder.getEasterSundayDay(month);

Then you will get the correct values.
package eastertester;
 

public class Easter {

	private int h;

	private int m;

	private int r;
 

	public Easter(int y) {
 

		int a = y % 19;

		int b = y / 100;

		int c = y % 100;

		int d = b  / 4;

		int e = b % 4;

		int g = (8 * b + 13) / 25;

		h = (19 * a + b -d - g + 15) % 30;

		int j = c / 4;

		int k = c % 4;
 

		m = (a + 11 * h) / 319;

		r = (2 * e + 2 * j - k - h + m + 32) % 7;        

	}
 

	public int getEasterSundayMonth() {

		return (h - m + r + 90) / 25;

	}
 

	public int getEasterSundayDay(int month) {  

		return (h - m + r + month + 19) % 32;

	}

}
 

------------------------------------------------------
 

package eastertester;
 

import java.util.Scanner;
 

public class EasterTester {  

	public static void main(String[] args) {

		Scanner myScanner  = new Scanner(System.in);

		int year;
 

		System.out.println("Please enter a year of your choice: ");

		year = myScanner.nextInt();
 

		Easter dateFinder = new Easter(Integer.valueOf(year));
 

		int month = dateFinder.getEasterSundayMonth();

		int day = dateFinder.getEasterSundayDay(month);
 

		System.out.print("In " + year);

		System.out.print(", Easter is in month " + month);

		System.out.println(" and on day " + day);

	}

}

Open in new window

0
 

Author Comment

by:ca1358
ID: 22731383
Let me clarify.

Change these two
**************************************
public int getEasterSundayMonth() {
            return (h - m + r + 90) / 25;
      }
 
public int getEasterSundayDay(int month) {  
            return (h - m + r + month + 19) % 32;
{
***************************************************
and the tester
change this part
**************************************
public int getEasterSundayDay(int month)
{  
return (h - m + r + month + 19) % 32;
}

Also,  
Thank you for going the extra mile.
0
 

Author Closing Comment

by:ca1358
ID: 31506550
Thank for the extra mile!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now