Problem #1: [Source: #8.17, Java Textbook (Deitel & Deitel) – 7th Edition, Page 425, Chapter 8]

 

Rational Numbers:

Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class—the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator.

 

Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations:

a) Add two Rational numbers: The result of the addition should be stored in reduced form.

b) Subtract two Rational numbers: The result of the subtraction should be stored in reduced form.

c) Multiply two Rational numbers: The result of the multiplication should be stored in reduced form.

d) Divide two Rational numbers: The result of the division should be stored in reduced form.

e) Print Rational numbers in the form a/b, where ‘a’ is the numerator and ‘b’ is the denominator.

f) Print Rational numbers in decimal format.

 

 

Problem #2: [Source: #8.14, Java Textbook (Deitel & Deitel) – 7th Edition, Page 425, Chapter 8]

 

Set of Integers:

Create class IntegerSet. Each IntegerSet object can hold integers in the range 0–100. The set is represented by an array of booleans. Array element a[i] is true if integer i is in the set. Array element a[j] is false if integer j is not in the set. The no-argument constructor initializes the Java array to the “empty set” (i.e., a set whose array representation contains all false values).

 

Provide the following methods: Method union creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the third set’s array is set to true if that element is true in either or both of the existing sets—otherwise, the element of the third set is set to false). Method intersection creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set’s array is set to false if that element is false in either or both of the existing sets—otherwise, the element of the third set is set to true). Method insertElement inserts a new integer k into a set (by setting a[k] to true). Method deleteElement deletes integer m (by setting a[m] to false). Method toSetString returns a string containing a set as a list of numbers separated by spaces. Include only those elements that are present in the set. Use --- to represent an empty set. Method isEqualTo determines whether two sets are equal. Write a program to test class IntegerSet. Instantiate several IntegerSet objects. Test that all your methods work properly.