Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java Help me solve me this

dragonzack
dragonzack 


Hey there I was wondering if you can help me Requirements: Create a java class called Fraction whose constructor accepts two integer arguments which define the numerator and the denominator of a fraction object. The constructor will call a private method named reduce() which will reduce the fraction. This method will accept no arguments, it will operate on the private variables numerator and denominator defined by the constructor. This method will not return anything. The reduce() method will call a private method named findGCD(). This method accepts no arguments. This method will return an integer, the greatest common divisor (gcd). This value, gcd, will be used to reduce the fraction. The Fraction class will have a public method named display() which will print out to the console the value of the numerator and denominator. Write accessor methods getNumerator() and getDenominator() which are public methods and return the appropriate integer. The Fraction class will have a public method named add() which will accept an object of type Fraction and return a new object of type Fraction. The work done by this method will add the passed in object values to the host object values and save the result in the new object which is returned to the main line of the program. The main() method of your application will prompt the user for a numerator and an denominator twice to define two objects of type Fraction. If the user enters a zero for the denominator stay in a loop prompting for another integer until you get something non-zero. The main() method will print the two fractions using the display() method. The main() method will add the two fractions together and display the sum. Include documentation which can be read by the javadoc utility. Include features suitable to an application of this size. Your documentation should include a general description of what this application does. (click to enlarge) Suggestions: Read this to help you with the math. The two sample outputs shown below are just that, samples. You will need to test your program many times with a variety of data. A big part of software testing is in carefully designing your data to test boundary conditions. Data should be chosen in such a way that all possible paths of execution through your code are performed by at least one set of inputs. You are not responsible for exception handling at this point in the course but go ahead and try some non-integer data anyway and get a sense of the error messages. The purpose of testing software is to define where it fails not to confirm that it works. It is not listed as a requirement but try negative numbers. Does it still work? If not, why not? I added somone images, I have done some but having trouble with some issue. thanks Attachments Tags Clarifications

Answers

Java Fraction Solution

Seeker
Seeker 



Java source code is attached Attachments
Fraction.zip (1 K) Download
Preview
System.out.println("First denominator: " + f1.getDenominator()); System.out.println("Second numerator: " + f2.getNumerator()); System.out.println("Second denominator: " + f2.getDenominator()); f1.display(); f2.display(); System.out.println("Reduced: " + f1.getReducedNum() + " / " + f1.getReducedDenom()); System.out.println("Reduced: " + f2.getReducedNum() + " / " + f2.getReducedDenom()); Fraction addedFraction = f1.add(f2); System.out.println("Sum

Preview
private void reduce(){ int lcm = findGCD(); this.reducedNum = this.numerator / lcm; this.reducedDenom = this.denominator / lcm; } public void display(){ System.out.println("Fraction: " + this.numerator + " / " + this.denominator); } public Fraction add(Fraction frac){ // multiply both

Screenshots
Purchase Answer

Fraction source code in Java

SW_wizard
SW_wizard 



Implemented as per the requirement in two versions:

One version does not handle negative numbers
Second version handles negative numbers appropriately

Explanation as to why second version will work for negative numbers as opposed to first version is also available in the attached zip

Attachments
FractionsProject.zip (3 K) Download
Preview
account, and hence gcd calculation will not work correctly for project: Fractions_NegativeNumHandled negative numbers (either numerator or denominator or

Preview
simpler form denom = denom/gcd; //divide by gcd to get simpler form } private int findGCD(){ int a = Math.abs(num); //removing minus sign if any - only for gcd calculation purpose int b = Math.abs(denom); //removing minus sign if any - only for

Preview
do{ System.out.println("First denominator"); b = scan.nextInt(); }while(b==0); Fraction f1 = new Fraction(a,b); System.out.println("Second numerator"); a = scan.nextInt(); do{ System.out.println("Second denominator"); b = scan.nextInt();

Preview
numerator, int denominator){ num = numerator; denom = denominator; reduce(); //fraction reduced to its simplest form } private void reduce(){ int gcd = findGCD(); num = num/gcd; //divide by gcd to get simpler form denom = denom/gcd; //divide

Preview
new Scanner(System.in); int a,b; System.out.println("First numerator"); a = scan.nextInt(); do{ System.out.println("First denominator"); b = scan.nextInt(); }while(b==0); Fraction f1 = new Fraction(a,b); System.out.println("Second numerator"); a

Screenshots
Purchase Answer

Fraction

mariobeka
mariobeka 



I have implemented the class Fraction, based on what you were saying.
It handles any type of Integer Value (Negative + non Negative)
Attachments
Fraction.java (2 K) Download Preview
} public static void main (String[] args) throws NumberFormatException, IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); Fraction f1, f2, sum; System.out.print("First numerator: "); int num_1 = Integer.parseInt(input.readLine()); System.out.print("First denumerator: "); int denom_1 = Integer.parseInt(input.readLine()); while (denom_1 == 0) { System.out.print("Denominator zero! It should be greater then or

Screenshots
Purchase Answer