# new java programmer with compile error and trouble writting a Recursive Function

Discussion in 'Java' started by judith, Oct 23, 2006.

1. ### judithGuest

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
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. this is the
compile error and the program. I would appreciate it if someone could
help or make any comments Judith

C:\>javac program4JS.java
program4JS.java:19: missing return statement
}
^
1 error

C:\>

// Author: Judith Spurlock
// Course: ITSE 2417
// Program No: 4
// Due Date:
//
// Program Name: program4JS.java

import java.util.*;

public class program4JS
{
public static int handshakes(int numpeople)
{
numpeople = 0;//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 - 1)*num_people); //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.");
}

}

judith, Oct 23, 2006

2. ### Manish PanditGuest

Hi,

First off, you did not put the closing bracket for your code, and hence
the compiler error. Secondly, looking at the code, at first glance, the
function should be n*(n-1)/2 and not n*(n-1) like you have coded. When
A shakes hands with B, B shakes hands with A as well, making the number
half, hence n*(n-1)/2.

-cheers,
Manish

Manish Pandit, Oct 24, 2006

3. ### Manish PanditGuest

Oops! Sorry about the closing bracket part - google hid it under 'show
quoted text'. The error you have could be because you have declared the
method to return int

public static int handshakes(int numpeople)
{
numpeople = 0;//fill in code
}

while it doesnt return one, but I could not match this to line 19.

-cheers,
Manish

Manish Pandit, Oct 24, 2006
4. ### judithGuest

Manish Pandit wrote:
> Oops! Sorry about the closing bracket part - google hid it under 'show
> quoted text'. The error you have could be because you have declared the
> method to return int
>
> public static int handshakes(int numpeople)
> {
> numpeople = 0;//fill in code
> }
>
>
> while it doesnt return one, but I could not match this to line 19.
>
> -cheers,
> Manish

This is the changed program and it won't output what it's supposed to
and i don't know what i'm doing wrong. It outputs the same num_people
that i'm entering instead of for 1 person 0 handshakes , 2 people 1
handshake ect. any suggestions?
// Author: Judith Spurlock
// Course: ITSE 2417
// Program No: 4
// Due Date:
//
// 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.");
}

}

This is the output

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.

C:\>

judith, Oct 24, 2006
5. ### LewGuest

Re: new java programmer with compile error and trouble writting aRecursive Function

judith wrote:
>
> This is the changed program and it won't output what it's supposed to
> and i don't know what i'm doing wrong. It outputs the same num_people
> that i'm entering instead of for 1 person 0 handshakes , 2 people 1
> handshake ect. any suggestions?
> 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.");
> }
>
> }

Pascal's Triangle!

Let's take a look at what "recursive" is, and which method should be "recursive".

"Recursive" means that a function calculates for an input based on the same
function's calculation for an earlier input. In other words, some function
a = f(n)
calculates a result based on { someCalculation op f( n - 1 ); }.

Patricia Shanahan has pointed out the value of "hand execution" (actually,
pen[cil]-and-paper execution) to understand what your program instructions
actually tell the program to do. Have you tried this?

num_handshakes = handshakes(num_people);

What will be in num_handshakes after the method call when num_people is 1? 2?
3? 4?

When is writeup() ever called from main()?

The calculation (num_people*(num_people - 1)/2) is direct, not recursive. In
other words, it directly returns an answer without need to invoke the same
method on the previous value. When a new person enters the room, does his
handshake count really multiply by the previous handshake count? Which
operation is appropriate for "op" in the recursive function?

A recursive function follows the pattern

public int func( int n )
{
if ( n == LOWEST_ALLOWED_VALUE )
{
return START_RESULT;
}
else
{
return someval "op" func( n - 1 );
}
}

where "op" is the appropriate calculation and "someval' is some appropriate
value (or variable).

Perhaps this is too much detail, but I see that you tried to solve the problem
and got sort-of part of the way there, after a fashion. The aim is to help
you actually learn the concepts from your course, not to give away the store.

Recursion is a fundamental concept of maths and computer science. See
http://en.wikipedia.org/wiki/Recursion

- Lew

Lew, Oct 26, 2006