question archive Create class Student with the private attributes int studentId String studentName, studentAddress, collegeName

Create class Student with the private attributes int studentId String studentName, studentAddress, collegeName

Subject:Computer SciencePrice:4.87 Bought7

Create class Student with the private attributes int studentId String studentName, studentAddress, collegeName. 

Include appropriate getter methods.

Assume most of the students are from "NIT" college. So user has to give input whether the student is from NIT or not. 

  1. If 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".
  2. If 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. 
  3. Instead of Yes / No, if user enters different input then display 'Wrong Input' and get the input again.

Based on the above assumptions write the necessary constructors in the Student class.

Write a class StudentMain with the main method and test the application.  

Sample Input 1:

Enter Student's Id:

12

Enter Student's Name:

John

Enter Student's address:

Chennai

Whether the student is from NIT(Yes/No):

NO

Enter the college name:

SVS

Sample Output 1:

Student id:12

Student name:John

Address:Chennai

College name:SVS

Sample Input 2:

Enter Student's Id:

43

Enter Student's Name:

Tom

Enter Student's address:

Coimbatore

Whether the student is from NIT(Yes/No):

y

Wrong Input

Whether the student is from NIT(Yes/No):

yes

Sample Output 2:

Student id:43

Student name:Tom

Address:Coimbatore

College name:NIT

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:
public class Student {
    private int studentId;
    private String studentName;
    private  String studentAddress;
    private String collegeName;

    public Student(int studentId, String studentName, String studentAddress) {
        this.studentId = studentId;
        this.studentName = studentName;
        this.studentAddress = studentAddress;
        this.collegeName = "NIT";
    }

    public Student(int studentId, String studentName, String studentAddress, String collegeName) {
        this.studentId = studentId;
        this.studentName = studentName;
        this.studentAddress = studentAddress;
        this.collegeName = collegeName;
    }

    public int getStudentId() {
        return studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public String getStudentAddress() {
        return studentAddress;
    }

    public String getCollegeName() {
        return collegeName;
    }
}

 

 

import java.util.Scanner;

public class StudentMain {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Student id ");
        int id = sc.nextInt();
        sc.nextLine();
        System.out.println("Enter Student Name ");
        String name = sc.nextLine();
        System.out.println("Enter Student Address ");
        String address = sc.nextLine();
        boolean inValid = true;
        String collegeName = "";
        String choice = "";
        Student student = null;
        while (inValid) {
            System.out.println("Whether the student is from NIT(Yes/No): ");
            choice = sc.nextLine();
            if (choice.equalsIgnoreCase("YES") || choice.equalsIgnoreCase("NO")) {
                inValid = false;
                if (choice.equalsIgnoreCase("YES")) {
                     student = new Student(id,name,address);
                     break;
                }
                else {
                    System.out.println("Enter College Name ");
                    collegeName = sc.nextLine();
                    student = new Student(id,name,address,collegeName);
                    break;
                }
            }
            else {
                System.out.println("Wrong Input");
                continue;
            }

        }
        System.out.println("Student ID : "+student.getStudentId());
        System.out.println("Student Name : "+student.getStudentName());
        System.out.println("Student Address : "+student.getStudentAddress());
        System.out.println("Student College Name : "+student.getCollegeName());
    }
}

Step-by-step explanation

Enter Student id 

12

Enter Student Name 

John

Enter Student Address 

Chennai

Whether the student is from NIT(Yes/No): 

no

Enter College Name 

SVS

Student ID : 12

Student Name : John

Student Address : Chennai

Student College Name : SVS

Enter Student id 

43

Enter Student Name 

Tom

Enter Student Address 

Coimbatore

Whether the student is from NIT(Yes/No): 

y

Wrong Input

Whether the student is from NIT(Yes/No): 

yes

Student ID : 43

Student Name : Tom

Student Address : Coimbatore

Student College Name : NIT