Need help with this. Java please!
Function:
public static int numPins(int num)
{
if(num == 0)
return 0;
// recursively find the number of pins in num - 1
else
return num + numPins(num - 1);
}
Sample Program:
public class Bowling
{
public static int numPins(int num)
{
if(num == 0)
return 0;
// recursively find the number of pins in num - 1
else
return num + numPins(num - 1);
}
public static void main(String[] args)
{
int i;
for( i = 0 ; i <= 10 ; i++ )
System.out.println("No of pins for num = " + i + " : " + numPins(i) + "\n");
}
}
Sample Output:
Need help with this. Java please! Write a recursive method that counts the number of bowling...
Write a recursive method that counts the number of bowling pins given the number of pins in the back row: 1. public static int numPins (int num) numPins(0)-> 0 numPins(1) → 1 numPins(2)-> 3
5. Given the following class definition, write Java code that implements the addFirst method public class MyLinkedList<E> { private Node<E> head, tail; private int size 0; // Number of elements in the list /** Add an element to the beginning of the list */ public void addFirst (E e) { private static class Node<E> { E element; Node<E> next; public Node (E element) { = element; this.element
in java
Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that returns an ArrayList of all permutations of the the parameter array. The ArrayList may contain the ar- rays in any order. Example: Suppose array = {4, 7, 1, 2}. Then the ArrayList would contain the following arrays, but in any order: 4 7 12 7 4 1 2 2 1 4 7 1 24 7 4 7 2 1 7 4...
+ – for n > 1, subject to Problem 5 (6 pts): Solve the recursive equation T(n the initial condition T(1) = 0.
I need help with these java codingbat questions please:
Create a method that creates a user id, where the user id is made up of the first letter of the first name and the entire last name. e.g. john smith ->jsmith getUserld("bill", "gates")-"bgates" getUserId("steve", "jobs")"sjobs" getUserId("larry", "page") -"lpage" Go Save, Compile, Run (ctrl-enter) public String getUserId( String firstName, String lastName) Given a number n, create an array with elements starting from 1 to n. e.g. 5 > getNumArray(1) [1] getNumArray...
How to recursive solve a fractional Knapsack problem in Java
with log(n) time.
Note!! And return a boolean array of each being take(T) or not
take(F)
this output shoud be: take=[0,1,1]
static double[]
fractionalKnap(double[] values,
int[] weights, int k) {
double[] take = new
double[values.length];
return take;
}
0-1 Knapsack Problem Weight 10; Value 60; Weight 20; Value 100; Weight = 30; Value = 120; Weight = (20+10): Value = (100+60); Weight = (30+10): Value = (120+60); Weight =...
on 2 Consider the following recursive method test: public static int test(String s, int last) if (last < 0) { return 0; if (s.charAt(last) == 0) { return 2 - test(s, last - 1); return 1 + 2 test(s, last - 1); What is the output of: test("11001", 4). TTT Arial 3 (12pt) - TEE 25
Java
Be able to write or understand simple recursive code, including code that uses divide and conquer static int calculate(int v)l System.out.println("calculate "+v); if( v<-1) return 2; return 7*calculate(v/2); What will be the output of the statement calculate(6)?
3) Consider the following recursive method, what will be the output for the following method calls? Explain. (25 pts.) public static void sample (int number) if (number < 0) { System.out.println( superwriteVertical (-number); System.out.println(number); superwriteVertical (number/ 10); else if (number 10) else ( System.out.print1n(number % 18); sample(-100); sample(50); sample(1024);
What is the time and space complexity of this code with
explanation?
public static int getv(int num, List<Integer> rating, int target) { int count 0; HashMap<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < num; i++){ if(!map.containsKey(rating.get(i))){ map.put(rating.-get(i), 0); } map.put(rating.get(i), map.get(rating.get(i))+1); } for(int i = 0; i < num; i++){ if(map.get(target -rating.get(i)) != null){ count += map.get(target-rating.get(i)); } if(target-rating.get(i) == rating.get(i)){ count--; } return count/2; }