Java assignment 08: Examen results

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  
Java Assignments   Green = Easy,  Blue = Normal,  Red = Hard
Select    01   02   03   04   05   06   07   08   09   10   By title 

The output of this code is nothing. We pass the variable score to the method. The method should do the following!

  1. If the number 10 is passed to the method, the ouput is “Excellent”.
  2. If the number 9 is passed to the method, the ouput is “Very good”.
  3. If the numbers 8, 7 and 6 are passed to the method, the output is “Passed”.
  4. If any other numbers are passed to the method, the output is “Try again”.

Write the code of the body of the method to achieve that. Pass the numbers 10, 9, 8, 7, 6, 4 to the method and test your method whether it works correctly.

Java assignment 08: Examen results

Level: Normal

import java.util.Scanner;

public class ExamResult
{
  void getResult(int score)
  {
    // add your code here!
  }
  public static void main(String args[])
  {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a score: ");
    int score = input.nextInt();
    ExamResult exr = new ExamResult();
    exr.getResult(score);
    input.close();
  }
}
/*
Below, thhe user inserted the score 8 
Enter a score: 8
passed
*/
Author: Sar Maroof

Answer explanation

 

Here below the code of the method is written.

import java.util.Scanner;

public class ExamResult
{
  void getResult(int score)
  {
    switch(score)
    {
      case 10:
        System.out.println("Excellent");
        break;
      case 9:
        System.out.println("Very good");
        break;
      case 8:
      case 7:
      case 6:
        System.out.println("passed");
        break;
      default:
        System.out.println("Try again");
    }
  }
  public static void main(String args[])
  {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a score: ");
    int score = input.nextInt();
    ExamResult exr = new ExamResult();
    exr.getResult(score);
    input.close();
  }
}





Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *