Write a Java method in the LinkedList class with the following header: public in
ID: 3935458 • Letter: W
Question
Write a Java method in the LinkedList class with the following header: public int[] toArray() The method should return a newly-allocated array of int values, containing all of the elements in the linked list in the same order as they are stored in the list. The array t should have the same size as the list. Write a recursive Java method with the following header: public int recMinimum Cint() values, int start, int end) The method should return the minimum value stored in the array between "start" and "end", including both endpoints. For example, recMinimum([3, -1, 5, 2, -5], 0, 3) should return -1. You may assume that (OExplanation / Answer
4) ans:-
import java.util.*;
public class LinkedListDemo {
public static void main(String[] args) {
// create a LinkedList
LinkedList list = new LinkedList();
// add some elements
list.add("Hello");
list.add(2);
list.add("Chocolate");
list.add("10");
// print the list
System.out.println("LinkedList:" + list);
// create an array and copy the list to it
Object[] array = list.toArray(new Object[4]);
// print the array
for (int i = 0; i < list.size(); i++) {
System.out.println("Array:" + array[i]);
}
}
}
5) ans:-
public class Minimum {
public Minimum(int[]numbers)
{
recursiveMinimumHelper(numbers);
}
//Pre:the minimum value must be less than the current minimum
//Post:current minimum is set to new minimum
private int recursiveMinimum(int[]numbers, int current, int i)
{
if(i==0)
return current;
else
{
if(numbers[i]<current)
recursiveMinimum(numbers,numbers[i], i);
else
recursiveMinimum(numbers,current,i-1);
return current;
}
}
public int recursiveMinimumHelper(int[]numbers)
{
int num = recursiveMinimum(numbers,numbers[0],numbers.length-1);
return num;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[]numbers ={29,40,99,3,80};
for(int i=0; i<numbers.length; i++)
System.out.print(numbers[i]+" ");
Minimum min = new Minimum(numbers);
int num = min.recursiveMinimumHelper(numbers);
System.out.println(" The smallest number in the array is " +num);
}
}
public class Minimum {
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.