Java Reference
In-Depth Information
14. Write a program to read english words and their equivalent spanish words into an object
array. request the user to type several english words. For each, print the equivalent spanish
word. Choose a suitable end-of-data marker. search for the typed words using binary search.
15. a date consists of day, month, and year. Write a class to create date objects and manipulate
dates. For example, write a function that, given two dates,
d1
and
d2
, returns
-1
if
d1
comes
before
d2
,
0
if
d1
is the same as
d2
, and
1
if
d1
comes after
d2
. also, write a function that
returns the number of days that
d2
is ahead of
d1
. If
d2
comes before
d1
, return a negative
value. and write a method to print a date in the format of your choice.
16. a time in 24-hour clock format is represented by two numbers; for example, 16 45 means
the time 16:45, that is, 4:45 p.m. using an object to represent a time, write a function that
given two time objects,
t1
and
t2
, returns the number of minutes from
t1
to
t2
. For example,
if the two given times are
16 45
and
23 25
, your function should return
400
.
17. Consider the problem of working with fractions, where a fraction is represented by two
integer values—one for the numerator and the other for the denominator. For example, 5/9 is
represented by the two numbers 5 and 9. Write a class to manipulate fractions. For example,
write methods to add, subtract, multiply, and divide fractions. also, write a method to reduce
a fraction to its lowest terms; you will need to find the hCF of two integers.
18. a bookseller needs to store information about topics. For each topic, he wants to store the
author, title, price, and quantity in stock. he also needs to know, at any time, how many book
objects have been created. Write java code for the class
Book
based on the following:
•
Write a no-arg constructor, which sets the author to no author, the title to no title, and the
price and the quantity in stock to 0.
•
Write a constructor that, given four arguments—author, title, price and quantity—creates a
Book
object with the given values. the price must be at least $5 and the quantity cannot be
negative. If any of these conditions is violated, the price and quantity are both set to 0.
•
Write accessor methods for the author and price fields.
•
Write a method that sets the price of a book to a given value. If the given price is not at least
$5, the price should remain unchanged.
•
Write a method that reduces the quantity in stock by a given amount. If doing so makes the
quantity negative, a message should be printed and the quantity left unchanged.
•
Write an instance method that prints the data for a book, one field per line.
•
Write a
toString()
method that returns a string that, if printed, will print the data for a
book, one field per line.
•
Write an
equals
method that returns true if the
contents
of two
Book
objects are the same
and false otherwise.
•
Write a
Test
class that creates three
Book
objects of your choice, prints their data, and
prints the number of
Book
objects created.
Search WWH ::
Custom Search