Question: 1) a) Use the given Recur.java to write a helper method public boolean hasLessThan which receives an integer array x and an integer n ,
1)
a)
Use the given Recur.java to write a helper method public boolean hasLessThan which receives an integer array x and an integer n, it then calls a private recursive method private boolean hasLessThan which checks whether the array x contains an integer less than n.
//Recur.java//
import java.util.Scanner;
public class Recur{ public static void main(String args []){ Scanner stdin = new Scanner(System.in); System.out.println("Enter array size: "); int size = stdin.nextInt(); int[] array = new int[size]; System.out.println("Enter " + size + " array elements: "); for(int k = 0; k < size; k++){ array[k] = stdin.nextInt(); } System.out.print("The array is: "); for(int k = 0; k < size; k++) System.out.print(array[k] + " "); System.out.print(" Enter the integer to test: "); int value = stdin.nextInt(); if(hasLessThan(array, value)) System.out.println("THE ARRAY HAS AN ELEMENT LESS THAN " + value); else System.out.println("THE ARRAY HAS NO ELEMENT LESS THAN " + value); } public static boolean hasLessThan(int[] x, int value){ // call to the auxillary method to be implemented by students
} // auxillary method to be implemented by students private static boolean hasLessThanAux(int[] x, int value, int index){
} }
b)
i)Write a helper method public Double sum(Double e) of SLL class that calls a private recursive method which returns the sum of all elements in the list that are greater than e. Assume that the list will only contain Double elements.
(ii) Modify the given LinkedListDriver.java to test the method sum.
Sample output:
Array elements:
20.0 30.0 10.0 15.0
Sum of elements greater than 10.0 is 65.0
///LinkedListDriver///
public class LinkedListDriver { public static void main(String[] args) { SLL myList = new SLL(); double[] array = {20, 30, 10, 15}; for(int i = 0; i < array.length; i++) myList.addToTail(array[i]); System.out.println("Array elements: "); myList.printAll(); double value = 10.0; System.out.println(" Sum of elements greater than " + value + " is " + myList.sum(10.0)); } }
///SLL.java///
public class SLL { protected SLLNode head, tail; public SLL() { head = tail = null; } public boolean isEmpty() { return head == null; } public void addToHead(T el) { head = new SLLNode(el,head); if (tail == null) tail = head; } public void addToTail(T el) { if (!isEmpty()) { tail.next = new SLLNode(el); tail = tail.next; } else head = tail = new SLLNode(el); } public T deleteFromHead() { // delete the head and return its info; if (isEmpty()) return null; T el = head.info; if (head == tail) // if only one node on the list; head = tail = null; else head = head.next; return el; } public T deleteFromTail() { // delete the tail and return its info; if (isEmpty()) return null; T el = tail.info; if (head == tail) // if only one node in the list; head = tail = null; else { // if more than one node in the list, SLLNode tmp; // find the predecessor of tail; for (tmp = head; tmp.next != tail; tmp = tmp.next); tail = tmp; // the predecessor of tail becomes tail; tail.next = null; } return el; } public void delete(T el) { // delete the node with an element el; if (!isEmpty()) if (head == tail && el.equals(head.info)) // if only one head = tail = null; // node on the list; else if (el.equals(head.info)) // if more than one node on the list; head = head.next; // and el is in the head node; else { // if more than one node in the list SLLNode pred, tmp;// and el is in a nonhead node; for (pred = head, tmp = head.next; tmp != null && !tmp.info.equals(el); pred = pred.next, tmp = tmp.next); if (tmp != null) { // if el was found; pred.next = tmp.next; if (tmp == tail) // if el is in the last node; tail = pred; } } } public void printAll() { for (SLLNode tmp = head; tmp != null; tmp = tmp.next) System.out.print(tmp.info + " "); } public boolean isInList(T el) { SLLNode tmp; for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next); return tmp != null; } public Double sum(Double e) { //to be implemented by students } private Double sum(SLLNode node, Double e){ //to be implemented by students } } ///SLLNode.java///
public class SLLNode { public T info; public SLLNode next; public SLLNode() { this(null,null); } public SLLNode(T el) { this(el,null); } public SLLNode(T el, SLLNode ptr) { info = el; next = ptr; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
