I MCA

Object-Oriented Programming
Assignment-I
1 Travel Tickets Company sells tickets for airlines, tours, and other travel-related
. services. Because ticket agents frequently mistype long ticket numbers, Travel
Tickets has asked you to write an application that indicates invalid ticket
number entries. The class prompts a ticket agent to enter a six-digit ticket
number. Ticket numbers are designed so that if you drop the last digit of the
number, then divide the number by 7, the remainder of the division will be
identical to the last dropped digit. This process is illustrated in the following
example:

Accept the ticket number from the agent and verify whether it is a valid
number.
Test the application with the following ticket numbers:
l 123454; the comparison should evaluate to true
l 147103; the comparison should evaluate to true
l 154123; the comparison should evaluate to false
2 a. Create a class named Student.A Student has fields for an ID number, number
of credit hours earned, and number of points earned. (For example, many
schools compute grade point averages based on a scale of 4, so a three-credithour class in which a student earns an A is worth 12 points.) Include methods
to assign values to all fields. A Student also has a field for grade point average.
Include a method to compute the grade point average field by dividing points
by credit hours earned. Write methods to display the values in each Student
field. Save this class as Student.java.

b. Write a class named ShowStudent that instantiates a Student object from the
class you created and assign values to its fields. Compute the Student grade
point average, and then display all the values associated with the Student. Save
the application as ShowStudent.java.
c. Create a constructor for the Student class you created. The constructor
should initialize each Student’s ID number to 9999, his or her points earned to
12, and credit hours to 3 (resulting in a grade point average of 4.0). Write a
program that demonstrates that the constructor works by instantiating an object
and displaying the initial values. Save the application as ShowStudent2.java.
3 a. Create a class named BankAccount with fields that hold an account number,
the owner’s name, and the account balance. Include a constructor that
initializes each field to appropriate default values. Also include methods to get
and set each of the fields. Include a method named deductMonthlyFee() that
reduces the balance by Rs.4.00. Include a static method named
explainAccountPolicy() that explains that the Rs.4 service fee will be deducted
each month. Save the class as BankAccount.java.
b. Create a class named TestBankAccount whose main() method declares four
BankAccount objects. Call a getData() method three times. Within the method,
prompt a user for values for each field for a BankAccount, and return a
BankAccount object to the main() method where it is assigned to one of
main()’s BankAccount objects. Do not prompt the user for values for the fourth
BankAccount object, but let it continue to hold the default values. Then, in
main(), pass each BankAccount object in turn to a showValues() method that
displays the data, calls the method that deducts the monthly fee, and displays
the balance again. The showValues() method also calls the method that
explains the deduction policy. Save the application as TestBankAccount.java.
4 Playing cards are used in many computer games, including versions of such
classics as solitaire, hearts, and poker. Design a Card class that contains a
character data field to hold a suit (s for spades, h for hearts, d for diamonds, or
c for clubs) and an integer data field for a value from 1 to 13. Include get and
set methods for each field. Save the class asCard.java.
a. Write an application that randomly selects two playing cards and displays
their values. Simply assign a suit to each of the cards, but generate a random
number for each card’s value. However, for now, you can copy the following
statements to generate a random number between 1 and 13 and assign it to a
variable:
final int CARDS_IN_SUIT = 13;
myValue = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1);

Determine the higher card.
b. Modify the Card class so the setValue() method does not allow a Card’s
value to be less than 1 or higher than 13. If the argument to setValue()is out of
range, assign 1 to the Card’s value.
c. This application also randomly selects two playing cards and displays their
values. In this application, all Card objects arbitrarily were assigned a suit
represented by a single character, but they could have different values, and the
player observed which of two Card objects had the higher value. Now, modify
the application so the suit and the value both are chosen randomly. Using two
Card objects, play a very simple version of the card game War. Deal two
Cards—one for the computer and one for the player—and determine the higher
card, then display a message indicating whether the cards are equal, the
computer won, or the player won. (Playing cards are considered equal when
they have the same value, no matter what their suit is.) For this game, assume
the Ace (value 1) is low. Make sure that the two Cards dealt are not the same
Card. For example, a deck cannot contain more than one Card representing the
2 of spades. If two cards are chosen to have the same value, change the suit for
one of them. Save the application asWar.java. Create an array in which you
create an entire deck without repeating cards.
5 a. Create a class named BloodData that includes fields that hold a blood type
(the four blood types are O, A, B, and AB) and an Rh factor (the factors are +
and –). Create a default constructor that sets the fields to “O” and “+”, and an
overloaded constructor that requires values for both fields. Include get and set
methods for each field. Save this file asBloodData.java. Create an application
named TestBloodData that demonstrates that each method works correctly.
Save the application asTestBloodData.java.
b. Create a class named Patient that includes an ID number, age, and
BloodData. Provide a default constructor that sets the ID number to“0”, the age
to 0, and the BloodData to “O” and“+”. Create an overloaded constructor that
provides values for each field. Also provide get methods for each field. Save
the file as Patient.java. Create an application named TestPatient that
demonstrates that each method works correctly, and save it as TestPatient.java
6 a. Using the Die class, write an application that randomly “throws” five dice
for the computer and five dice for the player. Display the values and then, by
observing the results, decide who wins based on the following hierarchy of Die
values. (The computer will not decide the winner; the player will determine the
winner based on observation.) Any higher combination beats a lower one; for
example, five of a kind beats four of a kind.

 Five of a kind
 Four of a kind
 Three of a kind
 A pair
Determine whether you or the computer had the better roll i.e. decide the
winner based on the above hierarchy of Die values. Save the application as
FiveDice.java.
b. For this game, the dice values do not count; for example, if both players have
three of a kind, it’s a tie, no matter what the values of the three dice are.
Additionally, the game does not recognize a full house (three of a kind plus two
of a kind). Save the application as FiveDice2.java. Look at the figure below for
demonstration

Improve theFiveDice2game so that when both players have the same
combination of dice, the higher value wins. For example, two6s beats two 5s.