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
Question
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
Solution
Here is the Java application that will help Sarah to create the constructor:
import java.util.Scanner;
public class Student {
private int studentId;
private String studentName;
private String studentAddress;
private String collegeName;
public Student(int studentId, String studentName, String studentAddress, String collegeName) {
this.studentId = studentId;
this.studentName = studentName;
this.studentAddress = studentAddress;
this.collegeName = collegeName;
}
public Student(int studentId, String studentName, String studentAddress) {
this.studentId = studentId;
this.studentName = studentName;
this.studentAddress = studentAddress;
this.collegeName = "NIT";
}
// Getters and Setters
// ...
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Student's Id:");
int id = scanner.nextInt();
System.out.println("Enter Student's Name:");
String name = scanner.next();
System.out.println("Enter Student's address:");
String address = scanner.next();
System.out.println("Whether the student is from NIT(Yes/No):");
String isNIT = scanner.next();
Student student;
if (isNIT.equalsIgnoreCase("yes")) {
student = new Student(id, name, address);
} else if (isNIT.equalsIgnoreCase("no")) {
System.out.println("Enter the college name:");
String college = scanner.next();
student = new Student(id, name, address, college);
} else {
System.out.println("Wrong Input");
return;
}
System.out.println("Student id:" + student.studentId);
System.out.println("Student name:" + student.studentName);
System.out.println("Address:" + student.studentAddress);
System.out.println("College name:" + student.collegeName);
}
}
This program first asks for the student's id, name, and address. Then it asks if the student is from NIT. If the answer is 'yes', it creates a Student object using the 3-argument constructor and sets the college name to 'NIT'. If the answer is 'no', it asks for the college name and creates a Student object using the 4-argument constructor. If the input is neither 'yes' nor 'no', it prints 'Wrong Input' and terminates the program. Finally, it prints the student's information.
Similar Questions
Fixing Error(s)In the above question, you identified the error(s) in the constructor for the University class which is a part of the code given below. How will you now fix the error(s)?class University extends School { private final String type = "University"; private boolean isPublic; public University(String name, int numStudents, boolean isPublic) { this.name = name; this.numStudents = numStudents; this.isPublic = isPublic; } public boolean getIsPublic() { return this.isPublic; }}
COMPILE TIME ERROR StudentDetails/src/UserInterface.java:16: error: constructor Student in class Student cannot be applied to given types; student = new Student(id, name, address); ^ required: int,String,String,String found: int,String,String reason: actual and formal argument lists differ in length 1 error
DebuggingLook at the code for the School and University classes given below. There is an error in the constructor for the University class. Can you identify the error?class School { private String name; private int numStudents; public School(String name, int numStudents) { this.name = name; this.numStudents = numStudents; } public String getName() { return this.name; } public int getNumStudents() { return this.numStudents; }}class University extends School { private final String type = "University"; private boolean isPublic; public University(String name, int numStudents, boolean isPublic) { this.name = name; this.numStudents = numStudents; this.isPublic = isPublic; } public boolean getIsPublic() { return this.isPublic; }}
Q1. Create an Employee class with the relevant attributes and methods in addition of thefollowing specifications: It contains a default constructor. It contains a parameterized constructor. It contains a copy constructor.Note: Each student should have different their own version and it is better to have morethe attributes and methods.Q2. Incorporate static attributes and static methods in the Employee class where required.note for chatgpt/bard : computer languages used for code is c#
How do you create a student1 object from class Student?Question 8Answera.Student student1 = new Student();b.Student student1 = new student;c.student Student1 = new Student();d.Student student1 = new student();e.Student student1 = new student[];
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.