Computer Science, asked by th3darkstar, 6 months ago

Define a class in java ‘Amicable’ described as below:

Instance Variables/Data Members

· int x - to store first number

· int y - to store second number

Member Methods

- Amicable(…): parameterised constructor to assign values to member variables

- int sumFactor(int num): to calculate and return sum of the factors of num

- void checkAmicable(): to check whether the number is an Amicable Number or not.

[If two numbers are such that the sum of the perfect divisors of one number is equal to the other number and the sum of the perfect divisors of the other number is equal to the first number, then the numbers are called Amicable Numbers. For example: a=220 and b=284

The divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110

The divisors of 284 are 1, 2, 4, 71, 142

Sum of divisors of 220 = 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284, which is equal to the number b = 284

Sum of divisors of 284 = 1 + 2 + 4 + 71 + 142 = 220, which is equal to the number a = 220]

Write a main method to create an object of the class and call the above member methods

Answers

Answered by awadhkishors162
0

Answer:

A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++, and Python.

Methods are time savers and help us to reuse the code without retyping the code.

Method Declaration

In general, method declarations has six components :

Modifier-: Defines access type of the method i.e. from where it can be accessed in your application. In Java, there 4 type of the access specifiers.

public: accessible in all class in your application.

protected: accessible within the class in which it is defined and in its subclass(es)

private: accessible only within the class in which it is defined.

default (declared/defined without using any modifier) : accessible within same class and package within which its class is defined.

The return type : The data type of the value returned by the method or void if does not return a value.

Method Name : the rules for field names apply to method names as well, but the convention is a little different.

Parameter list : Comma separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis. If there are no parameters, you must use empty parentheses ().

Exception list : The exceptions you expect by the method can throw, you can specify these exception(s).

Method body : it is enclosed between braces. The code you need to be executed to perform your intended operation

Explanation:

please add me in the brain list

Similar questions