Skip to content

Instantly share code, notes, and snippets.

@naveen21553
Last active March 12, 2019 05:19
Show Gist options
  • Save naveen21553/abd376fea6b81264c4d6c724dbebc954 to your computer and use it in GitHub Desktop.
Save naveen21553/abd376fea6b81264c4d6c724dbebc954 to your computer and use it in GitHub Desktop.
This java file is solution to Core Java Assignment by UpGrad
import java.util.Scanner;
import java.util.ArrayList;
// Declaration of student type
class student
{
String name;
float GPA;
int token=-1;
void Enter(String name, float GPA, int token)
{
this.name = name;
this.GPA = GPA;
this.token = token;
}
}
public class assignment
{
public static void main(String [] args)
{
// Creating Queue using java.util.ArrayList
ArrayList<student> arr = new ArrayList<student>();
// Generating Scanner object for reading from stdin
Scanner sc = new Scanner(System.in);
int n;
// Another Scanner object to avoid possible read errors that occur while using nextLine() method after using nextInt() method
n = sc.nextInt();
Scanner s = new Scanner(System.in);
for (int i=0; i<n; i++)
{
// Scaning the line and storing it as String of Strings
String [] event = s.nextLine().split(" ");
// Event == "ENTER"
if (event[0].toUpperCase().compareTo("ENTER") == 0)
{
// Getting the properties of object
String name = event[1];
float GPA = Float.parseFloat(event[2]);
int token = Integer.parseInt(event[3]);
int l = arr.size();
// Creating new student object to add to queue
student object = new student();
object.Enter(name, GPA, token);
// Arranging new object according to priority
for (int j = 0; j < l; j++)
{
boolean b1 = arr.get(j).GPA > GPA,
b2 = arr.get(j).GPA == GPA,
b3 = arr.get(j).name.compareTo(name) < 0,
b4 = arr.get(j).name.compareTo(name) == 0,
b5 = arr.get(j).token < token;
if (b1 || (b2 && b3) || (b2 && b4 && b5))
{
arr.add(j, object);
break;
}
}
// The new element is of highest priority
if (l == arr.size())
arr.add(object);
}
// event "SERVED"
else
if(arr.size() > 0)
arr.remove(arr.size()-1);
}
// releasing Scanner objects
s.close();
sc.close();
// print the resulting queue
if (arr.size() > 0)
for (int i=arr.size()-1; i>=0; i--)
System.out.println(arr.get(i).name);
else
System.out.println("EMPTY");
}
}
In this problem, there are types of events: ENTER (a student enters the queue) or SERVED.
A unique token is assigned to any student entering the queue. The queue serves the students
based on the following criteria:
The student having the highest Cumulative Grade Point Average (CGPA) is served first.
Any students having the same CGPA will be served by name in ascending case-sensitive alphabetical order.
Any students having the same CGPA and name will be served in ascending token order.
Given a sequence of events, print the names of students who are yet to be served(based on above criteria).
If the queue is empty, print EMPTY.
Input Format
The first line of input contains an integer, , denoting the total number of events.
Each of the subsequent lines will be of the following two forms:
ENTER name CGPA token - The student to be inserted into the priority queue.
SERVED - The highest priority student in the queue was served.
Constraints
where
where each token i is a unique integer.
Output Format
Print the names (based on the criteria) of the students who are not served at all after executing all events; if every student in the queue was served, then print EMPTY.
Sample Input
12
ENTER John 3.75 50
ENTER Mark 3.8 24
ENTER Shafaet 3.7 35
SERVED
SERVED
ENTER Samiha 3.85 36
SERVED
ENTER Ashley 3.9 42
ENTER Maria 3.6 46
ENTER Anik 3.95 49
ENTER Dan 3.95 50
SERVED
Sample Output
Dan
Ashley
Shafaet
Maria
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment