Stage 2:
public class Question7 { //begin class
//add an item to the end of the list
public Node add(int item, Node head) {
Node temp = new Node(item, null);
if (head == null) {
return temp;
} else {
Node cur = head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = temp;
return head;
}
}
public int size(Node cur) {
int count = 0;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
private Integer get(int idx, Node cur) {
for (int i = 0; i < idx && cur != null; i++) {
cur = cur.next;
}
if (cur == null) {
return null;
}
return cur.data;
}
/**
* 10 marks
* @param low
* @param high
* @param cur
* @return sum of nodes, starting at cur, that
hold a value in the range
* [low, high] (inclusive)
*/
public int sumInRange(int low, int high, Node cur) {
return 0;
}
Stage 3
public class Question8 { //begin class
//add an item to the end of the list
public Node add(int item, Node head) {
Node temp = new Node(item, null);
if (head == null) {
return temp;
} else {
Node cur = head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = temp;
return head;
}
}
public int size(Node cur) {
int count = 0;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
private Integer get(int idx, Node cur) {
for (int i = 0; i < idx && cur != null; i++) {
cur = cur.next;
}
if (cur == null) {
return null;
}
return cur.data;
}
/**
* 10 marks
* @param start
* @return first node, starting at start, that
occurs more than once
*/
public Node firstDuplicate(Node start) {
return null;
}
Stage 4:
public class Question11 { //begin class
/**
*
* @param list: list of items
* @param start: starting index
* @param target: value to construct
* @return list containing **indices** (from
lower to higher) of items that add up to the given target.
* all combinations with a given item must be considered before
rejecting it.
* return false if there is no combination of items adding up to
target
* if list = [30,80,-20,200,10], start = 0, target = 10, return
list [0, 2] as list.get(0) + list.get(2) = 10
* if list = [30,80,-20,200,10], start = 0, target = 50, return
null
*/
public static ArrayList<Integer>
indicesAddingUpto(ArrayList<Integer> list, int start, int
target) {
return null;
}
public class Question10 { //begin class
/**
*
* @param start: starting index
* @param nums: array
* @param target
* @return true if it is possible (and false if
not)
* to choose a group of some of the ints (starting at index
start),
* such that the groups product is the
* given target with these additional constraints:
* all multiples of 5 in the array must be included
* in the group. If the value immediately following
* a multiple of 5 is 1, the 1 must not be chosen.
* (Solution must be recursive AND should not contain any
loop)
*/
public static boolean productRecursive(int start, int[] nums,
int target) {
return false;
}
Stage 5:
class Box {
public int depth, height, width;
/**
* DO NOT MODIFY
*/
public Box() {
depth = 0;
height = 0;
width = 0;
}
/**
*
* @param depth: to be copied into instance
variable depth
* @param height: to be copied into instance
variable height
* @param width: to be copied into instance
variable width
*/
public Box(int depth, int height, int width) {
//to be completed
}
/**
* DO NOT MODIFY
*
* @return volume of the box as defined by the
product of the three sides
*/
public int volume() {
return depth * height * width;
}
/**
*
* @return true if the box is a Cube (all three
sides are equal), false otherwise
*/
public boolean isCube() {
return false;
}
/**
*
* @param other
* @return 1 if calling object has greater
volume than parameter object
* -1 if calling object has lesser
volume than parameter object
* 0 if calling object has the same
volume as parameter object
*/
public int compareTo(Box other) {
return 0;
}
}
Stage 2: public class Question7 { //begin class //add an item to the end of the list public Node add(int item, Node head
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am