Create a class “StudentName” with a main function. Take a variable of integer data type from user and store its value into variable name “rollno”. Print the name of specific student using switch statements.
Hint: Use 4 different cases and one default. The value of cases should be
Case 10
Case 11
Case 12
Case 13
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
package com.company;
import java.util.Scanner;
class StudentName{
private int rollno;
StudentName(int rollno)
{
this.rollno = rollno;
}
public void student_name_given_rollno()
{
switch(rollno) {
case 10:
System.out.println("Student name is Sylencer");
break;
case 11:
System.out.println("Student name is Daniel");
break;
case 12:
System.out.println("Student name is Nyamai");
break;
case 13:
System.out.println("Student name is kelly");
break;
default:
System.out.println("You have entered an invalid roll number");
}
}
}
//now this is our main class
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the roll number of the student: ");
int rollno = sc.nextInt();
//create an object of the studentName class
StudentName my_student = new StudentName(rollno);
//call the function to tell the name of the student
my_student.student_name_given_rollno();
}
}
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment