0% found this document useful (0 votes)
4 views1 page

Javanotes5 152

The document describes a subroutine called nextN that computes the next term in a sequence based on whether the current term is odd or even. It also includes a print3NSequence function that utilizes nextN to print the 3N+1 sequence starting from a given value, along with a count of the terms in the sequence. Additionally, it provides an example of a letterGrade function that returns a letter grade based on a numerical grade input.

Uploaded by

dsstudent05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Javanotes5 152

The document describes a subroutine called nextN that computes the next term in a sequence based on whether the current term is odd or even. It also includes a print3NSequence function that utilizes nextN to print the 3N+1 sequence starting from a given value, along with a count of the terms in the sequence. Additionally, it provides an example of a letterGrade function that returns a letter grade based on a numerical grade input.

Uploaded by

dsstudent05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

136 CHAPTER 4.

SUBROUTINES

static int nextN(int currentN) {


int answer; // answer will be the value returned
if (currentN % 2 == 1) // test if current N is odd
answer = 3*currentN+1; // if so, this is the answer
else
answer = currentN / 2; // if not, this is the answer
return answer; // (Don’t forget to return the answer!)
}
Here is a subroutine that uses this nextN function. In this case, the improvement from the
version of this subroutine in Section 4.3 is not great, but if nextN() were a long function that
performed a complex computation, then it would make a lot of sense to hide that complexity
inside a function:
static void print3NSequence(int startingValue) {
int N; // One of the terms in the sequence.
int count; // The number of terms found.
N = startingValue; // Start the sequence with startingValue.
count = 1;
TextIO.putln("The 3N+1 sequence starting from " + N);
TextIO.putln();
TextIO.putln(N); // print initial term of sequence
while (N > 1) {
N = nextN( N ); // Compute next term, using the function nextN.
count++; // Count this term.
TextIO.putln(N); // Print this term.
}
TextIO.putln();
TextIO.putln("There were " + count + " terms in the sequence.");
}

∗ ∗ ∗
Here are a few more examples of functions. The first one computes a letter grade corre-
sponding to a given numerical grade, on a typical grading scale:
/**
* Returns the letter grade corresponding to the numerical
* grade that is passed to this function as a parameter.
*/
static char letterGrade(int numGrade) {
if (numGrade >= 90)
return ’A’; // 90 or above gets an A
else if (numGrade >= 80)
return ’B’; // 80 to 89 gets a B
else if (numGrade >= 65)
return ’C’; // 65 to 79 gets a C
else if (numGrade >= 50)
return ’D’; // 50 to 64 gets a D
else

You might also like