Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1.

Write a program that asks how many names the user wants to input, asks for
those names, and shows the names afterwards. But it should only show names
that are longer than 5 characters (Array List/LinkedList)
Sample Input/Output
How many names: 4
Give name 1:Elvis
Give name 2:Priscilla
Give name 3:Vernon
Give name 4:LisaMarie
LisaMarie
Vernon
Priscilla
2. Suppose you have a set s containing some strings. Write a code fragment that
uses an iterator to print out each element in s.

3. Write Java code to define yet another Set S. Insert 3 floating point numbers in S
and find the sum of the numbers in S using Stream API.
4. Write a Java code and demonstrate the following:
a. Create a new HashMap and populate with below elements
i. Orange 50
ii. Apple 100
iii. Grapes 70
iv. Strawberry 150
b. Iterate over the HashMap and print key and value
c. Retrieve a value associated with a given key from the HashMap (key your
choice)
d. Check whether a particular Apple exist in a HashMap. If so print the<key,
Value> pair, if not print not exists
5. Julie has created a TreeMap containing name and marks as key value pairs of 6
students. Write a program, with separate user defined functions to perform the
following operations:
a. Push the keys (name of the student) of the hash map into a stack,
where the corresponding value(marks) is greater than 75.
b. Pop and display the content of the stack.
Sample Input /Output:
Om:76
Jai:45
Bob:89
Ali:65
Anu:90
Tom:82
Output:
Tom,Anu,Bob,Om
7. Implement a superclass Appointment and subclasses Onetime, Daily, and
Monthly. An appointment has a description (for example, “see the dentist”) and a
date.
Implement methods occursOn(int year, int month, int day) for each of the
subclasses that checks whether the appointment occurs on that date or not and
return true or false respectively. For example, for a monthly appointment, you
must check whether the day of the month matches.
Create a tester class that creates various objects from all the implemented
classes and call their occursOn method to see if you have implemented the
classes to do the right calculations.
Give the user the option to add new appointments. The user must specify the type
of the appointment, the description, and the date. You do NOT need to save these
in a file, once the program terminates all appointments are lost. Add another
method dayOfTheWeek in class Appointment which returns a string
corresponding to the day of the week that the appointment occurs, e.g if the
appointment occurs on 17 October 2015, the method returns “Sunday”. (Hint: use
the GregorianCalendar library class).
Implement a method displayAll() which displays all the appointments sorted by
date in ascending order, i.e. appointments occurring at an earlier date should be
displayed first.
8. In this, you will find a CatalogItem class which represents an item from a
product catalog. These catalog items are provided in form of a CSV file and are to
be loaded into a list, but it's ordering is randomized (Collections .shuffle()).

In the Catalog class we have a main(...) method where the content of the CSV


gets loaded into a list, then gets randomly ordered and finally is being displayed
with the printCatalog(...) method,but you should use it to easily display the list
after each sorting attempt.
Now, you can work on the following tasks.

Tasks for working with Comparable<T>:

1. Adapt the CatalogItem class to implement the Comparable<T> interface.


You will notice that you have to supplement the T for the type you are
comparing to, which should be CatalogItem. Come up with a natural sorting
order and implement it in the compareTo(...) method that you are overriding
from the interface. (You could, for example, sort by ID)
2. Try sorting the list of your now comparable CatalogItems using
the Collections.sort(...) method.
3. Print out the result and see if your sorting worked.

Tasks for working with Comparator<T>:

1. Create a new class PriceComparator and make it implement


the Comparator<T> interface. Again, you will have to supplement the T for
the type you are comparing, which should be CatalogItem. Override
the compare(...) method so that you can compare two CatalogItems based
on their price.
2. Try out whether your comparator works
using Collections.sort(<someCollection>, <someComparator>) or directly
using the lists sort(<someComparator>) method.
3. Print out the result and see if your sorting worked.
4. Now come up with at least two other ways of sorting the catalog and try
them out in the same fashion as the previous steps.

You might also like