Formatting and logic errors in java - java

brand new to java so im struggling a bit in this intro to java class. Doing my best to make this program work but it is just not happening and im not sure why. Right now running into an error where it is telling me it cannot find symbol?
Ive spent HOURS on this and i feel like im getting closer. Your help is much appreciated.
Here is the assignment:
Write a program DayOfWeek.java that takes a date as input from the command line arguments and prints the day of the week that date falls on. Your program should take three command-line arguments: m (month), d (day), and y (year). For m use 1 for January, 2 for February, and so forth. For output print 0 for Sunday, 1 for Monday, 2 for Tuesday, and so forth. Use the following formulas, for the Gregorian calendar:
y0 = y - (14 - m) / 12
x = y0 + y0/4 - y0/100 + y0/400
m0 = m + 12 * ((14 - m) / 12) - 2
d0 = (d + x + (31*m0)/ 12) mod 7
For example, on what day of the week was August 2, 1953?
y = 1953 - 0 = 1953
x = 1953 + 1953/4 - 1953/100 + 1953/400 = 2426
m = 8 + 12*0 - 2 = 6
d = (2 + 2426 + (31*6) / 12) mod 7 = 2443 mod 7 = 0 (Sunday)
and the output needs to look exactly like this:
8 2 1953 falls on 0.
And here is my code so far:
public class DayOfWeekTest
{
public static void main(String[] args)
{
int monrh, day, year;
month = Integer.parseInt(args[0]);
day = Integer.parseInt(args[1]);
year = Integer.parseInt(args[2]);
int y0 = year - (14 - month) / 12;
System.out.println(y0);
int x = y0 + y0/4 - y0/100 + y0/400;
System.out.println(x);
int m0 = month + 12 * ((14 - month) / 12) - 2;
System.out.println(m0);
int d0 = (day + x + (31 * m0)/ 12) % 7;
System.out.println(d0);
System.out.println("Falls on a " + d0);
}
}
Any help would be greatly appreciated, If you wouldnt mind kind of explaining what im doing wrong that would be even better. I really want to learn this stuff. thanks so much guys.

Your code does exactly what it should!
instead of printing y0, x and m0, you should instead print year, month and day. That will give you the right output. Your code should look something along the lines of this:
int y0 = year - (14 - month) / 12;
int x = y0 + y0/4 - y0/100 + y0/400;
int m0 = month + 12 * ((14 - month) / 12) - 2;
int d0 = (day + x + (31 * m0)/ 12) % 7;
System.out.print( month + " " + day + " " + year + " falls on " + d0 );

Related

where is the issue within my easter calculator program?

Can't find the calculation issue within my easter calculator program. If the input is 2019, the month output is 4, and day is -2 for some reason. 4 would be April which is correct but the day is wrong. Advice to make the code more efficient and solution?
import java.util.*;
import java.lang.Math;
class Main {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("\nWelcome to the Easter Calculator. Please enter the current year below.");
double y = userInput.nextInt();
double p = y/100;
double q = y - (19*(y/19));
double r = (p-17)/25;
double s = p - (p/4) - ((p-r)/3) + (19*q) + 15;
s = s - (30*(s/30));
s = s - ((s/28)*(1-((s/28)*(29/(2+1))*((21-q)/11))));
double t = y + (y/4) + s + 2 - p + (p/4);
t = t - (7*(t/7));
double u = s - t;
double m = 3 + ((u+40)/44);
double d = u + 28 - (31*(m/4));
System.out.println("Year = "+Math.round(y));
System.out.println("Month = "+Math.round(m));
System.out.println("Day = "+Math.round(d));
}
}
In the third line of s calculation
s = s - ((s / 28) * (1 - ((s / 28) * (29 / (2 + 1)) * ((21 - q) / 11))));
you have phrase (29 / (2 + 1) which is itself suspicuous and does not correspond to the notes you've attached. There should be (29 / (s + 1) instead.
Constructions like
y - (19 * (y / 19))
t - (7 * (t / 7))
s - (30 * (s / 30))
will always produce practical 0 been calculated in double all through.
There should be an integer division in brackets.
Is there any note to the page you quoted?
If we assume here we calculate sort of scaled operational margin, and use int division like this
y - (19 * (((int)y) / 19)) or y - (19 * (int)(y / 19)) (which delivers smaller delta)
in all these places, we'll get 17-th of April for 2019. Looks valid data, but not valid Easter date for the year (not sure about that, actually).
Turns out the solution was to simply convert all variable types from doubles to integers. Thanks for the help.
import java.util.*;
import java.lang.Math;
class Main {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("\nWelcome to the Easter Calculator. Please enter the current year below.");
int y = userInput.nextInt();
int p = y/100;
int q = y - (19*(y/19));
int r = (p-17)/25;
int s = p - (p/4) - ((p-r)/3) + (19*q) + 15;
s = s - (30*(s/30));
s = s - ((s/28)*1-((s/28)*(29/(s+1))*((21-q)/11)));
int t = y + (y/4) + s + 2 - p + (p/4);
t = t - (7*(t/7));
int u = s - t;
int m = 3 + ((u+40)/44);
int d = u + 28 - (31*(m/4));
System.out.println("Year = "+Math.round(y));
System.out.println("Month = "+Math.round(m));
System.out.println("Day = "+Math.round(d));
}
}

How to turn minutes into years and days?

In the textbook it explains how to convert seconds into minutes with how many seconds remain but the question I have is as follows.
Write a program that prompts the user to enter the minutes (e.g., 1
billion), and displays the number of years and days for the minutes.
For simplicity, assume a year has 365 days. Here is an example.
Enter the number of minutes: 100000000
100000000 minutes is approximately 1902 years and 214 days.
What I currently have is as follows:
import java.util.Scanner;
public class Ch2_Exercise2_7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt user for number of minutes
System.out.println("Enter the number of minutes:");
int minutes = input.nextInt();
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
System.out.println(minutes + " minutes is " + year + " years and " + remainingMinutes + " days ");
}
}
With what I have it isn't giving me the remaining minutes into days. For example, if I put in 525600 minutes it gives me 1 year and 365 days when it should just be 1 year.
I'm using Java Eclipse. Any help would be greatly appreciated! My apologies in advance if I post the code incorrectly.
You screwed up a bit over here:
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
You took the total number of minutes and divided by 1440, so the number of days you got was wrong. You should have taken the remainder and then divided by 1440.
Another thing was in your print statement. You wrote the number of minutes left after one year as the number of days.
This should work:
// Number of minutes in a year
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = remainingMinutes / 1440;
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days.");
My main method is separate:
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes) {
double days = minutes / 60 / 24;
double years = days / 365;
double remainingDays = days % 365;
System.out.println((minutes < 0 ? "Invalid Value" : minutes + " min = " + (int) years + " y and " + (int) remainingDays + " d"));
}
}
int mins,hours,remainingDays,days,years;
cout << "Enter minutes:";
cin >> mins;
hours = mins/60;
days = hours/24;
years = days/365;
remainingDays = days % 365;
cout << years << " years " << remainingDays << " days " << endl;
return 0;
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes)
{
if(minutes<0) System.out.println("Invalid Value");
else
{
long hours = minutes/60;
long day = hours/24;
long years = day/365;
long remainingDays = day % 365;
System.out.println(minutes +" min = "+ years+" y and "+remainingDays +" d");
}
}
import java.util.Scanner;
public class Prog13 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter The Minute");
int min=sc.nextInt();
int year= min/(365*24*60);
int remaining_min=(min%24*60*60);
int day=remaining_min/24*60;
System.out.println( min + " " + " Minutes is approximately:"+ year + " " +"year"+ ' '+ day + "Day" );
}
}
hello you can use this result
import java.util.Scanner;
public class MinToYearsAndDays {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("number of minutes:");
int minutes = input.nextInt();
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = (remainingMinutes / 1440);
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days ");
}
}
public class Mints_to_days_years {
// Given some numbers of minutes calculate the number of days and years
// from the given minutes.
// 1hr = 60 minutes
// 1 day = 24 hours
// 1 year = 365 days
public static void find_days_years(long minutes) {
if(minutes < 0) {
System.out.println("Invalid number of minutes.");
}
// to find the number of years, divide the nminutes by total numbers of minutes in a year.
long years = minutes / (60 * 24 * 365);
// then find the remainder minutes that does not goes exactly into year
long years_remainder = minutes % (60 * 24 * 365);
// divide the remainder by number of minutes in a day to find the numbers of days
long days = years_remainder / (60 * 24);
System.out.println(minutes + " mints = " + years + " yrs and " + days + " days.");
}
public static void main(String[] args) {
find_days_years(525600);
find_days_years(1051200);
find_days_years(561600);
}
}

print the last line of a loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
What I'm trying to do is print the results in increments of 5 - DONE. Then, print only the last line of the loop. For ex: if z=26, print results of z # 5, 10, 15, 20, 25. Then println with z # 26. I'm stuck and can't figure it out.
for (int i = 1; i <= z; i++) {
b = b + (b * y) + x + w;
if (i % 5 == 0)
//input 1
System.out.println("In " + i + " years, IRA value: " + b);
//input 2 - value of IRA when retirement is reached
System.out.println();
I don't know what initial values b, x, w and y got so I just initialized them somehow ..
But I think you want something like this:
public class HelloWorld{
public static void main(String []args){
int z = 26;
int b = 0;
int x = 1;
int w = 2;
int y = 1;
for (int i = 1;i<=z;i++) {
b = b+(b*y)+x+w;
if (i % 5 == 0)
System.out.println("In " + i + " years, IRA value: " + b);
}
System.out.print(z);
}
}
Output:
In 5 years, IRA value: 93
In 10 years, IRA value: 3069
In 15 years, IRA value: 98301
In 20 years, IRA value: 3145725
In 25 years, IRA value: 100663293
26
Change your condition to
if(i%5==0 || i==z)
Adding an or (|| i==z) in the if will do the work
for (int i = 1;i<=z;i++) {
b = b+(b*y)+x+w;
if (i%5==0 || i==z)
//input 1
System.out.println("In " + i + " years, IRA value: " + b);
//input 2 - value of IRA when retirement is reached
System.out.println();
//end
}

Calculation of times off milliseconds to time

This code below is used to format milliseconds. The calculations are off by a long shot. I need some help with it. Output of my code is below as well as the milliseconds into a Date class.
public static void main(String[] args) {
System.out.println("Wrong Time: " + getTime(999999999 * 599));
}
//Being called into this method is milliseconds = 598999999401
public static String getTime(long miliseconds) {
int years = (int) ((miliseconds / (1000*60*60*24*7*52*12)));
int months = (int) (miliseconds / (1000*60*60*24*7*52) % 12);
int weeks = (int) ((miliseconds / (1000*60*60*24*7)) % 52);
int days = (int) ((miliseconds / (1000*60*60*24)) % 7);
int hours = (int) ((miliseconds / (1000*60*60)) % 24);
int minutes = (int) ((miliseconds / (1000*60)) % 60);
int seconds = (int) (miliseconds / 1000) % 60;
Date date = new Date(598999999401L);//This gets the real time
System.out.println("Right Time: " + date.getYear() + " years " + (int)(date.getMonth() % 12) + " months " + (int)(date.getDay() % 52) + " weeks "
+ (int)(date.getDay() % 7) + " days " + (int)(date.getHours() % 24) + " hours " + (int)(date.getMinutes() % 60) + " minutes " +
+ (int)(date.getSeconds() % 60) + " seconds");
return (years <= 0 ? "" : years + " year" + (years != 1 ? "s" : "")) +
(months <= 0 ? "" : " " + months + " month" + (months != 1 ? "s" : "")) +
(weeks <= 0 ? "" : " " + weeks + " week" + (weeks != 1 ? "s" : "")) +
(days <= 0 ? "" : " " + days + " day" + (days != 1 ? "s" : "")) +
(hours <= 0 ? "" : " " + hours + " hour" + (hours != 1 ? "s" : "")) +
(minutes <= 0 ? "" : " " + minutes + " minute" + (minutes != 1 ? "s" : "")) +
(seconds <= 0 ? "" : " " + seconds + " second" + (seconds != 1 ? "s" : ""));
}
Correct Output(Date class)
Right Time: 88 years 11 months 6 weeks 6 days 14 hours 53 minutes 19 seconds
Wrong Output(My method)
Wrong Time: 1 month 3 weeks 2 days 3 hours 25 minutes 45 seconds
UPDATE 1 (NEW CALCULATIONS)(Still has logic errors):
int years = (int) ((miliseconds / (1000*60*60*24*7*4*12)));
int months = (int) (miliseconds / (1000*60*60*24*7*4) % 12);
int weeks = (int) ((miliseconds / (1000*60*60*24*7)) % 4);
int days = (int) ((miliseconds / (1000*60*60*24)) % 7);
int hours = (int) ((miliseconds / (1000*60*60)) % 24);
int minutes = (int) ((miliseconds / (1000*60)) % 60);
int seconds = (int) (miliseconds / 1000) % 60;
Two issues.
Your code seems to reflect a belief that there are 52 weeks in a month. These are the two lines at fault.
int years = (int) ((miliseconds / (1000*60*60*24*7*52*12)));
int months = (int) (miliseconds / (1000*60*60*24*7*52) % 12);
Also, you're using int where you should be using long. The maximum int number of milliseconds is just under 25 days, so you can never do date/time arithmetic with int.
One problem is int overflow since your main method is using int literals, not longs. Try:
System.out.println("Wrong Time: " + getTime(999999999L * 599L));
Also, don't use magic numbers. Use constants that make sense and that make your code self-commenting:
private static final int MILI_PER_SEC = 1000;
private static final int SEC_PER_MIN = 60;
private static final int MIN_PER_HR = 60;
private static final int HR_PER_DAY = 24;
private static final int DAYS_PER_YR = 365;
and then,
int years = (int) (miliseconds / (MILI_PER_SEC * SEC_PER_MIN * MIN_PER_HR *
HR_PER_DAY * DAYS_PER_YR));
Also, your calculations are all off. You shouldn't be using weeks in the year calculation at all. Use constants, have them make sense. And how do you know that the "correct" result is in fact correct? You are using deprecated code, and Date doesn't use miliseconds the way that you think that it does. It calculates the date relative to January 1, 1970, 00:00:00 GMT as per the Date API.

whats wrong with my java code to compute the say of the week for a given date [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The pseudocode for an algorithm to compute the day of the week for a given
date from the year 1753 onwards is as follows.
Let
d
be the day of the month (from 1 up to 31),
m
be an integer denoting the
month of the year (where
1
denotes January,
2
denotes February, and so on),
and
y
denote the year. The algorithm then performs the following steps in order:
If m is less than 3
Add 12 to m and subtract one from y
End if
Set C to be the year of the century (e.g., 10 for the year 2010)
Set D to be the century (e.g., 20 for the year 2010)
Divide 13 * (m + 1) by 5 and call the quotient W
Divide C by 4 and call the quotient X
Divide D by 4 and call the quotient Y
Set Z to be W + X + Y + d + C - 2 * D
Divide Z by 7 and call the remainder day
If day is less than 0
Add 7 to day
End if
The value of
day
then gives the day of the week, with
0=
Saturday,
1=
Sunday, up
to
6=
Friday
the code i have so far is:
public static String dayOfWeek( SimpleDate date ) {
// TO BE COMPLETED
int[] d = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
int[] m = {1,2,3,4,5,6,7,8,9,10,11,12};
int y = SimpleDate(int year);
if (m < 3) {
m + 12;
y - 1;
}
C = SimpleDate(int year[2:3];
D = SimpleDate(int year[0:1];
W = 13 * (m + 1) / 5;
X = C / 4;
Y = D / 4;
Z = W + X + Y + d + C - 2 * D;
day = Z % 7;
if (day < 0) {
day + 7;
}
im not sre how to set the year for C and D and at the beginning aswell.
also where i ahve used code such as m + 12 i get an error saying that + is not a statment
How about using java.util.Calendar and do
calendar.get(Calendar.DAY_OF_WEEK);
Using joda:
MutableDateTime dateTimeInstance = new MutableDateTime().setYear(year).setMonth(month).setDay(dayOfMonth); // and so on per [the docs](http://joda-time.sourceforge.net/api-release/org/joda/time/MutableDateTime.html)
String dayName = dateTimeInstance.dayOfWeek().getAsText();

Categories