Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction The goal of this project is to develop a computer program in the Java language which requires the design of a class that can

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Introduction The goal of this project is to develop a computer program in the Java language which requires the design of a class that can be used to perform arithmetic involving fractions. For the purposes of this project, the following facts about fractions will be used. 1. A fraction is of the form p/q, where p and q are integers, and q +0. 2. A fraction p/q is said to be in its lowest terms, or reduced form, if p and q are relatively prime (or do not have a common factor greater than 1). For instance, the fraction 4/6 is not in its lowest terms because it can be reduced to its equivalent 2/3. We say the fraction 2/3 is in its lowest terms or reduced form because 2 and 3 do not have a common factor other than 1. A fraction can be reduced to its lowest terms by dividing both the numerator and the denominator by their greatest common divisor (or god for short). For example, the fraction 12/18 is reduced to 2/3 by dividing both 12 and 18 by their god, which is 6. 3. A fraction p/qis in reduced form if god (p, q) = 1. Otherwise, p/q is reduced as [p/gcd (p, q)]/[g/gcd(p, q)] 4. The gcd can be calculated using: a, gcd(a,b) = if b = 0; gcd (b, a modb), otherwise. 5. The sum of two fractions a/b and c/d can be computed by first calculating a/b + c/d = (ad+bc)/bd and then reducing the resulting fraction to its lowest terms. 6. Similarly, we can subtract one fraction from another by observing that a/b - c/d = (ad-bc) /bd 7. The product of two fractions a/b and c/d can be computed by first obtaining the fraction ac/bd and then reducing it to its lowest terms. 8. The ratio (a/b)/(c/d) of two fractions can be calculated as ad/bc and reducing the resulting fraction to its lowest terms. Designing a Class for Doing Arithmetic with Fractions You will design a class named Fraction, which contains: i. A private int data field numerator that defines the numerator of the fraction, and has a default value of 0. ii. A private int data field denominator that defines the denominator of the fraction, and has a default value of 1. iii. A no-arg constructor that creates a fraction with default values. iv. A constructor that creates a fraction with specified values for the numerator and denominator. v. The accessor and mutator methods for all data fields. vi. The method print Fraction that will print the fraction in the form numerator / denominator using System.out.print("..."). vii. The method reduceFraction that will reduce the fraction to its lowest terms. viii. The static method gcd that computes the greatest common divisor of two given integers. ix. The method addFraction that will add this Fraction to another Fraction. x. The method subtract Fraction that will subtract another Fraction from this Fraction. xi. The method multiplyFraction that will multiply this Fraction and another Fraction. xii. The method divideFraction that will divide this Fraction by another Fraction. Specifically, the following tasks should be completed for this project: 1. Draw the UML for the class named Fraction. 2. Implement the Fraction class and test your implementation using a Test Fraction class, which will carry out the following tasks: a. Create an object named myFraction1 using the no-arg constructor. b. Set the numerator and denominator of my Fraction1 by prompting the user to pro- vide the numerator and the denominator for myFractioni. c. Prompt the user to provide the numerator and the denominator for another object named myFraction2 VIUL LIL HITILIMUI AU LIL ULIMIALUI IUI ILLYPIALLIUIII. c. Prompt the user to provide the numerator and the denominator for another object named myFraction2. d. Create the object myFraction2 with the constructor that uses the input data pro- vided. e. Print both fractions invoking the printFraction method, and labeling your frac- tions appropriately on the output. f. For each fraction, extract the numerator and denominator separately and print them as well (you cannot use the print Fraction method for this), identifying them with appropriate labels. i. Calculate myFraction1+myFraction2 and print out the result with appropriate labeling University of North Florida School of Computing Dr. Asaithambi Programming II Project-3 Page 3 j. Calculate myFraction1-myFraction and print out the result with appropriate labeling k. Calculate myFractionlxmyFraction and print out the result with appropriate labeling 1. Calculate myFraction1/myFraction and print out the result with appropriate labeling. Java Program for Testing the Fraction Class Java Program for Testing the Fraction Class Creating a working program is an iterative process. You may have to repeatedly make changes in your source code for removing syntax errors to begin with. Then you may need to make changes that will address any logical errors that you detect based on the results you obtain. Thus, I would recommend developing your code incrementally. Incremental development would mean writing a few lines of code at a time to complete a simple task that forms part of your final solution and running the program to make sure that those lines of code work. This way, if at any time you add something new an error pops up, you will be able to focus your attention on the latest addition in your code. In essence, you will be dividing the given problem into sub-problems first and solve the sub-problems one by one. This requires carrying out a careful analysis of the problem, followed by an appropriate design formulation for your solution. 1. On Eclipse, choose File, New, and Java Project. 2. Type a name like Fractions Project in the Project Name field. 3. Click Finish to create the project. A project named Fractions Project is created. 4. Creating a Class. Choose File, New, and Class. 5. Type TestFraction in the Name field. 6. Check the option public static void main(String[] args). 7. Click Finish to generate the template for the source code TestFraction.java. 8. If you are using the command line mode, you can create a plain text file named Test Fraction.java. 9. You will then enter (type the lines of the source code for TestFraction.java. 10. Right below the end of the block for the TestFraction.java class, in the same file, you will add class named Fraction. 11. See the example of an Account class along with a corresponding Account Test class (that contains the main method)) provided in the textbook in Chapter 7. This is the structure we will follow in order to create the Fraction class and the Test Fraction (driver) class. 12. Now, use the information provided at the beginning of this description document to define the Fraction class. 13. You may develop the Fraction class incrementally as well, and test what you have coded from the main method in the TestFraction class. Programming II Project-3 Page 4 14. Compiling and Running. To run the program, click on the icon that looks like the play button on an audio device. The program should be run on the Console pane within the Eclipse IDE and the output displayed there. 15. If you are using the command line environment, you will use javac and java commands to compile and execute the program. Code Development Stages for TestFraction.java and Fraction.java 1. Stage 1: Enter code for creating the object myFractionl with the no-arg constructor. This is just one line of code. However, this means that you also have to enter code for the no-arg constructor within the Fraction class. Again the example of the Account class from the text will help. Remember the default values for the no-arg constructor: numerator is 0 and the denominator is 1. 2. Stage 2: Prompt and receive two integers, call them nl and d1 to represent the actual numerator and the actual denominator you wish to use for myFraction. Once they are received, you will set the numerator and the denominator values to nl and di respectively. This requires entering code for the methods that (re)define the data fields in the Fraction class. 3. Stage 3: For myFraction2, you will first prompt and obtain two integers, call them n2 and d2, and then create the object myFraction2 using the second constructor which will take n2 and d2 as args. This would mean that you will add this new constructor within the Fraction class. 4. Stage 4: Print out the fractions myFraction and my Fraction by calling the method print Fraction from the Test Fraction class. The code for print Fraction lies within the Fraction class. 5. Stage 5: To get the data fields separately, the Fraction class should include accessor methods. This is what you will use to print out the numerator and the denominator separately. 6. Stage 6: Write code for addFraction first and test it out. This will use the formula discussed earlier. The other methods this will require are the gcd and reduceFraction methods The code for these methods will reside within the fraction class 6. Stage 6: Write code for addFraction first and test it out. This will use the formula discussed earlier. The other methods this will require are the gcd and reduceFraction methods. The code for these methods will reside within the Fraction class. 7. Stage 7: Once the addFraction method has been tested, the code for it can be repli- cated and adapted to produce code for all the other methods like subtractFraction, multiplyFraction, and divideFraction. All of these methods must be within the Fraction class. The TestFraction class will contain code for invoking these meth- ods for the addition, subtraction, multiplication, and division involving myFraction1 and myFraction2 A sample run of the code is shown on the next page. Other details are left for you to figure out and complete as part of the project. University of North Florida School of Computing Dr. Asaithambi Programming II Project-3 Page 5 Enter numerator for myFraction1: 5 Enter denominator for my Fraction1: 8 Enter numerator for my Fraction2: 7 Enter denominator for myFraction2: 12 my Fraction1 - 5 / 8 myFraction2 = 7 / 12 The numerator of myFraction1 is: 5 The denominator of my Fraction1 is: 8 Enter numerator for myFraction2: 7 Enter denominator for my Fraction2: 12 myFraction1 = 5 / 8 myFraction2 = 7 / 12 The numerator of myFraction1 is: 5 The denominator of my Fraction1 is: 8 The numerator of my Fraction2 is: 7 The denominator of my Fraction2 is: 12 5 / 8 + 7 / 12 = 29 / 24 5 / 8 - 7 / 12 = 1 / 24 5 / 8 * 7 / 12 = 35 / 96 (5 / 8) / (7 / 12) = 15 / 14 What To Submit on Canvas You will submit the following items on Canvas: 1. A Word file that contains the UML diagram for the Fraction class. 2. The source code for your program as the Test Fraction.java file that also includes the Fraction class. IMPORTANT: There is a grading rubric provided on Canvas for this project. Review this rubric both before you begin and just before you are ready to submit and make sure that your submission will get the maximum possible points. Also, note that I will be testing your program with arbitrary fractions, and not just the ones shown in the sample run above. VERY IMPORTANT: Your source code must also contain the following elements to receive full credit. The rubric will reflect the number of points allocated for these elements as well. 1. A block comment at the top of your program that includes your name, and a brief 2-3 lines of description of your program. 2. A block comment at the top of each method within the Fraction class that briefly (2-3 lines) what the method does. 3. A line comment for each stage in the TestFraction class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions

Question

1. Explain how business strategy affects HR strategy.

Answered: 1 week ago