J
judith
Hello anyone that can help. This program is suppposed to be written
with a recursive method and i'm having problems understanding how to do
this. These are the instructions
There are n people in the room , where n is an integer greater than or
equal to 1. Each person shakes hands once with every other person .
what is the total number h(n) of handshakes? Write a recursive function
to solve this problem. To get you started, if there are only one or two
people in the room, then.
handshake(1) = 0
handshake(2) = 1
handshake(3) = 3
handshake(4) = 6
handhshake(5) = ?
handshake (6) = 15
if a third person enters the room she must shake hands with each of the
two people already there. This is two handshakes in addition to the
number of handshakes that would be made in a room of two people, or a
total of three handshakes.
If a fourth person enters the room she must shake hands with each of
the three people already present. This is three handshakes in addition
to the number of handshakes that would be made in a room of three
people, or six handshakes.
I don't know how to calculate the number of handshakes per num_people.
This is the program that our instructor wrote and asked us to fill in
the code . I have made comments where to fill in the code with what i
wrote. I can't get the program to compile and i don't know if i wrote
it right there's not much information in the book on this. If anyone can help or make suggestions i would certainly be appreciative. I'm really not sure what i'm doing. Judith Heres the program
// Program Name: program4JS.java
import java.util.*;
public class program4JS
{
public static int handshakes(int numpeople)
{
return numpeople;//fill in code
}
public static void writeup(int num_people) //fill in code
{ //fill in code
if(num_people >=1) //fill in code
writeup(num_people*(num_people - 1)/2); //fill in code
}
//fill in code
public static void main(String[]args)
{
int num_people = 0;
int num_handshakes = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many people are in the room?");//fill in code
num_people=keyboard.nextInt();
//fill in code
num_handshakes = handshakes(num_people);
System.out.println("If everyone shakes hands once, there will be " +
num_handshakes + " handshakes.");
}
}
Here is what it's doing instead of what it should be doing i'm not sure
how to write it
C:\>java program4JS
How many people are in the room?
1
If everyone shakes hands once, there will be 1 handshakes. //should be
0
C:\>java program4JS
How many people are in the room?
2
If everyone shakes hands once, there will be 2 handshakes. //should be
1
C:\>java program4JS
How many people are in the room?
3
If everyone shakes hands once, there will be 3 handshakes. // should be
3
C:\>java program4JS
How many people are in the room?
4
If everyone shakes hands once, there will be 4 handshakes. //should be
6
C:\>java program4JS
How many people are in the room?
6
If everyone shakes hands once, there will be 6 handshakes. //should be
15
C:\>
Last edited by a moderator: