# Sum of intervals

• February 21st, 2012, 01:19 PM
cisneros778
Sum of intervals
Hello, I am fairly new at Java and just have a couple of questions to help me understand exactly what kind of program, class I am supposed to write.

Create an Interval class that represents an interval of numeric values. For example, it could represent the interval [1,2].

This class will be used to perform computations on uncertain quantities. For example, if you have a number in the range [1, 2] and another number in the range [0.1, 0.3], then their sum must be in the range [1.1, 2.3]. This can be used to answer questions like "If you have between 1000 and 2000 people, and between 10% and 15% of them have a pet goat, what are the most and least people that could have a pet goat?"

From what I understand so far is I have to ask the user to input two intervals one the range of people next the range of percentages.

Ensure that instances of your class are immutable (you can't change the interval that an object represents after you create it), and support the following operations:

add: add two intervals, returning the interval in which the sum must lie.

I do not understand what I need to add. The only operations I see necessary is multiplying the percentages by the range of people. This part of the assignment confuses me the most.

multiply: multiply two intervals, returning the interval in which the product must lie.
equals: test if two Interval objects represent the same numeric interval.

This is the second part of the assignment that confuses me as well. Why do I need to test two intervals to have the same numeric interval? I don't see the point in this.
Implement a toString method that provides a nice representation of the interval.
Include a main method that tests your class's functionality. It should test the operations using a variety of intervals, including the following cases:

0-width intervals (like [1, 1])
Intervals that lie in (0, +∞)
Intervals that lie in (-∞, 0)
Intervals containing 0.
Optional: support unbounded intervals, like (-∞, 3], [-3, ∞), or (-∞, ∞).

And finally, I am having trouble finding out how to get the user to enter the value for infinity.
Thank you, for any helpful hints.
• February 21st, 2012, 01:30 PM
KevinWorkman
Re: Sum of intervals
• February 21st, 2012, 01:36 PM
cisneros778
Re: Sum of intervals
I have looked at that and I have written it in English and have provided an answer to the question asked. I am not throwing you a code and asking what's wrong with it I am simply asking for a clue or a step into the right direction because I understand how to answer the question but don't understand why I need to add intervals or check to see if they're the same "numerical interval." I by no means want someone to do my homework for me.
• February 21st, 2012, 03:08 PM
Norm
Re: Sum of intervals
Do you have any specific java programming questions?

Quote:

how to get the user to enter the value for infinity.
If all integer values are valid, then you might have to change the input method to read Strings where you can have the user enter a non-numeric value of your choosing for infinity. Then use the parseInt method to convert the Strings to int values.