java - Rewriting the whole String Class methods -



java - Rewriting the whole String Class methods -

my professor have given me challenging homework, thought rewrite methods in string classes without using string, stringbuilder, , wrapper classes. intro java class. have methods done having hard time other ones. main class no creation of string inside.

what have: "data" char[] info "myownstring" object.

compareto method:

public int compareto(myownstring rhs){ if (this.data == rhs.data){ homecoming 0; } else if (this.data > rhs.data){ homecoming 1; } else { homecoming -1; } }

this 1 shows error. guess rhs needs declare before beingness able compare string beingness assigned myownstring object. since there compareto method , comparetoignorecase, have add together line ignore comparsion?

update:

this code went compareto method creating own method using length of array.

public int compareto(myownstring cts){ int word1 = data.length; int word2 = cts.length(); int result = 0; (int i=0; i<word1; i++){ (int j=0;j<word2; j++){ if(word1 == word2){ result = 0; } else if (word1 > word2){ result = 1; } else { result = -1; } } } homecoming result; }

since not allowed utilize string.compareto() , since java doesn't back upwards > custom objects or char[] matter (e.g doesn't back upwards operator overloading) have programmatically.

in other words have 2 char arrays , have loop through characters. compare first 2 characters each of char arrays (there can utilize > or <) if == compare sec 2 characters , on.. till find 2 characters break tie - can break loop , homecoming result -1, 1. if tied on every character homecoming 0.

if want implement equals phone call compareto , optimize bit checking lengths of strings. if different of course of study strings not equal

update: not sure if ran code above - seek compile code , run before move forward. believe won't compile. here is, believe, right unchecked version. have mixed -1 , 1s always..

public int compareto(myownstring cts){ int word1length = ((myownstring)this).data.length; int word2length = cts.data.length; (int i=0; < math.min(word1length,word2length); i++){ if(this.data[i] == cts.data[i]){ continue; } else if (this.data[i] > cts.cts[i]){ homecoming -1; } else if (this.data[i] < cts.cts[i]) { homecoming 1; } } if (word1length == word2length){ homecoming 0; } else if(word1length < word2length){ homecoming 1; } else { homecoming -1; } } public boolean equals(myownstring cts){ int word1length = ((myownstring)this).data.length; int word2length = cts.data.length; if (word1length != word2length){ homecoming false; } else { // if equal int comparing = this.compareto(cts); if (comparison==0){ homecoming true; } else { homecoming false; } } }

java compareto

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -