153 lines
8.8 KiB
HTML
153 lines
8.8 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>MyStringBuilder.java</title>
|
||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
body {color: #000000; background-color: #ffffff; font-family: monospace}
|
||
|
pre {color: #000000; background-color: #ffffff; font-family: monospace}
|
||
|
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
|
||
|
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
|
||
|
.ST2 {font-family: monospace; font-weight: bold}
|
||
|
.comment {color: #969696}
|
||
|
.ST5 {font-family: monospace; font-weight: bold; font-style: italic}
|
||
|
.ST4 {font-family: monospace; font-style: italic}
|
||
|
.ST6 {color: #ce54b8; font-family: monospace; font-style: italic}
|
||
|
.ST0 {color: #287bde}
|
||
|
.ST3 {color: #ce54b8}
|
||
|
.string {color: #1e9347}
|
||
|
.literal {color: #336bdd}
|
||
|
-->
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\Exam1_CalebFontenot\src\main\java\com\calebfontenot\exam1_calebfontenot\MyStringBuilder.java</td></tr></table>
|
||
|
<pre>
|
||
|
<span class="comment">/*</span>
|
||
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
||
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||
|
<span class="comment"> */</span>
|
||
|
<span class="literal">package</span> com.calebfontenot.exam1_calebfontenot;
|
||
|
|
||
|
<span class="literal">import</span> java.util.Arrays;
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">MyStringBuilder</span> {
|
||
|
|
||
|
<span class="comment">//private int size = 0; // Size is not necessary. Remove it and modify the code.</span>
|
||
|
<span class="comment">//private int capacity = 0; //not necessary</span>
|
||
|
<span class="literal">private</span> <span class="literal">char</span>[] <span class="ST3">buffer</span>;
|
||
|
|
||
|
<span class="literal">public</span> <span class="ST2">MyStringBuilder</span>() {
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="ST2">MyStringBuilder</span>(<span class="literal">char</span>[] chars) {
|
||
|
<span class="ST3">buffer</span> = chars;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="ST2">MyStringBuilder</span>(String s) {
|
||
|
<span class="ST3">buffer</span> = s.toCharArray();
|
||
|
}
|
||
|
|
||
|
<span class="literal">private</span> <span class="literal">void</span> <span class="ST2">increaseCapacity</span>(<span class="literal">int</span> newCapacity) {
|
||
|
<span class="literal">char</span>[] temp = <span class="literal">new</span> <span class="literal">char</span>[newCapacity];
|
||
|
System.<span class="ST4">arraycopy</span>(<span class="ST3">b</span><span class="ST3">uffer</span>, 0, temp, 0, <span class="ST3">b</span><span class="ST3">uffer</span>.<span class="ST3">length</span>);
|
||
|
<span class="ST3">buffer</span> = temp;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> MyStringBuilder <span class="ST2">append</span>(MyStringBuilder s) {
|
||
|
<span class="literal">int</span> oldLength = <span class="ST3">buffer</span>.<span class="ST3">length</span>;
|
||
|
<span class="literal">char</span>[] toAdd = s.<span class="ST3">buffer</span>;
|
||
|
increaseCapacity(toAdd.<span class="ST3">length</span> + oldLength);
|
||
|
<span class="literal">int</span> j = 0;
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = oldLength; i < (oldLength + s.<span class="ST3">buffer</span>.<span class="ST3">length</span>); ++i) {
|
||
|
<span class="ST3">buffer</span>[i] = toAdd[j++];
|
||
|
}
|
||
|
<span class="literal">return</span> <span class="literal">this</span>;
|
||
|
}
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> * </span><span class="ST1">public</span> <span class="ST1">MyStringBuilder</span> <span class="ST1">append</span><span class="ST1">(</span><span class="ST1">int</span> <span class="ST1">i</span><span class="comment">) {</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * }</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@return</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">length</span>() {
|
||
|
<span class="literal">return</span> <span class="ST3">buffer</span>.<span class="ST3">length</span> -1;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">char</span> <span class="ST2">charAt</span>(<span class="literal">int</span> index) {
|
||
|
<span class="literal">return</span> <span class="ST3">buffer</span>[index];
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> MyStringBuilder <span class="ST2">toLowerCase</span>() {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < <span class="ST3">buffer</span>.<span class="ST3">length</span>; ++i) {
|
||
|
<span class="ST3">buffer</span>[i] = Character.<span class="ST4">toLowerCase</span>(<span class="ST3">buffer</span>[i]);
|
||
|
}
|
||
|
<span class="literal">return</span> <span class="literal">this</span>;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> MyStringBuilder <span class="ST2">substring</span>(<span class="literal">int</span> begin, <span class="literal">int</span> end) {
|
||
|
String s = <span class="string">""</span>;
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = begin; i < end; ++i) {
|
||
|
s += <span class="ST3">buffer</span>[i];
|
||
|
}
|
||
|
<span class="literal">return</span> <span class="literal">new</span> MyStringBuilder(s.toCharArray());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
<span class="literal">public</span> String <span class="ST2">toString</span>() {
|
||
|
String s = <span class="string">""</span>;
|
||
|
<span class="literal">for</span>(<span class="literal">char</span> c: <span class="ST3">buffer</span>)
|
||
|
{
|
||
|
s += c;
|
||
|
}
|
||
|
<span class="literal">return</span> s;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> MyStringBuilder <span class="ST2">reverse</span>() {
|
||
|
<span class="literal">char</span>[] reversed = <span class="literal">new</span> <span class="literal">char</span>[<span class="ST3">buffer</span>.<span class="ST3">length</span>];
|
||
|
<span class="literal">int</span> j = 0;
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="ST3">buffer</span>.<span class="ST3">length</span> - 1; i >= 0; --i) {
|
||
|
reversed[i] = <span class="ST3">buffer</span>[j];
|
||
|
j++;
|
||
|
}
|
||
|
<span class="ST3">buffer</span> = reversed;
|
||
|
<span class="literal">return</span> <span class="literal">this</span>;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> MyStringBuilder <span class="ST2">substring</span>(<span class="literal">int</span> begin) {
|
||
|
<span class="literal">return</span> substring(begin, <span class="ST3">b</span><span class="ST3">uffer</span>.<span class="ST3">length</span>);
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> MyStringBuilder <span class="ST2">toUpperCase</span>() {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < <span class="ST3">buffer</span>.<span class="ST3">length</span>; ++i) {
|
||
|
<span class="ST3">buffer</span>[i] = Character.<span class="ST4">toUpperCase</span>(<span class="ST3">buffer</span>[i]);
|
||
|
}
|
||
|
<span class="literal">return</span> <span class="literal">this</span>;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
|
||
|
MyStringBuilder s1 = <span class="literal">new</span> MyStringBuilder(<span class="string">"</span><span class="string">Welcomne to</span><span class="string">"</span>);
|
||
|
MyStringBuilder s2 = <span class="literal">new</span> MyStringBuilder(<span class="string">"</span><span class="string"> Java</span><span class="string">"</span>);
|
||
|
|
||
|
System.<span class="ST6">out</span>.println(s1.length());
|
||
|
System.<span class="ST6">out</span>.println(s1.charAt(3));
|
||
|
System.<span class="ST6">out</span>.println(s1.toUpperCase().toString());
|
||
|
s1.append(s2);
|
||
|
System.<span class="ST6">out</span>.println(s1.toString());
|
||
|
System.<span class="ST6">out</span>.println(s1.substring(1, 4));
|
||
|
System.<span class="ST6">out</span>.println(s1.reverse());
|
||
|
System.<span class="ST6">out</span>.println(s1.reverse());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
</pre></body>
|
||
|
</html>
|