Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Fractions

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Fractions

    I need help with an assignment I have for school. I'm a sophomore in high school in Computer Science 1. We use Eclipse (Java) for our programs.

    Here is the PDF for the assignment:
    Classes

    I uploaded my Classes project too. Here is what I have so far The class is called "FractionClass" and "FractionClassTester".
    Fraction Class Assignment.pdf

    PLEASE HELP ME!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Fractions

    Do you have any specific questions about your assignment?
    Please post your code and any questions about problems you are having.

    Post everything here on the forum. No links.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fractions

    Assignment:
    You will create a class to represent fractions. The requirements below will establish how this class should be structured.
    Part 1
    * The class should store a numerator and denominator
    (remember, data members should be private at this point in your programs) * The class should have both a default and non*default constructor
    * Do not make any set() accessor methods
    Part 2
    * The class should have the following methods:
    + Fraction add(Fraction other)
    + Fraction subtract(Fraction other)
    + Fraction multiply(Fraction other)
    + Fraction divide(Fraction other)
    + print() : print the fraction to the screen [as a fraction]
    ** NOTE: none of the arithmetic methods should modify the values of either operand; they should simply return the result of the calculation.
    Part 3
    The main method in your tester class should have the following...
    * 10 different Fraction variables initialized with different values
    * 1 additional Fraction variable to store the result of the calculations
    * 5 calls to the addition method, print out the results [not within the add method]
    * 5 calls to the subtraction method, print out the results [not within the subtract method] * 5 calls to the multiply method, print out the results [not within the multiply method]
    * 5 calls to the divide method, print out the results [not within the divide method]


    For some reason my code won't open in Eclipse from my USB. But all I had was part one down.

    --- Update ---

    Please use the link for the Classes. I can't open it for some reason.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Fractions

    If you have questions about your code, post the code and your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fractions

    FractionClass:

    public class FractionClass {
    private int numerator;
    private int denominator;

    public FractionClass()
    {
    numerator = 1;
    denominator = 2;
    }

    public FractionClass(int num, int den)
    {
    numerator = num;
    if (den == 0)
    den =1;
    denominator = den;
    }

    public FractionClass add(FractionClass other)
    {

    return null;
    }

    public FractionClass subtract(FractionClass other)
    {
    return null;
    }

    public FractionClass multiply(FractionClass other)
    {
    int num = numerator * other.numerator;
    int den = denominator * other.denominator;

    return new FractionClass(num, den);
    }

    public FractionClass divide(FractionClass other)
    {
    int num = numerator * other.denominator;
    int den = denominator * other.numerator;

    return new FractionClass(num, den);
    }

    public void printFractionClass()
    {
    // Print values as a fraction

    }
    }

    FractionClassTester:

    public class FractionClassTester {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    }

    }

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fractions

    FractionClass:

    public class FractionClass {
    private int numerator;
    private int denominator;

    public FractionClass()
    {
    numerator = 1;
    denominator = 2;
    }

    public FractionClass(int num, int den)
    {
    numerator = num;
    if (den == 0)
    den =1;
    denominator = den;
    }

    public FractionClass add(FractionClass other)
    {

    return null;
    }

    public FractionClass subtract(FractionClass other)
    {
    return null;
    }

    public FractionClass multiply(FractionClass other)
    {
    int num = numerator * other.numerator;
    int den = denominator * other.denominator;

    return new FractionClass(num, den);
    }

    public FractionClass divide(FractionClass other)
    {
    int num = numerator * other.denominator;
    int den = denominator * other.numerator;

    return new FractionClass(num, den);
    }

    public void printFractionClass()
    {
    // Print values as a fraction

    }
    }


    FractionClassTester:

    public class FractionClassTester {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    }

    }

Similar Threads

  1. Question about for loop to print the sum of series of fractions
    By _K_ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 08:08 AM
  2. [SOLVED] Keeping Negative Fractions
    By Kerrigan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 10th, 2011, 05:03 PM
  3. Fractions Code help
    By SOK in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 9th, 2010, 11:07 AM
  4. Fractions
    By debug in forum Java Theory & Questions
    Replies: 1
    Last Post: March 8th, 2010, 11:29 PM

Tags for this Thread