Java assignment 06: Find the sum of integers

  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. 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();
  }
}





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 *