- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to check Eligibility of TPP students for appearing in Interviews
Consider the following table for eligibility criteria of different companies −
CGPA |
Eligible Company |
---|---|
Greater than or equal to 8 |
Google, Microsoft, Amazon, Dell, Intel, Wipro |
Greater than or equal to 7 |
Tutorials point, accenture, Infosys, Emicon, Rellins |
Greater than or equal to 6 |
rtCamp, Cybertech, Skybags, Killer, Raymond |
Greater than or equal to 5 |
Patronics, Bata, Nobroker |
Let’s jump into the java program to check eligibility of tpp students for appearing in interviews.
Approach 1: Using if else if condition
Generally, we use if else if statements when we have to check multiple conditions. It follows top down approach.
Syntax
if(condition 1) { // code will be executed only when condition 1 is true } else if(condition 2) { // code will be executed only when condition 2 is true } else { // code will be executed when all of the above condition is false }
Example
public class Eligible { public static void main(String[] args) { int regd = 12109659; double CGPA = 8.08; if( CGPA >= 8 ) { System.out.println(regd + " is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wipro"); } else if(CGPA >= 7) { System.out.println(regd + " is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins"); } else if(CGPA >= 6) { System.out.println(regd + " is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond"); } else if( CGPA >= 5 ) { System.out.println(regd + " is eligible for companies: Patronics, Bata, Nobroker"); } else { System.out.println("Improve yourself!"); } } }
Output
12109659 is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wiproe
In the above code, we have declared and initialized two variables named ‘regd’ and ‘CGPA’. When we run this code compiler will check the 1st if condition and for the given value of ‘CGPA’, it is true. Therefore, it has executed the code inside 1st if block.
Approach 2: Using Switch Statements
The switch statement works only with int, short, byte and char datatype only. It doesn’t support decimal values. It first evaluates the expression and if any of the cases matches then, it will execute that block of code. If none of the cases matched then, default case gets executed.
Syntax
// expression and value must be of same datatype switch(expression) { case value: // code will be executed only when the expression and case value matched break; case value: // code will be executed only when the expression and case value matched break; . . . case value n: // n is required number of value default: // If none of the case matched then it will be executed }
Example
public class Main { public static void main(String[] args){ int regd = 12109659; double CGPA = 6.55; int GPA = (int) CGPA; // typecasting double to integer type switch(GPA){ // here GPA = 6 case 10: case 9: case 8: System.out.println(regd + " is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wipro"); break; case 7: System.out.println(regd + " is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins"); break; case 6: System.out.println(regd + " is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond"); break; case 5: System.out.println(regd + " is eligible for companies: Patronics, Bata, Nobroker"); break; default: System.out.println("Improve yourself!"); } } }
Output
12109659 is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond
In the above code, we have taken the same variables again. Since switch is not compatible with double variable we have type-casted it into integer type variable named ‘GPA’. For the given value of ‘GPA’, case 6 matched with expression. Hence, compiler has executed case 6 code.
Approach 3: Using user-defined method
Methods are block of code that can be reused multiple times to perform a single operation. It saves our time and also reduces the size of code.
Syntax
accessSpecifier nonAccessModifier return_Type method_Name(Parameters){ //Body of the method }
accessSpecifier − It is used to set the accessibility of the method. It may be public, protected, default and private.
nonAccessModifier − It shows additional functionalities or behaviour of the method such as static and final.
return_Type − The datatype a method is going to return. We use void keyword when method does not return anything.
method_Name − Name of the method.
parameters − It contains name of the variable followed by datatype.
Example
public class Main { public static void eligible(int regd, double CGPA){ if(CGPA >= 8){ System.out.println(regd + " is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wipro"); } else if(CGPA >= 7){ System.out.println(regd + " is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins"); } else if(CGPA >= 6){ System.out.println(regd + " is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond"); } else if(CGPA >= 5){ System.out.println(regd + " is eligible for companies: Patronics, Bata, Nobroker"); } else { System.out.println("Improve yourself!"); } } public static void main(String[] args){ eligible(12109659, 7.89); } }
Output
12109659 is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins
The logic for the above program is same as 1st program we have discussed in this article. The main differences are we have created a user-defined method named ‘eligible()’ with two parameters ‘regd’ and ‘CGPA’ and we have called this method with two arguments in main method.
Conclusion
In this article, we have discussed three approaches to the java program to check eligibility of tpp students for appearing in interviews. We have seen the use of if else if condition and switch statements. We have also created a user-defined method for the given problem.