Question

In: Computer Science

write modules to sort by 2 field then 3 fields then 4 fields Use the data...

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

Solutions

Expert Solution

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());

}

}}


Related Solutions

WRITE MODULES TO SORT BY 2 FIELD THEN 3 FIELD, THEN 4 FIELD use data structure...
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...
A 2-3-4 tree can be used as a sorting machine. Write a sort() method that has...
A 2-3-4 tree can be used as a sorting machine. Write a sort() method that has passed an array of key values from main() and writes them back to the array in sorted order.
Given the following data, illustrate Selection Sort. index 1 2 3 4 5 6 data 11...
Given the following data, illustrate Selection Sort. index 1 2 3 4 5 6 data 11 10 21 3 7 5
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists....
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
Write a program, using any language you want, to sort the following using QuickSort: {3, 4,...
Write a program, using any language you want, to sort the following using QuickSort: {3, 4, 5,1, 2, 6, 7, 8}
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly...
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly linked list in ascending order. Why is this a bad idea? How does insertion sort’s runtime complexity change if you were to use it to sort a linked list? 2. Your classmate thinks that input arrays to the merge operation of merge sort don’t need to be in sorted order. Is your classmate right or wrong? Why?
4) Write a brief reflection of Task 2 and 3 which may include description on data...
4) Write a brief reflection of Task 2 and 3 which may include description on data type used to solve the given task, variable used, and objects created. Reflection should also include justification on logic used to solve the given task along with proper references
data set West SouthEast MidWest NewEngland 4 3 2 3 7 2 2 3 8 4...
data set West SouthEast MidWest NewEngland 4 3 2 3 7 2 2 3 8 4 8 4 3 2 9 8 4 8 10 7 4 3 12 3 4 5 1 5 5 6 9 6 6 2 9 2 10 4 3 2 9 3 6 1 7 3 5 4 7 9 6 3 4 2 4 3 3 3 4 5 2 1 4 10 1 2 4 10 2 1 2 9 3 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT