In: Computer Science
write modules to sort by
2 field then 3 fields then 4 fields Use the data Structure to guide you.
//HW sort by 5 of these fields and ask the user which field to sort by !!!
// 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
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[]);{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the fields to sort by");
String s=sc.nextInt();
if(s=="name"){
String str[]=name.split(" ");
List<String> slist= new ArrayList<String>();
str=Arrays.asList(str);
List<String> sortedList=slist.stream().sorted().collect().(collector().toList());
sortedList.forEach(System.out.println());
}
if(s=="address"){
String str1[]=address.split(" ");
List<String> slist1= new ArrayList<String>();
str1=Arrays.asList(str1);
List<String> sortedList=slist1.stream().sorted().collect().(collector().toList());
sortedList.forEach(System.out.println());
}
if(s=="hours"){
String str2[]=hours.split(" ");
List<String> slist2= new ArrayList<String>();
str2=Arrays.asList(str2);
List<String> sortedList=slist2.stream().sorted().collect().(collector().toList());
sortedList.forEach(System.out.println());
}
if(s=="dependent"){
String str[]3=dependent.split(" ");
List<String> slist3= new ArrayList<String>();
str3=Arrays.asList(str3);
List<String> sortedList=slist3.stream().sorted().collect().(collector().toList());
sortedList.forEach(System.out.println());
}
}}