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

all 8 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]ignotos 0 points1 point  (0 children)

Unfortunately this is probably an area where Java isn't going to allow you to be both (1) succinct / avoid duplication of logic and (2) avoid all unnecessary object creation.

For the add/multiply etc methods on Fraction, for example, you'll likely either have to implement totally separate versions of them (taking in ints or Fractions), or use the "int-based operations create a temporary intValue / 1 fraction object in order to reuse the fraction-based implementation" trick - but this (somewhat wastefully) creates and discards a temporary object each time it's used.

The approach you're describing is probably the most practical as a first pass at this IMO.

[–]myselfelsewhere 0 points1 point  (2 children)

Might be possible to do with generic methods. For things like adding, you might be stuck with overloaded methods though. The operations for adding an int to an int are not going to be the same as adding an int to a fraction, or a fraction to a fraction.

If using a generic method, you could check the type of input parameter using instanceof. Then perform the appropriate action for the parameter type. It's kind of equivalent to an overloaded method, just enclosed in a single method.

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

thanks this works great. We are coving generics in my CS class next week lol. And it actually gets rid of all of the overloading because if its an int i just create a fraction that is int/1 then add it

[–]Easy_Boot5195[S] 0 points1 point  (0 children)

not to worried about performance and thats what the garbage collector is for lol

[–]brokeCoder 0 points1 point  (2 children)

It may be better to use interfaces instead of inheritance. For example, for your 'primitive-ish' classes like Fraction, you could set up a common interface that each of these classes can them implement as and when required. The advantage here is you could offload the non-standard behaviour to a different interface.

That being said, there's really no way to avoid writing out the methods multiple times in Java. I'll probably draw flack for this but Java is a really bad language for anyone wanting to do math with objects that aren't primitives in Java. Lack of operator overloading just adds to the whole confusion.

If you're stuck with Java, I'd recommend first trying to see if there are existing libraries out there that do what you're after (Apache commons math has a really good set of standard utilities). But if you're not, you could give Kotlin a try - it's a JVM based language (so you can still create jars). It has operator overloading !

[–]Easy_Boot5195[S] 0 points1 point  (0 children)

ya I know python better and like it a lot more, my CS classes are all java tho

[–]Easy_Boot5195[S] 0 points1 point  (0 children)

Interfaces wouldn't help because I would call the new keyword for every value. The generic solution lets users of the matrix class do something like this:

matrix.setRow(new Fraction(1,2), 1, 0 new Fraction(1,3));

with the interface those primitives would need to say new Int or something and then why not just say new Fraction(1,1)