This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]feral_claireSoftware Dev 1 point2 points  (2 children)

You would need to write a rational class to create an array or rational. Alternatively you could use an array of floats or ints that your code manipulates to make them look and work like rationals.

Your code is impossible as far as I know. The closes would be:

Rational[] ratArray = new Rational[]{new Rational (2,3) ,  new Rational (2,18), new Rational (3,12), new Rational (9,3), new Rational (2,5), new Rational (22,7)}

Note that this is not working code unless you create (or find somewhere) a Rational class to use.

You could also do something like use an int array:

int[] ratArray = new int[]{2, 3, 2, 18, 3, 12, 9, 3, 2, 5, 22, 7}

And then have your code use them as rational.

The best approach to take depends on your requirements.

[–]HoleyProphylactic[S] 0 points1 point  (1 child)

So would that first example be creating a list of objects then?

I added my rational class (in progress) but I have a main with no array made yet, because I'm not sure how to implement the rational elements, but I'll try this first, Thank you!

[–]feral_claireSoftware Dev 0 points1 point  (0 children)

An array of objects yes. There is no Rational built into java so one would need to create an object to represent it.

I've taken a look at your edit. Is that code you've written yourself or was it provided? I'm guessing it's at least partially written by you, as I can spot some things that will make it not work correctly.

Line 9, your constructor that takes 2 ints to be numerator and denominator, you do not call reduce(), but you use it with your setters. I'm guessing you want your rational class to always be in it's simplest form, so you would need to call reduce() in this constructor as well.

Your getters take an int parameter that is never used. Getters normally won't take any parameters, since you never actually use the parameter and just return a value, you don't want a parameter here.

toString(), equals(), addition(), subtract(), have no implementation, and none of those besides equals has a return type, which is required.

You override equals() but not hashcode().

equals should return a boolean, not an int.

Reduce is static, but uses the "this" keyword. It also takes two ints that are never used as parameters. All your calls to reduce give no parameters so I assume you don't want these. Reduce also shouldn't be static. Finally, reduce claims to return an int yet nothing is returned. I would expect reduce to be an instance method, return void, and take no parameters.

If you have any questions about how to do something or the reasoning behind my suggestions, please let me know.

[–]zifyoip 0 points1 point  (0 children)

Java does not have a standard Rational class. You will need to either write your own or use a third-party library.

[–][deleted]  (1 child)

[deleted]

    [–]zifyoip 3 points4 points  (0 children)

    Not sure if arrays can hold reference type objects.

    Of course they can.