CSC 1311 – Computer Programming II Test 4 Spring 2018 Please save your project i
ID: 3712309 • Letter: C
Question
CSC 1311 – Computer Programming II Test 4 Spring 2018 Please save your project in the following format: LastnameTest4 for example CaldwellTest4 The Caldwell Robotics Corporation(CRC) has decided to offer Merit Scholarships for Computer Science and Information Technology Majors Only for $10,000. The requirements to be eligible for the scholarships are as follows: Scholarship Requirements 1. Require ACT Score of 31 or above on the ACT and total SAT Score of 1300 or above on the combined Evidence Based Reading/Writing and Math 2. Classification must be a Freshman (1) or Sophomore (2). 3. Must have a GPA of 3.6 or above. The data has been stored in a text as follows in textfile named Applied.txt. The file consists of Name – String Major - String GPA – Double ACT Score – Integer SAT Reading/Writing Score – Integer SAT Math Score – Integer Classification – 1(Freshman), 2(Sophomore), 3(Junior), 4(Senior) The data has been stored in the following format. Name Major GPA ACT Score SATReadingWritingScore SATMATH Classification Sample line of data: BuxleyB MAT 4.0 36 500 800 1 Complete the following tasks using the java programming language.
Task 1: Read and display the data in the file. (Demonstration class) (15 points)
Task 2: Display the names, GPA, ACT and total SAT scores. (Working Class/method) (25 points)
task 3: Display the names (alphabetically) of all eligible scholarship recipients with major and GPAs. (Working Class/method) (20 points)
Task 4: Display the average ACT and SAT scores (10 points)
Task 5: Display the total amount that will be awarded in scholarships. (Working Class/method)
(10 points) Other grading areas: (20 points) Formatted output (5 points) Documentation (5 points) Use of programming structures (10 points – variable declarations, loops, arrays, decision, classes)
Explanation / Answer
import java.io.*;
import java.util.*;
// Class StudentInformation
class StudentInformation
{
// Instance variables to store data
String name;
String Majo;
double GPA;
int ACTScore;
int SATReadingOrWritingScore;
int totalSAT;
int SATMathScore;
int classification;
String ClassificationType [] = {"Freshman", "Sophomore", "Junior", "Senior"};
}// End of class
// Class MeritScholarships definition
class MeritScholarships
{
// Instance variables to store data
int recordCounter;
int totACT, totSAT;
int totalAmount;
double averageACT, averageSAT;
// Declares an array of object of class StudentInformation
StudentInformation student[];
// Method to read data from file
void readFile()
{
// Record counter is initialized to zero
recordCounter = 0;
totACT = 0;
totSAT = 0;
// try block begins
try
{
// Scanner class object created to read data from file
Scanner fileRead = new Scanner(new File("Applied.txt"));
// Loops till end of the file
while(fileRead.hasNextLine())
{
// Reads data
fileRead.nextLine();
// Increase the record counter by one
recordCounter++;
}// End of while loop
// Dynamically allocates number of records to student array of object of size recored counter
student = new StudentInformation[recordCounter];
// Loops till number of records
for(int c = 0; c < recordCounter; c++)
// Dynamically allocates memory to each object
student[c] = new StudentInformation();
// Close the file
fileRead.close();
// Re-opens the file for reading data from file
fileRead = new Scanner(new File("Applied.txt"));
// Re initializes the record counter to zero
recordCounter = 0;
// Loops till end of the file
while(fileRead.hasNextLine())
{
// Reads data from file and stores at student recordCounter index position data member
student[recordCounter].name = fileRead.next();
student[recordCounter].Majo = fileRead.next();
student[recordCounter].GPA = fileRead.nextDouble();
student[recordCounter].ACTScore = fileRead.nextInt();
student[recordCounter].SATReadingOrWritingScore = fileRead.nextInt();
student[recordCounter].SATMathScore = fileRead.nextInt();
student[recordCounter].classification = fileRead.nextInt();
// Calculates total SAT score
student[recordCounter].totalSAT = (student[recordCounter].SATReadingOrWritingScore + student[recordCounter].SATMathScore);
// Calculates grand total of ACT score
totACT += student[recordCounter].ACTScore;
// Calculates grand total of SAT score
totSAT += student[recordCounter].totalSAT;
// Increase the record counter by one
recordCounter++;
}// End of while loop
// Close the file
fileRead.close();
}// End of try block
// Catch block to handle FileNotFoundException exception
catch(FileNotFoundException fe)
{
System.out.println("Cannot open the file for reading.");
}// End of catch
}// End of method
// Method to display all the student information
void displayInformation()
{
// Loops till number of records
for(int c = 0; c < recordCounter; c++)
{
System.out.printf(" Name: %s GPA = %.2f ACT = %d Total SAT = %d ",
student[c].name, student[c].GPA, student[c].ACTScore, student[c].totalSAT);
}// End of for loop
}// End of method
// Method to display merit list information
void displayMeritList()
{
/* Requirements 1. Require ACT Score of 31 or above on the ACT and total SAT Score of 1300
* or above on the combined Evidence Based Reading/Writing and Math
* 2. Classification must be a Freshman (1) or Sophomore (2).
* 3. Must have a GPA of 3.6 or above.
*/
String scholarName[] = new String[recordCounter];
String scholarMajor[] = new String[recordCounter];
double scholarGPA[] = new double[recordCounter];
int scholarsCounter = 0;
String tempName, tempMajor;
double tempGPA;
// Loops till number of records
for(int c = 0; c < recordCounter; c++)
{
// Checks if the current student ACT score is greater than or equals to 31
// and totalSAT is greater than or equals to 1300
if(student[c].ACTScore >= 31 && student[c].totalSAT >= 1300)
{
// Checks if the current student classification is either one or two
if(student[c].classification == 1 || student[c].classification == 2)
{
// Checks if the current student GPA score is greater than or equals to 3.6
if(student[c].GPA >= 3.6)
{
// Stores the scholar name, GPA and Major
scholarName[scholarsCounter] = student[c].name;
scholarGPA[scholarsCounter] = student[c].GPA;
scholarMajor[scholarsCounter] = student[c].Majo;
// Increase the scholar counter by one
scholarsCounter++;
}// End of if condition for GPA
}// End of if condition for classification
}// End of if condition for ACT
}// End of for loop
// Sorting process
// Loops till number of scholars
for(int x = 0; x < scholarsCounter; x++)
{
// Loops till number of records minus outer for loop variable value minus one
// Because after each iteration one record is sorted
for(int y = 0; y < scholarsCounter - x - 1; y++)
{
// Checks if the current scholar name is greater than the next scholar name
if(scholarName[y].compareTo(scholarName[y + 1]) > 0)
{
// Swapping name
tempName = scholarName[y];
scholarName[y] = scholarName[y + 1];
scholarName[y + 1] = tempName;
// Swapping GPA
tempGPA = scholarGPA[y];
scholarGPA[y] = scholarGPA[y + 1];
scholarGPA[y + 1] = tempGPA;
// Swapping Major
tempMajor = scholarMajor[y];
scholarMajor[y] = scholarMajor[y + 1];
scholarMajor[y + 1] = tempMajor;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
// Displaying scholar information
System.out.print(" All eligible scholarship recipients ");
// Loops till number of scholars
for(int c = 0; c < scholarsCounter; c++)
{
System.out.printf(" Name: %s Major = %s GPA = %.2f ",
scholarName[c], scholarMajor[c], scholarGPA[c]);
totalAmount += 10000;
}// End of for loop
// Calculates the average ACT score
averageACT = (double)totACT / recordCounter;
// Calculates the average SAT score
averageSAT = (double)totSAT / recordCounter;
// Displays the averages
System.out.printf(" Average ACT: %.2f Average SAT = %.2f ",
averageACT, averageSAT);
// Displays the total amount for scholarship
System.out.printf(" Total amount that will be awarded in scholarships = %d", totalAmount);
}// End of method
}// End of class
// Driver class MeritScholarshipsDemo definition
public class MeritScholarshipsDemo
{
// main method definition
public static void main(String[] args)
{
// Creates an object of MeritScholarships class
MeritScholarships ms = new MeritScholarships();
// Calls the method to read the file contents and stores it
ms.readFile();
// Calls the method to display all student information
ms.displayInformation();
// Calls the method to display merit list information
ms.displayMeritList();
}// End of main method
}// End of driver class
Sample Output:
Name: BuxleyB
GPA = 4.00
ACT = 36
Total SAT = 1300
Name: Suresh
GPA = 2.00
ACT = 40
Total SAT = 1450
Name: Ramesh
GPA = 5.00
ACT = 30
Total SAT = 1450
Name: Sita
GPA = 5.00
ACT = 37
Total SAT = 1250
Name: Gita
GPA = 4.50
ACT = 38
Total SAT = 1550
Name: Mohan
GPA = 3.90
ACT = 39
Total SAT = 1400
All eligible scholarship recipients
Name: BuxleyB
Major = MAT GPA = 4.00
Name: Mohan
Major = MAT GPA = 3.90
Average ACT: 36.67
Average SAT = 1400.00
Total amount that will be awarded in scholarships = 20000