Help troubleshooting java CharList code. Need to get this class working and outp
ID: 3757825 • Letter: H
Question
Help troubleshooting java CharList code.
Need to get this class working and outputting the correctly. Class takes in a string, then puts the individual chars of that string into an array. I have commented the methods that need work, or I am unsure of.
I'll post my class code and drive code below! Thanks!
Here is what is suppose output:
a is :katnis and has 6 chars
b is :Batman and has 6 chars
c is :Batman and has 6 chars
B and A are equal : false
B and C are equal : true
//////////// Java code
public class CharList {
private char[] storeChars = new char[ 100 ];
private int numChars = 0;
private String input;
public CharList() {
// default no-arg constructor
}
public CharList( String startStr ) { // sets up internals
this.input = startStr;
for ( int i = 0; i < startStr.length(); i++ ) {
char a = startStr.charAt( i );
this.add( a );
}
}
public CharList( CharList other ) { // Copy constructor
if ( other == null ) { // checks null input
throw new IllegalArgumentException( "Null input" );
} else {
//this.input = other.(); NOT SURE HOW THIS GOES
this.numChars = other.size();
// this.storeChars = other.
}
}
public void add( char next ) {
this.storeChars[ numChars ] = next;
this.numChars++;
}
public char get( int index ) {
// returns the char at that particular index
}
public int size() {
return numChars;
}
public boolean equals() {
// checks to see if equal
}
@Override
public String toString() {
// concatenates the chars in the array to form a String
}
}
////// Driver code
public class CharListDriver {
public static void main( String[] args ) {
charListDriver();
}
private static void charListDriver() {
CharList a = new CharList();
CharList b = new CharList( "Batman" );
CharList c = new CharList( b );
a.add( 'k' );
a.add( 'a' );
a.add( 't' );
a.add( 'n' );
a.add( 'i' );
a.add( 's' );
System.out.println( "a is :" + a.toString() + " and has " + a.size() + " chars" );
System.out.println( "b is :" + b.toString() + " and has " + b.size() + " chars" );
System.out.println( "c is :" + c.toString() + " and has " + c.size() + " chars" );
//System.out.println( "B and A are equal : " + b.equals( a ) );
//System.out.println( "B and C are equal : " + b.equals( c ) );
}
}
Explanation / Answer
//////////// Java code
class CharList {
private char[] storeChars = new char[ 100 ];
private int numChars = 0;
private String input;
public CharList() {
// default no-arg constructor
}
public CharList( String startStr ) { // sets up internals
this.input = startStr;
for ( int i = 0; i < startStr.length(); i++ ) {
char a = startStr.charAt( i );
this.add( a );
}
}
public CharList( CharList other ) { // Copy constructor
if ( other == null ) { // checks null input
throw new IllegalArgumentException( "Null input" );
} else {
//this.input = other.(); NOT SURE HOW THIS GOES
this.numChars = other.size();
// this.storeChars = other.
}
}
public void add( char next ) {
this.storeChars[ numChars ] = next;
this.numChars++;
}
public char get( int index ) {
return storeChars[index];
}
public int size() {
return numChars;
}
public boolean equals(CharList b) {
return this.input == b.input;
// checks to see if equal
}
@Override
public String toString() {
// concatenates the chars in the array to form a String
input = "";
for(int i = 0; i < numChars; i++){
input += storeChars[i];
}
return input;
}
}
////// Driver code
public class CharListDriver {
public static void main( String[] args ) {
charListDriver();
}
private static void charListDriver() {
CharList a = new CharList();
CharList b = new CharList( "Batman" );
CharList c = new CharList( b );
a.add( 'k' );
a.add( 'a' );
a.add( 't' );
a.add( 'n' );
a.add( 'i' );
a.add( 's' );
System.out.println( "a is :" + a.toString() + " and has " + a.size() + " chars" );
System.out.println( "b is :" + b.toString() + " and has " + b.size() + " chars" );
System.out.println( "c is :" + c.toString() + " and has " + c.size() + " chars" );
System.out.println( "B and A are equal : " + b.equals( a ) );
System.out.println( "B and C are equal : " + b.equals( c ) );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.