Computer Science, asked by rockzzpoornima, 9 months ago

SRV college wants to recognize the department which has succeeded in getting the maximum number of placements for this academic year. The departments that have participated in the recruitment drive are CSE,ECE, MECH. Help the college find the department getting maximum placements. Check for all the possible output given in the sample snapshot.Write a java program for the following

Note : If any input is negative, the output should be "Input is Invalid". If all department has equal number of placements, the output should be "None of the department has got the highest placement".

Sample Input 1:

Enter the no of students placed in CSE:90
Enter the no of students placed in ECE:45
Enter the no of students placed in MECH:70
Sample Output 1:
Highest placement
CSE

Sample Input 2:
Enter the no of students placed in CSE:55
Enter the no of students placed in ECE:85
Enter the no of students placed in MECH:85
Sample Output 2:
Highest placement
ECE
MECH

Sample Input 3:
Enter the no of students placed in CSE:0
Enter the no of students placed in ECE:0
Enter the no of students placed in MECH:0
Sample Output 3:
None of the department has got the highest placement


Sample Input 4:
Enter the no of students placed in CSE:10
Enter the no of students placed in ECE:-50
Enter the no of students placed in MECH:40
Sample Output 4:
Input is Invalid

Answers

Answered by QGP
7

Max Placements - Java

We first take the user input of all the values using Scanner. We put the input part inside a try-catch block, so if the user enters a non-integer value, the exception can be caught and an appropriate error message can be printed.

Now, we check if any value is negative. If there is a negative value, we print an appropriate error message and exit the program.

Next, we check if all of the values are equal. If they are, then print the appropriate message and exit.

Now, we start by checking if any two values are equal and greater than the third. If there are, we print the corresponding Department names.

After that, we know that all values are distinct. We just check if one value is greater than the other two and print the Department name.

A few if-else statements do the trick.

The code is given below and also attached. A couple of screenshots are also attached, showing the code and a few example runs using the sample input.

 \rule{300}{1}

MaxPlacements.java

import java.util.Scanner;

public class MaxPlacements

{

   public static void main(String[] args)

   {

       int cse;

       int ece;

       int mech;

       // Try-Catch block to sanitize non-integer input

       try

       {

           // Initialize Scanner object

           Scanner sc = new Scanner(System.in);

           // Take user input

           System.out.print("Enter the no. of students placed in CSE: ");

           cse = sc.nextInt();

           System.out.print("Enter the no. of students placed in ECE: ");

           ece = sc.nextInt();

           System.out.print("Enter the no. of students placed in MECH: ");

           mech = sc.nextInt();

           sc.close();

       }

       catch (Exception e)

       {

           System.out.println("Invalid Input. Please enter integers only.");

           return;

       }

       

       // If any integer is negative, print message and exit

       if (cse < 0 || ece < 0 || mech < 0)

       {

           System.out.println("Invalid Input. Please only enter non-negative integers.");

           return;

       }

       // If all values are equal, print message and exit

       if (cse == ece && ece == mech)

       {

           System.out.println("None of the departments got the highest placement.");

           return;

       }

       System.out.println("Highest Placement:");

       

       // First, check if any two values are equal and greater than the third

       if (cse == ece && cse > mech)

       {

           System.out.println("CSE");

           System.out.println("ECE");

       }

       else if (cse == mech && cse > ece)

       {

           System.out.println("CSE");

           System.out.println("MECH");

       }

       else if (ece == mech && ece > cse)

       {

           System.out.println("ECE");

           System.out.println("MECH");

       }

       // Now, if we reached here, all values are distinct

       // Check if one value is greater than both

       else if (cse > ece && cse > mech)

       {

           System.out.println("CSE");

       }

       else if (ece > mech)

       {

           System.out.println("ECE");

       }

       else

       {

           System.out.println("MECH");

       }

   }

}

Attachments:
Similar questions