// Arup Guha
// 9/23/2026
// Example Class: My Own String Class

import java.util.*;

// This is the main class, the one we will run.
public class MyStringExample {

	public static void main(String[] args) {
		
		// Create two of our objects.
		MyString word1 = new MyString("antidisestablishmentarianism");
		MyString word2 = new MyString("computer");
		
		// See what they look like.
		System.out.println(word1+", "+word2);
		
		// Test out concat.
		MyString newW = word1.concat(word2.toString());
		System.out.println(word1+", "+word2+", "+newW);
		
		// Test out addToEnd.
		word1.addToEnd(word2);
		System.out.println(word1+", "+word2+", "+newW);
	}
}

// Here is the String class we have built.
// Default visibility.
class MyString {

	// List the instance variables. The different pieces of the object.
	private char[] str;
	private int len;
	
	// Constructor: Uses Java String object.
	public MyString(String s) {
		
		// Inside of an instance method, the key word "this" is the reference to the
		// object the method was called upon.
		this.len = s.length();
		this.str = new char[this.len];
		
		// If I am inside of an instance method, I am free to omit "this.". The compiler
		// knows that I am referring the instance variable in this object naturally.
		for (int i=0; i<len; i++)
			str[i] = s.charAt(i);
	}
	
	// Returns a string representation of this object.
	public String toString() {
		return new String(str);
	}
	
	// Modifies this object by adding the contents of other to the end of it.
	public void addToEnd(MyString other) {
		
		// Give this enough room.
		char[] newStr = new char[len + other.len];
		
		// Copy old stuff.
		for (int i=0; i<len; i++)
			newStr[i] = this.str[i];
		
		// Copy in new stuff.
		for (int i=len; i<len+other.len; i++)
			newStr[i] = other.str[i-len];
		
		// Update the object.
		this.str = newStr;
		this.len = newStr.length;
	}
	
	public MyString concat(MyString other) {
		
		
		MyString res = new MyString(this.toString());
		res.addToEnd(other);
		
		/*** Alternate implementation.
		MyString tmp = new MyString("");
		tmp.addToEnd(this);
		tmp.addToEnd(new MyString(other));
		return tmp; ***/
	}

}