CSci 151: Foundations of computer science II
Home Syllabus Assignments Tests

printable version

Exam 1

[1] [2] [3] [4] [5] [6] [7] [8]

Problem X1.1.

[14 pts] Write a class definition Interval for representing an interval of numbers. The class should incorporate the following methods.

Interval(double a, double b)

(Constructor) Constructs an object representing the range of numbers between a and b, inclusive. The constructor may assume that a < b.

void add(double x)

Extends this interval, if necessary, so that x is included within this interval.

boolean contains(double q)

Returns true if this interval contains the number q.

The below program fragment illustrates how a different class might use the class you write.

Interval range = new Interval(4, 10);
System.out.println(range.contains(2)); // says false
System.out.println(range.contains(4)); // says true
System.out.println(range.contains(6)); // says true
range.add(2);
System.out.println(range.contains(1)); // says false
System.out.println(range.contains(2)); // says true
System.out.println(range.contains(3)); // says true