Knowee
Questions
Features
Study Tools

Given the following set of classes:public class Customer { private String userName; private String password; public Customer(String userName, String password) { this.userName = userName; this.password = password; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; }} class CustomerTest { public static void main(String[] args) { Customer testAcct = new Customer("user","pass"); testAcct.setPassword("Java"); System.out.println(testAcct.toString()); }}What does the toString() method need to look like to result in the following output:UserName: userPassword: Javaa.)public String toString() { String state = "UserName: " + userName + "\n"; state += "Password: " + password + "\n"; return state;}b.)public String toString() { String state = "UserName: " + userName + "\n"; state += "Password: " + password + "\n"; return password;}c.)public String toString() { String state = "UserName: " + userName + "\n"; state = "Password: " + password + "\n"; return state;}d.)public String toString() { String state = "username: " + userName + "\n"; state += "password: " + password + "\n"; return state;}

Question

Given the following set of classes:public class Customer { private String userName; private String password; public Customer(String userName, String password) { this.userName = userName; this.password = password; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; }} class CustomerTest { public static void main(String[] args) { Customer testAcct = new Customer("user","pass"); testAcct.setPassword("Java"); System.out.println(testAcct.toString()); }}What does the toString() method need to look like to result in the following output:UserName: userPassword: Javaa.)public String toString() { String state = "UserName: " + userName + "\n"; state += "Password: " + password + "\n"; return state;}b.)public String toString() { String state = "UserName: " + userName + "\n"; state += "Password: " + password + "\n"; return password;}c.)public String toString() { String state = "UserName: " + userName + "\n"; state = "Password: " + password + "\n"; return state;}d.)public String toString() { String state = "username: " + userName + "\n"; state += "password: " + password + "\n"; return state;}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is a.)

Here's why:

The toString() method in Java is used to convert an object into a String. In this case, we want to convert the Customer object into a String that represents the state of the object.

a.) This option correctly concatenates the userName and password fields with the appropriate labels ("UserName: " and "Password: ") and returns the resulting String. The "\n" is used to add a new line between the userName and password.

b.) This option incorrectly returns the password field instead of the state String.

c.) This option incorrectly overwrites the state String with the password field instead of appending it.

d.) This option uses incorrect labels ("username: " and "password: ") instead of the ones specified in the question ("UserName: " and "Password: ").

This problem has been solved

Similar Questions

Given the following class, what sets of statements would have the end result of the myUser instance having the username as testuser and the password as testpassword?public class User { private String userName; private String password; public User(String userName, String password) { this.userName = userName; this.password = password; } public String getUserName() { return userName; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public void setPassword(String password) { this.password = password; }}a.)User myUser = newUser("testpassword","testuser");b.)User myUser = newUser("username","password");myUser.setUserName("testuser");myUser.setPassword("testpassword");c.)User myUser = new User();myUser.setUserName("testuser");myUser.setPassword("testpassword");d.)User myUser = newUser("testuser","testpassword");myUser.setUserName("user");myUser.setPassword("pass");

Demonstrate ability to set and change the values of object properties.Given the following class, what sets of statements would have the end result of the myCustomer instance having the username as XC1001 and the password as testpass?public class Customer { private String customerCode; private String password; public Customer(String customerCode, String password) { this.customerCode = customerCode; this.password = password; } public String getCustomerCode() { return customerCode; } public String getPassword() { return password; } public void setCustomerCode(String customerCode) { this.customerCode = customerCode; } public void setPassword(String password) { this.password = password; }}a.)Customer myCustomer = newCustomer("testpass","XC1001");b.)Customer myCustomer = new Customer("username","password");myCustomer.setCustomerCode("XC1001");myCustomer.setPassword("testpass");c.)Customer myCustomer = newCustomer("XC1001","testpass");myCustomer.setCustomerCode("user");myCustomer.setPassword("pass");d.)Customer myCustomer = new Customer();myCustomer.setCustomerCode("XC1001");myCustomer.setPassword("testpass");

Given the following set of classes, what needs to be fixed to make the program produce the output Name: Java?public class Customer { private String userName; private String password; public Customer(String userName, String password) { this.userName = userName; this.password = password; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; }}class CustomerTest { public static void main(String[] args) { Customer c1 = new Customer("user","pass"); c1.setUserName("Java"); outputName(); } public static void outputName(){ System.out.println("Name: " + c1.getUserName()); }} a.)The c1 should be returned as a return value from outputName().b.)The c1 should be passed into the outputName() method as a parameter.c.)The instance c1 is not in scope in the method outputName(). The instance c1 needs to be declared in outputName()d.)The instance c1 is not in scope in the method outputName(). The c1 should be globally in CustomerTest to be accessed in the outputName() method.

You have a class Student:public class Student{ public int studentId; public String studentName; public String studentGrade; public Student() { } public Student(int studentId, String studentName, String studentGrade){ this.studentId = studentId; this.studentName = studentName; this.studentGrade = studentGrade; }}You have a child class CollegeStudent:public class CollegeStudent extends Student {}Then you execute the following snippet of code:CollegeStudent myCollegeStudent = new CollegeStudent(1, "Alice", "3rd");What is the error in this snippet of code?Instruction: Choose the option that best answers the question. Answer ChoicesYou cannot specify multiple constructors for the parent class, though this is allowed in child classesYou cannot use the parameterized constructor in the base class to instantiate the CollegeStudent derived classYou need to have additional constructors in the base class to support the instantiation of the CollegeStudent derived classYou have to use the “object” keyword to create an object of a derived class

public class Employee {    private String name;        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getSal() {        return sal;    }    public void setSal(int sal) {        this.sal = sal;    }    private int sal;    public Employee(String name,int sal)    {        this.name = name;        this.sal = sal;    }    public String toString()    {        return name+" "+sal;    }    public int applyInc(int percentage)    {        return sal*percentage;    }}EmployeeStarter.java:public class EmployeeStarter {    public static void main(String[] args)     {        Employee employee = new Employee("Sangamithra",30000);  System.out.println(employee.applyInc(2));//calling the method of Employee that doubles the salary        System.out.println(employee.applyInc(e1->e1.getSal()*2));//line-1    }} How can the method, applyInc() of Employee class be redefined so that the functionality getting applied on Employee (doubling the salary) can be passed as an argument as in line-1? public int applyInc(Function<Employee, Integer> function) { return function.apply(this); }public int applyInc (Predicate<Employee> c) { return c.test(this); }public int applyInc (Consumer<Employee> c){ return c.accept(this); }Submit

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.