Question

In: Computer Science

Write a java program that creates a hashtable with 10 objects and 5 data members using...

Write a java program that creates a hashtable with 10 objects and 5 data members using mutator and accessor methods for each data member.

Solutions

Expert Solution

if i miss something please et me know by comment as very less details given

Created Employee class 5 data members for adding to hashtable

//Employee.java

import java.util.Objects;

public class Employee {
   // class with 5 data members
   
   String fname;
   String lname;
   String ssn;
   int id;
   String city;
   
   // constructor
   public Employee(String fname, String lname, String ssn, int id, String city) {
      this.fname = fname;
      this.lname = lname;
      this.ssn = ssn;
      this.id = id;
      this.city = city;
   }
   
   public Employee() {
   }
   
   // setters and getters
   public String getFname() {
      return fname;
   }
   
   public void setFname(String fname) {
      this.fname = fname;
   }
   
   public String getLname() {
      return lname;
   }
   
   public void setLname(String lname) {
      this.lname = lname;
   }
   
   public String getSsn() {
      return ssn;
   }
   
   public void setSsn(String ssn) {
      this.ssn = ssn;
   }
   
   public int getId() {
      return id;
   }
   
   public void setId(int id) {
      this.id = id;
   }
   
   public String getCity() {
      return city;
   }
   
   public void setCity(String city) {
      this.city = city;
   }
   
   @Override
   public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
      Employee employee = (Employee) o;
      return id == employee.id &&
            Objects.equals(fname, employee.fname) &&
            Objects.equals(lname, employee.lname) &&
            Objects.equals(ssn, employee.ssn) &&
            Objects.equals(city, employee.city);
   }
   
   @Override
   public int hashCode() {
      return Objects.hash(fname, lname, ssn, id, city);
   }
   
   @Override
   public String toString() {
      return "Employee{" +
            "fname='" + fname + '\'' +
            ", lname='" + lname + '\'' +
            ", ssn='" + ssn + '\'' +
            ", id=" + id +
            ", city='" + city + '\'' +
            '}';
   }
}

Created main class for adding the object into hashtable , for key taken as int value

//EmployeeMain.java

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

public class EmpoyeeMain {
   public static void main(String[] args) {
      // hashtable created
      Hashtable<Integer, Employee> hashtable = new Hashtable<>();
      for (int i = 0; i < 10; i++) {
         Employee e = new Employee();
         e.setFname(getRandomName());
         e.setLname(getRandomName());
         e.setSsn(getRandonSSN());
         e.setId(i+1);
         e.setCity("Pune");
         hashtable.put(i,e);//adding to hashtable
      }
      // printing table
      Set<Integer> keys = hashtable.keySet();
      Iterator<Integer> itr = keys.iterator();
      while (itr.hasNext()) {
         int i = itr.next();
         System.out.println("Key: "+i+" & Value: "+hashtable.get(i));
      }
      
   }
   
   //helper function
   public static String getRandomName() {
      String alphabets = leetter_digits.toLowerCase().substring(0, 25);
      StringBuilder builder = new StringBuilder();
      Random rand = new Random();
      int length = rand.nextInt(5) + 5;
      for (int i = 0; i < length; i++) {
         builder.append(alphabets.charAt(rand.nextInt(alphabets.length())));
      }
      return builder.toString();
      
   }
   
   final static String leetter_digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890";
   
   public static String getRandonSSN() {
      Random rand = new Random();
      StringBuilder builder = new StringBuilder();
      
      int length = rand.nextInt(5) + 5;
      for (int i = 0; i < length; i++) {
         builder.append(leetter_digits.charAt(rand.nextInt(leetter_digits.length())));
      }
      
      return builder.toString();
   }
}

//OUTPUT it may differ as it used random

Key: 9 & Value: Employee{fname='igndlhmw', lname='mfwuurwy', ssn='1WN8JX', id=10, city='Pune'}
Key: 8 & Value: Employee{fname='pnqmoa', lname='jmjtww', ssn='BMEPH', id=9, city='Pune'}
Key: 7 & Value: Employee{fname='rbapxq', lname='uhpbcknyv', ssn='4IGO8', id=8, city='Pune'}
Key: 6 & Value: Employee{fname='htkjhxfg', lname='tnojnwgv', ssn='XRJ8XYJGR', id=7, city='Pune'}
Key: 5 & Value: Employee{fname='bsqsv', lname='onfqurc', ssn='QAHIDVZM', id=6, city='Pune'}
Key: 4 & Value: Employee{fname='qlncsa', lname='ehfprjg', ssn='9O40NOBHB', id=5, city='Pune'}
Key: 3 & Value: Employee{fname='mswogm', lname='kbitah', ssn='ACDJ7W4W', id=4, city='Pune'}
Key: 2 & Value: Employee{fname='unqgcupg', lname='ycpxvf', ssn='RXPZVQL', id=3, city='Pune'}
Key: 1 & Value: Employee{fname='smbqjuqob', lname='jwjwo', ssn='9NJ4K', id=2, city='Pune'}
Key: 0 & Value: Employee{fname='glerxjfg', lname='vccgc', ssn='ALHKV4YY7', id=1, city='Pune'}


Related Solutions

java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
Using Java, design a program that creates an array of Card objects, shuffles them, then recursively...
Using Java, design a program that creates an array of Card objects, shuffles them, then recursively sorts them by value and then recursively sorts them by value and suit You will need to create a Card class with the following two fields: • value (a String) • suit (a String) You may include any other fields, constructors, or methods as needed. Your single array will need to contain 52 card objects, one for each value of each suit. The values...
Write a program that creates three vector objects IN C++. Fill the first two objects with...
Write a program that creates three vector objects IN C++. Fill the first two objects with 25 floating-point numbers using a for loop as follows: 1. fill the first vector object with the loop counter value; 2. fill the second vector object with the loop counter value squared; 3. finally, write a for loop that adds the corresponding elements in the first two vectors, and puts the result in the corresponding element of the third vector. Display all three vectors...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. Note: Use dialog boxes for communicating with the user. Remember to use the JOptionPane class which is the graphical user interface (GUI) and Comments that are...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. Note: Use dialog boxes for communicating with the user. Remember to use the JOptionPane class which is the graphical user interface (GUI) and Comments that are...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE numeric elements. Prompt the User for ASIZE numbers and store them in the array. After storing the values, calculate the sum of all the values in the array and display the sum. Modify the program you wrote for Problem 5 such that it calculates the product of all the values instead of the sum. Modify the program you wrote in Problem 6 such that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT