Introduction
In this document, we will provide answers to Blue Pelican Java Lesson 19 Exercises. It is intended to help developers and technical professionals get the answers they need to Blue Pelican Java Lessons.
This document includes a step-by-step guide to solving each exercise in Blue Pelican Java Lesson 19 and offers some tips for problem-solving. It also contains a Frequently Asked Questions (FAQ) section to resolve any other relevant queries. All headlines and sub-headlines have SEO-friendly headline tags and links to related resources with anchor texts.
Let’s get started.
Answers to Blue Pelican Java Lesson 19 Exercises
Exercise 1
Complete the following code segment.
public static int kittens(int x){
if (x < 1){
return 0;
}
else {
return 2 + kittens(x-1);
}
}
Answer: The complete code segment is as follows:
public static int kittens(int x){
if (x < 1){
return 0;
}
else {
return 2 + kittens(x-1);
}
}
This code segment will perform a recursive function that will return the number of kittens as per the given input. It takes the value of x and passes it to the recursive function if the value of x is greater than 1. The recursive function keeps subtracting the value of x by 1 and adding 2 to the total until the value of x becomes less than 1.
Exercise 2
Write a code segment in Java to find the sum of all values in an array.
Answer:
public static int sumArray(int[] arr)
{
int sum = 0;
for (int i=0; i<arr.length; i++)
sum = sum + arr[i];
return sum;
}
This code segment will find the sum of all values in the given array. It takes the array as a parameter and then iterates over each element of the array and adds their values to the sum. Once the iteration has been completed, it will return the sum of the array.
Exercise 3
Write a code segment in Java to find the sum of all even numbers between 1 and 10.
Answer:
public static int sumEven(int num)
{
int sum = 0;
for (int i=2; i<=num; i=i+2)
sum = sum + i;
return sum;
}
This code segment will find the sum of all even numbers between 1 and 10. It takes the number as a parameter and then iterates over all even numbers between 1 and the number and adds their values to the sum. Once the iteration has been completed, it will return the sum of the even numbers.
Frequently Asked Questions
What is the purpose of the code segment in Exercise 1?
The purpose of the code segment in Exercise 1 is to perform a recursive function which will return the number of kittens as per the given input.
What is the purpose of the code segment in Exercise 2?
The purpose of the code segment in Exercise 2 is to find the sum of all values in the given array.
What is the purpose of the code segment in Exercise 3?
The purpose of the code segment in Exercise 3 is to find the sum of all even numbers between 1 and 10.
How does the recursive function work?
The recursive function takes the value of x as input and passes it to the recursive function if the value of x is greater than 1. The recursive function keeps subtracting the value of x by 1 and adding 2 to the sum until the value of x becomes less than 1.
Related Links
- Java Tutorials: https://www.tutorialspoint.com/java/
- Java Documentation: https://docs.oracle.com/javase/8/docs/
- Java Stack Overflow: https://stackoverflow.com/questions/tagged/java
- Java Learning Resources: https://www.edx.org/learn/java