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));
}
}
Related
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 );
I am solving a staircase problem and came up with multiple solutions in mind. It looks like below:
Question: You will be given number of stairs say N. What is the maximum step you can make?
For N = 5, The maximum step you can make is 2 because
5 = 1 + 2 + 2
Similarly for 8, its, 8 = 1 + 2 + 3 + 2, maximum step is 3
Similarly for 16, its, 16 = 1 + 2 + 3 + 4 + 5 + 1, maximum step is 5.
When the next number is less than previous then the series will stop.
Clearly, The maximum step is maximum number in the series.
Solution 1:
I came up with a simple solution. It works fine but not optimized.
Below is the following code:
public static long stairCase(long N) {
long i = 1;
long curr = N;
while (i < N) {
curr = curr - i;
if (i >= curr) {
return i;
}
i = i + 1;
}
return i;
}
Solution 2:
Then i figured out that its n(n+1) / 2. So, if i put n(n+1)/2 = no. of
Stairs.I cant get the solution by calculating its roots and taking
highest of the roots. My Code looks like below but it doesn't works
for N = 16 and many other cases.
int b = 1;
int var = (1) - (4 * -c);
double temp1 = Math.sqrt(var);
double root1 = (-b + temp1) / (2 * a);
double root2 = (-b - temp1) / (2 * a);
double root1Abs = Math.abs(root1);
double root2Abs = Math.abs(root2);
return (long) (root1Abs > root2Abs ? Math.floor(root1Abs) : Math
.floor(root2Abs));
Solution 3:
I came up with another solution but still its not working for N = 4
and many other cases. Below is my code:
double answer = Math.sqrt(c * 2);
return (long) (Math.floor(answer));
Does Anyone have the optimized solutions(preferably in constant time) because the input is too big(long).
m = number of stair
n = result
The equation is
n * (n+1) < 2m
The solution is
n < (sqrt(8*m+1)-1)/2
We try to find maximum integer so
n = floor((sqrt(8*m+1)-1)/2)
The Java Code:
import java.io.*;
public class Solution {
public static int staircase(int m){
return (int) Math.floor((Math.sqrt(8*(double)m+1)-1)/2);
}
public static void main(String[] args){
System.out.println("Result:"+staircase(16));
}
}
Actually I figured the solution by myself ..I should use 2* c
int a = 1;
int b = 1;
int var = (1) - (4 * -2 * c);
double temp1 = Math.sqrt(var);
double root1 = (-b + temp1) / (2 * a);
double root2 = (-b - temp1) / (2 * a);
return (long) (root1 > root2 ? Math.floor(root1) : Math.floor(root2));
This question already has answers here:
Why double width = 50/110000; the output is 0.000000000000000?
(3 answers)
Closed 9 years ago.
We've been set a task to calculate user input. My code compiles, however when I test it my output for the refund is always 0 :(
Users are meant to enter their distance and self contribution when prompted, but what exactly is wrong with my formula for refund? Can anybody shed some light on this for me?
import java.util.*;
public final class UserCalc {`
public static void main(String[] args) {
Scanner scanner = new Scanner(System. in );`
System.out.print("Please enter the distance ");
int distance = scanner.nextInt();
System.out.print("Please enter the percentage of self contribution ");
int selfcon = scanner.nextInt();
int trainprice = (75 + (2 / 10)) * distance;
int carprice = (26 + (7 / 10)) * distance;
int refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();
}
}
Because result of ((100 - selfcon)/100) is always zero and you works with integers you should use float or double.
You are using integer arithmetic rather than floating point arithmetic, so expressions like 2/10 evaluate to 0.
double distance = scanner.nextDouble();
System.out.print("Please enter the percentage of self contribution ");
double selfcon = scanner.nextDouble();
double trainprice = (75 + (2 / 10)) * distance;
double carprice = (26 + (7 / 10)) * distance;
doulbe refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();
Okay so I am a complete Java noob, and I'm trying to create a program for class that runs a quadratic equation using scanner inputs. So far what I've got is this:
import java.util.*;
public class QuadraticFormulaSCN {
public static void main(String[]args) {
System.out.println("insert value for a:");
Scanner scan1 = new Scanner(System.in);
double a = scan1.nextDouble();
System.out.println("insert value for b:");
Scanner scan2 = new Scanner(System.in);
double b = scan2.nextDouble();
System.out.println("insert value for C:");
Scanner scan3 = new Scanner(System.in);
double c = scan3.nextDouble();
double answer =((Math.sqrt(Math.pow(b,2)-(4*a*c))-b)/2);
double final2 =(-b + Math.sqrt(Math.pow(b,2)-(4*a*c)))/2;
System.out.println("The x values are:" + answer + final2);
}
}
But I get a weird output, specifically NaNaN... What do I do to fix this? What am I doing wrong?
I'm a little late to answer, but I corrected your problems (described in the other answers), fixed one of your calculations, and cleaned up your code.
import java.util.*;
public class Test {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Insert value for a: ");
double a = Double.parseDouble(s.nextLine());
System.out.println("Insert value for b: ");
double b = Double.parseDouble(s.nextLine());
System.out.println("Insert value for c: ");
double c = Double.parseDouble(s.nextLine());
s.close();
double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
if (Double.isNaN(answer1) || Double.isNaN(answer2))
{
System.out.println("Answer contains imaginary numbers");
} else System.out.println("The values are: " + answer1 + ", " + answer2);
}
}
NaN is something you get when the calculation is invalid. Such as dividing by 0 or taking the squareroot of -1.
When I test your code with a = 1, b = 0 and c = -4 the answers is 2.02.0
The formatting is not right and the calculation of final2 is not negated.
Otherwise the code is right.
To improve you could check whether the discriminant is negative.
double d = b*b -4 * a * c;
if (d < 0){
System.out.println("Discriminant < 0, no real solutions" );
return;
}
double x1 = (-b -sqrt(d))/(2*a);
double x2 = (-b +sqrt(d))/(2*a);
System.out.format("The roots of your quadratic formula are %5.3f and %5.3f\n",x1,x2);
Or, if you prefer support for solutions from the complex domain:
if (d < 0) {
System.out.println("Discriminant < 0, only imaginary solutions");
double r = -b / (2 * a);
double i1 = -sqrt(-d) / (2 / a);
double i2 = sqrt(-d) / (2 / a);
System.out.format("The roots of your quadratic formula are (%5.3f + %5.3fi) and (%5.3f + %5.3fi)\n",r, i1, r, i2);
return;
}
You are getting NaN because you are attempting to take the square root of a negative number. In math that's not allowed unless you are allowing complex numbers, e.g. 1 +/- 2i.
This can happen in quadratic formulas when the discriminant (the thing in the square root) is negative, e.g. x^2 + 6*x + 100: b^2 - 4ac = 36 - 400 = -364. Taking the square root of a negative number in Java leads to NaN. (not a number)
To test for NaN, use Double.isNaN and handle the NaN appropriately.
In addition, your calculations are incorrect even if NaN isn't being encountered:
$ java QuadraticFormulaSCN
insert value for a:
1
insert value for b:
5
insert value for C:
6
The x values are:-2.0-2.0
This should have outputted 2.0 and 3.0
You should only do the calculation when
discriminant is equal or greater than zero
if(((Math.pow(b,2)-(4*a*c))>= 0){ /* Calculation here */ }
else {/*error message or complex number calculus*/};
One thing I always try to do is put all my math in appropriate parenthesis to avoid an, all too easy, Order of Operations mistake. The NaN is saying "Not a number." You would also get that message if the user input numbers that could not produce a result, such as a trying to get the square root of a negative number. Also, just as a note, you can save sometime by only using on Scanner for a,b, and c.
public class QuadraticFormula{
public static void main(String[] args){
java.util.Scanner input = new java.util.Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double quadPos = (-b + Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
double quadNeg = (-b - Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
System.out.println("-b - = " + quadNeg + "\n-b + = " + quadPos);
}
}
This is the code that I have thus far. The goal of the project is to have the user enter any integers for a, b, c for the ax^2+bx+c equation. For some reason I am not getting the correct roots for any numbers that are input into the program. Can anyone point out my wrong doings?
import java.util.*;
public class Quad_Form {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double a = 0;
double b = 0;
double c = 0;
double discrim = 0;
double d = 0;
System.out.println("Enter value for 'a'");
String str_a = sc.nextLine();
a = Integer.parseInt(str_a);
System.out.println("Enter value for 'b'");
String str_b = sc.nextLine();
b = Integer.parseInt(str_b);
System.out.println("Enter value for 'c'");
String str_c = sc.nextLine();
c = Integer.parseInt(str_c);
double x1 = 0, x2 = 0;
discrim = (Math.pow(b, 2.0)) - (4 * a * c);
d = Math.sqrt(discrim);
if(discrim == 0){
x1 = (-b + d) / (2* a);
String root_1 = Double.toString(x1);
System.out.println("There is one root at: " + root_1);
}
else {
if (discrim > 0)
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);
String root_1 = Double.toString(x1);
String root_2 = Double.toString(x2);
System.out.println("There are two real roots at:" + root_1 + "and" + root_2);
}
if (discrim < 0){
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);
String root_1 = Double.toString(x1);
String root_2 = Double.toString(x2);
System.out.println("There are two imaginary roots at:" + root_1 + "and" + root_2);
}
}
}
#Smit is right about one of the issues, but there's a second one as well.
Math.sqrt(discrim) won't work when discrim is negative. You should be taking Math.sqrt(Math.abs(discrim)) instead.
a, b, c, d are double and you are parsing them as Integer. So this could be one of problem.
Use
Double.parseDouble();
Another problem is you can not make square root of negative numbers. This will result in NaN. For that use following, but you should handle that properly to get exact result.
Math.sqrt(Math.abs());
Moreover you should use following formula for getting roots
Taken from Wikipedia Quadratic equation
Class Double
Class Math