Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method called reportScore that takes as a parameter a Scanner con

ID: 3791848 • Letter: W

Question

Write a static method called reportScore that takes as a parameter a Scanner containing information about a student's performance and that prints a report for that student. Students are given gold stars for tasks they solved particularly well and minuses for tasks where they performed poorly. The information for each student is presented as a series of count/type pairs where each count is a positive integer and each type is either "*" or "-". These count/type pairs are followed by the student's name which is guaranteed to be a single word composed entirely of alphabetic characters. Consider this report:

It indicates that Erica got 3 stars followed by 2 minuses followed by 1 star. The overall score is computed by giving 1 plus point for each star and one minus point for each minus. The method produces exactly one line of output reporting this score and the total number of tasks. The table below includes several examples.

You may not construct any extra data structures to solve this problem.

Explanation / Answer

Please find my implementation.

please let me know in case of any issue.

import java.util.Scanner;

public class Test {

   public static void reportScore(Scanner scanner){

       while(scanner.hasNextLine()){

           String line = scanner.nextLine();

           int star = 0;

           int minus = 0;

           String name = "";

           for(int i=0; i<line.length()-1; i=i+2){

               char c = line.charAt(i);

               char c1 = line.charAt(i+1);

               if(Character.isDigit(c)){ // if current character is digit then next character should be

                                           // * or -

                   if(c1 == '*')

                       star = star + (c-'0');

                   else if(c1 == '-')

                       minus = minus + (c-'0');

               }else{

                   name = line.substring(i);

                   break;

               }

           }

          

           System.out.println(name+" got "+(star-minus)+" of "+(star+minus));

       }

   }

  

   public static void main(String[] args) {

      

       Scanner sc = new Scanner("3*2-1*Erica"); // there should not be any space

      

       reportScore(sc);

   }

}

/*

Sample run:

Erica got 2 of 6

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote