Hi please make sure the answer is right before you post the answer, The first tw
ID: 3901948 • Letter: H
Question
Hi please make sure the answer is right before you post the answer, The first two pictures are the one question and the rest of the pics are info you need to do the problem correctly!!!
Driver.java
package com.company;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Created by naseef on 5/16/18.
*/
public class Driver {
public static void main(String[] args) {
Grades gradesA = initialize("inputHW9A.txt");
Grades gradesB = initialize("inputHW9B.txt");
Grades gradesC = initialize("inputHW9C.txt");
Grades repeat = initialize("inputRepeat.txt");
Grades mergedGrades = merge(gradesA, gradesB, gradesC);
Grades sortedGrades = sort(mergedGrades, repeat);
flush(sortedGrades);
}
public static Grades initialize(String section) {
// PLEASE IMPLEMENT THIS METHOD.
return null;
}
public static Grades merge(Grades gradesA, Grades gradesB, Grades
gradesC) {
Grades mergedGrades = new Grades();
// Identify which method in the Grades class can you use to take
these three Grades and merge them
return mergedGrades;
}
public static Grades sort(Grades mergedGrades, Grades repeat) {
// PLEASE COMPLETE THE METHOD USING THE FOLLOWING LOGIC
// 1: Create a an empty Grades list.
Grades sortedGrades = new Grades();
// 2: Loop through the mergedGrades list and find student with the
highest grade.
// 3: Add this to the sortedGrades list ( use Grades::add Method )
// 4: Remove it from the mergedGrades list ( use Grades::remove
Method)
// 5: Do this till mergedGrades list becomes empty
// If you find collision (two students with the same grade ) this
is how you will resolve it
// 1: if the student has repeated the course, then he will be
moved to the end ( within the GPA block )
// 2: If both students have repeated, or both have not repeated,
but their GPA is the same then sort them by First Name
// 3: If the first name is same, then sort by last name
return sortedGrades;
}
public static void flush(Grades sortedGrades) {
try {
PrintWriter writer = new PrintWriter("outputHW8.txt");
writer.write(sortedGrades.toString()); // Notice how I am
calling toString on Grades class
// Grades toString
calls toString on Grade class that you need to implement.
writer.close();
} catch (FileNotFoundException e) {
}
}
}
Grade.java
package com.company;
/**
* Created by naseef on 5/16/18.
*/
public class Grade {
private String id;
private double gpa;
private String firstName;
private String lastName;
// Please implement the following
// 1: Constructor that takes String id, double gpa, String firstName,
String lastName
// 2: Constructor that takes String id, double gpa, String firstName.
Sets Last Name to NLN
// Getters to get GPA, firstName and LastName. You will use it in the
sort Method in driver class.
// Override the equal method.
// You use it in the sort method in the Driver class to check if a
particular GradeObject exists in the repeat list
// Override the toString method.
// flush method in driver calls toString for Grades class.
// The Grades class has implemented toString override.
// It internally calls Grade::toString.
// So you need to implement this method so that the file is printed
correctly
// In addition, you will need the below two methods
public boolean isLessGPA(Grade grade) {
// implement logic
return false;
}
public boolean isSameGPA(Grade grade) {
// implement logic
return false;
}
}
Grades.java
package com.company;
/**
* Created by naseef on 5/21/18.
*/
public class Grades {
private int capacity;
private int size;
private Grade[] gradeArray;
public final int OFFSET = 4;
public Grades() {
this(4);
}
public Grades(int capacity) {
this.capacity = capacity;
this.gradeArray = new Grade[capacity];
this.size = 0;
}
public void add(Grade grade) {
if(size < capacity) {
gradeArray[size++] = grade;
} else {
this.capacity += OFFSET;
Grade[] temp = new Grade[this.capacity];
for(int i = 0; i < size; i++) {
temp[i] = gradeArray[i];
}
temp[size++] = grade;
this.gradeArray = temp;
}
}
public void add(Grades grades) {
for(int i = 0; i < grades.size; i++) {
add(grades.get(i));
}
}
public void remove(int index) {
for(int i = index; i < size-1; i++) {
this.gradeArray[i] = this.gradeArray[i+1];
}
this.gradeArray[size-1] = null;
this.size --;
}
public Grade get(int index) {
return gradeArray[index];
}
public int size() {
return this.size;
}
public boolean contains(Grade grade) {
for(int i = 0; i < this.size; i++) {
if(this.gradeArray[i].equals(grade)) {
return true ;
}
}
return false;
}
@Override
public String toString() {
String data = "" ;
for(int i = 0; i < size; i++) {
data += gradeArray[i].toString();
data += " ";
}
return data;
}
}
Inputhw9A.txt
S001 3.3 John Rodgers
S002 3.9 Jim
S003 3.9 Misty Fang
S004 3.9 Aseef Hernandez
S005 4.0 Stacy Lu
S006 3.9 Aseef Nilkund
S009 3.9 Steve Calderon
S010 3.8 Raj Singh
Inputhw9B.txt
S011 3.3 Jason Kramer
S012 3.5 Kathy Calderon
S013 3.2 Roopa Singh
S014 2.4 Amid Naveed
S015 1.0 Faith Williams
S016 3.9 Aseef Simmons
Inputhw9C.txt
S017 3.3 Hifza Nilkund
S018 3.5 Hamza Nilkund
S019 3.2 Chris Peach
S020 2.4 Ramona Luke
InputRepeat.txt
S001 3.3 John Rodgers
S013 3.2 Roopa Singh
S014 2.4 Amid Naveed
S016 3.9 Aseef Simmons
Outputhw8.txt
S005: Stacy, Lu 4.0
S004: Aseef, Hernandez 3.9
S006: Aseef, Nilkund 3.9
S002: Jim, NLN 3.9
S003: Misty, Fang 3.9
S009: Steve, Calderon 3.9
S016: Aseef, Simmons 3.9
S010: Raj, Singh 3.8
S018: Hamza, Nilkund 3.5
S012: Kathy, Calderon 3.5
S017: Hifza, Nilkund 3.3
S011: Jason, Kramer 3.3
S001: John, Rodgers 3.3
S019: Chris, Peach 3.2
S013: Roopa, Singh 3.2
S020: Ramona, Luke 2.4
S014: Amid, Naveed 2.4
S015: Faith, Williams 1.0
UW CSS 142C Spring Qtr. 2018 Homework #8 Due 06/02 11:59 PM Processing data using Files and Arrays of Objects-Part2 This is your final HW. You can work in groups or 2 if you like. You will be given 3 files: Driver.java -The entry point of execution. Reads the input files, builds up the data structures in memory, does the merging and sorting and finally flushes the data back to the output file. Grade.java-Class that represents a student entity that has firstName, lastName, grade and student ID Grades java Class that models a dynamic array. via this class we get the ability to create an array of Grades which is dynamic. It can grow or shrink as needed. You will also be given 3 input files: inputHW9A.txt, inputHW9B.txt, inputHW9C.txt These files conceptually represent all the students in Css142C per section along with their GPAs You will also be given a fourth file called "inputRepeat.txt". Contains all the students that have repeated this class before. You are expected to create a project and include the 3 source files and the 4 input files. Go through the comments in the file and implement missing methods and incomplete methods When you finish the project and run your code, you will generate an output file called "outputHW9.txt". This is a file where the grades of all the Students across the three sections are written in a sorted order-highest to lowest after applying the rules as outlined in the merge method. IMP NOTE: . I am keeping the description a bit brief for two reasons o Iwant you to develop the ability to start reading code and interpret what is.needed o We also went at depth in our class where I explained many details about the project. code submitted with compilation errors will get a 0 Code that immediately throws an exception upon running will get a o . Cod e that executes correctly without error, and without any exceptions but does not create the output file or has an empty output file will get a 5 points overall deduction. Please test that your code works with the sample input files provided in this assignment and generates the sample output again provided in this assignmentExplanation / Answer
I have completed your program exactly as per the specifications. Here is the code. Please find the comments , learn how things work and let me know if you have any doubts. Thanks
EDIT: I’m getting troubles submitting the answer without losing the format. Showing character limit exceeded error. So I have to paste it as a plain text, which will cause the loss of code formatting and indentations. Sorry for the trouble. If you are using eclipse ,copy the code and press ctrl+shift+F to format the code
// Driver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
*
* Created by naseef on 5/16/18.
*/
public class Driver {
public static void main(String[] args) {
Grades gradesA = initialize("inputHW9A.txt");
Grades gradesB = initialize("inputHW9B.txt");
Grades gradesC = initialize("inputHW9C.txt");
Grades repeat = initialize("inputRepeat.txt");
Grades mergedGrades = merge(gradesA, gradesB, gradesC);
Grades sortedGrades = sort(mergedGrades, repeat);
flush(sortedGrades);
}
public static Grades initialize(String section) {
//creating empty Grades object
Grades grades = new Grades();
File file = new File(section);
//reading input file
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
//getting line
String line = scanner.nextLine();
//splitting by white space
String fields[] = line.split(" ");
//extracting each fields
String id = fields[0];
double gpa = Double.parseDouble(fields[1]);
String firstName = fields[2];
String lastName;
Grade grade;
if (fields.length == 4) {
//last name exists
lastName = fields[3];
grade = new Grade(id, gpa, firstName, lastName);
} else {
//last name doesn't exist
grade = new Grade(id, gpa, firstName);
}
//adding to the grades
grades.add(grade);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
return grades;
}
public static Grades merge(Grades gradesA, Grades gradesB, Grades
gradesC) {
Grades mergedGrades = new Grades();
//merging all grades together
mergedGrades.add(gradesA);
mergedGrades.add(gradesB);
mergedGrades.add(gradesC);
return mergedGrades;
}
public static Grades sort(Grades mergedGrades, Grades repeat) {
// COMPLETED THE METHOD USING THE FOLLOWING LOGIC
// 1: Create a an empty Grades list.
Grades sortedGrades = new Grades();
// 2: Loop through the mergedGrades list and find student with the
// highest grade.
// 3: Add this to the sortedGrades list ( use Grades::add Method )
// 4: Remove it from the mergedGrades list ( use Grades::remove Method)
// 5: Do this till mergedGrades list becomes empty
// In case of collision (two students with the same grade ) this is how
// you will resolve it
// 1: if the student has repeated the course, then he will be moved to
// the end ( within the GPA block )
// 2: If both students have repeated, or both have not repeated, but
// their GPA is the same then sort them by First Name
// 3: If the first name is same, then sort by last name
while (mergedGrades.size() > 0) {
Grade highest = null;// a reference to the current highest grade in
// the mergedGrades
int index = -1; // index of the highest grade
// looping through all grades
for (int i = 0; i < mergedGrades.size(); i++) {
// getting the current grade
Grade g = mergedGrades.get(i);
if (highest == null) {
// first entry
highest = g;
index = i;
} else if (g.isSameGPA(highest)) {
// g and heighest have same gpa
// checking if both students are repeated or none are
// repeated
if ((repeat.contains(g) && repeat.contains(highest))
|| (!repeat.contains(g) && !repeat
.contains(highest))) {
// comparing by first name
int compare = g.getFirstName().compareTo(
highest.getFirstName());
if (compare < 0) {
highest = g;
index = i;
} else if (compare == 0) {
// first names are equal, comparing by last name
compare = g.getLastName().compareTo(
highest.getLastName());
if (compare < 0) {
highest = g;
index = i;
}
}
} else if (repeat.contains(highest)) {
// student in highest variable is repeated, so pushing
// it down by making current grade the highest
highest = g;
index = i;
}
} else if (highest.isLessGPA(g)) {
//g has more gpa than highest, so making it highest
highest = g;
index = i;
}
}
//adding highest to sorted grades
sortedGrades.add(highest);
//removing from mergedgrades
mergedGrades.remove(index);
}
return sortedGrades;
}
public static void flush(Grades sortedGrades) {
try {
PrintWriter writer = new PrintWriter("outputHW8.txt");
writer.write(sortedGrades.toString()); // Notice how I am calling
// toString on Grades class
// Grades toString calls toString on Grade class that you need to
// implement.
writer.close();
} catch (FileNotFoundException e) {
}
}
}
// Grade.java
public class Grade {
private String id;
private double gpa;
private String firstName;
private String lastName;
// Please implement the following
// 1: Constructor that takes String id, double gpa, String firstName,String
// lastName
public Grade(String id, double gpa, String firstName, String lastName) {
this.id = id;
this.gpa = gpa;
this.firstName = firstName;
this.lastName = lastName;
}
// 2: Constructor that takes String id, double gpa, String firstName.Sets
// Last Name to NLN
public Grade(String id, double gpa, String firstName) {
this.id = id;
this.gpa = gpa;
this.firstName = firstName;
this.lastName = "NLN";
}
// Getters to get GPA, firstName and LastName. You will use it in the sort
// Method in driver class.
public double getGpa() {
return gpa;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
// Override the equal method.
@Override
public boolean equals(Object o) {
if(o instanceof Grade){
Grade g=(Grade) o;
if(this.id.equals(g.id)){
//returning true if two grades have same id
return true;
}
}
return false;
}
// You use it in the sort method in the Driver class to check if a
// particular GradeObject exists in the repeat list
// Override the toString method.
@Override
public String toString() {
return id+": "+firstName+", "+lastName+" "+gpa;
}
// In addition, you will need the below two methods
public boolean isLessGPA(Grade grade) {
if(this.gpa<grade.gpa){
//this gpa is smaller than grade's gpa
return true;
}
return false;
}
public boolean isSameGPA(Grade grade) {
if(this.gpa==grade.gpa){
//this gpa is equal to grade's gpa
return true;
}
return false;
}
}
// Grades.java (no change)
public class Grades {
private int capacity;
private int size;
private Grade[] gradeArray;
public final int OFFSET = 4;
public Grades() {
this(4);
}
public Grades(int capacity) {
this.capacity = capacity;
this.gradeArray = new Grade[capacity];
this.size = 0;
}
public void add(Grade grade) {
if (size < capacity) {
gradeArray[size++] = grade;
} else {
this.capacity += OFFSET;
Grade[] temp = new Grade[this.capacity];
for (int i = 0; i < size; i++) {
temp[i] = gradeArray[i];
}
temp[size++] = grade;
this.gradeArray = temp;
}
}
public void add(Grades grades) {
for (int i = 0; i < grades.size; i++) {
add(grades.get(i));
}
}
public void remove(int index) {
for (int i = index; i < size - 1; i++) {
this.gradeArray[i] = this.gradeArray[i + 1];
}
this.gradeArray[size - 1] = null;
this.size--;
}
public Grade get(int index) {
return gradeArray[index];
}
public int size() {
return this.size;
}
public boolean contains(Grade grade) {
for (int i = 0; i < this.size; i++) {
if (this.gradeArray[i].equals(grade)) {
return true;
}
}
return false;
}
@Override
public String toString() {
String data = "";
for (int i = 0; i < size; i++) {
data += gradeArray[i].toString();
data += " ";
}
return data;
}
}
/*outputHW8.txt*/
S005: Stacy, Lu 4.0
S004: Aseef, Hernandez 3.9
S006: Aseef, Nilkund 3.9
S002: Jim, NLN 3.9
S003: Misty, Fang 3.9
S009: Steve, Calderon 3.9
S016: Aseef, Simmons 3.9
S010: Raj, Singh 3.8
S018: Hamza, Nilkund 3.5
S012: Kathy, Calderon 3.5
S017: Hifza, Nilkund 3.3
S011: Jason, Kramer 3.3
S001: John, Rodgers 3.3
S019: Chris, Peach 3.2
S013: Roopa, Singh 3.2
S020: Ramona, Luke 2.4
S014: Amid, Naveed 2.4
S015: Faith, Williams 1.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.