CS 1301 Lab 6

100 Points

Attendance is worth 10 points.

(45 points) Team Problem 1:
For this program you need to submit the source code (.java), including prolog.

Write a program that will ask the user for the value of an integer n, and compute the sum 1 + 2 + 3 + 4 + ... + n. The requirement for this lab is that you write a recursive function that returns the result of the sum (integer) and takes one argument, n, of type integer. You will then call the function and print out its results as follows:

 
int mysum = recursive_addition(n);
System.out.println("The sum 1+2+...+n is: "+ mysum);

(45 points) Individual Problem 1:
For this program you need to submit the source code (.java), including prolog and comments.

Given the following main function:

 
   
        public static void main(String[] args)
        {
                Scanner s = new Scanner(System.in);
               System.out.print("Please enter a word: ");
               String text = s.nextLine();
               System.out.print("The word with the letters in reverse order is: ");
               reverse(text, text.length()-1);
        }

write a recursive function reverse, whose return type is void and has two parameters: first parameter is a String and the second parameter is an integer. The function prints the sentence with the order of letters reversed. Below are some sample runs:

 
Sample run 1:
Please enter a sentence: abcdef
The sentence with the letters in reverse order is: fedcba
 
Sample run 2:
Please enter a sentence: A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!
The sentence with the letters in reverse order is: !amanaP-lanac a ,tah a ,may a ,kay a ,mah a ,tac a ,nalp a ,nam A
 
Sample run 3:
Please enter a sentence: racecar
The sentence with the letters in reverse order is: racecar
 
Sample run 4:
Please enter a sentence: madam
The sentence with the letters in reverse order is: madam
 

Sentences (or words) that read the same in reverse order are called palindromes .

You are not allowed to use loops for this assignment

To successfully complete this lab you should: