http://www.site.uottawa.ca/~turcotte/teaching/iti-1521/src/11/01/Iterator.java h
ID: 3810173 • Letter: H
Question
http://www.site.uottawa.ca/~turcotte/teaching/iti-1521/src/11/01/Iterator.java
http://www.site.uottawa.ca/~turcotte/teaching/iti-1521/src/11/01/BitList.java
http://www.site.uottawa.ca/~turcotte/teaching/iti-1521/src/11/01/BitListTest.java
The class BitList has an inner class. This is a second private class definition in the same folder as the public class BitList.This inner classis BitListlterator. BitListlterator implements the interface Iterator forcing itto implement the three methods: hasNext,next, and add. Take the time to understand each method andtheir implementations. lterato BitList java BitList Because most operations on bits work from right to left, your implementation must store the bits in the right-to-left" order e with the rightmost bit in the first node of the list. For example, the bits 11010 must be stored in a list suchthat the bit0is the first, followed by 1.followed by 0, followed by, 1,followed by 1: 1. Complete the implementation of the class BitList. I public BitList( Strings This constructor has to areate alistrepresenting the chain of0s and 1sgiven as parameter The given string, s, must be a string of 0s and 1s,otherwise the constructor must throw an exception oftype IllegalArgumentException. This constructor initializes the new BitList instance to represent the value in the string. Each characterin the string represents one bit of the list, with the ightmost character in the string being the loworder bit. For example, given the string "1010111 the constructor should initialize the new Bi st toinclude this list of bits: If given the empty string,the const ctor should create an empty list ull is a not avalid argument. The constructor must not remove the leading zeroes. For example, given "0001"the constructor must create a list of size 4: Use the class BitListTest to test your implementation. BitList Test.javaExplanation / Answer
Made changes only to BitList.java . Please see output below
PROGRAM CODE:
BitList.java
package util;
//-*- Mode: Java -*-
//BitList.java --- data structure to store a sequence of bits (ints)
//Author : Marcel Turcotte
//Created On : Fri Apr 9 09:00:27 2004
//Last Modified By: Marcel Turcotte
//Last Modified On: Fri Mar 24 10:00:36 2006
//Unlike the list of the current assignment, this one 1) does not
//detect concurrent modifications and 2) does not start with a dummmy
//node.
import java.util.NoSuchElementException;
//Stores the bits in reverse order!
public class BitList {
// useful constants
public static final int ZERO = 0;
public static final int>
// instance variables
private Node first;
// constructors
public BitList() {
first = null;
}
public BitList( String s ) {
for(int i=0; i<s.length(); i++)
{
int bit = Integer.valueOf(String.valueOf(s.charAt(i)));
if(first == null)
first = new Node(bit, null);
else
addFirst(bit);
}
}
public void addFirst( int bit ) {
if ( ( bit != ZERO ) && ( bit != ONE ) ) {
throw new IllegalArgumentException( Integer.toString( bit ) );
}
first = new Node( bit, first );
}
public int removeFirst() {
if ( first == null ) {
throw new NoSuchElementException();
}
int saved = first.bit;
first = first.next;
return saved;
}
public Iterator iterator() {
return new BitListIterator();
}
public String toString() {
String str = "";
if ( first == null ) {
str += ZERO;
} else {
Node p = first;
while ( p!=null ) {
str = p.bit + str; // reverses the order!
p = p.next;
}
}
return str;
}
// The implementation of the nodes (static nested class)
private static class Node {
private int bit; // <- NEW
private Node next;
private Node( int bit, Node next ) { // <- ACCORDINGLY ...
this.bit = bit;
this.next = next;
}
}
// The implementation of the iterators (inner class)
private class BitListIterator implements Iterator {
private Node current = null;
private BitListIterator() {
current = null;
}
public boolean hasNext() {
return ( ( current == null && first != null ) ||
( current != null && current.next != null ) );
}
public int next() {
if ( current == null ) {
current = first ;
} else {
current = current.next ; // move the cursor forward
}
if ( current == null ) {
throw new NoSuchElementException() ;
}
return current.bit ;
}
public void add( int bit ) {
if ( ( bit != ZERO ) && ( bit != ONE ) ) {
throw new IllegalArgumentException( Integer.toString( bit ) );
}
Node newNode;
if ( current == null ) {
first = new Node( bit, first );
current = first;
} else {
current.next = new Node( bit, current.next );
current = current.next;
}
}
}
}
OUTPUT:
a is 1010111
b is 1010111
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.