Java assignment 08: Examen results

Java Assignments   Green = Easy,  Blue = Normal,  Red = Hard
 A01   A02   A03   A04   A05   A06   A07   A08   By title 

The output of this code is nothing. We pass the variable grade 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

public class MyClass
{
  void getResult(int grade)
  {
    // add your code here!
  }
  public static void main(String args[])
  {
    MyClass mc = new MyClass();
    mc.getResult(14);
  }
}
Author: Sar Maroof

Answer explanation

 

Here below the code of the method is written.

public class MyClass
{
  void getResult(int grade)
  {
    switch(grade)
    {
      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[])
  {
    MyClass mc = new MyClass();
    mc.getResult(10);
    mc.getResult(9);
    mc.getResult(8);
    mc.getResult(7);
    mc.getResult(6);
    mc.getResult(4);
  }
}

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 *

Sarmaroof © 2017