Java assignment 06: Find the sum of integers

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. The method writeSum should write the sum of three integers, namely i, i2 and i3 to the standard output.

Java assignment 6: Find the sum of integers

Level: Easy

import java.util.Scanner;

public class Math
{
  void writeSum(int i, int i2, int i3)
  {
    // add the body of the method here!
  }
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the first nr: ");
    int i = input.nextInt();
    System.out.print("Enter the second nr: ");
    int i2 = input.nextInt();
    System.out.print("Enter the third nr: ");
    int i3 = input.nextInt();
    Math math = new Math();
    math.writeSum(i, i2, i3);
    input.close();
  }
}
/*
In the following example the user enters 33, 21 and 12
Enter the first nr: 33
Enter the second nr: 21
Enter the third nr: 12
The sum is: 66

*/
Author: Sar Maroof

Answer explanation

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

The following program allows the users to enter three integers respectively and it calculates the sum of them.

import java.util.Scanner;

public class Math
{
  void writeSum(int i, int i2, int i3)
  {
    int sum = i + i2 + i3;
    System.out.println("The sum is: " + sum);
  }
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the first nr: ");
    int i = input.nextInt();
    System.out.print("Enter the second nr: ");
    int i2 = input.nextInt();
    System.out.print("Enter the third nr: ");
    int i3 = input.nextInt();
    Math math = new Math();
    math.writeSum(i, i2, i3);
    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 *