Java Reference
In-Depth Information
where
i
is
-
1
Write a program to test your class. Use floating-point variables to represent the
private
data of the
class. Provide a constructor that enables an object of this class to be initialized when it's declared.
Provide a no-argument constructor with default values in case no initializers are provided. Provide
public
methods that perform the following operations:
a) Add two
Complex
numbers: The real parts are added together and the imaginary parts
are added together.
b) Subtract two
Complex
numbers: The real part of the right operand is subtracted from
the real part of the left operand, and the imaginary part of the right operand is sub-
tracted from the imaginary part of the left operand.
c) Print
Complex
numbers in the form
(
realPart
,
imaginaryPart
)
.
8.12
(Date and Time Class)
Create class
DateAndTime
that combines the modified
Time2
class of
Exercise 8.7 and the modified
Date
class of Exercise 8.8. Modify method
incrementHour
to call
method
nextDay
if the time is incremented into the next day. Modify methods
toString
and
toUni-
versalString
to output the date in addition to the time. Write a program to test the new class
Da-
teAndTime
. Specifically, test incrementing the time to the next day.
8.13
(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
boolean
s. 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 array to the “empty set” (i.e., all
false
values).
Provide the following methods: The
static
method
union
creates a set that's the set-theoretic
union of two existing sets (i.e., an element of the new set's array is set to
true
if that element is
true
in either or both of the existing sets—otherwise, the new set's element is set to
false
). The
static
method
intersection
creates a set which is the set-theoretic intersection of two existing sets (i.e.,
an element of the new set's array is set to
false
if that element is
false
in either or both of the
existing sets—otherwise, the new set's element 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
toString
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
Inte-
gerSet
. Instantiate several
IntegerSet
objects. Test that all your methods work properly.
8.14
(Date Class)
Create class
Date
with the following capabilities:
a) Output the date in multiple formats, such as
MM/DD/YYYY
June 14, 1992
DDD YYYY
b) Use overloaded constructors to create
Date
objects initialized with dates of the formats
in part (a). In the first case the constructor should receive three integer values. In the
second case it should receive a
String
and two integer values. In the third case it should
receive two integer values, the first of which represents the day number in the year.
[
Hint:
To convert the
String
representation of the month to a numeric value, compare
String
s using the
equals
method. For example, if
s1
and
s2
are
String
s, the method
call
s1.equals(s2)
returns
true
if the
String
s are identical and otherwise returns
false
.]
8.15
(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