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

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 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 ( )...
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...
Java - Write a test program that creates an Account object with an account number of...
Java - Write a test program that creates an Account object with an account number of AC1111, a balance of $25,000, and an annual interest rate of 3.5. Use the withdraw method to withdraw $3,500, use the deposit method to deposit $3,500, and print the balance, the monthly interest, and the date when this account was created.
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
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...
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT