Home > Java > javaTutorial > body text

Java program used to check if TPP students are eligible for interviews

王林
Release: 2023-09-06 22:33:07
forward
921 people have browsed it

Java program used to check if TPP students are eligible for interviews

Please consider the table below to know the eligibility criteria of different companies -

The Chinese translation of is:

CGPA

Grade Point Average

Eligible companies

is greater than or equal to 8

Google, Microsoft, Amazon, Dell, Intel, Wipro

is greater than or equal to 7

Tutorial point, accent, Infosys, Emicon, Rellins

is greater than or equal to 6

rtCamp、Cybertech、Skybags、Killer、Raymond

is greater than or equal to 5

Patronics, Shoes, NoBrokers

Let us enter the java program to check the eligibility of tpp students for interview.

Method 1: Use if else if condition

Usually, when we have to check multiple conditions, we use if else if statement. It follows a top-down approach.

grammar

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
}
Copy after login

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!");
      }
   }
}
Copy after login

Output

12109659 is eligible for companies: Google, Microsoft, Amazon, Capgemini, Dell, Intel, Wiproe
Copy after login

In the above code, we declare and initialize two variables named "regd" and "CGPA". When we run this code, the compiler will check the first if condition and for the given "CGPA" value, it is true. Therefore, it executes the code inside the first if block.

How to use Switch statement

The switch statement is only available for int, short, byte and char data types. It does not support decimal values. It first evaluates the expression and if any of the conditions match, it executes the block of code. If no cases match, the default case is executed.

grammar

// 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
}
Copy after login

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!");
      }
   } 
}
Copy after login

Output

12109659 is eligible for companies: rtCamp, Cybertech, Skybags, Killer, Raymond
Copy after login

In the above code, we have used the same variable again. Since switch is not compatible with double variables, we typecast it to an integer type variable named "GPA". Case 6 matches the expression for the given value of "GPA". Therefore, the compiler executes the case 6 code.

Method 3: Use user-defined method

Methods are blocks of code that can be reused multiple times to perform a single operation. It saves us time and also reduces code size.

grammar

accessSpecifier nonAccessModifier return_Type method_Name(Parameters){
   //Body of the method
}
Copy after login

accessSpecifier - used to set the accessibility of a method. It can be public, protected, default, and private.

nonAccessModifier - It shows additional functionality or behavior of a method such as static and final.

return_Type − The data type that the method will return. We use void keyword when the method does not return anything.

method_Name - The name of the method.

Parameters - It contains the variable name followed by the data type.

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);
   }
}
Copy after login

Output

12109659 is eligible for companies: Tutorials point, accenture, Infosys, Emicon, Rellins
Copy after login

The logic of the above program is the same as the first program we discussed in this article. The main difference is that we created a user-defined method called "eligible()" with two parameters "regd" and "CGPA" and we called that method with two parameters in the main method.

in conclusion

In this article, we have discussed three java program methods for checking whether a tpp student is eligible for interview. We saw the use of if else if condition and switch statement. We also created a user-defined method for a given problem.

The above is the detailed content of Java program used to check if TPP students are eligible for interviews. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template