Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:12:56+00:00 2026-05-27T10:12:56+00:00

I have written this code as my answer to an assignment I recieved in

  • 0

I have written this code as my answer to an assignment I recieved in AP Computer Science, and my teacher is having my redo this because the answers don’t work. I can only compile it on my machine, so I am relying on my teacher as to what results is produces now, which are below. This assignment is do by midnight, tonight, and the procrastinator I am waited till now to ask for help here, but still, it is VERY much appreciated. Also, it’s my first time on here, so sorry that some of the code isn’t formatted properly. Thanks for the help!

Results ATM

if a = 3/4 and b = 5/6

a.add(b) produces 35/24; This should be 19/12
a.subtract(b) produces 1/0; This should be -1/12
a.divide(b) produces 11/1; This should be 9/10
a.multiply(b) produces 6/-5; This should be 5/8

Here’s the code.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.TitledBorder;

class Fraction{
    private int numer;
    private int denom;
    private Fraction answer;

    //Definition
    public Fraction(int num, int den)
    {
        numer = num;
        denom = den;
        simplify();
    }


    //Simplify
    void simplify()
    {
        int gcd = findGCD(numer, denom);
        numer /= gcd;
        denom /= gcd;
    }


    //GCD Function
    int findGCD(int a, int b)
    { int temp;
        while(b != 0)
        {
            temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }


    //GetNumerator
    public int getNumer()
    {
        return numer;
    }

    //GetDenominator
    public int getDenom()
    {
        return denom;
    }

    //Fraction Add Method
    Fraction add(Fraction x)
    {
        Fraction answer;

        if(denom == x.denom)
        {
            answer = new Fraction(x.numer + numer, denom);
            }
        else
        {
          int den = denom * x.getDenom();
          int num = numer * x.getNumer();
          num += x.getNumer() * denom;
          answer = new Fraction(num, den);

        }
     return answer;
    }


    //Fraction Subtract Method
    Fraction subtract(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer - x.numer, denom);
        }
        else
        {
            int den = denom / x.getDenom();
            int num = numer / x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }

    //Fraction Multiply Method
    Fraction multiply(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer + x.numer, denom);
        }
        else
        {
            int den = denom + x.getDenom();
            int num = numer + x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }

    //Fraction Divide Method
    Fraction divide(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer - x.numer, denom);
        }
        else
        {
            int den = denom - x.getDenom();
            int num = numer - x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }


    //@Override
    public boolean equals(Fraction x)  
    {
        boolean answer = false;

        if(numer == x.numer && denom == x.denom)
      {
            answer = true;
        }

        return answer;
    }


    //ToString
    public String toString()
    {
    return (Integer.toString(numer) + "/" +
             Integer.toString(denom));
    }


    //Main Method
    public void main(String[]args)
    {
        Fraction a = new Fraction(3,4);
        Fraction b = new Fraction(5,6);
        System.out.println(a.toString());
        System.out.println(b.toString());

        Fraction c = a.add(b);
        System.out.println(c.toString());

        c = a.subtract(b);
        System.out.println(c.toString());

        c = a.divide(b);
        System.out.println(c.toString());

        c = a.multiply(b);
        System.out.println(c.toString());





    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:12:57+00:00Added an answer on May 27, 2026 at 10:12 am

    So just one thing to begin : use all the time getDenom(), because in your code sometimes it is getDenom, sometimes denom so ..

    For add() method:
    You’re wrong in the case where denoms are differents.
    You have to do :

    denom = this.getDenom() * x.getDenom();
    num = this.getNum() * x.getDenom() + x.getNumer() * this.getDenom();
    //and check if simplification is possible.
    return new Fraction(num,denom);
    

    And it is kind of the same things for others operations. Check you multiply by the good denominator or numerator because it seems it is not the case, and don’t forget to simplify.

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I have this code in my ASP.NET application written in C# that is trying
In our application, I have seen code written like this: User.java (User entity) public
I have always written my boolean expressions like this: if (!isValid) { // code
This is similar to this question, except the code is already written. I have
I have written a code from the answer given here My sample code is
I have written this clone method for when the parent of the Employee class
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.