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.
Question
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.
Solution
The correct answer is 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.
In Java, the scope of a variable is determined by the block in which it is declared. In this case, the variable c1 is declared in the main method and cannot be accessed in the outputName method. To fix this, you could declare c1 as a static variable in the CustomerTest class, outside of any method. This would make it globally accessible within the class. Here's how you could do it:
public class CustomerTest {
static Customer c1; // declare c1 here
public static void main(String[] args) {
c1 = new Customer("user","pass"); // initialize c1 here
c1.setUserName("Java");
outputName();
}
public static void outputName(){
System.out.println("Name: " + c1.getUserName());
}
}
This way, the outputName method can access the c1 variable and the program will output "Name: Java" as expected.
Similar Questions
Given the following set of classes, what would need to be fixed to make the program work?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"); resetAcct(); } public static void resetAcct(Customer acct){ acct.setPassword(""); acct.setUserName(""); }}The instance testAcct is not in scope in the method resetAcct(). The instance testAcct needs to be declared in resetAcct()The testAcct should be returned as a return value from resetAcct().The testAcct should be declared at the class level to be accessed in the resetAcct() method.The testAcct should be passed into the resetAcct() method as a parameter.
Given the following class, what sets of statements would have the end result of the myCustomer instance having the customername as AB1002 password as test as testpassword and then outputting the customername and password?public class Customer { private String customerName; private String password; public Customer(String customerName, String password) { this.customerName = customerName; this.password = password; } public String getCustomerName() { return customerName; } public String getPassword() { return password; } public void setCustomerName(String customerName) { this.customerName = customerName; } public void setPassword(String password) { this.password = password; }}Customer myCustomer = new Customer("AB1002","testpassword");System.out.println("Customer: " + myCustomer.customername());System.out.println("Password: " + myCustomer.password());Customer myCustomer = new Customer();myCustomer.setCustomerName("AB1002");myCustomer.setPassword("testpassword");System.out.println("Customer: " + myCustomer.getCustomerName());System.out.println("Password: " + myCustomer.getPassword());Customer myCustomer = new Customer("AB1002","testpassword");System.out.println("Customer: " + myCustomer.getCustomerName());System.out.println("Password: " + myCustomer.getPassword());Customer myCustomer = new Customer("AB1002","testpassword");System.out.println("Customer: " + myCustomer.customername);System.out.println("Password: " + myCustome
Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone_number, and e-mail address. A student has a status (freshman, sophomore, junior, or senior). An employee has an office, salary. A faculty member has office_hours and a rank. A staff member has a title. Override the toString() method in each of these classes to display their details. Write a Java application and subsequent pseudocode to implement/simulate the same.
public class Account{ private int accountNumber; private String holderName; private double balance; public Account(int accountNumber, String holderName, double balance) { this.accountNumber = accountNumber; this.holderName = holderName; this.balance = balance; } } public class Main{ public static void main(String args[]) { Account accObj1=new Account(1001,"Chandru",50000); System.out.println(accObj1); }}If the above code should give the output as “Account Number : 1001, Holder : Chandru, Balance : 50000”, which method should be overridden and that method should be written in which class.Select one:a.Override toString() method in Account classb.Override tostring() method in Account classc.Override tostring() method in Main classd.Override toString() method in Main class
Predict the output of the code snippet given alongside.public class Test{public static void main(String[] args){Teacher t = new Student();System.out.println(t.name);}}class Teacher{String name="Sam";}class Student{String name="Kevin";} SamKevinCompilation errorRun-time error
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.