How would I reset the instance variable in this situation? I'm new to java so forgive me if it's a simple answer

huangapple go评论86阅读模式
英文:

How would I reset the instance variable in this situation? I'm new to java so forgive me if it's a simple answer

问题

  1. import java.util.Scanner;
  2. public class TalkerTester
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner input = new Scanner(System.in);
  7. System.out.println("Enter some text: ");
  8. String words = input.nextLine();
  9. Talker talky = new Talker(words);
  10. String yelling = talky.yell();
  11. String whispers = talky.whisper();
  12. System.out.println(talky);
  13. System.out.println("Yelling: " + yelling);
  14. System.out.println("Whispering: " + whispers);
  15. }
  16. }
  1. public class Talker
  2. {
  3. private String text;
  4. // Constructor
  5. public Talker(String startingText)
  6. {
  7. text = startingText;
  8. }
  9. // Returns the text in all uppercase letters
  10. public String yell()
  11. {
  12. return text.toUpperCase();
  13. }
  14. // Returns the text in all lowercase letters
  15. public String whisper()
  16. {
  17. return text.toLowerCase();
  18. }
  19. // Reset the instance variable to the new text
  20. public void setText(String newText)
  21. {
  22. text = newText;
  23. }
  24. // Returns a String representation of this object
  25. public String toString()
  26. {
  27. return "I say, \"" + text + "\"";
  28. }
  29. }
英文:

No idea what anything is called but I'm pretty sure the one that runs the code is the main and that would be this one below this text. Also I'm using an online thing called codeHS so if it's not in the exact perfect format it won't accept what I do even if it works.

  1. import java.util.Scanner;
  2. public class TalkerTester
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner input = new Scanner(System.in);
  7. System.out.println("Enter some text: ");
  8. String words = input.nextLine();
  9. Talker talky = new Talker(words);
  10. String yelling = talky.yell();
  11. String whispers = talky.whisper();
  12. System.out.println(talky);
  13. System.out.println("Yelling: " + yelling);
  14. System.out.println("Whispering: " + whispers);
  15. }
  16. }

And below this is the other part that does all the work, this is where the problem is. The above part was given to us beforehand so I'm not allowed to change it. If any other part is wrong let me know as well.

  1. public class Talker
  2. {
  3. private String text;
  4. // Constructor
  5. public Talker(String startingText)
  6. {
  7. text = startingText;
  8. }
  9. // Returns the text in all uppercase letters
  10. // Find a method in the JavaDocs that
  11. // will allow you to do this with just
  12. // one method call
  13. public String yell()
  14. {
  15. return text.toUpperCase();
  16. }
  17. // Returns the text in all lowercase letters
  18. // Find a method in the JavaDocs that
  19. // will allow you to do this with just
  20. // one method call
  21. public String whisper()
  22. {
  23. return text.toLowerCase();
  24. }
  25. // Reset the instance variable to the new text
  26. public void setText(String newText)
  27. {
  28. }
  29. // Returns a String representation of this object
  30. // The returned String should look like
  31. //
  32. // I say, "text"
  33. //
  34. // The quotes should appear in the String
  35. // text should be the value of the instance variable
  36. public String toString()
  37. {
  38. return "I say, \"" + text + "\"";
  39. }
  40. }

答案1

得分: -1

我在考虑你想要将文本设置为新文本,这确实很繁琐。

  1. public void setText(String newText) {
  2. // 默认情况下会创建一个新的字符串,它不是引用。希望我可以理解你想要表达的,如果需要其他信息,请回复。
  3. this.text = newText;
  4. }
英文:
  1. // i am thinking you want to set the text to newText and this very much is borther.
  2. public void setText(String newText) {
  3. // By default a new string created and it is not the reference hope i
  4. // can what you want to say want anything else info please reply.
  5. this.text = newText;
  6. }

答案2

得分: -1

  1. public class Talker
  2. {
  3. private String text;
  4. // 构造函数
  5. public Talker(String startingText)
  6. {
  7. text = startingText;
  8. }
  9. // 返回大写形式的文本
  10. // 在JavaDocs中查找一个方法,
  11. // 可以通过一次方法调用来实现这个功能
  12. public String yell()
  13. {
  14. return text.toUpperCase();
  15. }
  16. // 返回小写形式的文本
  17. // 在JavaDocs中查找一个方法,
  18. // 可以通过一次方法调用来实现这个功能
  19. public String whisper()
  20. {
  21. return text.toLowerCase();
  22. }
  23. // 将实例变量重置为新文本
  24. public void setText(String newText)
  25. {
  26. text = newText;
  27. }
  28. // 返回此对象的字符串表示
  29. // 返回的字符串应该类似于
  30. //
  31. // 我说, "文本"
  32. //
  33. // 引号应该出现在字符串中
  34. // 文本应该是实例变量的值
  35. public String toString()
  36. {
  37. return "我说, \"" + text + "\"";
  38. }
  39. }
英文:

A very basic answer as I said in the title, thanks for the help

  1. public class Talker
  2. {
  3. private String text;
  4. // Constructor
  5. public Talker(String startingText)
  6. {
  7. text = startingText;
  8. }
  9. // Returns the text in all uppercase letters
  10. // Find a method in the JavaDocs that
  11. // will allow you to do this with just
  12. // one method call
  13. public String yell()
  14. {
  15. return text.toUpperCase();
  16. }
  17. // Returns the text in all lowercase letters
  18. // Find a method in the JavaDocs that
  19. // will allow you to do this with just
  20. // one method call
  21. public String whisper()
  22. {
  23. return text.toLowerCase();
  24. }
  25. // Reset the instance variable to the new text
  26. public void setText(String newText)
  27. {
  28. text = newText;
  29. }
  30. // Returns a String representation of this object
  31. // The returned String should look like
  32. //
  33. // I say, "text"
  34. //
  35. // The quotes should appear in the String
  36. // text should be the value of the instance variable
  37. public String toString()
  38. {
  39. return "I say, \"" + text + "\"";
  40. }
  41. }

huangapple
  • 本文由 发表于 2020年9月28日 02:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64091700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定