Question 1 Unlimited tries (Convert decimals to fractions) Write a program that prompts the user to...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Question 1 Unlimited tries (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is 13/4 Sample Run 2 Enter a decimal number: -0.45452 The fraction number is -11363/25000 Class Name: Exercise13_19 If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html. (Ⓒ) 1 /** Return numerator */ public long getNumerator() { return numerator; } /** Return denominator */ public long getDenominator() { return denominator; } /** Add a rational number to this rational */ public Rational add(Rational secondRational) { long n = numerator * secondRational.getDenominator () + denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rat onal (n, d); } /** Subtract a rational number from this rational */ public Rational subtract (Rational secondRational) { long n = numerator * secondRational.getDenominator () denominator * secondRational.getNumerator(); } /** Multiply a rational number to this rational */ public Rational multiply (Rational secondRational) { long n = numerator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rational(n, d); long d = denominator * secondRational.getDenominator(); return new Rational (n, d); } /** Divide a rational number from this rational */ public Rational divide (Rational secondRational) { long n = numerator * secondRational.getDenominator(); long d = denominator * secondRational.numerator; return new Rational (n, d); } @Override public String toString() { if (denominator == 1) return numerator + ""; return numerator + "/" + denominator; } else } @Override // Override the equals method in the Object class public boolean equals (Object other) { if (this.subtract ((Rational) (other))).getNumerator () return true; } } } @Override // Implement the abstract intValue method in Number public int intValue() { return (int) doubleValue(); else } return false; @Override // Implement the abstract floatValue method in Number public float floatValue() { return (float) doubleValue (); @Override // Implement the doubleValue method in Number public double doubleValue() { return numerator * 1.0 / denominator; } } == @Override // Implement the abstract longValue method in Number public long longValue() { return (long) doubleValue(); @Override // Implement the compareTo method in Comparable public int compareTo (Rational o) { if (this.subtract (o).getNumerator () > 0) return 1; 0) else if (this.subtract (o).getNumerator () < 0) return -1; return 0; else /* You have to use the following template to submit to Revel. Note: To test the code using the CheckExerciseTool, you will submit entire code. To submit your code to Revel, you must only submit the code enclosed between // BEGIN REVEL SUBMISSION */ import java.util.Scanner; // BEGIN REVEL SUBMISSION public class Exercise13_19 { public static void main(String[] args) { // Write your code } // END REVEL SUBMISSION } // END REVEL SUBMISSION // Copy from the book class Rational extends Number implements Comparable<Rational> { // Data fields for numerator and denominator private long numerator = 0; private long denominator = 1; /** Construct a rational with default properties */ public Rational () { this (0, 1); } /** Construct a rational with specified numerator and denominator */ public Rational (long numerator, long denominator) { long gcd = gcd (numerator, denominator); this.numerator = (denominator> 0 ? 1 : -1) * numerator / gcd; this.denominator = Math.abs (denominator) / gcd; } /** Find GCD of two numbers */ private static long gcd (long n, long d) { long nl Math.abs (n); = long n2 Math.abs (d); int gcd 1; for (int k = 1; k <= n1 && k <= n2; k++) { if (n1 % k == 0 && n2 % k == 0) gcd = k; } } return gcd; Question 1 Unlimited tries (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is 13/4 Sample Run 2 Enter a decimal number: -0.45452 The fraction number is -11363/25000 Class Name: Exercise13_19 If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html. (Ⓒ) 1 /** Return numerator */ public long getNumerator() { return numerator; } /** Return denominator */ public long getDenominator() { return denominator; } /** Add a rational number to this rational */ public Rational add(Rational secondRational) { long n = numerator * secondRational.getDenominator () + denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rat onal (n, d); } /** Subtract a rational number from this rational */ public Rational subtract (Rational secondRational) { long n = numerator * secondRational.getDenominator () denominator * secondRational.getNumerator(); } /** Multiply a rational number to this rational */ public Rational multiply (Rational secondRational) { long n = numerator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rational(n, d); long d = denominator * secondRational.getDenominator(); return new Rational (n, d); } /** Divide a rational number from this rational */ public Rational divide (Rational secondRational) { long n = numerator * secondRational.getDenominator(); long d = denominator * secondRational.numerator; return new Rational (n, d); } @Override public String toString() { if (denominator == 1) return numerator + ""; return numerator + "/" + denominator; } else } @Override // Override the equals method in the Object class public boolean equals (Object other) { if (this.subtract ((Rational) (other))).getNumerator () return true; } } } @Override // Implement the abstract intValue method in Number public int intValue() { return (int) doubleValue(); else } return false; @Override // Implement the abstract floatValue method in Number public float floatValue() { return (float) doubleValue (); @Override // Implement the doubleValue method in Number public double doubleValue() { return numerator * 1.0 / denominator; } } == @Override // Implement the abstract longValue method in Number public long longValue() { return (long) doubleValue(); @Override // Implement the compareTo method in Comparable public int compareTo (Rational o) { if (this.subtract (o).getNumerator () > 0) return 1; 0) else if (this.subtract (o).getNumerator () < 0) return -1; return 0; else /* You have to use the following template to submit to Revel. Note: To test the code using the CheckExerciseTool, you will submit entire code. To submit your code to Revel, you must only submit the code enclosed between // BEGIN REVEL SUBMISSION */ import java.util.Scanner; // BEGIN REVEL SUBMISSION public class Exercise13_19 { public static void main(String[] args) { // Write your code } // END REVEL SUBMISSION } // END REVEL SUBMISSION // Copy from the book class Rational extends Number implements Comparable<Rational> { // Data fields for numerator and denominator private long numerator = 0; private long denominator = 1; /** Construct a rational with default properties */ public Rational () { this (0, 1); } /** Construct a rational with specified numerator and denominator */ public Rational (long numerator, long denominator) { long gcd = gcd (numerator, denominator); this.numerator = (denominator> 0 ? 1 : -1) * numerator / gcd; this.denominator = Math.abs (denominator) / gcd; } /** Find GCD of two numbers */ private static long gcd (long n, long d) { long nl Math.abs (n); = long n2 Math.abs (d); int gcd 1; for (int k = 1; k <= n1 && k <= n2; k++) { if (n1 % k == 0 && n2 % k == 0) gcd = k; } } return gcd;
Expert Answer:
Answer rating: 100% (QA)
Below is the completed Java program for converting decimals to fractions using the provided template ... View the full answer
Related Book For
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these programming questions
-
assumption 1 assume there will be 4000 visits, each visit will yield 1400 of revenue
-
A block of ice with mass 2.00 kg slides 0.750 m down an inclined plane that slopes downward at an angle of 36.9 below the horizontal. If the block of ice starts from rest, what is its final speed?...
-
A fictional news report stated that starship Enterprise had just returned from a 5-year voyage while traveling at 0.70c. (a) If the report meant 5.0 years of Earth time, how much time elapsed on the...
-
For the following exercises, rewrite the given equation in standard form, and then determine the vertex (V), focus (F), and directrix (d) of the parabola. x = 8y 2
-
True or False: If \(\operatorname{IRR}(\mathrm{A})>\operatorname{IRR}(\mathrm{B})\), then \(\operatorname{ERR}(\mathrm{A})>\operatorname{ERR}(\mathrm{B})\).
-
Walshs Fruit Company contracts with growers in Ohio, Pennsylvania, and New York to purchase grapes. The grapes are processed into juice at the farms and stored in refrigerated vats. Then the juice is...
-
A Rolls-Royce Phantom weighing 2.49 x 104 N is traveling in the +x direction with an initial speed of 40.2 m/s when the driver hits the brakes. The car slows to a stop in 5.60 s. If the acceleration...
-
Question 1: How much is the average capital balance of Ms. King for the year 2019? Question 2: How much is the average capital balance of Ms. Bee for the year 2019? Question 3: How much is the share...
-
A new partner contributes property worth $ 1 0 0 , 0 0 0 and an adjusted basis to new partner of $ 2 5 , 0 0 0 to a partnership in return for a 2 0 % interest in the partnership. The property was...
-
Write a program that extracts all the text without any tags and attribute values from an HTML document. Sample text: News Telerik Academyaims to provide free real-world practical training...
-
The Hotel has net income of $ 1 0 million, sales of $ 4 0 0 million, assets of $ 2 0 0 million, and liabilities of $ 1 0 0 million. The Domain has the same profit margin but an asset turnover ratio...
-
4. Payday loans are very short-term loans that charge very high interest rates. You can borrow $650 today and repay $700 in 10 days. What is the compound annual rate implied by this percent rate...
-
A 85-cm long solenoid is to produce a magnetic field of 0.85 mT at its center. How much current should the solenoid carry if it has 2000 turns of wire? A. Normal format with 3 SF
-
Define your assumptions and identify the cost management opportunities including but not limited to the following: o High quality material with higher variable cost, sure. But how about lower quality...
-
On average there are four traffic accidents in a city during one hour of rush-hour traffic. Use the Poisson distribution to calculate the probability that in one such hour there arc (a) No accidents...
-
Write a GUI program for Programming Exercise 8.19, as shown in Figures 16.49ab. Let the user enter the numbers in the text fields in a grid of 6 rows and 7 columns. The user can click the Solve...
-
Rewrite Exercise 37.8 using servlets. Data from Exercise 37.8 Write a servlet that enables the user to change the password from an HTML form, as shown in Figure 37.30a. Suppose the user information...
-
A perfect binary tree is a complete binary tree with all levels fully filled. Define a new class named BSTWithTestPerfect that extends BST with the following methods: (Hint: The number of nodes in a...
-
Gross profit is (A) Excess of sales over cost of goods sold (B) Sales less purchases (C) Cost of goods sold + opening inventory (D) Net profit less expenses of the period
-
To find the value of closing inventory at the end of a period we (A) Do this by physically counting the inventory (i.e. stocktaking) (B) Look in the inventory account (C) Deduct opening inventory...
-
Net profit is calculated in the (A) Trading account (B) Profit and loss account (C) Trial balance (D) Statement of financial position
Study smarter with the SolutionInn App