Java assignment 08: Examen results

Java Assignments   Green = Easy,  Blue = Normal,  Red = Hard
 01   02   03   04   05   06   07   08   09   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();
  }
}

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 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 *