Return statements to be completed with no infinite loops and
using recursion and recursive structures please.
A Block represents one number in the Collatz chain. Every Block
contains a `rank`, indicating the position in the Chain and 'id'
indicating the value. Given that every Block is formatted as `(id,
rank)`, the chain for **6** will look like: `(6, 1) -> (3, 2)
-> (10, 3) -> (5, 4) -> (16, 5) -> (8, 6) -> (4, 7)
-> (2, 8) -> (1, 9)`
A faster Collatz chain has odd numbers only (3n+1/2
directly, as 3n+1 is always even)
public class Block {
public int id; // Number
public int rank; // position in the Chain
public Block next;
// attributes to be added
public int size;
public Block(int id, int rank) {
this.id = id;
this.rank = rank;
this.next = null;
// attributes to be added
}
public Block(int id, int rank, Block next)
{
this.id = id;
this.rank = rank;
this.next = next;
// attributes to be added
}
// Additional constructors may be added
// DO NOT MODIFY
public String toString() {
String str = "(" + id + ", " + rank
+ ")";
if (this.next == null) {
return
str;
}
return str + " -> " +
next.toString();
}
/**
*
* @return the number of Blocks in the Chain that has
an even ID.
*/
public int numberEvenBlocks() {
return -1;
}
/**
*
* @return the number of Blocks in the Chain that has
an odd ID.
*/
public int numberOddBlocks() {
return -1;
}
/**
*
* @return true if the chain starting at the current
node is a valid Collatz
* chain and false otherwise. For a chain to be valid,
it needs to
* follow the 3n+1 rule but not necessarily have a
valid rank. The fast
* collatz chain should be treated as invalid.
*/
public boolean isValid() {
return false;
}
/**
* IMPORTANT: Even thought this method is in the Block
class, it may be harder
* than most in the Chain class.
*
* @return true if the chain starting at the current
node is either a valid
* Collatz chain or a fast Collatz chain. In this
question, you also
* need to ensure that the rank is valid. A valid rank
starts at 1 and
* increases with one.
*/
public boolean isValidAdvanced() {
return false;
}
}
Return statements to be completed with no infinite loops and using recursion and recursive structures please. A Block re
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am