Question: package homework; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class HW5 { public static void main (String[]args) throws FileNotFoundException { // change the
package homework;
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class HW5 { public static void main (String[]args) throws FileNotFoundException {
// change the name and path of the files accordingly Scanner sc = new Scanner (new File ("testCase.txt")); PrintWriter myout = new PrintWriter (new File ("Output.txt"));
//Variable declaration int counter = 0, validCounter = 0, invalidCounter = 0; int score1, score2, score3;
while (sc.hasNext ()) { //looping file content counter++; String[] str = sc.nextLine ().split (","); //reading file and converting it to array score1 = Integer.parseInt (str[0]); score2 = Integer.parseInt (str[1]); score3 = Integer.parseInt (str[2]);
//using the described methods if (validGroup (score1, score2, score3, myout)) { myout.println ("The group is valid"); validCounter++;
onegamescore (score1, myout); onegamescore (score2, myout); onegamescore (score3, myout);
myout.println ("Average score of the group is " + avg3scores (score1, score2, score3));
} else invalidCounter++;
myout.println (); myout.println (); myout.println (); }
sc.close ();
myout.println ("Total Group Counter: " + counter); myout.println ("Total Valid Group Counter: " + validCounter); myout.println ("Total Invalid Group Counter: " + invalidCounter);
myout.flush (); myout.close ();
}
//checking for valid group public static boolean validGroup (int score1, int score2, int score3, PrintWriter myout) { boolean check = true;
int[] ar = { score1, score2, score3 }; //using array for storing score
for (int i = 0; i < ar.length; i++) { //looping the process if (ar[i] < 0 || ar[i] > 300) { myout.println ("This group is invalid"); check = false; if (ar[i] < 0) myout.println (ar[i] + " is negeative"); else myout.println (ar[i] + " is over 300"); } } return check; }
//getting player rating public static void onegamescore (int score, PrintWriter myout) { String rating = ""; if (score >= 250 && score <= 300) rating = "professional game"; else if (score >= 200 && score <= 249) rating = "excellent game"; else if (score >= 140 && score <= 199) rating = "very good game"; else if (score >= 100 && score <= 139) rating = "good game"; else if (score >= 50 && score <= 99) rating = "poor game"; else rating = "horrible game";
myout.println (score + " is a " + rating); }
//finding average of the scores public static int avg3scores (int score1, int score2, int score3) { return (score1 + score2 + score3) / 3; } }
please us the following
Local Variables in Methods
Defined Value-Returning Method:
Return Values of Data Type boolean
File I/O within a Method
Object Reference Parameters with Return
Value Parameters
Multiplication Table
void Methods
Using Multiple return Statements:
Method max3:
Value-Returning Method:
Data Type boolean
Methods (Member Functions)
Location of Methods
Parameterless Methods
Object Reference Parameters
please dont use .hasnext
please dont use .length
1. The Java source code file (e.g., HW5.java) 2. The test cases input file (e.g., testCases.txt) 3. The program generated output file (e.g., output.txt)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
