Exercise (Solutions)

E1

Write a method that takes a string from Scanner input and returns the number of unique characters in the string. The program should be in a loop so that you can give as many strings as you like, one at at time. When the user enters 'Quit', the program should end.

Hint: Look at String Class methods to get individual characters of a String and what would be wrapper class you would use to store this 'char'? And which collection classes would you use to keep only unique values? Does the collection class you choose have a method to get the number of elements stored in the collection?

E2

Write a program which stores name and the list of telephone numbers that the name belongs to. User provides this from Scanner. User can choose two options; add or search.

  • If the user chooses to 'add' then the program adds a given contact number.
  • If the user chooses to 'search', then the program takes the name as input and displays the telephone numbers stored for that name.

A name can have many telephone numbers. Keep a data structure which can satisfy this requirement. If the user enters 'quit' the program ends.

E3

Generate 10 random numbers between 1 and 20 and keep it in some type of collection. Ensure that the random numbers that are kept in the collection are unique.

results matching ""

    No results matching ""