Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Fraction class to program

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i take a fraction class that is given to me and use it to design a new program that will print 10 random fractions and their simplified from.
 
Saloon Keeper
Posts: 7393
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing the code for the fraction class, or at least some documentation, it's hard to say.
 
Marshal
Posts: 70684
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same way as, “how do I eat an elephant?” *#x201c;One bite at a time.”
Divide the task into small parts and do one of them. Maybe create one non‑random Fraction object and reduce it to its simplest form.

A well‑designed Fraction object should have a toSimplestForm() method or similar already, because it should take care of itself (look up single responsibility principle). It would breach the conventions of object orientation to make somebody do anything like that from outside the object.

And . . . welcome to the Ranch
 
Sam Lewis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi that is what i am having trouble doing.  There is a simplify method in the fraction class how to i call that into my new program in order to simply fractions
 
Carey Brown
Saloon Keeper
Posts: 7393
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam, if you want any help then you'll have to help us. We have no knowledge of this 'fraction' class of which you speak.
 
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably the code would be something like this:



I'm assuming here that the designer of the Fraction class has made Fraction objects immutable. However that isn't a sure thing and it's possible that the simplify() method changes the internal state of a Fraction object, rather than returning a new simplified Fraction object. Or perhaps it's even worse and there's a static method simplify(Fraction) which has the side effect of modifying the internal state of a Fraction object... and the list could go on.

But I'm answering the question "How do I write code to call the simplify method", and that wasn't quite what you asked. You asked how to "call that into my new program" and I didn't know what that meant, so I made a reasonable (I think) assumption. If you meant something else, then sorry, please let us know.
 
Sam Lewis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Here is what he is wanting me to do.  He has given us a code for the fraction class.  he wants us to make a new program using that fraction class and it should randomly generate 10 fractions in their simplest form.  I am not sure how to start to get the 10 fractions ramdoly they can be between 1-100000 but cannot equal 1,
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Lewis wrote:I am not sure how to start to get the 10 fractions ramdoly they can be between 1-100000 but cannot equal 1,



So is your problem how to create a fraction? Or how to choose random numbers for its numerator and denominator? Or something else? All of the above?

Do you have any code at all which you would like to point out and say "Here's where I am stuck"?
 
Sam Lewis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all of the above.  I need my program to randomly generate 10 fractions or numerator and denominator they have to be less than 1  and their simplest from.  I am not sure how to get them to randomly generate.  I think once i get that i can use the fraction class method to simplify to simplify them.  I hope that make sense.
 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to get “random” numbers? Hint: don't use Math#random().
 
Carey Brown
Saloon Keeper
Posts: 7393
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Lewis wrote:...He has given us a code for the fraction class.,

Can we see this code? Please post.
 
Sam Lewis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i post that.  I try to copy it into the form and it does not like it.
 
Carey Brown
Saloon Keeper
Posts: 7393
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to select the text you want to copy, then copy (Ctrl+C) and paste (Ctrl+V) the text of the code into a post.
 
Sam Lewis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HEre is my code.  I am trying to add the random part so that it generates a random fraction any help would be awesome.

 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call it SimpFrac. That name won't help you if you come back to it in six months. I would use FractionDemo myself, but you can use something different. The method name setUpFrac() is also probably too short.
You need a main() method to start your application. From the code you have, you can write new FractionDemo(...).setUpFractions();
You are not doing badly about “random” numbers. You can get two “random” numbers as you showed:-...but, please check here for the range of possible results. You would probably do better to use this version of that method instead. That way you can avoid negative numbers, and make sure that the numerator is less than the denominator. You are setting up two “random” numbers, but you aren't using them. You are using 6 and 12 and getting a result but not printing it out (line 30).
Suggested changes:-
  • 1: Reinitialise both numerator and denominator in the loop. I would declare them as local to the loop myself.
  • 2: Use those two numbers for the fraction objects.
  • 3: Print out the output and see whether you are getting any changes with simplification. Remember only about 30% of random fractions can be simplified at all.
  •  
    Sam Lewis
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is my main progam where i can test.  This is what i am using right now hwere i am putting in the two numbers for my fractions.

    class FractionTester{
    public static void main(String[] args){
     
         SimpFrac myFractionTester = new SimpFrac(10);
         myFractionTester.setUpFrac();
         System.out.println(myFractionTester.getFraction());
      }
    }
     
    Sam Lewis
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    so when you say reinlize it in my loop are you talking about my for loop.
     
    Carey Brown
    Saloon Keeper
    Posts: 7393
    66
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sam Lewis wrote:... get the 10 fractions ramdoly they can be between 1-100000 but cannot equal 1,


    In other words, 2-100000 (inclusive). So, how would you use Random#nextInt(N) to return that range?
     
    Sam Lewis
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Attched is what my teacher is wanitng us to do.  I have a little of the codign done where i can tell them the two numbers for my fraction but i need it to be generated not inputed by the person.  Any help would help me out greatly.
     
    Sam Lewis
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok.  I now can get my program to give me 10 random fractions.  Now i need it to give me 10 of them that simplifys and if they dont simplify then it skips thems.  Can you give  me any information on how to do this.  Below is my code.


    import java.util.*;

    class SimpFrac{

      private int count = 0;
      private String result = "";
      public int a = 0;
      public int b = 0;
      Random random = new Random();
     


      public SimpFrac(int x)
      {
         count = x;
      }
     
      public void setUpFrac()
      {
           
           
     
         for (int i = 0; i < count; i++)
         {
         
            a = random.nextInt(100000);
            b = random.nextInt(100000);
            Fraction f1 = new Fraction(a,b);
            Fraction simplifiedFrac = f1.simplify();
            result = result + " " + f1 + " Simplfies to " + simplifiedFrac + "\n";
           
           
         }
      }
     
     
     
      public String getFraction()
      {
         return result;
      }
    }
     
    Campbell Ritchie
    Marshal
    Posts: 70684
    288
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your loop appears to be reassigning the result field repeatedly but you aren't doing anything with the altered values. You are only using the last value.
    I suggest you add 1 to some of your “random” numbers, otherwise there is a risk of getting denominator=0, which as you know shouldn't happen.
    I suggest you use denominator as the argument for the second nextInt() call, if you want the numerator always to be smaller.
    You can predict that a fraction can be simplified if its numerator and denominator both share a (prime) factor. You would only have to test up to the square root of the largest value permitted, but I don't think that was the intention of the whole exercise. Do you have any means of distinguishing between fractions which did and fractions which didn't turn into a simpler form?
     
    Paul Clapham
    Marshal
    Posts: 25961
    70
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sam Lewis wrote:if they dont simplify then it skips thems.



    I assume that the "simplify" method doesn't throw any exceptions, as I don't see your code handling any. So I have to assume that "don't simplify" probably means that simplifying the fraction doesn't change anything.

    So if that's the case, wouldn't you just compare the simplified fraction to the original fraction to see if anything changed?
     
    Carey Brown
    Saloon Keeper
    Posts: 7393
    66
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Sam Lewis wrote:... get the 10 fractions ramdoly they can be between 1-100000 but cannot equal 1,


    In other words, 2-100000 (inclusive). So, how would you use Random#nextInt(N) to return that range?


    Better but not correct. This range is 0-99999 (inclusive).

    To see if you need to skip and if your fraction class supports this.
     
    Sam Lewis
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi all so i have my progam now working where it gives me 10 random fractions.  My problem is that those 10 fractions on a couple of them are simplifed.  I need all of them to simplify if not it should skip them.  Here is my code i know i need to add an if statment i think in my getAGoodFraction can someone help me with what it should look like.
    Thanks


    import java.util.Random;

    class SimpFrac{

      private int count = 0;
      private String result = "";
      public int a = 0;
      public int b = 0;
      Random random = new Random();
     
      public SimpFrac()
      {
      }

      public SimpFrac(int x)
      {
         count = x;
      }
     
      public void setUpFrac()
      {
         for (int i = 0; i < count; i++)
         {
           
            //a = random.nextInt(100000);
            //b = random.nextInt(100000);
            //Fraction f1 = new Fraction(a,b);
            Fraction f1 = getACorrectFraction();
            Fraction simplifiedFrac = f1.simplify();
            //Fraction f1 = getACorrectFraction();
            result = result + " " + (int)(i +1) + ": " + f1 + " Simplfies to " + simplifiedFrac + "\n";
            //result = result + " " + f1 + " Simplfies to " + simplifiedFrac + "\n";
          }
      }
     
      private Fraction getACorrectFraction()
      {
         Fraction frac;
        // if (frac.getNumerator() < frac.Denominator())
            //return frac;
         do
            frac = new Fraction(random.nextInt(100000) + 1, random.nextInt(100000) + 1);
            while (frac.getNumerator() >= frac.getDenominator());
            return frac;
       }
     
      public String getFraction()
      {
         return result;
      }
    }
     
    Campbell Ritchie
    Marshal
    Posts: 70684
    288
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To go back to what we said earlier:-
  • 1: Have you got any way of determining whether a fraction has a simpler form or not?
  • 2: Once you have that worked out, please consider your loop, so it only counts fractions that can be simplified. Then collect ten fractions or pairs of fractions.


  • [additional:-]
  • 3: Calculate the denominator first, then use that value as an argument for the Random method, so the numerator will be smaller. Work out whether you need to avoid numerator=0.
  •  
    Bartender
    Posts: 4109
    156
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Or have a method 'getASimplifiableFraction' where numerator and denominator are both multiplied by say 2 or 3.

    Why must numerator be smaller than denominator? That will be problematic when defining sum or product of two Fractions.
     
    Sam Lewis
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The numerator must be smaller than the denominator because it has to be less then 0
     
    Campbell Ritchie
    Marshal
    Posts: 70684
    288
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sam Lewis wrote:. . . it has to be less then 0

    You mean the fraction must be less than 1, probably. As Piet says, you will have problems:-
     
    bacon. tiny ad:
    the value of filler advertising in 2020
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic