How to Submit Please submit your answers to the lab instructor once you have completed. Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS.
1. Implement a method factorial(int n), that takes a number (int) and returns its factorial. Factorial of an integer is the product of all positive integers less than or equal to it. Method needs to be implemented using recursion.
2. Implement a method fibonacci(int n), that takes a number (int) and prints Fibonacci series up to a nth term. The Fibonacci series is a series where the next term is the sum of pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1.
3. In main() method, create an array list of size 5, that contains the following values: 1, 3, 5, 7, 9. For each element in the array list, calculate factorial and Fibonacci series using methods from 1. and 2., and print the results. Note: use ‘for-each’ loop.
Answer:
Explanation:
Both the methods are defined as static inside Main class and then main() method defines an arraylist of 5 integers and uses a for each loop to call both the methods for each integer and print the output.
Code:
import java.util.ArrayList;
public class Main
{
public static int factorial(int n)
{
if(n==1 || n==0)
return 1;
return n*factorial(n-1);
}
public static void fibonacci(int n)
{
int a = 0;
int b = 1;
if(n==1)
System.out.print(a);
else
System.out.print(a+" "+b+" ");
int c;
for(int i=2; i<n; i++)
{
c = a+b;
a = b;
b = c;
System.out.print(c+" ");
}
}
public static void main(String[] args) {
ArrayList<Integer> arr = new
ArrayList<Integer>();
arr.add(1);
arr.add(3);
arr.add(5);
arr.add(7);
arr.add(9);
for(Integer n: arr)
{
System.out.println(factorial(n));
fibonacci(n);
System.out.println();
}
}
}
Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
How to Submit Please submit your answers to the lab instructor once you have completed. Failure...
Need some help with this java problem, thanks!
Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. Write a program that prints out the lists alter the following methods are called. Method takes an of integers as a parameter. The method moves the minimum value in the list to the front, otherwise preserving the order of the elements. For example, if a variable called list stores the following values: [4. 7, 9, 2. 7, 7. 5....
Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...
In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....
please be thorough with
explanations. thank you
Question 2 Consider the implementation we made in class for ArrayList, and its extensions you did in the lab. In this question, we will add two more methods to this class: the insert method and the pop method. For this question, please submit the modified ArrayList class. a) Implement the method insert (self, index, val) that inserts val before index (shifting the elements to make room for val). For example, your implementation should...
PYTHON PLEASE!!
Thank you in advance!
11.56 Lab Exam 3 - factors This section has been set as optional by your instructor Function Name: factors Parameters: int Returns: list of int Description: Write a function, factors, that takes an integer n, and returns a list of values that are the positive divisors of n. Note: 0 is not a divisor of any integer, 1 divides every number, and n divides itself Sample Inputs/Outputs: factors (49) Example Call: Expected Return: [7]...
Name: Flame Test Lab Prelab questions: Your instructor may ask you to answer these in your lab notebook in a "prelab" section. online or they may ask you to write your answers in your lab manual and submit them before starting your experiment. Follow your instructor's directions 1. Determine the frequency of photon of light that gives off 2.85 x 10-19 Joules. (Show all your work) so. The color would be orange/ red. 5hv (2.85 x/ 0J) - 16.626810**/s) v...
Java Programming
Lab 07 coding
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab07;
/**
*
* @author ckandjimi
*/
public class Lab07 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int outcome = methodX(5);
System.out.printf("Result from Method X= %d",outcome);
String [] studentNames =
{"Kingston","John","Nangula","Daniela"};
int Test01[]...
kindly Explain, briefly IN PLAIN ENGLISH, how this exercise was completed and the algorithm you used /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ // package u10a1_ooconsoleregisterforcourse; import java.util.Scanner; /** * * @author omora */ public class U10A1_OOConsoleRegisterForCourse { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO...
This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification. As you should know by now, bucket sort works by placing items into buckets and each bucket...
I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2 - Bank Account of the assigned lab in its entirety. Please note that the lab below is somewhat lengthy, and I do not expect you to answer each question! If coding the full lab is too much to ask, please focus soley on Part 2 of the lab, titled Bank Account and the subsections that...