Create a new project called fractions

Assignment Help JAVA Programming
Reference no: EM131187134

Assignment

In this Application, you are going to write an API for a Fractions class. It will accept fractions as input and perform various mathematical operations on them. Include screenshots of the application running. Since this application is an API and not a complete program,include screenshots of your usage of JUnit.

Programming

NetBeans organizes Java programs as Projects. These projects are where you'll write your program, and they contain all the classes, library references, and code necessary to run the program. Create a new project called Fractions. Within Fractions, create a new class called Fraction. NetBeans will supply a main method; delete this method, as you will be writing an API, not a complete program.

Before performing any operations on two fractions, you should first put them in lowest terms. For this you will also need to compute the greatest common divisor (GCD) of the numerator and denominator and then divide both by the GCD. You can compute the GCD of two numbers by using Euclid's Algorithm. If you're not familiar with this algorithm, there are a number of good definitions available on the web. (You'll find that searching the web for definitions, code snippets, and best practices is an invaluable tool for any programmer.) A Java implementation of Euclid's Algorithm is included as the gcd method below.

In your Fraction class, define two private int fields; these will hold the numerator and denominator of a fraction.

Define four static methods, named add, subtract, multiply, and divide. Each of these methods should be defined to take two Fraction arguments and return a new Fraction. For now, though, each of these methods should return null.

Define a constructor that takes twoint parameters, corresponding to the numerator and denominator of a fraction. For now, the constructor should do nothing with these parameters; the constructor body should be empty. You may think this odd, but the reasons will be explained shortly.

Define one more static method to compute the greatest common divisor and name it gcd. The method should take twoint parameters and return an int result. A Java implementation of gcd that uses Euclid's Algorithm is provided when you get to the testing portion of this assignment.

Finally, define an equals method, with exactly this signature:

Override publicboolean equals(Object o) { ... }

It should initially return false.

Add "doc" comments to each of the methods above. As you may recall, a documentation (doc) comment is a comment placed immediately before a class or method, to provide information to a programmer who wants to use these methods. (Other kinds of documentation, such as how to use the program or the details of how the method does its job, do not belong here.) In NetBeans, choose Tools → Analyze Javadoc, check all relevant checkboxes, and click Fix Selected; this will write skeleton doc comments for you, but you still have to fill in words to tell what each method does, what parameters it expects, and what it returns.

The JUnit Testing Framework Except for the fully-coded gcd method, the methods you've just defined are essentially "stubs"; they provide a framework for the method and show what the input and output will be, but they don't perform the actual computations. At this point, it makes sense to provide a brief explanation of why you should first write method "stubs" that do nothing, or worse, return the wrong answer. It is a generally accepted truism that you should decide what a method is supposed to do before you write the method. When you have a plan, it is much easier to figure out where you are going or what you need to do. Many developers find it effective to write the tests for the methods before they write the actual code. This approach, called Test-Driven Development (TDD), turns out to have a number of benefits, including the following:

- TDD encourages the user of smaller, easily tested methods
- Errors are caught sooner and are more easily localized
- Long and painful debugging sessions are largely eliminated
- The tests actually get written, and are available for future use

Unit is a testing framework that provides you with the ability to perform Test-Driven Development within NetBeans. With JUnit, you can have NetBeans create your test methods for you. Since NetBeans doesn't know what your methods are supposed to do, the test methods it creates will also be stubs; you need to fill in the actual test code.

To install the JUnit plug-in, select your Fraction class in NetBeans, and then go to File > New File... > JUnit > Test for Existing Class. Click Next>, and then enter the name fraction.Fraction in the Class Name: box. Click Finish, choose JUnit 4.x, and then click Select. If you have done this correctly, you will see that NetBeans has written a FractionTest class for you, with several test method stubs.

Run the tests by choosing Run > Test Project (Fraction). You will get an easy-to-read display showing that all your tests failed, as they should have since you haven't written the code yet. Notice that double-clicking on an error message will move the cursor to the failed test.

Since the TDD process requires you to navigate frequently between the code and testing, you may want to adjust NetBeans to show them side by side. To see both Fraction and FractionTestat the same time, click and hold on either of those tabs and drag it to the right or left side of the edit window.

You will find it helpful to create a version of toString for the Fraction class. This will allow failed JUnit tests to print your fractions in a readable format. Here is an example:

public String toString() {

return numerator + "/" + denominator;

}

Creating and testing the methods in the Fraction Class

Usually it's best to work on one method at a time, starting with the methods that don't depend on other methods. Sometimes that's a bit tricky. In this case, if you don't have a proper constructor, you have no fractions to test; and if you don't have a working equals method, you can't test that your results are correct. As a result, you should start by going back and forth between the two.

To develop each method, follow this procedure:

1. Fill in the test stub for the method with some actual tests.

2. Run the tests and make sure your new test code fails.

3. Fill in the method stub with actual code to do the work.

4. Run the tests and make sure the new test passes (if not, debug and repeat).

5. If the method should do more, return to step 1 to add more tests.

The following procedure steps you through this test-code-test-code sequence for the equalsmethod. Afterward, you'll use this same sequence for developing the other methods.

For the equals method, three cases need to be tested.

- Case 1 is when two fractions are identical and the equals method should say they are equal.
- Case 2 is when two fractions are completely different and the equals method should say they are unequal.
- Case 3 is when two fractions are equal but one or both aren't in lowest terms and appear to be different at first glance.

Case 1

In the testEquals method, replace the body (all eight lines) with the following:

assertEquals(new Fraction(1, 2), new Fraction(1, 2));

Now run the tests. All of them should fail, but for now just pay attention to testEquals. It should fail, because you defined an equals method that always returns false.

Next, fill in code for the equals test. It should cast the argument to a Fraction; then test that the two numerators are equal and the two

denominators are equal. Run the tests again. This time, testFraction should pass. (If it doesn't, debug your code and try again.)

Case 2

Add the following line to testEquals:

assertFalse(new Fraction(1, 2).equals(new Fraction(1, 4)));

Run the test again. It does not pass because the default (empty) constructor just sets both the numerator and the denominator to zero. Fix the constructor to save its parameters, and run the tests again; this time testEquals should pass.

Case 3

Add the following line to testEquals:
assertEquals(new Fraction(1, 2), new Fraction(2, 4));

This really should pass because 1/2 = 2/4, but it doesn't. The best way to fix this is to keep all fractions in lowest terms, so that when you call new Fraction(2, 4), it is really saved as 1/2, not as 2/4.

To reduce a fraction to the lowest terms, divide both the numerator and the denominator by their greatest common divisor. Use the gcd method above for testing.

In the testGcd method, add one or more statements such as:
assertEquals(5, Fraction.gcd(15, 25));

Now use this Java implementation of the gcd method in your Fraction constructor:
staticintgcd(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
while (a != b) {
if (a > b) a = a - b;
else b = b - a;
}
return a;
}

Although the gcd method has been provided for you, writing tests will help ensure that you understand what the method does.

Follow the test-code-test-code sequence for the add, subtract, multiply, and divide methods. Take a look at the formulae above for help implementing these methods.

You should also think about whether your code works for negative fractions.

As you can see, the TDD approach involves a lot of going back and forth between the test cases and the code being tested, never spending more than a few minutes in each. This may seem strange at first, but you will find that TDD practically guarantees you will make steady progress, finish your programs, and get them largely correct, because you will rarely if ever have to look through large amounts of code to find a bug.

Verified Expert

This program demonstrate the use of the J Unit that is how to integrate the Unit with the program and run the Junit test cases with Net beans IDE environment. The program is all the about all the operation that can be performed with the fraction i.e. addition, subtraction, multiplication and the division. It also compute the gcd (greatest common divisor) of the numbers. Junit cases are provided to verify all the results generated by the respective operation.

Reference no: EM131187134

Questions Cloud

Football jersey number quantitative or categorical variable : Is a football jersey number a quantitative or a categorical variable?- Support your answer with a detailed explanation.
Provide a summary and overview of the form : Once you have obtained a sample form: Provide a summary and overview of the form. Evaluate any gaps in the knowledge and skills assessed
Describe managerial accounting reports : Which of the following would not describe managerial accounting reports?
How would this punishment interact with the laws : Do you feel that juvenile delinquency can effectively be curbed by the threat of corporal punishment? Why or why not? Do you feel that juvenile delinquency issues should be addressed by the threat of corporal punishment? Why or why not?
Create a new project called fractions : You are going to write an API for a Fractions class. It will accept fractions as input and perform various mathematical operations on them -  Create a new project called Fractions. Within Fractions, create a new class called Fraction. NetBeans will..
Why is ethical behaviour important for managerial accounting : Describe how this difference in accounting policy has affected the companies' financial statements over that period of time.
Inventory shows the cost of completed goods on hand : n a manufacturing company, the calculation of cost of goods sold is the beginning finished goods inventory plus the cost of goods manufactured less the ending finished goods inventory. Raw Materials Inventory shows the cost of completed goods on hand..
How do you feel about your followership style : Take the Leadership Self-Insight 7.1 "The Power of Followership" Test on page 199 of the textbook.  What type of followership style do you have? How do you feel about your followership style
Find as much as you can about it : Please select a computer hardware manufacturer. You may choose Dell, HP, Apple, or a Wireless device to investigate. Find as much as you can about it. Try and get images of the motherboard, front and back of the case so you can see the types of ..

Reviews

mai1187134

8/30/2016 10:12:46 PM

Please include screenshots of the program running ?Please ask what it means by application running program as Fraction class is an API program without the main method so it can't be run unless some driver code is written which is the Junit program in this case. Provide screenshots showing test runs for each of the three cases ? It is asking for the testing of equals methods as for only equal these cases are described which itself come in the JUnit test screenshot which shows equals method run successfully provide one screenshot showing the final run of the API. ? Junit causes the API program run and that screenshot is included in the doc. Show screenshots of the program being tested with JUnit. ? Junit screen shots of tests passed and the output are included

len1187134

8/30/2016 5:17:22 AM

Add screenshots of your program running as part of your submission. Provide screenshots showing test runs for each of the three cases. Then provide one screenshot showing the final run of the API. Show screenshots of your program being tested with JUnit.Save your NetBeans Project and screen shots of the working program as a ".zip" file.

Write a Review

JAVA Programming Questions & Answers

  Find the average of every cell in a two dimensional array

Find the average of every cell in a two dimensional array based on the value of row one and column one.

  Planning software application

The solution is a desktop Course-Planning Software application to replace their existing electronic Course Scheduling - System Features that meet the functional requirements specified in the Requirements Document.

  Write java method which fills the array with random numbers

Write a Java method which takes an integer array parameter and fills the array with random numbers between 1 and 1000, including 1 and 1000.

  Declare an array and initialize it

Which one of the following will declare an array and initialize it with five numbers?

  Design a java application to read a file containing data

Design a Java application that will read a file containing data related to the passengers on the Titanic. The description of the file is shown below. The application should provide statistical results on the passengers including

  Write a program that implements the distribution counting

Write a program that implements the distribution counting sort algorithm as discussed in class to sort a list of letters from a small set {a, b, c, d}. For example, the list contains b, a, c, c, d, d, a, your program should output a, a, b, c, c, d..

  Define output operators for clock and travelclock

Define output operators for Clock and TravelClock. Modify the classes Clock and TravelClock to declare the output operators as friends.

  Concept of web based information system

Design and implement a simple and small email server using the concept of web based information system

  Consumer and business product and discuss

Name a product that could be described as both a consumer and business product and discuss why. Describe its attributes, uses and product perceptions of the customers in both markets.

  Display the values that were typed in to the form

This assignment will focus on creating Java Server Pages (JSP) and deploying them onto a Java Servlet Container.

  Design and implement a java version of the game memory

Design and implement a Java version of the game Memory

  Write a java program that draws a square abcd

Write a java program that draws a square ABCD. The points A and B are arbitrarily specified by the user by clicking the mouse button. The orientation of the points should be counter-clockwise.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd