Fraction class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Fraction class

  1. #1
    Join Date
    Oct 2006
    Location
    ohio/ Long Beach, CA
    Posts
    9

    Fraction class

    Why wont this work ????


    import java.util.*;
    import java.io.*;
    public class Fraction
    {
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {
    Fraction num1 = new Fraction(2,10);
    Fraction num2 = new Fraction(15,5);
    Fraction num3 = new Fraction(24,18);

    System.out.println(" " + num1);
    System.out.println(" " + num2);

    num3 = num1.add(num2);

    System.out.println(" " + num3);
    System.out.println("Addition = " + num1 + " + " + num2
    + " = " + num1.add(num2));
    System.out.println("Multiplication = " + num1 + " * " + num2
    + " = " + num1.multiply(num2));

    num3 = num1.subtract(num2);


    System.out.println("Subtraction = " + num1 + " - " + num2
    + " = " + num1.subtract(num2));
    System.out.println("Division = (" + num1 + ") / (" + num2
    + ") = " + num1.divide(num2));
    }
    }

  2. #2
    Join Date
    Jul 2005
    Posts
    78
    What errors are you getting?

    You're making a few Fraction objects. Do you have a class that has the Fraction constructor? The Fraction class looks like it should also have add(), subtract(), multiply() and divide() methods.

  3. #3
    Join Date
    Oct 2006
    Location
    ohio/ Long Beach, CA
    Posts
    9
    It would seem its the fractions themselves, the ones I have choosen there 9 errors.

    Fraction.java:11: cannot find symbol
    symbol : constructor Fraction(int,int)
    location: class Fraction
    Fraction num1 = new Fraction(2,10);
    ^
    Fraction.java:12: cannot find symbol
    symbol : constructor Fraction(int,int)
    location: class Fraction
    Fraction num2 = new Fraction(15,5);
    ^
    Fraction.java:13: cannot find symbol
    symbol : constructor Fraction(int,int)
    location: class Fraction
    Fraction num3 = new Fraction(24,18);
    ^
    Fraction.java:18: cannot find symbol
    symbol : method add(Fraction)
    location: class Fraction
    num3 = num1.add(num2);
    ^
    Fraction.java:22: cannot find symbol
    symbol : method add(Fraction)
    location: class Fraction
    + " = " + num1.add(num2));
    ^
    Fraction.java:24: cannot find symbol
    symbol : method multiply(Fraction)
    location: class Fraction
    + " = " + num1.multiply(num2));
    ^
    Fraction.java:26: cannot find symbol
    symbol : method subtract(Fraction)
    location: class Fraction
    num3 = num1.subtract(num2);
    ^
    Fraction.java:30: cannot find symbol
    symbol : method subtract(Fraction)
    location: class Fraction
    + " = " + num1.subtract(num2));
    ^
    Fraction.java:32: cannot find symbol
    symbol : method divide(Fraction)
    location: class Fraction
    + ") = " + num1.divide(num2));

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    As Masher said, you do not have a constructor defined for your Fraction class which takes two ints as arguments. That will take care of your first 3 errors. You need to define the multiply, subtract, divide, and add classes, which explains the other six errors.

  5. #5
    Join Date
    Oct 2006
    Location
    ohio/ Long Beach, CA
    Posts
    9
    you're right I re-did it and came up with this , works fine now. Thanks for the feedback guys .



    import java.util.*;

    public class Fraction
    {
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {
    Fraction num1 = new Fraction(3,4);
    Fraction num2 = new Fraction(2, 10);
    Fraction num3 = new Fraction(1, 8);

    System.out.println("Fraction : " + num1);
    System.out.println("Fraction : " + num2);

    num3 = num1.add(num2);

    System.out.println("Fraction : " + num3);
    System.out.println("Addition = " + num1 + " + " + num2
    + " = " + num1.add(num2));
    System.out.println("Multiplication = " + num1 + " * " + num2
    + " = " + num1.multiply(num2));

    num3 = num1.subtract(num2);


    System.out.println("Subtraction = " + num1 + " - " + num2
    + " = " + num1.subtract(num2));
    System.out.println("Division = (" + num1 + ") / (" + num2
    + ") = " + num1.divide(num2));
    }
    }

  6. #6
    Join Date
    Jul 2005
    Posts
    78
    So what did you change?

    That code looks exactly the same....


    Additionally, you should use the [ code ] tags

  7. #7
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    You've posted the old one

    Please use:[code] put your code here
    and after your code put :[]
    inside the [] put /code.


    Code:
    import java.util.*;
    
    public class Fraction
    {---}

Similar Threads

  1. Replies: 27
    Last Post: 02-11-2016, 05:29 AM
  2. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  3. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  4. fraction class [was:need help plzz]
    By malehda3y in forum C++
    Replies: 13
    Last Post: 12-03-2005, 02:35 PM
  5. Replies: 5
    Last Post: 10-17-2002, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center