Please code the following in: JAVA! Please use many comments so I can understand
ID: 3709053 • Letter: P
Question
Please code the following in: JAVA!
Please use many comments so I can understand.
Full points will be awarded, thanks in advance!
Some classes are unusual in that once created & initialized via a constructor, they never change their internal data for the lifetime of the object. The term mutable (think editable) is used to describe classes whose internal state (data) can change as the object is used. An Immutable Object is an object that once defined, never alters or changes its internal data - the java.lang.String class is one example of this. Immutability is like a "read-only" characteristic; the data may be observed and "gotten" but is never changed or "written to". We'll discuss privacy leaks shortly in class, but it's worth noting here that since an Immutable object can't be changed, we don't have to worry about privacy leaks across getter/setter boundaries when those that do the getting/setting cannot change your object. Think of this as similar to a "public static" data item - we don't worry about making it private, since there is no damage an external client can do to these data items. We'll build a String class here that is really just a CharList or IntList variation that we've seen before. Start by defining a new class called "CharList" and implement the data and method members below Class Invariants .Ask the instructor if you may assume no string will grow beyond 100 characters in this lab Data An array to store characters. . Should we make this data private? An integer variable used to track the number of characters Methods . public CharList) . public CharList(String startStr); //use string to set up internals . public CharList(CharList other); //copy c'tor; be sure to check for null input . public void add(char next); //Could we make this so it dynamically grows to accommodate more than 100 elements? public char get(int index); public String toString( o Return a string that is the concatenated result of combining every character in your char arrayExplanation / Answer
ScreenShot
--------------------------------------------------------------------
Program
//Import packages
import java.util.*;
import java.io.*;
import java.io.CharArrayWriter;
//Create a class named CharList
class CharList{
//Initialize an array of 100 size
char[] Array;
//Default constructor
CharList(){
}
//Parameterised constructor
CharList(String startStr){
Array=startStr.toCharArray();
}
//Copy constructor
CharList(CharList other){
Array=new char[100];
}
//Add new character in array
public void add(char next) {
Array[Array.length+1]=next;
}
//Get each character
public char get(int index) {
return Array[index];
}
//Print the string
public String toString() {
String str=new String(Array);
return ""+str+" and has "+Array.length;
}
//boolean to check the array values
public boolean equals(Object other) {
if(other==null) {
return false;
}
else if(Array==other) {
return true;
}
else {
return false;
}
}
}
//Main class
public class CharacterArray {
//Main method
public static void main(String[] args) {
//Object creation and printing
CharList ch=new CharList("Hello");
System.out.println("A is: "+ch);
CharList ch1=new CharList("bye");
System.out.println("B is: "+ch1);
//comparison of two arrays
System.out.println("A and B are equal: "+ch.equals(ch1));
}
}
---------------------------------------------------------------------------------------------
Note:-
I assume you need to display the data in this way.
In the boolean method,i assume you want o check two array datas ,if you need clarification,let me know