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评论76阅读模式
英文:

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

问题

import java.util.Scanner;

public class TalkerTester
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter some text: ");
        String words = input.nextLine();
        
        Talker talky = new Talker(words); 
        String yelling = talky.yell();
        String whispers = talky.whisper();
        
        System.out.println(talky);
        System.out.println("Yelling: " + yelling);
        System.out.println("Whispering: " + whispers);
    }
}
public class Talker
{
    private String text;
    
    // Constructor
    public Talker(String startingText)
    {
        text = startingText;
    }
    
    // Returns the text in all uppercase letters
    public String yell()
    {
        return text.toUpperCase();
    }
    
    // Returns the text in all lowercase letters
    public String whisper()
    {
        return text.toLowerCase();
    }
    
    // Reset the instance variable to the new text
    public void setText(String newText)
    {
        text = newText;
    }
    
    // Returns a String representation of this object
    public String toString()
    {
        return "I say, \"" + text + "\"";
    }
}
英文:

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.

import java.util.Scanner;

public class TalkerTester
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter some text: ");
        String words = input.nextLine();
        
        
        Talker talky = new Talker(words); 
        String yelling = talky.yell();
        String whispers = talky.whisper();
        
        System.out.println(talky);
        System.out.println("Yelling: " + yelling);
        System.out.println("Whispering: " + whispers);
        
    }
}

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.

public class Talker
{
    private String text;
    
    // Constructor
    public Talker(String startingText)
    {
        text = startingText;
    }
    
    // Returns the text in all uppercase letters
    // Find a method in the JavaDocs that
    // will allow you to do this with just
    // one method call
    public String yell()
    {
        return text.toUpperCase();
    }
    
    // Returns the text in all lowercase letters
    // Find a method in the JavaDocs that
    // will allow you to do this with just
    // one method call
    public String whisper()
    {
        return text.toLowerCase();
    }
    
    // Reset the instance variable to the new text
    public void setText(String newText)
    {
        
    }
    
    // Returns a String representation of this object
    // The returned String should look like
    // 
    // I say, "text"
    // 
    // The quotes should appear in the String
    // text should be the value of the instance variable
    public String toString()
    {
        return "I say, \"" + text + "\"";
    }
}

答案1

得分: -1

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

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

答案2

得分: -1

public class Talker
{
    private String text;

    // 构造函数
    public Talker(String startingText)
    {
        text = startingText;
    }

    // 返回大写形式的文本
    // 在JavaDocs中查找一个方法,
    // 可以通过一次方法调用来实现这个功能
    public String yell()
    {
        return text.toUpperCase();
    }

    // 返回小写形式的文本
    // 在JavaDocs中查找一个方法,
    // 可以通过一次方法调用来实现这个功能
    public String whisper()
    {
        return text.toLowerCase();
    }

    // 将实例变量重置为新文本
    public void setText(String newText)
    {
        text = newText;
    }

    // 返回此对象的字符串表示
    // 返回的字符串应该类似于
    //
    // 我说, "文本"
    //
    // 引号应该出现在字符串中
    // 文本应该是实例变量的值
    public String toString()
    {
        return "我说, \"" + text + "\"";
    }
}
英文:

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

public class Talker
{
    private String text;
    
    // Constructor
    public Talker(String startingText)
    {
        text = startingText;
    }
    
    // Returns the text in all uppercase letters
    // Find a method in the JavaDocs that
    // will allow you to do this with just
    // one method call
    public String yell()
    {
        return text.toUpperCase();
    }
    
    // Returns the text in all lowercase letters
    // Find a method in the JavaDocs that
    // will allow you to do this with just
    // one method call
    public String whisper()
    {
        return text.toLowerCase();
    }
    
    // Reset the instance variable to the new text
    public void setText(String newText)
    {
        text = newText;
    }
    
    // Returns a String representation of this object
    // The returned String should look like
    // 
    // I say, "text"
    // 
    // The quotes should appear in the String
    // text should be the value of the instance variable
    public String toString()
    {
        return "I say, \"" + text + "\"";
    }
}

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:

确定