Knowee
Questions
Features
Study Tools

Assume there is a class Student with attributes, id, name and cgpa.Assume there is a constructor with attributes id and name.You are provided with the three argument constructor shuffled.  Rearrange it in correct order.this.cgpa = cgpa;public Student{int id, String name,float cgpa){this(id,name);}

Question

Assume there is a class Student with attributes, id, name and cgpa.Assume there is a constructor with attributes id and name.You are provided with the three argument constructor shuffled.  Rearrange it in correct order.this.cgpa = cgpa;public Student{int id, String name,float cgpa){this(id,name);}

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

Solution 1

The correct arrangement of the constructor in Java would be:

public class Student {
    int id;
    String name;
    float cgpa;

    public Student(int id, String name, float cgpa) {
        this.id = id;
        this.name = name;
        this.cgpa = cgpa;
    }
}

Here's the step by step explanation:

  1. First, we define the class Student with the attributes id, name, and cgpa.

  2. Then, we define the constructor Student which takes three parameters: id, name, and cgpa.

  3. Inside the constructor, we use the this keyword to refer to the current instance of the class.

  4. this.id = id; assigns the value of the parameter id to the instance variable id.

  5. this.name = name; assigns the value of the parameter name to the instance variable name.

  6. this.cgpa = cgpa; assigns the value of the parameter cgpa to the instance variable cgpa.

This problem has been solved

Solution 2

The correct arrangement of the constructor in Java would be:

public class Student {
    int id;
    String name;
    float cgpa;

    public Student(int id, String name, float cgpa) {
        this.id = id;
        this.name = name;
        this.cgpa = cgpa;
    }
}

Here's the step by step explanation:

  1. First, we define the class Student with its attributes id, name, and cgpa.
  2. Then, we define the constructor Student which takes three parameters: id, name, and cgpa.
  3. Inside the constructor, we use the this keyword. this is a reference variable that refers to the current object. Here, this.id = id means that the id attribute of the current object should be set to the id passed into the constructor. The same goes for name and cgpa.

This problem has been solved

Similar Questions

Drag and Drop the code so that the constructor for Student class is overloaded correctly.public class Student { private int studentId; private String name; private float grade; public Student(int studentId, String name) {                     blank                } public Student(int studentId, String name, float grade) {  blank  blank }}this.grade = grade; this(studentId,name); this.studentId = studentId;  this.name = name;

Arrange the code shuffle provided such that -   first you provide the no argument constructor that prints "In default constructor" , -   next is the constructor with id, name and salary as arguments that invokes the no arg constructor and sets the attributes passed as attribute and -   finally the constructor with id, name, salary and pancardno as arguments that invokes the 3 argument constructor and then sets the pancardno.public class Employee {public Employee(){private int id;private String name;private float salary;private String pancardno;}this();public Employee(int id, String name, float salary) {this.id = id;this.name = name;this.salary = salary;} this.pancardno = pancardno; }this(id,name,salary);System.out.println("In default constructor");}public Employee(int id, String name, float salary, String pancardno) {

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

public class Employee {private int id;private String name;private float salary;private String pancardno;public Employee(){System.out.println("In default constructor");}public Employee(int id, String name, float salary) {this.id = id;this.name = name;this.salary = salary;}public Employee(int id, String name, float salary, String pancardno) {this(id,name,salary); this.pancardno = pancardno; }this();}

Arrange the code shuffle provided such that -   first you provide the no argument constructor that prints "In default constructor" , -   next is the constructor with id, name and salary as arguments that invokes the no arg constructor and sets the attributes passed as attribute and -   finally the constructor with id, name, salary and pancardno as arguments that invokes the 3 argument constructor and then sets the pancardno.this.id = id;this.name = name;this.salary = salary;}this(id,name,salary); this.pancardno = pancardno; }public Employee(int id, String name, float salary) {this();System.out.println("In default constructor");}public Employee(){}public Employee(int id, String name, float salary, String pancardno) {public class Employee {private int id;private String name;private float salary;private String pancardno;

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.