5 Replies - 2287 Views - Last Post: 25 February 2011 - 05:07 PM Rate Topic: -----

#1 infiniteuse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-February 11

Java HW help: User input

Posted 24 February 2011 - 11:41 PM

Hello, I'm quite new to Java and am trying to design a class that will compute the day and month of Easter when a user has input a specific year. However, I am struggling to get my user input into the class variable I have created.

Here is my class:
public class Easter
{
//Instance Variables
	private int n;
	private int p;
	
//Constructor

	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;
		int h = (19 * a + b - d - g + 15) % 30;
		int j = c / 4;
		int k = c % 4;
		int m = (a + 11 * h) / 319;
		int r = (2 * e + 2 * j - k - h + m + 32) % 7;
		n = (h - m + r + 90) / 25;
		p = (h - m + r + 19) % 32;	
	}//end constructor Easter
		
//toString method
	public String toString() 
	{
		return "The month is: " + n + " and the day is: " + p;
	}//end toString method 
	
}//end class Easter



I know the variable names are not descriptive, but that's how the instructions state to use them.
Here is my driver:

import java.util.Scanner;
public class FindEaster
{
	public static void main(String[] args)
	{
		System.out.println("*This program calculates the month and day Easter falls on for the year input*");
		
		Scanner year = new Scanner(System.in);
		int y = 0;
		Easter newEaster = new Easter(y);
		System.out.print("Please enter year: ");
		y = year.nextInt();
		System.out.println(newEaster);
		
	}//end main
}//end class FindEaster.java


When I run the program, I keep getting this output:

*This program calculates the month and day Easter falls on for the year input*
Please enter year: 2011
The month is: 4 and the day is: 5

I doesn't seem to matter what year I input, the output is always "The month is: 4 and the day is: 5", which leads me to believe that my "y" variable is being read as "0" and not the given user input.
Thanks for any help you guys could provide.

Is This A Good Question/Topic? 0
  • +

Replies To: Java HW help: User input

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Java HW help: User input

Posted 25 February 2011 - 03:35 AM

Well, the year is never printed because you never print it. this is your toString method:
return "The month is: " + n + " and the day is: " + p;

as you can see, you only print day and month.
Was This Post Helpful? 0
  • +
  • -

#3 infiniteuse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-February 11

Re: Java HW help: User input

Posted 25 February 2011 - 01:58 PM

Hey, thanks for the reply, but I'm not actually looking for the year as an output. I want the (yyyy) to be a user input, and then the program is supposed to take that and input it as "y" into the algorithm I have in the class Easter.java. Problem is, I can't figure out how to initialize (?) the user input to equal the "y" variable.

For example:

If the user were to input 2001, the output would be:

Please enter year: 2011 //user input
The month is: 4 and the day is: 24

However, I am currently only getting:

Please enter year: 2011 //No matter what the user inputs here, I get the below answer, which leads me to believe my program is not taking the user input and inserting it as my "y" variable.
The month is: 4 and the day is: 5
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: Java HW help: User input

Posted 25 February 2011 - 03:54 PM

You are passing y to newEaster before you get user input. You need to get user input first, then instantiate your Easter object.
int y = 0;
Easter newEaster = new Easter(y);
System.out.print("Please enter year: ");
y = year.nextInt();
System.out.println(newEaster);


Was This Post Helpful? 1
  • +
  • -

#5 infiniteuse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-February 11

Re: Java HW help: User input

Posted 25 February 2011 - 04:52 PM

Ah, many thanks. That kills me that was so simple...
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: Java HW help: User input

Posted 25 February 2011 - 05:07 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1