In: Computer Science
please , im sorting by name, address, dependent and hours. i got a lot of errors i need help please. Thanks
package application;
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 abstract class CompDemo3Sorts_Improved {
public abstract void main(String args[]);{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the fields to sort by");
String s=sc.next();
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());
}
}}
// Modified working code
//Main.java
package application;
import java.util.*;
public abstract class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the fields to sort by");
String s=sc.next();
if(s.equals("name")){
System.out.println("Enter name"); // when the desired sorting is selected . The list of it is taken input .
sc.nextLine();
String name=sc.nextLine();
String str[]=name.split(" ");
List<String> slist= new ArrayList<String>();
slist=Arrays.asList(str);
Collections.sort(slist); // used Collection class for sorting
for(int i=0;i<slist.size();i++)
System.out.println(slist.get(i)); //Printing of sorted list
}
if(s.equals("address")){
System.out.println("Enter address");
sc.nextLine();
String address=sc.nextLine();
String str1[]=address.split(" ");
List<String> slist1= new ArrayList<String>();
slist1=Arrays.asList(str1);
Collections.sort(slist1);
for(int i=0;i<slist1.size();i++)
System.out.println(slist1.get(i));
}
if(s.equals("hours")){
System.out.println("Enter hours");
sc.nextLine();
String hours=sc.nextLine();
String str2[]=hours.split(" ");
List<String> slist2= new ArrayList<String>();
slist2=Arrays.asList(str2);
Collections.sort(slist2);
for(int i=0;i<slist2.size();i++)
System.out.println(slist2.get(i));
}
if(s.equals("dependent")){
System.out.println("Enter dependent");
sc.nextLine();
String dependent=sc.nextLine();
String str3[]=dependent.split(" ");
List<String> slist3= new ArrayList<String>();
slist3=Arrays.asList(str3);
Collections.sort(slist3);
for(int i=0;i<slist3.size();i++)
System.out.println(slist3.get(i));
}
}}
//Emprec.java
package application;
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