Java assignment 06: Find the sum of integers

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. The method writeSum should write the sum of three integers, namely i, i2, i3 to the standard output.
If your method works correctly, the output of the program will be 23 + 17 + 3 = 43.
Test your method by passing the following set of parameters to it.

  1. mc.writeSum(12, -4, 1);
    The output of the program becomes 9.
  2. mc.writeSum(24, -20, -7);
    The output of the program becomes -3.

Java assignment 6: Find the sum of integers

Level: Easy

public class MyClass
{
  void writeSum(int i, int i2, int i3)
  {
    // add the body of the method here!
  }
  public static void main(String[] args)
  {
    MyClass mc = new MyClass();
    mc.writeSum(23, 17, 3);
  }
}
Author: Sar Maroof

Answer explanation

The body of the method is written in the following code.

public class MyClass
{
  void writeSum(int i, int i2, int i3)
  {
    int sum = i + i2 + i3;
    System.out.println(sum);
  }
  public static void main(String[] args)
  {
    MyClass mc = new MyClass();
    mc.writeSum(23, 17, 3);
    /*
     * we pass the parameter to the 
     * method to check out the result
     */
    mc.writeSum(12, -4, 1);
    mc.writeSum(24, -20, -7);
  }
}

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