In: Computer Science
In Java
The Problem
In his book Irreligion, the mathematician John Allen Paulos tells an amusing story about the Dutch astronomer Cornelis de Jager, "who concocted the following algorithm for personalized physical constants, [and] used it to advance a charming theory about the metaphysical properties of Dutch bicycles." First select any positive real-valued universal physical or mathematical constant that seems interesting to you, e.g., π, e, Planck's constant, the atomic weight of molybdenum, the boiling point of water in Kelvin, whatever you like. Call this constant μ. Then select any four positive real numbers not equal to 1 that have personal meaning to you, e.g., your favorite number, day or month or year of birth, age in fortnights or seconds, weight in stones or grams, height in furlongs or millimeters, number of children, house number, apartment number, zip code, last four digits of SSN, whatever you like. Call these four personal numbers w, x, y, and z.
Now consider the de Jager formula waxbyczd, where each of a, b, c, and d is one of the 17 numbers {-5, -4, -3, -2, -1, -1/2, -1/3, -1/4, 0, 1/4, 1/3, 1/2, 1, 2, 3, 4, 5}. The "charming theory" asserts that the de Jager formula with your four personal numbers can be used to approximate μ within a fraction of 1% relative error. For example, suppose you choose to approximate the mean distance from the earth to the moon in miles: μ = 238,900. And suppose you are an OSU sports fan, so your personal numbers are the number of wins in OSU's last national championship season (14), the seating capacity of Ohio Stadium (102,329), the year of Jesse Owens' four gold medals in Berlin (1936), and your jersey number when you played high school field hockey (13). Then the value of 14-5102329119361/2134 is about 239,103, which is within about 0.08% of μ.
Your job is to create a Java program that asks the user what constant μ should be approximated, and then asks in turn for each of the four personal numbers w, x, y, and z. The program should then calculate and report the values of the exponents a, b, c, and d that bring the de Jager formula as close as possible to μ, as well as the value of the formula waxbyczd and the relative error of the approximation to the nearest hundredth of one percent (see SimpleWriter print(double, int, boolean) for a method you may find useful for this). Note that your program must find the combination of exponents that minimizes the error of the approximation of μ and then print the exponents, best approximation, and corresponding relative error. (Essentially this program could be used to disprove the "charming theory" by finding μ, w, x, y, and z such that the best approximation of μ results in a relative error that is greater than 1%.)
Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * Repeatedly asks the user for a positive real number until the user enters * one. Returns the positive real number. * * @param in * the input stream * @param out * the output stream * @return a positive real number entered by the user */ private static double getPositiveDouble(SimpleReader in, SimpleWriter out) {...}
/** * Repeatedly asks the user for a positive real number not equal to 1.0 * until the user enters one. Returns the positive real number. * * @param in * the input stream * @param out * the output stream * @return a positive real number not equal to 1.0 entered by the user */ private static double getPositiveDoubleNotOne(SimpleReader in, SimpleWriter out) {...} |
Please go through code. I can not compile it because I don't have I idea about SimpleReader class. from where you importing it.
CODE:
ABCDGuesser1.java
import components.simplereader.SimpleReader;
import components.simplewriter.SimpleWriter;
import components.utilities.FormatChecker;
public class ABCDGuesser1
{
/**
* Repeatedly asks the user for a positive real number
until the user enters
* one. Returns the positive real number.
*
* @param in
* the input stream
* @param out
* the output stream
* @return a positive real number entered by the
user
*/
private static double getPositiveDouble(SimpleReader
in, SimpleWriter out)
{
double n = 0;
String str = "";
boolean flag = false;
while(flag == false)
{
out.println("Enter number: ");
str =
in.nextLine();
if(FormatChecker.canParseDouble(str))
{
n = Double.parseDouble(str);
if(n > 0)
return n;
else
{
out.println("Entered value is
invalid.");
}
}
else
{
out.println("Entered value is invalid.");
}
}
}
/**
* Repeatedly asks the user for a positive real number
until the user enters
* one. Returns the positive real number.
*
* @param in
* the input stream
* @param out
* the output stream
* @return a positive real number entered by the
user
*/
private static double
getPositiveDoubleNotOne(SimpleReader in, SimpleWriter out)
{
double n = 0;
String str = "";
boolean flag = false;
while(flag == false)
{
out.println("Enter number: ");
str =
in.nextLine();
if(FormatChecker.canParseDouble(str))
{
n = Double.parseDouble(str);
if(n > 0 && n != 1.0)
return n;
else
{
out.println("Entered value is
invalid.");
}
}
else
{
out.println("Entered value is invalid.");
}
}
}
}
ABCDGuesser2.java
import components.simplereader.SimpleReader;
import components.simplewriter.SimpleWriter;
import components.utilities.FormatChecker;
public class ABCDGuesser2
{
/**
* Repeatedly asks the user for a positive real number
until the user enters
* one. Returns the positive real number.
*
* @param in
* the input stream
* @param out
* the output stream
* @return a positive real number entered by the
user
*/
private static double getPositiveDouble(SimpleReader
in, SimpleWriter out)
{
double n = 0;
String str = "";
boolean flag = false;
for(;flag == false;)
{
out.println("Enter number: ");
str =
in.nextLine();
if(FormatChecker.canParseDouble(str))
{
n = Double.parseDouble(str);
if(n > 0)
return n;
else
{
out.println("Entered value is
invalid.");
}
}
else
{
out.println("Entered value is invalid.");
}
}
}
/**
* Repeatedly asks the user for a positive real number
until the user enters
* one. Returns the positive real number.
*
* @param in
* the input stream
* @param out
* the output stream
* @return a positive real number entered by the
user
*/
private static double
getPositiveDoubleNotOne(SimpleReader in, SimpleWriter out)
{
double n = 0;
String str = "";
boolean flag = false;
for(;flag == false;)
{
out.println("Enter number: ");
str =
in.nextLine();
if(FormatChecker.canParseDouble(str))
{
n = Double.parseDouble(str);
if(n > 0 && n != 1.0)
return n;
else
{
out.println("Entered value is
invalid.");
}
}
else
{
out.println("Entered value is invalid.");
}
}
}
}