Question

In: Computer Science

In Java The Problem In his book Irreligion, the mathematician John Allen Paulos tells an amusing...

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. Create a new Eclipse project by copying ProjectTemplate or a previous project you have created, naming the new project Pseudoscience. In the src folder of this project and the default package, create a class called ABCDGuesser1.
  2. Edit ABCDGuesser1.java to satisfy the problem requirements stated above, as well as the following additional requirements:
    • Use only while loops for iteration.
    • Check that the inputs provided by the user are valid, i.e., the input for μ is a positive real value and the inputs for w, x, y, and z are each a positive real value not equal to 1. You should implement and use two new static methods declared as follows:

      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) {...}

      Note that you cannot assume the user will provide a number; the user can type pretty much anything. So your methods should read the input as a String (use SimpleReader nextLine() method), then make sure that the input is a real number (use FormatChecker.canParseDouble()), and finally convert the string to a double (use Double.parseDouble()).
  3. Copy ABCDGuesser1.java to create ABCDGuesser2.java. Change it so the while loops in the main method are replaced by for loops (but you should not change the loops in the bodies of getPositiveDouble and getPositiveDoubleNotOne), and so it uses at least one additional private static method.

Solutions

Expert Solution

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.");
           }
       }  
   }
}


Related Solutions

In his book “A Theory of Justice” John Rawls. What are the problems(cons) with his principle...
In his book “A Theory of Justice” John Rawls. What are the problems(cons) with his principle of equal opportunity and his difference principles?
In his book “A Theory of Justice” John Rawls puts forward two principles of justice. According...
In his book “A Theory of Justice” John Rawls puts forward two principles of justice. According to the second principle unequal distributions of social goods are only permissible if (a) the positions for offices are open to all (principle of equal opportunity) and (b) if they are to the greatest benefit of the least advantaged (difference principle). Explain Rawls’s justification for this principle. Why do you find his argument not convincing?
In his book “A Theory of Justice” John Rawls puts forward two principles of justice. According...
In his book “A Theory of Justice” John Rawls puts forward two principles of justice. According to the second principle unequal distributions of social goods are only permissible if (a) the positions for offices are open to all (principle of equal opportunity) and (b) if they are to the greatest benefit of the least advantaged (difference principle). Explain Rawls’s justification for this principle. Why you find his argument not convincing? How would you modify the principle?
Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the...
Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the "Game of Life". Though not a "game" in any traditional sense, it provides interesting behavior that is specified with only a few rules. This project asks you to write a program that allows you to specify an initial configuration. The program follows the rules of LIFE to show the continuing behavior of the configuration. LIFE is an organism that lives in a discrete, two-dimensional...
Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain...
Problem in chapter 15 of Java book. Need a solution please! Problem: Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have three letters associated with them (as seen below). Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use this tool to develop the...
A South African mathematician, John Kerrich, was visiting Copenhagen in 1940 when Germany invaded Denmark. Kerrich...
A South African mathematician, John Kerrich, was visiting Copenhagen in 1940 when Germany invaded Denmark. Kerrich was forced to spend the next five years in an internment camp, and to pass the time, he carried out a series of experiments. One such experiment involved flipping a coin 10,000 times and keeping track how many heads he obtained. Of all the 10,000 coin flips, 5067 came up heads. a.Use the normal approximation to calculate a 95% confidence interval for the true...
A South African mathematician, John Kerrich, was visiting Copenhagen in 1940 when Germany invaded Denmark. Kerrich...
A South African mathematician, John Kerrich, was visiting Copenhagen in 1940 when Germany invaded Denmark. Kerrich was forced to spend the next five years in an internment camp, and to pass the time, he carried out a series of experiments. One such experiment involved flipping a coin 10,000 times and keeping track how many heads he obtained. Of all the 10,000 coin flips, 5067 came up heads. a.Use the normal approximation to calculate a 95% confidence interval for the true...
Looking at the poem Pocahontas to her english husband john rolfe by Paula Gunn Allen answer...
Looking at the poem Pocahontas to her english husband john rolfe by Paula Gunn Allen answer the following question: how does the text invoke, oppose, or revise popular stereotypes or other misconceptions of Native Americans? How does the text represent or portray Native American culture, tradition, childhood/girlhood, womanhood, family, community, and/or identity (past and present)? Please discuss key literary devices that contribute to this portrayal as well.
Case #5: DJ John and his business of "Music on Wheels" John has started his second...
Case #5: DJ John and his business of "Music on Wheels" John has started his second year of the BAS at York University. John was not interested in working for others so he decided to start his own business with what is his passion: be a DJ in parties. A family friend that works as a lending officer in one of Canada's largest banks explained to him how the business plan has to be presented so he can get some...
What amount can Allen deduct as an itemized deduction on his tax return?
Allen paid the following taxes this year:Property taxes on rental property he owns$4,000Property taxes on his own residence3,600Estimated Federal income taxes paid in 20198,000California State income taxes withheld from paycheck  3,400Estimated California Income Taxes paid in 2019500What amount can Allen deduct as an itemized deduction on his tax return?Select one:a. $19,500b. $10,000c. $11,500d. $7,500
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT