Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*USE JAVA (THIS IS A GPA CALCULATOR AND SHOULD BE ABLE TO HANDLE MULTIPLE CLIENT

ID: 3732366 • Letter: #

Question

*USE JAVA (THIS IS A GPA CALCULATOR AND SHOULD BE ABLE TO HANDLE MULTIPLE CLIENTS AT THE SAME TIME)

For this project you will design a system to allow various users (unknown number connecting at any one time) to access a grade point calculation service on a "server". This service will allow users to either create an account or login to their previously created account. Then the user will need to complete the following items:

Add a Course Grade

Remove a Course Grade

Update a Course Grade

Get their GPA

This project must be completed in the Client Server architecture (you do not have to worry about persistance, but you do need a stateful server) Your program should handle many client requests at once.

As usual submit all source code files along with a ReadMe.txt to enable me to run your Server and how to run 1+ clients.

*USE JAVA (THIS IS A GPA CALCULATOR AND SHOULD BE ABLE TO HANDLE MULTIPLE CLIENTS AT THE SAME TIME)

Explanation / Answer

** Calculate GPA

import java.util.Scanner;

import java.util.List;

import java.util.ArrayList;

public class CalculateGPA

{

               

    public static void main(String[] args)

    {

                                boolean is_stop = false;

        Scanner in = new Scanner(System.in);

        List<Course> objects = new ArrayList<Course>();

                                int course_id, credits;

                                String course_name, grade;

                               

                                while(!is_stop) {

                                                System.out.println(" ------------------------------" );

                                                System.out.println("Choose Option : " );

                                                System.out.println("1. Add a Course Grade" );

                                                System.out.println("2. Remove a Course Grade" );

                                                System.out.println("3. Update a Course Grade" );

                                                System.out.println("4. Get their GPA" );

                                                int option = in.nextInt();

                                               

                                                switch(option) {

                                                                case 1:

                                                                                System.out.println("Course Id : " );

                                                                                course_id = in.nextInt();

                                                                                System.out.println("Course Name : " );

                                                                                course_name = in.next();

                                                                                System.out.println("Grade : " );

                                                                                grade = in.next();

                                                                                System.out.println("Credits : " );

                                                                                credits = in.nextInt();

                                                                               

                                                                                objects.add(new Course(course_id, course_name, grade, credits));

                                                                               

                                                                                System.out.println("Total Courses : " + objects.size() );

                                                                                printCourse(objects);

                                                                               

                                                                                break;

                                                                case 2:

                                                                                System.out.println("Course Id : " );

                                                                                course_id = in.nextInt();

                                                                               

                                                                                removeCourse(course_id, objects);

                                                                                printCourse(objects);

                                                                                break;

                                                                case 3:

                                                                                System.out.println("Course Id : " );

                                                                                course_id = in.nextInt();

                                                                                System.out.println("Course Name : " );

                                                                                course_name = in.next();

                                                                                System.out.println("Grade : " );

                                                                                grade = in.next();

                                                                                System.out.println("Credits : " );

                                                                                credits = in.nextInt();

                                                                               

                                                                                updateCourse(new Course(course_id, course_name, grade, credits), objects);

                                                                                printCourse(objects);

                                                                                break;

                                                                case 4:

                                                               

                                                                                calculateGPA(objects);

                                                               

                                                                                break;

                                                                default:

                                                                                is_stop = true;

                                                                                break;

                                                }

                                               

                                }

                               

    }

               

                public static void printCourse(List<Course> objects) {

                                for(int i=0; i<objects.size(); i++) {

                                                System.out.println(objects.get(i).getCourse_id() + " | " +

                                                                                                                objects.get(i).getCourse_name() + " | " +

                                                                                                                objects.get(i).getGrade() + " | " +

                                                                                                                objects.get(i).getCredits());

                                }

                }

               

                public static void calculateGPA(List<Course> objects) {

                                double gradeValue = 0;

                                double total_points = 0;

                                double gpa = 0;

                                int total_credits = 0;

                                boolean is_valid = true;

                               

                                for(int i=0; i<objects.size(); i++) {

                                                String grade = objects.get(i).getGrade();

                                                if (grade.equals ("A") || grade.equals ("A+"))

                                                                gradeValue= 4.00;

                                                else if (grade.equals("A-"))

                                                                gradeValue= 3.7;

                                                else if (grade.equals("B+"))

                                                                gradeValue = 3.3;

                                                else if (grade.equals("B"))

                                                                gradeValue = 3.0;

                                                else if (grade.equals ("B-"))

                                                                gradeValue = 2.7;

                                                else if (grade.equals("C+"))

                                                                gradeValue = 2.3;

                                                else if (grade.equals("C"))

                                                                gradeValue = 2.0;

                                                else if (grade.equals("C-"))

                                                                gradeValue = 1.7;

                                                else if (grade.equals ("D+"))

                                                                gradeValue = 1.3;

                                                else if (grade.equals ("D"))

                                                                gradeValue = 1.0;

                                                else if (grade.equals ("D-"))

                                                                gradeValue = 0.7;

                                                else if (grade.equals ("F"))

                                                                gradeValue = 0.0;

                                                else if (grade.equals ("FX"))

                                                                gradeValue = 0.0;

                                                else {

                                                                System.out.println ("Invalid Grade");

                                                                is_valid = false;

                                                                break;

                                                }

                                                total_points += (gradeValue * objects.get(i).getCredits());

                                                total_credits += objects.get(i).getCredits();

                                }

                                if(is_valid) {

                                                gpa= total_points / total_credits;

                                                printCourse(objects);

                                                System.out.printf(" Your GPA is: %.2f ", + gpa);

                                }

                }

               

                public static void removeCourse(int course_id, List<Course> objects) {

                                boolean is_remove = false;

                                for(int i=0; i<objects.size(); i++) {

                                                if(objects.get(i).getCourse_id() == course_id) {

                                                                objects.remove(i);

                                                                is_remove = true;

                                                                break;

                                                }

                                }

                               

                                if(is_remove)

                                                System.out.println("Total Courses : " + objects.size() );

                                else

                                                System.out.println("Course not found with this id...");

                }

               

                public static void updateCourse(Course course, List<Course> objects) {

                                boolean is_update = false;

                                for(int i=0; i<objects.size(); i++) {

                                                if(objects.get(i).getCourse_id() == course.getCourse_id()) {

                                                                objects.set(i, course);

                                                                is_update = true;

                                                                break;

                                                }

                                }

                               

                                if(is_update)

                                                System.out.println("Course Updated");

                                else

                                                System.out.println("Course not found with this id...");

                }

}

** Socket Connection

import java.io.*;

import java.net.*;

public class MyServer {

                public static void main(String[] args){

                                try{

                                                ServerSocket ss = new ServerSocket(6666);

                                                Socket s = ss.accept();//establishes connection  

                                                DataInputStream dis = new DataInputStream(s.getInputStream());

                                                String str = (String)dis.readUTF();

                                                System.out.println("" + str);

                                               

                                               

                                               

                                                ss.close();

                                }catch(Exception e){ System.out.println(e); }

                }

}

import java.io.*;

import java.net.*;

public class MyClient {

                public static void main(String[] args) {

                                try{     

                                                Socket s=new Socket("localhost",6666);

                                                DataOutputStream dout=new DataOutputStream(s.getOutputStream());

                                                dout.writeUTF("connect");

                                                dout.flush();

                                                dout.close();

                                                s.close();

                                }catch(Exception e){System.out.println(e);}

                }

}

** Output ::

------------------------------
Choose Option :
1. Add a Course Grade
2. Remove a Course Grade
3. Update a Course Grade
4. Get their GPA
4


1 | c1 | A | 3
2 | c2 | A- | 3
3 | c3 | B | 2

Your GPA is: 3.64