Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > C# 2012/5.0 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2012/5.0 General Discussion Discussions about the C# 2012 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2012/5.0 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 22nd, 2014, 11:47 AM
Registered User
Points: 19, Level: 1
Points: 19, Level: 1 Points: 19, Level: 1 Points: 19, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default GPA Calculator Final Method

Hello all, plz am actually working on a project to develop a GPA Calculator but am kinda stuck at the moment. Am tryin to come up with a Method that will compute the GPA of a student according to their courses grades and the corresponding credit units.

Below here is the source code of this project:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GPA_Calculator
{
  class Program
    {
        public static int CSCS101, BIO101, MAT101 = 3;
        static int ICH101 = 2;
        static int GSS101 = 1;
        public static int a, b, c, d, e, f;
        static string studentName;
        static float GPA;
        static int regNo;
        public static int csc, bio, mat, ich, gss;

        public static void StudentName()
        {
            studentName = Console.ReadLine();
        }
        public static void RegNo()
        {
            regNo = int.Parse(Console.ReadLine());
        }
        public static void ComputeGPA()
        {
            GPA = 
            Console.WriteLine("Thank You {0} For Using Our GPA Calculator. Hence, Your Overall GPA is: {1) ", studentName, GPA);
        }
    }

    class CourseGrades:Program
    { 
        public static void CSC101_Grade(int csc)
        {

            if (csc >= 70 && csc <= 100)
            {
                 a = 5;
                Console.WriteLine("Grade: A");
            }
            else if (csc <= 69 && csc >= 60)
            {
                b = 4;
                Console.WriteLine("Grade: B");
            }
            else if (csc >= 50 && csc <=59)
            {
                c = 3;
              Console.WriteLine("Grade: C");
            }
            else if (csc >=45 && csc <= 49)
            {
                d = 2;
                Console.WriteLine("Grade: D");
            }
            else if (csc >=40 && csc <= 44)
            {
                e = 1;
                Console.WriteLine("Grade: E");
            }
            else if (csc < 40 && csc >= 0)
            {
                f = 0;
                Console.WriteLine("Grade: F");
            }            
            else 
            {
                Console.WriteLine("Please Enter Your Score");
            }
        }
        public static void BIO101_Grade()
        {
            CSC101_Grade(bio);
        }
        public static void MAT101_Grade()
        {
            CSC101_Grade(mat);
        }
        public static void ICH101_Grade()
        {
            CSC101_Grade(ich);
        }
        public static void GSS101_Grade()
        {
            CSC101_Grade(gss);
        }


        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter Your Name");
            StudentName();
            Console.WriteLine("Please Enter Your Reg No");
            RegNo();
            Console.WriteLine("Enter Your CSC 101 Score");
            csc = int.Parse(Console.ReadLine());
            CSC101_Grade(csc);
            Console.WriteLine("Enter Your Mat 101 Score");
            mat = int.Parse(Console.ReadLine());
            MAT101_Grade();
            Console.WriteLine("Enter Your Bio 101 Score");
            bio = int.Parse(Console.ReadLine());
            BIO101_Grade();
            Console.WriteLine("Enter Your ICH 101 Score");
            ich = int.Parse(Console.ReadLine());
            ICH101_Grade();
            Console.WriteLine("Enter Your GSS 101 Score");
            gss = int.Parse(Console.ReadLine());
            GSS101_Grade();
            ComputeGPA();
            Console.ReadKey();
        }
       
       
    }
}
As you can see in the above code, am yet to complete the code for the ComputeGPA method - am stuck here. Plz any help will be very useful as I need to submit this project on Monday. Thanks all.

Note: the credit unit for the courses are CSC101 = 3, BIO101 = 3, MAT101 =3, ICH101 = 2, GSS101 = 1. The grades points are A = 5, B = 4, C = 3, D = 2, E = 1, F = 0. Also, the formula to calculate the GPA is: credit units * the corresponding grade points / sum of credit units.

Thanks once again.
Reply With Quote
  #2 (permalink)  
Old March 23rd, 2014, 04:58 PM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

I am not sure what functional problem you are having with your code because you have not mentioned anything about errors. What i do see some design practice issues, and i am thinking this is a class assingment. When you design your slns you to need to approach them in oop fashion.

What I will help you with is to define a base set of code that you can finish.
In your GPA calcualtor I see at a minimum for 2 classes and an enum that can be defined solve your problem Student, Course, and Grade (i will leave you to define the Grade enum).


Course.cs
Code:
 public class Course
    {
        private string m_Name;
        private int m_CreditHours = 0;
        private double m_Score = 0.0;

        public string Name { get { return m_Name; } }
        public int CreditHours { get { return m_CreditHours; } }

        public Course( string courseName, int creditHours )
        {
            m_Name = courseName;
            m_CreditHours = creditHours;
        }

       public double Score 
         { 
             get { return m_Score; } 
             set { m_Score = value; }
         }

       
        public Grade Grade 
        {
             // using the static method to convert a score to a grade
             // since it is static it must be referenced by the class name
             get { return Course.GetGrade(this.Score); }
         }

        /// <summary>
        /// We declare this method static so that it can be used without 
        /// creating an instance of the class
        /// </summary>
        public static Grade GetGrade( double score )
        {
            // if score is greater than the max score of B range 
            // then we return an A
            if (score > 89.9) 
                return Grade.A;

            // if score is greater than the max score of C range 
            // then we return an B
            if (score > 79.9)
                return Grade.B;

            // if score is greater than the max score of D range 
            // then we return an C
            if (score > 69.9)
                return Grade.C;

            // if score is greater than the max score of E range 
            // then we return an D
            if (score > 59.9)
                return Grade.D;

            // if score is greater than the max score of F range 
            // then we return an E
            if (score > 49.9)
                return Grade.E;

            // if no other if block was fired then all that is left is an F
            return Grade.F;
        }

}
Student.cs
Code:
 public class Student
    {
        private List<Course> m_RegisteredCourses = new List<Course>(); 

        public string Name { get; set; }
        public int RegistrationNumber {get; set;}
        public List<Course> RegisteredCourses { get { return m_RegisteredCourses; } }

        public Student()
        {
        }

        public Student( string name, int registrationNumber )
        {
            // code to init the student object 
        }        

        public Course RegisterCourse( String courseName, int creditHours )
        {
            // code to register a course

        }
       
       public Course GetCourse(String courseName)
        {
            Course rVal = null;

            // find the course that we want 
            foreach (var c in m_RegisteredCourses)
            {
                if (c.Name.Equals(courseName, StringComparison.CurrentCultureIgnoreCase))
                {
                    rVal = c;

                    // we have found the course so we dont need
                    // to continue to iterate through the course list
                    // so we break out of the loop
                    break;
                }
            }

            return rVal;
        }

        public void SetScore(string courseName, double score)
        {
            var course = GetCourse( courseName );

            // check to see if we have course
            // if we dont we can either raise an exception
            // or do nothing and return, typically 
            // we would want to raise an exception
            if (course == null)
                throw new InvalidOperationException(String.Format("Course {0} not found", courseName));


            // if we make this far a course was found and 
           //an exception was not raised so we set the score of the corse
            course.Score = score;
        }

        public double GetScore(string courseName)
        {
            Course course = null;

            // code to get a course

            
            //return the course score
           return course.Score;
        }

        public Grade GetGrade(string courseName)
        {
            Course course = null;

            // code to get and returning course grade

            
        }

        public Double GetGPA()
        {
            var rVal = 0.0;


            // caluclate gpa and set rVal


            return rVal;
        }       
    }
Program.cs
Code:
public class Program
    {
        static void Main(string args)
        {
            var student = GetStudent();
            RegisterClasses(student);
            GetClassScores(student);
            DisplayClassGrades(student);
            DisplayGPA(student);
        }

        private static void DisplayClassGrades(Student student)
        {
           // code to display class grades for student
        }       

        private static void DisplayGPA(Student student)
        {
            Console.WriteLine("Your Grade point average is {0}", student.GetGPA());
        }

        private static void GetClassScores(Student student)
        {
            // get student course scores 
            foreach (var course in student.RegisteredCourses)
                GetScore(course);
        }

        private static void GetScore(Course course)
        {
           // console interaction to get course score
           // generic enough to handle any registered class for a given student
            Console.Write( "Please enter your score for {0}:", course.Name );

            // score capture code here
        }

        private static void RegisterClasses(Student student)
        {
            // register all student classes
            student.RegisterCourse("CSC101", 3);
            student.RegisterCourse("BIO101 ", 3);
            student.RegisterCourse("MAT101", 3);
        }

        private static Student GetStudent()
        {
            var rVal = new Student();

            // code to get student
            return rVal;
        }        
    }

Last edited by mmorgan30; March 24th, 2014 at 12:28 AM.
Reply With Quote
  #3 (permalink)  
Old March 23rd, 2014, 05:46 PM
Registered User
Points: 19, Level: 1
Points: 19, Level: 1 Points: 19, Level: 1 Points: 19, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks so much Mmorgan, I really appreciate ur effort. But to be honest with you, I seem can't understand hw ur code works - am a beginner and ur code looks too advance. So, I think I should better explain clearly what the GPA Calculator Project is all about, then you could probably complete your code and let's see it it works.
The GPA calculator computes the grades and GPA of Students given their individual scores in five courses with course codes and credit units as given below:

CSC101, 3units.
BIO101, 3units.
MAT101, 3units.
ICH101, 2units.
GSS101, 1unit.

Also below is a possible test data:
Name: IsoftTech.
RegNo: 111233.
CSC101 Score: 76
BIO101 Score: 56
MAT101 Score: 85
ICH101 Score: 34
GSS101 Score: 79

Thanks.
Reply With Quote
  #4 (permalink)  
Old March 23rd, 2014, 09:30 PM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

i have updated the above code blocks and removed some more advanced topics such as Linq and added some comments. However classes and oop (object oriented programming ) are programming fundementals in programming Languages like C#.

As an overview to help you more

The Program class is the main entry point of the executeable, not because it called program but because it contains the static method main. It also control all interaction between the user and the console.

The Student class is container for courses and relates a sudent to courses and grades. It has methods that can register a course with the student (register is just a fancy word for adding, so look at what data structure is being used and go from there), set a courses score, and calcuate a students GPA, along with a few other supporting methods.

The Course class is responible for encapsulating data about course, such as the name, credithours, with a few others. It is responsible of converting a score into a grade.

The Grade is an enum (short for enumeration) is similar to a class is a perfect mechinism for representing a grade. Please do some reading on enums this is not an advance concept and is typical convered in programming 101.


I dont want to finish the assignement for you but look at what i laid out for you and with alittle thought you could finish this in just a day or so.

If there is something in here that you are not following just ask for some more explanation, no one in the forums want to write your home work for you but we will mentor and answer questions if asked.

Last edited by mmorgan30; March 23rd, 2014 at 09:55 PM.
Reply With Quote
  #5 (permalink)  
Old June 14th, 2018, 05:40 AM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2018
Location: 4505 Waldeck Street Fort Worth, TX 76112
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In actual it is very simple and easy to worked out on a project to develop a GPA Calculator,don't be depressed or guilty.I'll tell you how to computed the GPA on the courses grades.I mentioned below a code hope that helped you !

include <iosrtream>

using namespace std;

int main()
{
int grades[30]
float gpa;
int i = 0;
int i2 = 0;
int check = 0;
while(i < 30){
scanf("%d", &grades[i]);
printf("do you want to see your gpa? hit 1 to calculate it now: \n")
scanf("%d", &check);
if(check == 1){
i2 = 0;
while(i2 < i){
gpa = gpa + grades[i2];
i2++;
}
gpa = gpa / i2;
printf("%d", gpa);
}
i++
}
return 0;
}
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Keyword final Leville Java Basics 4 September 19th, 2008 09:35 PM
GPA Calculator xenoalien C# 1 November 30th, 2007 02:25 AM
XML final output sanek55555 XSLT 8 November 2nd, 2006 03:12 AM
Blank Final Consigliori Java Basics 1 September 27th, 2006 09:25 AM
Blank Final Consigliori Java GUI 0 September 7th, 2006 01:57 PM



All times are GMT -4. The time now is 03:55 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.