Introduction to Objects
13. Write a program to read names and phone numbers into an array of objects. Request a name and print the person’s phone number
Invalid vote: 8 Invalid vote: 9 Number of voters: 30 Number of valid votes: 28 Number of spoilt votes: 2 Candidate Score Avasa Tawari 6 Saskia Kalicharan 6 Nirvan Singh 4 Torrique Granger 4 Dawren Greenidge 3 Jordon Cato 3 Denise Duncan 2 The winner(s)
Avasa Tawari Saskia Kalicharan
EXERCISES 2
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,
d1and
d2, returns
-1if
d1comes before
d2,
0if
d1is the same as
d2, and
1if
d1comes after
d2. Also, write a function that returns the number of days that
d2is ahead of
d1. If
d2comes 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,
t1and
t2, returns the number of minutes from
t1to
t2. For example, if the two given times are
16 45and
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 books. For each book, 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
Bookbased 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
Bookobjects are the same and false otherwise.
Write a
• Test
class that creates three
Bookobjects of your choice, prints their data, and
prints the number of
Bookobjects created.
19. A multiple-choice examination consists of 20 questions. Each question has five choices, labeled
A,
B,
C,
D, and
E. The first line of data contains the correct answers to the 20 questions in the first 20 consecutive character positions. Here’s an example:
BECDCBAADEBACBAEDDBE
Each subsequent line contains the answers for a candidate. Data on a line consists of a candidate number (an integer), followed by one or more spaces, followed by the 20 answers given by the candidate in the next 20 consecutive character positions. An
Xis used if a candidate did not answer a particular question. You may assume all data is valid and stored in a file called
exam.dat. A sample line is as follows:
4325 BECDCBAXDEBACCAEDXBE
There are at most 100 candidates. A line containing a “candidate number”
0indicates only the end of the data.
Points for a question are awarded as follows: correct answer: 4 points; wrong answer:
-1 point; no answer: 0 points.
Write a program to process the data and print a report consisting of candidate number and the total points obtained by the candidate, in ascending order by candidate number. (This question is in chapter 1 as well, but this time you’ll solve it using objects).
20. A data file contains registration information for six courses—CS20A, CS21A, CS29A, CS30A,
CS35A, and CS36A. Each line of data consists of a seven-digit student registration number
followed by six (ordered) values, each of which is
0or
1. A value of
1indicates that the
student is registered for the corresponding course;
0means the student is not. Thus,
1 0 1 0 1 1means that the student is registered for CS20A, CS29A, CS35A, and CS36A, but not
for CS21A and CS30A. You may assume that there are no more than 100 students and a
registration number
0ends the data. Write a program to read the data and produce a class
list for each course. Each list begins on a new page and consists of the registration numbers
of those students taking the course.
Dalam dokumen
Pelajari tentang Advanced topics in Java
(Halaman 70-73)