Question

In: Computer Science

sort by the following (name, address, dependent and gender) of these fields and ask the user...

sort by the following (name, address, dependent and gender) of these fields and ask the user which field to sort by !. this mean the following java must sort by address if we need, by name , by dependent , and by gender it depend on the following java it must have an option which we need to sort. please i need your help now, you just add the sorting on the following java.

// Use a custom comparator.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

class Emprec {
   String name;
   String address;
   double hours;
   double rate;
   int dependents;
   char gender;
   boolean degree;

   // This is the classes's constructor !!!!

   Emprec(String name, String address, String hours,String dependents) {

       try {
           this.name = name;
           this.address = address;
           this.hours = Double.valueOf(hours).doubleValue();
           this.dependents = Integer.parseInt(dependents);
       } catch (NumberFormatException errmsg) {
           System.out.println("Invalid format" + errmsg);

           this.name = "";
           this.hours = 0.0;

       }// catch

   }// Emprec constructor !!!!

   double calc_fed_tax(double hours, double rate) {

       double yearly_income;

       yearly_income = hours * rate * 52;

       if (yearly_income < 30000.00)
           return (hours * rate * .28);

       else if (yearly_income < 50000.00)
           return (hours * rate * .32);

       else
           return (hours * rate * .38);

   }// calc_fed_tax

   double calc_state_tax(double hours, double rate) {

       double state_tax;

       state_tax = hours * rate * .0561;

       return (state_tax);

   }// calc_state_tax

   public void setName(String name) {
       this.name = name;
   }

   public String getName() {
       return name;
   }
  
   public String getAddress() {
       return address;
   }

   public double getHours() {
       return hours;
   }
  
   public int getDependents() {
       return dependents;
   }
  
public double getRate(){
return rate;
}
  
public char getGender(){
return gender;
}

   public String toString() {

       return ("\n Name: " + name +
               "\n Address: " + address +
               "\n Hours: " + hours+
               "\n Dependents " + dependents);

   }// toString
}// Emprec

public class CompDemo3Sorts_Improved {
   public static void main(String args[]) throws IOException {
       // Create a tree set
       BufferedReader inData = new BufferedReader(new InputStreamReader(
               System.in));

       // create strings for the input data for the Emprec object
       String str_name;
       String str_address;
       String str_hours;
       String str_dependents;

       TreeSet ts = new TreeSet(new MyCompHours());// eclipse ask for casting
       for (;;) {
           System.out.print(" name: ");
           str_name = inData.readLine();

           if (str_name.equalsIgnoreCase("exit")) break;
          
           System.out.print(" Address: ");
           str_address = inData.readLine();
          
           System.out.print(" Hours: ");
           str_hours = inData.readLine();
          
               System.out.print(" Dependents: ");
           str_dependents = inData.readLine();

           Emprec employee = new Emprec(str_name, str_address, str_hours,str_dependents);

           ts.add(employee);
       }// for

       // Get an iterator
       Iterator i = ts.iterator();

       // Display elements
       while (i.hasNext()) {
           Object element = i.next();
           System.out.print(element + "\n");// calls the toString()
       }//while
       System.out.println();
   }
}

class MyCompName implements Comparator{ // eclipse ask for casting object
   public int compare(Object emp1, Object emp2) {

       String emp1Name = ((Emprec) emp1).getName();

       String emp2Name = ((Emprec) emp2).getName();

       return ((emp2Name.compareTo(emp1Name) <= 0) ? -1 : +1);
      
   }
}


class MyCompHours implements Comparator{ // eclipse ask for casting object
   public int compare(Object emp1, Object emp2) {

       double emp1Hours = ((Emprec) emp1).getHours();

       double emp2Hours = ((Emprec) emp2).getHours();

      
       return (emp1Hours <= emp2Hours)? -1:+1;
   }
}


class MyCompAddress implements Comparator{ // eclipse ask for casting object
   public int compare(Object emp1, Object emp2) {

       String emp1Address = ((Emprec) emp1).getAddress();

       String emp2Address = ((Emprec) emp2).getAddress();

      
       return ((emp2Address.compareTo(emp1Address) <= 0) ? -1 : +1);
      
   }
}


class MyCompRate implements Comparator{
public int compare(Object emp1, Object emp2){
double emp1Rate = ((Emprec) emp1).getRate();
double emp2Rate = ((Emprec) emp2).getRate();
      
return ((emp1Rate <= emp2Rate) ? -1 : +1);
}
}


class MyCompDependents implements Comparator{
public int compare(Object emp1, Object emp2){
int emp1Dependents = ((Emprec) emp1).getDependents();
int emp2Dependents = ((Emprec) emp2).getDependents();
      
return ((emp1Dependents <= emp2Dependents) ? -1 : +1);
}
}


class MyCompGender implements Comparator{
public int compare(Object emp1, Object emp2){
char emp1Gender = ((Emprec) emp1).getGender();
char emp2Gender = ((Emprec) emp2).getGender();
      
return ((emp1Gender <= emp2Gender) ? -1 : +1);
}
}

Solutions

Expert Solution

//Sorting program depending on user choice

// Use a custom comparator.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.*;

class Emprec

{

   String name;

   String address;

   double hours;

   double rate;

   int dependents;

   char gender;

   boolean degree;

   // This is the classes's constructor !!!!

   Emprec(String name, String address, String hours,String dependents)

{

       try

       {

           this.name = name;

           this.address = address;

           this.hours = Double.valueOf(hours).doubleValue();

           this.dependents = Integer.parseInt(dependents);

       }

      catch (NumberFormatException errmsg)

      {

           System.out.println("Invalid format" + errmsg);

           this.name = "";

           this.hours = 0.0;

       }// catch

   }// Emprec constructor !!!!

   double calc_fed_tax(double hours, double rate)

   {

       double yearly_income;

       yearly_income = hours * rate * 52;

       if (yearly_income < 30000.00)

           return (hours * rate * .28);

       else if (yearly_income < 50000.00)

           return (hours * rate * .32);

       else

           return (hours * rate * .38);

   }// calc_fed_tax

   double calc_state_tax(double hours, double rate)

    {

       double state_tax;

       state_tax = hours * rate * .0561;

       return (state_tax);

   }// calc_state_tax

   public void setName(String name)

{

       this.name = name;

   }

   public String getName()

   {

       return name;

   }

   public String getAddress()

   {

       return address;

   }

   public double getHours()

{

       return hours;

   }

   public int getDependents()

{

       return dependents;

   }

public double getRate()

{

return rate;

}

public char getGender()

{

return gender;

}

   public String toString()

   {

      return ("\n Name: " + name +

               "\n Address: " + address +

               "\n Hours: " + hours+

               "\n Dependents " + dependents);

   }// toString

}// Emprec

public class CompDemo3Sorts_Improved

{

   public static void main(String args[]) throws IOException

{

   CompDemo3Sorts_Improved ob=new CompDemo3Sorts_Improved();

ob.sort();

   // Create a tree set

       BufferedReader inData = new BufferedReader(new InputStreamReader(

               System.in));

       // create strings for the input data for the Emprec object

       String str_name;

       String str_address;

       String str_hours;

       String str_dependents;

       TreeSet ts = new TreeSet(new MyCompHours());// eclipse ask for casting

       for (;;)

{

           System.out.print(" name: ");

           str_name = inData.readLine();

           if (str_name.equalsIgnoreCase("exit")) break;

           System.out.print(" Address: ");

           str_address = inData.readLine();

           System.out.print(" Hours: ");

           str_hours = inData.readLine();

            System.out.print(" Dependents: ");

           str_dependents = inData.readLine();

           Emprec employee = new Emprec(str_name, str_address, str_hours,str_dependents);

          ts.add(employee);

       }// for

       // Get an iterator

       Iterator i = ts.iterator();

       // Display elements

       while (i.hasNext())

        {

           Object element = i.next();

           System.out.print(element + "\n");// calls the toString()

       }//while

       System.out.println();

   }

}

class MyCompName implements Comparator

{ // eclipse ask for casting object

   public int compare(Object emp1, Object emp2)

{

String emp1Name = ((Emprec) emp1).getName();

  String emp2Name = ((Emprec) emp2).getName();

   return ((emp2Name.compareTo(emp1Name) <= 0) ? -1 : +1);  

   }

}

class MyCompHours implements Comparator

{ // eclipse ask for casting object

   public int compare(Object emp1, Object emp2)

{

double emp1Hours = ((Emprec) emp1).getHours();

double emp2Hours = ((Emprec) emp2).getHours();

  return (emp1Hours <= emp2Hours)? -1:+1;

   }

}

class MyCompAddress implements Comparator

{ // eclipse ask for casting object

   public int compare(Object emp1, Object emp2)

{

String emp1Address = ((Emprec) emp1).getAddress();

   String emp2Address = ((Emprec) emp2).getAddress();

return ((emp2Address.compareTo(emp1Address) <= 0) ? -1 : +1)

   }

}

class MyCompRate implements Comparator

{

public int compare(Object emp1, Object emp2)

{

double emp1Rate = ((Emprec) emp1).getRate();

double emp2Rate = ((Emprec) emp2).getRate();

return ((emp1Rate <= emp2Rate) ? -1 : +1);

}

}

class MyCompDependents implements Comparator

{

public int compare(Object emp1, Object emp2)

{

int emp1Dependents = ((Emprec) emp1).getDependents();

int emp2Dependents = ((Emprec) emp2).getDependents();   

return ((emp1Dependents <= emp2Dependents) ? -1 : +1);

}

}

class MyCompGender implements Comparator

{

public int compare(Object emp1, Object emp2)

{

char emp1Gender = ((Emprec) emp1).getGender();

char emp2Gender = ((Emprec) emp2).getGender();   

return ((emp1Gender <= emp2Gender) ? -1 : +1);

}

}

//sort method sort the data depending on user choice

   void sort()

{

                        String name[]=new String[10];

                        String address[]=new String[10];

                        String dependent[]=new String [10];

                        String gender[]=new String[10];

                        int l=arg.length;

                        String temp;

                        int ch1;

                      ch1=Integer.parseInt(arg[0]);

                     switch(ch1)

                     {

                 //sort by name

                                case 1:

                                   for(int i=0;i<l;i++)

                                    {

                                                name[i]=arg[i];

                                    }

                                    for(int j=0;j<l;j++)

                                    {

                                                for(int k=j+1;k<l;k++)

                                                {

if((name[j].compareTo(name[k]))>0)

                                                            {

                                                                        temp=name[j];

                                                                        name[j]=name[k];

                                                                        name[k]=temp;

                                                            }

                                                }

                                    }

                                    System.out.println("Sorted names");

                                    for(int i=0;i<l;i++)

                                    {        

                                                System.out.println(name[i]);

                                    }

                                    break;

//sort by address

                                     case 2:                               

                                     for(int i=0;i<l;i++)

                                    {

                                                address[i]=arg[i];

                                    }

                                    for(int j=0;j<l;j++)

                                    {

                                                for(int k=j+1;k<l;k++)

                                                {

if((address[j].compareTo(address[k]))>0)

                                                            {

                                                                        temp=address[j];

                                                                        address[j]=address[k];

                                                                        address[k]=temp;

                                                            }

                                                }

                                    }

                                    System.out.println("Sorted address");

                                    for(int i=0;i<l;i++)

                                    {        

                                                System.out.println(address[i]);

                                    }

                                    break;

//sort by gender

                                   case 3:                               

                                     for(int i=0;i<l;i++)

                                    {

                                                gender[i]=arg[i];

                                    }

                                    for(int j=0;j<l;j++)

                                    {

                                                for(int k=j+1;k<l;k++)

                                                {

if((gender[j].compareTo(gender[k]))>0)

                                                            {

                                                                        temp=address[j];

                                                                      gender[j]=gender[k];

                                                                      gender[k]=temp;

                                                            }

                                                }

                                    }

                                    System.out.println("Sorted Gender");

                                    for(int i=0;i<l;i++)

                                    {        

                                                System.out.println( gender[i]);

                                    }

                                         break;

//sort by dependent value

                                 case 4:                               

                                     for(int i=0;i<l;i++)

                                    {

                                                dependent[i]=arg[i];

                                    }

                                    for(int j=0;j<l;j++)

                                    {

                                                for(int k=j+1;k<l;k++)

                                                {

                                                            if((dependent[j].compareTo(dependent[k]))>0)

                                                            {

                                                                        temp=address[j];

                                                                     dependent[j]=dependent[k];

                                                                      dependent[k]=temp;

                                                            }

                                                }

                                    }

                                    System.out.println("Sorted dependent");

                                    for(int i=0;i<l;i++)

                                    {        

                                                System.out.println( dependent[i]);

}

                                   break;

                                  default:

                                System.out.println("\n\n please enter valid choice");

}

}

Output:


Related Solutions

Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
In Kali Linux Write a script that ask the user to enter an IP address and...
In Kali Linux Write a script that ask the user to enter an IP address and a port number, then use the provided entries from the user to perform a query on your local network and determine if the given port is open on the provide network. Need to submit solutions for both below. 1.A short report showing the functionality of your code 2. your bash script
Your Application should ask the user to enter their name and their salary.
Create a C# Console Application, name the solution Homework 6 and the project TaxRate.Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
Create a form that has the following fields: Name Address City Country (Please Select Country, US,...
Create a form that has the following fields: Name Address City Country (Please Select Country, US, Canada) State/Province (Drop Down List) Postal Code Submit Button Create two arrays of objects, one for US States, one for Canadian Provinces. The object should contain Name and Abbreviation. You don’t need to do all the states or provinces, just a few to make sure your code works. (3 OF EACH) When the user selects either US or Canada, populate the drop down using...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Ask the user for the name of a car maker. Display the oldest and newest car...
Ask the user for the name of a car maker. Display the oldest and newest car from that maker. Modify your display to include the VIN of the car. Format the output in columns of 15, 25, 5, and 18. Standard Input                 Files in the same directory Toyota car-list.txt Required Output What car make are you looking for?\n Oldest Toyota\n Toyota MR2 1985 WAUFFAFL2CN997894\n Newest Toyota\n Toyota Venza 2013 WAUEH54B01N735764\n Standard Input                 Files in the same directory Chevrolet...
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
Name the script thirsty.sh. Ask the user if they are thirsty. Read the user's response. If...
Name the script thirsty.sh. Ask the user if they are thirsty. Read the user's response. If they answer no or No, print an appropriate message and exit. If they answer yes or Yes, ask what they would like to drink. Read the user's response. If they answer water print "Clear crisp and refreshing." If they answer beer print "Let me see some id." If they answer wine print "one box or two." If they answer anything else print "Coming right...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT