In: Computer Science
WRITE MODULES TO SORT BY 2 FIELD THEN 3 FIELD, THEN 4 FIELD use
data structure to guide you. please i need help. im so glad if
anyone help me. thank you a lot and be safe
// 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<Emprec> ts = new TreeSet<Emprec>(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<Emprec> 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<Object> { // 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<Object> { // 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<Object> { //
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<Object>{
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<Object>{
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<Object>{
public int compare(Object emp1, Object
emp2){
char emp1Gender =
((Emprec) emp1).getGender();
char emp2Gender =
((Emprec) emp2).getGender();
return ((emp1Gender
<= emp2Gender) ? -1 : +1);
}
}
WRITE MODULES TO SORT BY 2 FIELD THEN 3 FIELD, THEN 4
FIELD use data structure to guide you. please i need help. im so
glad if anyone help me. thank you a lot and be safe
I am using java 8 .. it will work .. please refer my code for multiple sorting.....
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class CompDemo {
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;
ArrayList<Emprec> list =
new ArrayList<Emprec>();// 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);
list.add(employee);
}
//name comparator
Comparator<Emprec>
compareByName = Comparator.comparing( Emprec::getName );
//Address comparator
Comparator<Emprec>
compareByAddress = Comparator.comparing( Emprec::getAddress
);
//hours comparator
Comparator<Emprec>
compareByhours = Comparator.comparing( Emprec::getHours );
//Dependent comparator
Comparator<Emprec>
compareByDependent = Comparator.comparing( Emprec::getDependents
);
//Compare by first name and then
last name (multiple fields)
Comparator<Emprec>
compareByFullName =
compareByName.thenComparing(compareByAddress)
.thenComparing(compareByhours).thenComparing(compareByDependent);
//Using Comparator - pseudo
code
List<Emprec> sortedEmployees
= list.stream()
.sorted(compareByFullName)
.collect(Collectors.toList());
// Get an iterator
Iterator<Emprec> i =
sortedEmployees.iterator();
// Display elements
while (i.hasNext()) {
Object element =
i.next();
System.out.print(element + "\t");// calls the toString()
}//while
System.out.println();
}
}
output :
name: Arjun
Address: Hys
Hours: 12
Dependents: 3
name: Arun
Address: Gan
Hours: 42
Dependents: 13
name: Rajesh
Address: Sec
Hours: 76
Dependents: 45
name: Ganesh
Address: Bangalore
Hours: 23
Dependents: 23
name: exit
Name: Arjun
Address: Hys
Hours: 12.0
Dependents 3
Name: Arun
Address: Gan
Hours: 42.0
Dependents 13
Name: Ganesh
Address: Bangalore
Hours: 23.0
Dependents 23
Name: Rajesh
Address: Sec
Hours: 76.0
Dependents 45