Given class Student with attribute studentName. Fill the method signature correctly to frame the setter method for the attribute.(Follow camel case notation for the method).public class Student{ private String studentName ; //Setter { }}public class Main{ public static void main(String args[]) { Student studentObj = new Student(); //Set the value for name as Peter }}
Question
Given class Student with attribute studentName. Fill the method signature correctly to frame the setter method for the attribute.(Follow camel case notation for the method).public class Student{ private String studentName ; //Setter { }}public class Main{ public static void main(String args[]) { Student studentObj = new Student(); //Set the value for name as Peter }}
Solution
Here is the correct way to frame the setter method for the attribute studentName in the Student class:
public class Student{
private String studentName ;
//Setter
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}
public class Main{
public static void main(String args[]) {
Student studentObj = new Student();
//Set the value for name as Peter
studentObj.setStudentName("Peter");
}
}
In the setter method, we are passing a parameter (in this case, String studentName) and assigning it to the class attribute this.studentName. The this keyword is used to refer to the current instance of the class.
In the main method, we are creating an object of the Student class (studentObj) and using the setter method (setStudentName) to set the value of the studentName attribute to "Peter".
Similar Questions
Sarah got confused to creating the constructor. Write a Java application to help Sarah to do this.Type(Class)AttributesMethodsResponsibilitiesStudentint studentIdString studentNameString studentAddressString collegeNameInclude the getters and setters method for all the attributes. Student Include a public parametrized constructor of four arguments in the following order - studentId, studentName, studentAddress, and collegeName to initialize the values for the Student objectIf student belongs to other college, give input as 'no/NO' and get college name from the user and create student object with 4-argument constructor to initialize all the values. Student Include a public parametrized constructor of three arguments in the following order - studentId, studentName, studentAddress, and collegeName should be "NIT" to initialize the values for the Student objectIf student belongs to NIT, give input as 'yes/YES' and skip input for the attribute collegeName and create student object with 3-argument constructor to initilze the values for studentId, studentName and studentAddress and collegeName as "NIT". Note: The class and methods should be declared as public and all the attributes should be declared as private. In the UserInterface class, write the main method to test the application.Assume most of the students are from "NIT" college. So user has to give input whether the student is from NIT or not.Instead of Yes / No, if user enters different input then display 'Wrong Input' and get the input again. Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program. Sample Input 1:Enter Student's Id:12Enter Student's Name:JohnEnter Student's address:ChennaiWhether the student is from NIT(Yes/No):NOEnter the college name:SVS Sample Output 1:Student id:12Student name:JohnAddress:ChennaiCollege name:SVS Sample Input 2:Enter Student's Id:43Enter Student's Name:TomEnter Student's address:CoimbatoreWhether the student is from NIT(Yes/No):yWrong InputWhether the student is from NIT(Yes/No):yes Sample Output 2:Student id:43Student name:TomAddress:CoimbatoreCollege name:NIT
Create a base class Person and a derived class Student with Person as its base class.Add two methods setname() (which takes the parameter self and name)and getname() which prints the name in the base class.Add two methods in the derived class: setage() (which takes the parameters self and age) which sets the age and getage() which prints the age.Create an instance of Student and name it as s1.Take name and age as inputs from the console.Call the setname() and setage() on this instance by passing the name and age parameters.Call the getname() and getage() on this class, which prints the passed parametersNotice that we have created an object of derived class but it could access the methods of base class.This is inheritance.
public class MyClass{private int value;public void setValue(int i){ / code / }// Other methods…}The method setValue assigns the value of i to the instance field value. What could you write for the implementation of setValue?Question 3Answera.value=iandthis.value = i;b.value = i; c.this.value = i;d.value == i;
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;}
What is the correct syntax to create a class named Student that will inherit properties and methods from a class named Person?
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.