Question: JAVA PROGRAM 1: A perfect number is a positive integer that is equal to th e sum of its positive divisors, including 1 but excluding

JAVA PROGRAM 1:

A perfect number is a positive integer that is equal to th e sum of its positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number exactly without remainder. For example, When you consider 6, Divisors of 6 are 1, 2 and 3. Then the total of divisors=1+2+3=6. Hence 6 is a perfect number. Write a Java program that takes an integer from the user(say x) and displays whether it is a perfect number or not. The user interface/output of your program may look like as follows (Values in green are user input):

JAVA PROGRAM 1: A perfect number is a positive integer that is

Note: Steps in the main method ofthis program can be given as follows.

1) Take the user input to an integer variable (Say x).

2) Declare an integer variable (Say divisorSum) to store the sum of divisors. Initialize it to zero.

3)Use for loop as,

for(int i=1; i

{

/*

a)if i is a divisor of x, then update divisorSum such that divisorSum equals to the sum of divisorSum in the previous iteration and i.

*/

}

4)If divisorSum equals to x,

Display x is a perfect number

Otherwise,

Display x is not a perfect number

JAVA PROGRAM 2:

Scissors-Rock-Paper (SRP) Game:

When we were children, many of us played this game in which two players simultaneously display a hand in one of three configurations. A flat hand represents paper, a fist represents a rock, and an extended index and middle finger stand for a pair of scissors. The outcome of the game is determined by the following rule:

Scissors cut PaperScissors wins

Paper wraps RockPaper wins

Rock breaks ScissorsRock wins

In this question, you need to write a Java programto simulate SRP game.

Part(A)

Include following methods to your program.

?- findRandom-This method takes in no input parameter and returns a random integer between -3. Statement to generate random numbers between a-b:

int x = (int) (Math.random()*(b-a+1)) + a

Here, a ? x ? b. (a, b are inclusive to the possible value range)

- numbersToSign- This method takes in an integer parameter which can be any number from 1 to 3 and returns a String corresponding to the number. The String for each numberis as follows. (Assume that the input is always a 1-3 integer.)

equal to th e sum of its positive divisors, including 1 but

Part(B):

Within the main method do the following:

1)Declare two integer variables (Say r1 and r2). Initialize them to zero.

2)Declare two string variables (Say s1 and s2). Initialize them to empty string.

3)Use do-while loop as

do{

/*

a)Assign new values to r1 by calling the findRandom method.

b)Assign new values to r2 by calling the findRandom method.

c)Assign new value to s1 by calling numbersToSign method by passing r1.

d)Assign new value to s2 by calling numbersToSign method by passing r2.

*/

/*Display the value of s1 and s2 to user as player 1 and player2 hands.*/

System.out.println(Player 1 hand:+ s1);

System.out.println(Player 2 hand:+ s2);

} while (//e)s1 is equal to s2);

Once the program exit from the above loop displays the winner based on following conditions.

excluding itself. A divisor of a number is one which divides the

The output of your program may look like as follows . (Note that this program does not take any user input).

number exactly without remainder. For example, When you consider 6, Divisors of

Enter an integer: 6 6 is a perfect number! or Enter an integer:8 B is not a perfect number

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!