Results 1 to 2 of 2
  1. #1
    dnawiam is offline Member
    Join Date
    Jun 2013
    Rep Power

    Default help with assignment

    import hsa.Console;

    public class FractionTester
    static Console c;

    public static void main (String [] args)
    Fraction A = new Fraction(); // default 0/1
    Fraction B = new Fraction(-3,4); // -3/4
    Fraction X = new Fraction(A); // copy A to X (canít do X=A Ė aliasing)

    // The Fraction class 1.0
    class Fraction
    protected int numerator, denominator; // protected gives access to subclass (5.3)

    // default constructor is automatically defined, setting all fields to 0 or null
    // unless you create your own, which you should generally do
    public Fraction ()
    numerator = 0;
    denominator = 1; // makes more sense than 0/0

    // copy constructor creates a distinct duplicate of an object by copying data;
    // canít simply assign one object to another because two names will now point
    // to the same memory address (called aliasing) meaning that when one is changed,
    // the other one is too because they are the same object Ė usually not desirable
    public Fraction (Fraction F)
    numerator = F.numerator;
    denominator = F.denominator;

    // alternate constructor (there may be several to initialize object)
    public Fraction (int num, int den)
    numerator = num;
    denominator = den;

    public String toString ()
    return numerator + "/" + denominator;

    i need to add the following to the fraction class:
    Improve the read method so that it reads a fraction in as a string without prompt messages and does error-checking to ensure valid input.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: help with assignment

    You need a lot more than that to make it a useful class; but for starters: read an integer number, it must be the numerator. Next, if you can read the '/' character, another integer number should follow, it is the denominator, otherwise the denominator equals 1. The method should be static because you don't have a Fraction object yet (it is constructed by that method).

    Tip: find a greatest common divisor method (google is your friend); you'll need it.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. assignment help
    By xyknight in forum New To Java
    Replies: 6
    Last Post: 03-13-2011, 07:19 PM
  2. Need help with a HW assignment
    By mackavelirip in forum New To Java
    Replies: 4
    Last Post: 02-17-2011, 01:36 AM
  3. assignment help
    By esallender in forum New To Java
    Replies: 4
    Last Post: 10-25-2010, 12:10 PM
  4. help with an assignment!
    By Tek in forum New To Java
    Replies: 6
    Last Post: 10-24-2010, 11:32 PM
  5. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts