Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this java server project. I have posted my code in screenshots because Chegg said the question was too long. Sorry for

I need help with this java server project.

I have posted my code in screenshots because Chegg said the question was too long. Sorry for the inconvenienceimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

package project1;

public interface CarADT {

public void setBuying (String s);

public void setMaint (String m);

public void setDoors (int numDoors);

// set more as 5

public void setPersons (int numPersons ); // set more as 5

public void setTrunk(String t);

public void setSafety (String s);

public void setRating (String c);

public String getRating();

}

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

package project1;

public interface PredictorADT {

public double getTrainingAccuracy ();

public String getPrediction (CarADT instance);

}

image text in transcribedimage text in transcribed

Overview: The program will create a Servlet that will respond to HTML requests with text streams. The servlet will have locally instantiated Car and Predictor objects. The website wil1 display three different files. The first will allow the user to enter their user name and password. If authenticated, the user will then be presented with the second page which wil1 allow the user to select descriptors of the car they wish to find the value of. These selectors can be any combination of drop down lists, radio buttons etc. Once the user fills in information about the trade in car and the original value of the car, they will click a button that will display the third page. This page will display some information found from the Predictor class. This will include the car(s) closest to the input car and the value of the trade in car. The value needs to relate to the prediction. Specifics index html - Webpage that allows the user to input their name and password secondPage.jsp -Webpage that displays the results from the servlet as outlined above thirdPage jsp - Webpage that displays the results from the user's selection in secondPage.jsp DataServlet.java -The servlet class that manages all of the requests and responses 1 package projecti; 2 3 public class Car implements CarADT private String buying; private String maint; private int doors; private int persons; private String lug_boot; private String safety; private String rating; 9 12 13 14 public Car) //default constructor with no meaningful values 15 setBuying(null); setMaint (null); setDoors (0); setPersons (); setTrunk (null); setSafety (null); setRating (null); 17 19 20 21 23 24 25 26 27 //constructor with all values public Car (String b, String m, int d, int p, String t, String s, String c) i 29 30 31 32 setBuying (b); setMaint (m); setDoors (d); setPersons (p); setTrunk(t); setSafety (s); setRating(c); 34 //constructor for a Car without rating 36e public Car(String b, String m, int d, int p, String t, String s) setBuying (b); setMaint(m); setDoors (d); setPersons (p); setTrunk(t); setSafety (s); setRating (null); 38 39 40 42 43 45 46 public String getRatingO 47 48 49 50e public void setRating(String rating) return rating; this.rating-rating; 52 53 53 54 public String getsafety() return safety; 56 57 58e public void setSafety(String safety) 59 60 61 this. safety safety; 62 public String getTrunk) 5 63 64 65 return lug_boot; 66 public void setTrunk (String lug_boot) i 67 68 69 this. lug-boot lug-boot; 7 public int getPersons() 71 72 73 return persons; 74 public void setPersons (int persons) 75 this.personspersons; 76 78 public int getDoors() { 79 80 81 82 public void setDoors (int doors) H 83 84 85 86 public String getMaint) 5 87 return doors; this.doors-doors; return maint; 89 90 public void setMaint (String maint) { this.maint-maint; 92 93 94 public String getBuying() [ 95 96 97 98 public void setBuying (String buying) return buying; this.buying-buying; 100 101 //checks if this holds the same values as c except for rating public boolean equals (Car c) { return false; return false; return false; return false; return false; return false; if (! (getBuying().equals (c.getBuying)))) if (! (getMaint).equals (c.getMaint()))) if (!(getDoors()c.getDoors ())) if (!(getPersons)c.getPersons())) if (! (getTrunk().equals (c.getTrunk)))) if (!(getSafety).equals (c.getsafety()))) return true; //toString method that returns the private variables and some description for them public String toString() 1 return "price:getBuying) ", maintance:"getMaint() + ", doors:" getDoors() + ", people: " + getPersons () + ", trunk size: " + getTrunk() + ", safety:"getSafety)", rating: " +getRating".In"; package projectl; import java.io.* public class Predictor implements PredictorADT{ private ArrayList getClosest (CarADT car2) 83 ArrayList toReturnnew ArrayList); Car car -(Car) car2; toReturn.add(cars.get()); toReturn.add(cars.get(1); toReturn.add(cars.get(2); int max -0; for (Car c: cars) 85 86 87 89 90 91 92 93 94 95 int thisScore-0; if (car.getDoors)c.getDoors)) if (car.getPersons C.getPersons ()) if (car.getMaint().equals (c.getMaint())) if (car.getBuying().equals(c.getBuying))) if (car.getTrunk().equals(c.getTrunk))) if (car.getSafety().equals(c.getSafety))) if (thisScore6) f thisScore++; thisScore++; thisScore++; thisScore++; thisScore++; thisScore++; 97 98 L00 01 L02 L03 // found an exact match toReturn.clear(); toReturn.add (c) L05 toReturn.add (c); return toReturn; if (thisScore max) i max- thisScore; toReturn.remove (2); to Return.add(e, c); ) // end of processing all cars return toReturn; //a toString method that returns a string of the values inside public String toString) String returnString; double unacc - 0; double acc 0; double good-0; double vgood0; double errNum-0; for (Car c: cars) switch (c.getRating()) { case "unacc": unacc+t break; case "acc": acc+t break; case "good": break; case "vgood": vgood++; break; default: errNum - 0; returnStringhe Car objects contained are of these ratings.In" returnString "ratingltentities tpercentln"; returnstring += "unacc\t" + (int) unacc + "\t\t" + (unacc * 100 / cars . size()) + "% "; returnSt ring +.. "acc\t" + (int) acc + "\t\t" + (acc * 100 / cars. size()) + "% "; returnstring +- "good\t" + (int) good + "\t\t" + (good * 100 / cars . size()) + "% "; returnstring += "vgood\t" + (int) vgood + "\t\t" + (vgood * 100 / cars. size()) + "% "; returnstring += "Errors\t" + (int) errNum + "\t\t" + (errNum * 100 / cars . size()) + "X"; return returnString; //runs through the cars acraylist, counts each accurate prediction, and divides the count by cars.size public double getTrainingAccuracy () double correct-0: for (Car c: cars) if (c.getRating().equals (getPrediction(c))) { correct++; return (correct / cars.size()) //a fair decision tree based off of weka's j48 tree (minor edits and interpretations) //joke reference -> https://www.xkcd.com/221 public String getPrediction (CarADT instance) { //checks if instance is an instance of Car if (!(instance instanceof Car)) { System.out.println( "Cannot predict. Not an instance of Car."); return null; //so its not type casting instance throughout the tree when its called Car c (Car) instance; //runs c through the decision tree if (c.getSafety).equals"low")) 1t return "unacc" else if (c.getSafety().equals( "med")) if (c.getPersons()2) return "unacc"; if (c.getTrunk().equals("small")) if (c.getTrunk().equals( "med") if (c.getPersons() 4) return "unacc" if (c.getMaint().equals("vhigh")) return "unacc" if (c.getMaint().equals( "med") if (c.getBuying).equals"low")) 1t return "unacc" else return "acc"; else return "acc"; if (c.getTrunk().equals("big")) [ if (c.getMaint().equals( "vhigh")) { if (c.getBuying).equals"low")) 1t 211 212 213 214 215 216 217 218 219 220 221 if (c.getBuying).equals"low")) 1t return "acc" if (c.getBuying().equals ( "med")) 5 return "acc"; else return "unacc" if (c.getMaint().equals( "med") if (c.getBuying).equals"low")) 1t return "good"; 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 else return "acc"; else return "acc"; if (c.getPersons ()-- 5) if (c.getBuying).equals"vhigh")) if (c.getTrunk().equals( "big") if (c.getMaint().equals( "low")) { return "acc"; if (c.getMaint().equals( "med") return "acc" else return "unacc" else return "unacc" if (c.getBuying).equals"high")) if (c.getTrunk().equals( "big") if (c.getMaint().equals( "vhigh")) { return "unacc" else return acc else return "unacc" else if (c.getPersons ()2) return "unacc"; if (c.getPersons()4) if (c.getMaint).equals("vhigh")) 5 if (c.getBuying).equals("vhigh")) return "unacc"; if (c.getBuying).equals("high")) 1i return "unacc" else return acc if (c.getMaint).equals("high")) { if (c.getBuying).equals("vhigh")) return "unacc" else return "acc"; if (c.getMaint).equals("med")) { if (c.getBuying).equals("low")) i return "good" else return "acc"; if (c.getMaint).equals("low")) { if (c.getBuying).equals("vhigh")) return "acc" if (c.getBuying).equals("high")) 1t return "acc" else return "good" else if (c.getBuying().equals ("vhigh")) { if (c.getMaint().equals("vhigh")) { return "unacc" if (c.getMaint().equals("high")) [ return "unacc" else return "acc"; else return acc if (c.getBuying().equals ("high")) 5 if (c.getMaint().equals("vhigh")) { return "unacc" else return acc if (c.getBuying().equals ("med")) { if (c.getMaint().equals("vhigh")) { return "acc"; if (c.getMaint().equals("high")) [ return "acc" if (c.getMaint).equals("med")) { if (c.getTrunk().equals("small"))t return "unacc" else return "vgood" else return "good" else if (c.getDoors ()2) return "good" if (c.getDoors ()2) return "vgood" else return "acc"; // if somehow get here, just return unacs return "unacc" package projectl5 2 import java.io.BufferedReader 7 public class PredictorTester public static void main(String args[]) // build a predictor from Training Data Predictor prednew Predictor); System . out. printin("Predictor nformation: "+pred+" "); double accuracy -pred.getTrainingAccuracy); System . out. printin ("Tra ining Accuracy : " + accuracy* 100 + "% "); // open the testing data and for each line create a Car // object and compare its ranking with the predicted ranking String line - ""; int tota!Tested = 0; int testedCorrect0; BufferedReader mylnfile null; try FileReader fr-new FileReader("./carTest.DATA"); myInfilenew BufferedReader(fr); while ((line =myInfile . read Line()) != null){ String] tokensline.split(","); int doors 0; if (tokens [2].equals("5more")) doors 5; else doors-Integer.parseInt (tokens [2]); int peoplect = 0; if (tokens [3].equals("more")) peoplect - 5; else peoplect = Integer.parseInt(tokens[3]); string buying = tokens[0]; String maint-tokens [1]; String lugboottokens [4]; String safetytokens [51: String accept-tokens[6] Car testCar-new Car(buying, maint, doors, peoplect, lugboot, safety, "unknown"); String predRating -pred.getPrediction(testCar); totalTested++; if (accept.equals(predRating)) i testedCorrect++; 53 54 catch (Exception e) System.err.println( "Didn't open file" ); 56 57 Finally{ 58 if (myInfile!-null) 59 try myInfile.close); catch (IOException e) i e.printStackTrace); 60 61 62 63 64 65 System . out.println("Testing Accuracy "+100.0*testedCorrect / totalTested+ "% "); - Car testCarnew Car("low" , "high", 2, 2, "small, "low", "unknown"); System.out.println("closest to "+testCar); System.out.println(pred.getClosest (testCar)); 68 69 70 71 72 73 74 75 76 Overview: The program will create a Servlet that will respond to HTML requests with text streams. The servlet will have locally instantiated Car and Predictor objects. The website wil1 display three different files. The first will allow the user to enter their user name and password. If authenticated, the user will then be presented with the second page which wil1 allow the user to select descriptors of the car they wish to find the value of. These selectors can be any combination of drop down lists, radio buttons etc. Once the user fills in information about the trade in car and the original value of the car, they will click a button that will display the third page. This page will display some information found from the Predictor class. This will include the car(s) closest to the input car and the value of the trade in car. The value needs to relate to the prediction. Specifics index html - Webpage that allows the user to input their name and password secondPage.jsp -Webpage that displays the results from the servlet as outlined above thirdPage jsp - Webpage that displays the results from the user's selection in secondPage.jsp DataServlet.java -The servlet class that manages all of the requests and responses 1 package projecti; 2 3 public class Car implements CarADT private String buying; private String maint; private int doors; private int persons; private String lug_boot; private String safety; private String rating; 9 12 13 14 public Car) //default constructor with no meaningful values 15 setBuying(null); setMaint (null); setDoors (0); setPersons (); setTrunk (null); setSafety (null); setRating (null); 17 19 20 21 23 24 25 26 27 //constructor with all values public Car (String b, String m, int d, int p, String t, String s, String c) i 29 30 31 32 setBuying (b); setMaint (m); setDoors (d); setPersons (p); setTrunk(t); setSafety (s); setRating(c); 34 //constructor for a Car without rating 36e public Car(String b, String m, int d, int p, String t, String s) setBuying (b); setMaint(m); setDoors (d); setPersons (p); setTrunk(t); setSafety (s); setRating (null); 38 39 40 42 43 45 46 public String getRatingO 47 48 49 50e public void setRating(String rating) return rating; this.rating-rating; 52 53 53 54 public String getsafety() return safety; 56 57 58e public void setSafety(String safety) 59 60 61 this. safety safety; 62 public String getTrunk) 5 63 64 65 return lug_boot; 66 public void setTrunk (String lug_boot) i 67 68 69 this. lug-boot lug-boot; 7 public int getPersons() 71 72 73 return persons; 74 public void setPersons (int persons) 75 this.personspersons; 76 78 public int getDoors() { 79 80 81 82 public void setDoors (int doors) H 83 84 85 86 public String getMaint) 5 87 return doors; this.doors-doors; return maint; 89 90 public void setMaint (String maint) { this.maint-maint; 92 93 94 public String getBuying() [ 95 96 97 98 public void setBuying (String buying) return buying; this.buying-buying; 100 101 //checks if this holds the same values as c except for rating public boolean equals (Car c) { return false; return false; return false; return false; return false; return false; if (! (getBuying().equals (c.getBuying)))) if (! (getMaint).equals (c.getMaint()))) if (!(getDoors()c.getDoors ())) if (!(getPersons)c.getPersons())) if (! (getTrunk().equals (c.getTrunk)))) if (!(getSafety).equals (c.getsafety()))) return true; //toString method that returns the private variables and some description for them public String toString() 1 return "price:getBuying) ", maintance:"getMaint() + ", doors:" getDoors() + ", people: " + getPersons () + ", trunk size: " + getTrunk() + ", safety:"getSafety)", rating: " +getRating".In"; package projectl; import java.io.* public class Predictor implements PredictorADT{ private ArrayList getClosest (CarADT car2) 83 ArrayList toReturnnew ArrayList); Car car -(Car) car2; toReturn.add(cars.get()); toReturn.add(cars.get(1); toReturn.add(cars.get(2); int max -0; for (Car c: cars) 85 86 87 89 90 91 92 93 94 95 int thisScore-0; if (car.getDoors)c.getDoors)) if (car.getPersons C.getPersons ()) if (car.getMaint().equals (c.getMaint())) if (car.getBuying().equals(c.getBuying))) if (car.getTrunk().equals(c.getTrunk))) if (car.getSafety().equals(c.getSafety))) if (thisScore6) f thisScore++; thisScore++; thisScore++; thisScore++; thisScore++; thisScore++; 97 98 L00 01 L02 L03 // found an exact match toReturn.clear(); toReturn.add (c) L05 toReturn.add (c); return toReturn; if (thisScore max) i max- thisScore; toReturn.remove (2); to Return.add(e, c); ) // end of processing all cars return toReturn; //a toString method that returns a string of the values inside public String toString) String returnString; double unacc - 0; double acc 0; double good-0; double vgood0; double errNum-0; for (Car c: cars) switch (c.getRating()) { case "unacc": unacc+t break; case "acc": acc+t break; case "good": break; case "vgood": vgood++; break; default: errNum - 0; returnStringhe Car objects contained are of these ratings.In" returnString "ratingltentities tpercentln"; returnstring += "unacc\t" + (int) unacc + "\t\t" + (unacc * 100 / cars . size()) + "% "; returnSt ring +.. "acc\t" + (int) acc + "\t\t" + (acc * 100 / cars. size()) + "% "; returnstring +- "good\t" + (int) good + "\t\t" + (good * 100 / cars . size()) + "% "; returnstring += "vgood\t" + (int) vgood + "\t\t" + (vgood * 100 / cars. size()) + "% "; returnstring += "Errors\t" + (int) errNum + "\t\t" + (errNum * 100 / cars . size()) + "X"; return returnString; //runs through the cars acraylist, counts each accurate prediction, and divides the count by cars.size public double getTrainingAccuracy () double correct-0: for (Car c: cars) if (c.getRating().equals (getPrediction(c))) { correct++; return (correct / cars.size()) //a fair decision tree based off of weka's j48 tree (minor edits and interpretations) //joke reference -> https://www.xkcd.com/221 public String getPrediction (CarADT instance) { //checks if instance is an instance of Car if (!(instance instanceof Car)) { System.out.println( "Cannot predict. Not an instance of Car."); return null; //so its not type casting instance throughout the tree when its called Car c (Car) instance; //runs c through the decision tree if (c.getSafety).equals"low")) 1t return "unacc" else if (c.getSafety().equals( "med")) if (c.getPersons()2) return "unacc"; if (c.getTrunk().equals("small")) if (c.getTrunk().equals( "med") if (c.getPersons() 4) return "unacc" if (c.getMaint().equals("vhigh")) return "unacc" if (c.getMaint().equals( "med") if (c.getBuying).equals"low")) 1t return "unacc" else return "acc"; else return "acc"; if (c.getTrunk().equals("big")) [ if (c.getMaint().equals( "vhigh")) { if (c.getBuying).equals"low")) 1t 211 212 213 214 215 216 217 218 219 220 221 if (c.getBuying).equals"low")) 1t return "acc" if (c.getBuying().equals ( "med")) 5 return "acc"; else return "unacc" if (c.getMaint().equals( "med") if (c.getBuying).equals"low")) 1t return "good"; 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 else return "acc"; else return "acc"; if (c.getPersons ()-- 5) if (c.getBuying).equals"vhigh")) if (c.getTrunk().equals( "big") if (c.getMaint().equals( "low")) { return "acc"; if (c.getMaint().equals( "med") return "acc" else return "unacc" else return "unacc" if (c.getBuying).equals"high")) if (c.getTrunk().equals( "big") if (c.getMaint().equals( "vhigh")) { return "unacc" else return acc else return "unacc" else if (c.getPersons ()2) return "unacc"; if (c.getPersons()4) if (c.getMaint).equals("vhigh")) 5 if (c.getBuying).equals("vhigh")) return "unacc"; if (c.getBuying).equals("high")) 1i return "unacc" else return acc if (c.getMaint).equals("high")) { if (c.getBuying).equals("vhigh")) return "unacc" else return "acc"; if (c.getMaint).equals("med")) { if (c.getBuying).equals("low")) i return "good" else return "acc"; if (c.getMaint).equals("low")) { if (c.getBuying).equals("vhigh")) return "acc" if (c.getBuying).equals("high")) 1t return "acc" else return "good" else if (c.getBuying().equals ("vhigh")) { if (c.getMaint().equals("vhigh")) { return "unacc" if (c.getMaint().equals("high")) [ return "unacc" else return "acc"; else return acc if (c.getBuying().equals ("high")) 5 if (c.getMaint().equals("vhigh")) { return "unacc" else return acc if (c.getBuying().equals ("med")) { if (c.getMaint().equals("vhigh")) { return "acc"; if (c.getMaint().equals("high")) [ return "acc" if (c.getMaint).equals("med")) { if (c.getTrunk().equals("small"))t return "unacc" else return "vgood" else return "good" else if (c.getDoors ()2) return "good" if (c.getDoors ()2) return "vgood" else return "acc"; // if somehow get here, just return unacs return "unacc" package projectl5 2 import java.io.BufferedReader 7 public class PredictorTester public static void main(String args[]) // build a predictor from Training Data Predictor prednew Predictor); System . out. printin("Predictor nformation: "+pred+" "); double accuracy -pred.getTrainingAccuracy); System . out. printin ("Tra ining Accuracy : " + accuracy* 100 + "% "); // open the testing data and for each line create a Car // object and compare its ranking with the predicted ranking String line - ""; int tota!Tested = 0; int testedCorrect0; BufferedReader mylnfile null; try FileReader fr-new FileReader("./carTest.DATA"); myInfilenew BufferedReader(fr); while ((line =myInfile . read Line()) != null){ String] tokensline.split(","); int doors 0; if (tokens [2].equals("5more")) doors 5; else doors-Integer.parseInt (tokens [2]); int peoplect = 0; if (tokens [3].equals("more")) peoplect - 5; else peoplect = Integer.parseInt(tokens[3]); string buying = tokens[0]; String maint-tokens [1]; String lugboottokens [4]; String safetytokens [51: String accept-tokens[6] Car testCar-new Car(buying, maint, doors, peoplect, lugboot, safety, "unknown"); String predRating -pred.getPrediction(testCar); totalTested++; if (accept.equals(predRating)) i testedCorrect++; 53 54 catch (Exception e) System.err.println( "Didn't open file" ); 56 57 Finally{ 58 if (myInfile!-null) 59 try myInfile.close); catch (IOException e) i e.printStackTrace); 60 61 62 63 64 65 System . out.println("Testing Accuracy "+100.0*testedCorrect / totalTested+ "% "); - Car testCarnew Car("low" , "high", 2, 2, "small, "low", "unknown"); System.out.println("closest to "+testCar); System.out.println(pred.getClosest (testCar)); 68 69 70 71 72 73 74 75 76

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions