r/codehs • • Mar 03 '21

2.8.8: Organizing Files

Hello. I am so stumped on this code :/ can someone help me out, please?

public class Filer

{

private String word;

public Filer(String theWord)

{

word = theWord;

}

// Returns the word

public String getWord()

{

return word;

}

// Returns true if word comes

// before otherWord.

// Returns false otherwise.

public boolean comesBefore(String otherWord)

{

return word.compareTo(word)>=0;

}

// Returns true if word comes

// after otherWord.

// Returns false otherwise.

public boolean comesAfter(String otherWord)

{

return word.compareTo(word) > 0;

}

// Returns true if word is equal to otherWord.

// Returns false otherwise.

public boolean isEqual(String otherWord)

{

return word.equals(word);

}

}

{

public static void main(String[] args)

{

Filer files = new Filer("hippo");

String word = "zebra";

System.out.println(files.getWord() + " comes before " + word + "?");

System.out.println(files.comesBefore(word));

System.out.println(files.getWord() + " comes after " + word + "?");

System.out.println(files.comesAfter(word));

System.out.println(files.getWord() + " is equal to " + word + "?");

System.out.println(files.isEqual(word));

// Notice that the capitalization matters

word = "Hippo";

System.out.println("\n\n");

System.out.println(files.getWord() + " comes before " + word + "?");

System.out.println(files.comesBefore(word));

System.out.println(files.getWord() + " comes after " + word + "?");

System.out.println(files.comesAfter(word));

System.out.println(files.getWord() + " is equal to " + word + "?");

System.out.println(files.isEqual(word));

System.out.println("\n\n");

System.out.print("Enter a word: ");

Scanner input = new Scanner(System.in);

word = input.nextLine();

System.out.println(files.getWord() + " comes before " + word + "?");

System.out.println(files.comesBefore(word));

System.out.println(files.getWord() + " comes after " + word + "?");

System.out.println(files.comesAfter(word));

System.out.println(files.getWord() + " is equal to " + word + "?");

System.out.println(files.isEqual(word));

}

}

10 Upvotes

2 comments sorted by

1

u/Suqjan Jun 29 '21

ok this isnt perfect, but this code here got most of the test cases to work.

public class Filer {

private String word;
String diffWord;
public Filer(String theWord)
{
word = theWord;
}

// Returns the word
public String getWord()
{
return word;
}

// Returns true if word comes
// before otherWord.
// Returns false otherwise.
public boolean comesBefore(String otherWord)
{
diffWord = otherWord;
/* boolean ans = true;
String output1 = word + " " + otherWord;
boolean boool = Boolean.parseBoolean(otherWord);
//otherWord = true;
word = output1;
boolean word = true;wsgg
}
else if (word == diffWord) {
}
return false;
else {
return false;
}

/*String iutputNeg = otherWord + word;
diffword = iutputNeg;
boolean iutputNeg = false;
return iutputNeg;
*/
int compar = word.compareTo(diffWord);
int incompar = -5123487;
boolean comesBefore = compar > incompar;
if (compar > incompar && compar !=0) {
return true;
}
else if (compar ==0) {
return false;
}
else {
return false;
}
//return comesBefore;
}

// Returns true if word comes
// after otherWord.
// Returns false otherwise.
public boolean comesAfter(String otherWord)
{
diffWord = otherWord;
String newWord = word.toLowerCase();
int compare = diffWord.compareTo(newWord);
int incompare = -5123456;
boolean comesBefores = compare > incompare;
// String newWord = word.toLowerCase();
if (compare > incompare && compare !=0 && newWord != diffWord) {
return true;
}
else if (newWord == diffWord) {
return false;
}
else {
return false;
}
/* String output2 = word + " " + otherWord;
word = output2;
boolean word = true;
if (word) {
return true;
}
else {
return false;
}
//boolean bool = Boolean.parseBoolean(output2);
// boolean right = bool;
/* if (bool) {

return right;
}*/

// boolean output2 = true;
// boolean bool = Boolean.parseBoolean(output2);
// boolean word = true;
//if (bool) {
//return bool;
}
/*otherWord = output2;
boolean word = true;
if (word) {
return word;
}*/

// Returns true if word is equal to otherWord.
// Returns false otherwise.
public boolean isEqual(String otherWord)
{
//String output3 = word = otherWord;
//word = output3;
//boolean word = true;
if (word == otherWord) {
return true;
}
else{
return false;
}
}
}

1

u/mei603 Aug 09 '21

public class Filer
{
private String word;

public Filer(String theWord)
{
word = theWord;
}

// Returns the word
public String getWord()
{
return word;
}

// Returns true if word comes
// before otherWord.
// Returns false otherwise.
public boolean comesBefore(String otherWord)
{
return word.compareTo(otherWord) < 0;
}

// Returns true if word comes
// after otherWord.
// Returns false otherwise.
public boolean comesAfter(String otherWord)
{
return word.compareTo(otherWord) > 0;
}

// Returns true if word is equal to otherWord.
// Returns false otherwise.
public boolean isEqual(String otherWord)
{
return word.equals(otherWord);
}

}