T
T
Tim Tatman2018-02-01 12:08:38
Java
Tim Tatman, 2018-02-01 12:08:38

How to add a method to a class (Java)?


It is necessary to add a public int sum (int x, int y) method to the class
Returns the sum of the numbers x and y.
here is my example but i cant figure out what i did wrong

public class FL {

    public int sum(int x, int y)
    {
        int z;
        z = x + y;
        return z;
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Goncharenko, 2019-08-30
@sergeygsd

kak-answer.imysite.ru/12308_kak_nayti_summu_dvuh_ch...
// This example is better.
import java.util.Scanner;
public class main_class {
public static void main(String[] args){
int sum = 0, firstNumber, secondNumber;
Scanner inputNumScanner = new Scanner(System.in);
System.out.println("Enter First Number: ");
firstNumber = inputNumScanner.nextInt();
System.out.println("Enter Second Number: ");
secondNumber = inputNumScanner.nextInt();
sum = firstNumber + secondNumber;
System.out.println("The sum of the two numbers you entered = " + sum);
}
}
/*
[email protected]:~/Consolejava/SprinklerSystem$ javac main_class
[email protected]:~/Consolejava/SprinklerSystem$ java main_class
Enter First Number:
4
Enter Second Number:
5
The sum of the two numbers you entered = 9
[email protected]:~/ Consolejava/SprinklerSystem$
*/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question