Showing posts with label Java Collections. Show all posts
Showing posts with label Java Collections. Show all posts

Monday, January 13, 2014

Java Calculator

Create a java based GUI calculator as shown below ...
Implement only the following functionalities ...
  • Addition
  • Difference
  • Multiplication
  • Division
  • CLR button
  • Back Space button

Saturday, November 23, 2013

Java HashSet Example

Create a class Employee with class members firstName(String), lastName(String) and salary(int).Do the following:

  • Create a HashSet of Employees.
  • Print the employee details.
  • Sort the employees in HashSet by salary and print the result.
  • Print the employee with the least salary.
  • Print the employee with the highest salary.
  • Search for an employee.

Monday, November 18, 2013

Java PriorityQueue Example

Write a program that creates and prints a priority queue of some country names in descending order.

Java HshMap using StringTokenizer

Write a program that gets an array of words and does the following:

  • Prints each word and it's frequency
  • Prints only the words
  • Prints only the frequency of all words

Here's a sample run assuming the array of words as {i , came , i , saw , i , conquered}
came : 1
conquered : 1
i : 3
saw : 1
[came, conquered, i, saw]
[1, 1, 3, 1]

Note: Print in the same order as shown above.

Java HashMap Example (Phone Book)

Implement a PhoneBook using HashMap.
Hint: Create the following classes:

  1. Person Class contains firstName and surName.
  2. PhoneNumber Class contains areaCode and number.
  3. BookEntry contains Person object and PhoneNumber object.
  4. PhoneBook contains a HashMap<Person,BookEntry>.
  5. TestPhoneBook Class contains main() to do the following:

The user should be prompted with the following menu

  • Enter 1 to add a new phonebook entry
  • Enter 2 to find the number for a name.
  • Enter 3 to find name for a number
  • Enter 9 to quit

Saturday, November 16, 2013

Java List Example using Comprable class

Create a Student class with name, id and age. Write a program that gets some students from user and then sorts by

  • Ascending order of Name (Use Comparable)
  • Descending order of Name
  • Both age and name.(Use Comparator)

Java Map Example

Write a program that gets an array of words and does the following:

  • Prints each word and it's frequency
  • Prints only the words
  • Prints only the frequency of all words

Here's a sample run assuming the array of words as {i , came , i , saw , i , conquered}

  • saw : 1
  • came : 1
  • conquered : 1
  • i : 3
  • [sa, cam, cons, i]
  • [1, 1, 1, 3]

Note: Order doesn't matter in any of the above outputs.

Java HsahMap (Collection) example

Write a program that takes an array of words and does the following:

  • Prints out any duplicate words
  • Prints the number of distinct words
  • Prints the list of the words with duplicates eliminated.

Eg: if the input array = i came i saw i conquered then outputs are

  • Duplicate present: i
  • Number of distinct elements: 4
  • Distinct elements: [saw, came, conquered, i]

Java StringBuilder Example

Write a method ..

public static int[] append(String[] a,String[] b)

that appends one array(of Strings) to another.
For example, if
a ={qqq www eee}
b ={ aaa, sss, ddd}

result ={qqq, www, eee, aaa, sss, ddd}

Java Vector class

Create a Person Class which contains two member variables firstName and lastName.Write a program that prompts the user to enter details of several persons.Model the collection of persons using Vector.

  • Print the vector containing person details.
  • Remove a person from vector and print.(Prompt the user to enter first name of the person to be deleted).
  • Insert a new person at some position entered by user.
  • Print details of first person.Replace it with “Harry Smith”
  • Convert the vector of Person objects to an array of Person objects.

Java Arrays class

Write a program that accepts an input string array/s and prompts the user to select a task from the menu as shown below.

        Menu
        ====

  1. Sort array
  2. Perform binary search in array
  3. Check if two arrays are equal
  4. Replace/fill array with a default value

Monday, November 11, 2013

Java Collection: ArrayList

Using Collections, implement a data structure for storing numbers(int) in such a fashion that they can be accessed either in the order in which they were added or in their natural sorted order (ascending).

Hint: The nodes should have two references.
Implement three cases, where the 1st case [insert] is for adding numbers into the data structure, 2nd case [ascendingorder] is for displaying the elements of data structure in ascending order and the 3rd case [insertionorder] is for displaying the elements of data structure the order of their insertion.