By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,818 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,818 IT Pros & Developers. It's quick & easy.

representing rational numbers using java objects

P: 1
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that might actually be represented as 0.49998, which may not be good enough for some applications.
In this assignment we shall explore a way of representing rational numbers using structures. For each rational number we shall represent/store the numerator and denominator as integers. We shall use a structure like this:
struct Rational {int a, int b;};

to represent a number . In C we declare a new structure to represent our rational number using:
typedef struct Rational Rational;

This declares the new type Rational to be an alias for struct Rational. We can now define operations on data of this kind. In particular we need to implement rational number arithmetic. We shall limit ourselves to the operations of addition and multiplication. (Note that subtraction is really addition in disguise and is no more complicated.)
Your first task will be to implement the functions:
Rational R_init(int a, int b);
Rational R_add(Rational x, Rational y);
Rational R_mult(Rational x, Rational y);
int R_show(Rational x);

The functions should do the following: R_init should return a rational representation for where a and b are the integers. R_add should add two rational numbers returning their sum, R_mult should return the product of the two rational arguments it is passed. R_show should print out the rational number (in the form a/b, not as real decimal).
This part should be pretty straightforward. You should not need to reduce the rational numbers to their lowest form. That is, if you add and , it is OK to report 2/6 as the answer, rather than 1/3.

The next part of the assignment requires you to compute a close rational approximation to e, which is often approximated to 2.17 Note that e is actually 1 + 1/1+1/2+1/6+1/24+1/120 + Compute this sum as far as it will go without giving you integer overflow problems. In short, write a function:
void e(void);
This function should compute and print out e using the rational arithmetic functions you wrote above.
May 10 '07 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that might actually be represented as 0.49998, which may not be good enough for some applications.
In this assignment we shall explore a way of representing rational numbers using structures. For each rational number we shall represent/store the numerator and denominator as integers. We shall use a structure like this:
struct Rational {int a, int b;};

to represent a number . In C we declare a new structure to represent our rational number using:
typedef struct Rational Rational;

This declares the new type Rational to be an alias for struct Rational. We can now define operations on data of this kind. In particular we need to implement rational number arithmetic. We shall limit ourselves to the operations of addition and multiplication. (Note that subtraction is really addition in disguise and is no more complicated.)
Your first task will be to implement the functions:
Rational R_init(int a, int b);
Rational R_add(Rational x, Rational y);
Rational R_mult(Rational x, Rational y);
int R_show(Rational x);

The functions should do the following: R_init should return a rational representation for where a and b are the integers. R_add should add two rational numbers returning their sum, R_mult should return the product of the two rational arguments it is passed. R_show should print out the rational number (in the form a/b, not as real decimal).
This part should be pretty straightforward. You should not need to reduce the rational numbers to their lowest form. That is, if you add and , it is OK to report 2/6 as the answer, rather than 1/3.

The next part of the assignment requires you to compute a close rational approximation to e, which is often approximated to 2.17 Note that e is actually 1 + 1/1+1/2+1/6+1/24+1/120 + Compute this sum as far as it will go without giving you integer overflow problems. In short, write a function:
void e(void);
This function should compute and print out e using the rational arithmetic functions you wrote above.
Please read the guidelines.
May 10 '07 #2

Expert 10K+
P: 11,448
Please read the guidelines.
On top of that: I've seen that exact same assignment description before here ;-)

kind regards,

Jos
May 10 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
On top of that: I've seen that exact same assignment description before here ;-)

kind regards,

Jos
Several times.
May 10 '07 #4

Expert 10K+
P: 11,448
Several times.
You've been around here much longer than I have ;-) I do like the assignment
though: it's well defined and a clean implementation is almost written by just
reading the assignment text carefully.

kind regards,

Jos
May 10 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
I don't like it because it has to do with Taylor series (the infinite series to calculate the value of e). This is one of the topics I had to learn on my own for the big AP Calculus exam I took yesterday, and was by far the hardest topic. I don't wanna talk about series again until Calc II in college.
May 10 '07 #6

Expert 10K+
P: 11,448
I don't like it because it has to do with Taylor series (the infinite series to calculate the value of e). This is one of the topics I had to learn on my own for the big AP Calculus exam I took yesterday, and was by far the hardest topic. I don't wanna talk about series again until Calc II in college.
Series are fun :-P I answered the series part last week in this very same forum
and I even managed to upset a few folks in the Cafe/Lounge section with those
series ;-) but I'm too lazy to answer it again (and again and again) just because
people are too lazy too google or read.

kind regards,

Jos
May 10 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.