Hello to all! Please answer the questions 1-5 below using JAVA!!! Thank you! Let
ID: 3673960 • Letter: H
Question
Hello to all! Please answer the questions 1-5 below using JAVA!!! Thank you!
Let Point be a simple class like Point(), Point(int x, int y), getX()/getY(), setX(int x), setY(int y)
1- Let reverse be a void method that takes as a parameter an array arr of object references and it reverses it. e.g. called with an array containing Points (1, 1), (2, 2) and (3, 3), modifies it to contain Points (3, 3), (2, 2) and (1, 1) (the original point objects). Let the code below be in the main:
Point[] points = new Point[3];
points[0] = new Point(1, 1);
points[1] = new Point(2, 2);
points[2] = new Point(3, 3);
reverse(points);
Please draw a box and arrow diagram showing the state of all the variables before and after the method call (no code; just the diagram). Any objects should only be drawn once. References in the main should be in black. Any modified references (by the method) should be in red (or some other color). Variables that go out of scope when the method returns should be shown crossed out.
2- Write a method called doubleArray that takes as a parameter an array of Points and returns another array that is double the size and contains the same point objects in the parameter array and has double the capacity. e.g. called with an array containing Points (1, 1), (2, 2) and (3, 3) it returns an array containing (1, 1), (2, 2), (3, 3), -, -, -. Draw a box and arrow diagram of the original array and the returned array. Any objects should only be drawn once.
3- Write a recursive function called printNums that takes as a parameter an int n and prints all the numbers from 0 to n.
e.g.: printNums(5) should print 0 1 2 3 4 5 (in that order).
– Point out the base case and the recursive step.
– How many stack frames deep is this for a given number n?
4- Write a recursive function called sumArr that computes the sum of the elements of the array. It takes as parameters an int array. e.g. sum([1, 2, 1, 4, 1]) should return 9.
5- We are using the binary search algorithm to look for a value in the array below. For each step of the algorithm write the value that is looked at and the values in the array that are left in consideration for the next step. No code to write.
3 4 8 11 17 21 25 30 33 39
a - Look for the value 25. How many steps does it take until we find it?
b – Look for the value 5. How many steps does it take to find out that 5 is not present?
Explanation / Answer
Multiple Questions : Answering one question (Question 3.)
---------------------------------------------------------------
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void printNums (int n){
if(n==0){//base case
System.out.print(n+ " ");
}
else{
int temp = n;
printNums(n-1);
System.out.print(temp+" ");
}
}
public static void main (String[] args) throws java.lang.Exception
{
printNums(5); // your code goes here
}
}
-------------------------
1. base case mentioned as the coment.
2. n+1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.