user9494656
user9494656

Reputation:

Trying to calculate a simple gpa program with parameters

I am writing a program to calculate a gpa. the only thing that doesn't work is when I try to write code to prevent the gpa from going over 4 or under 0. here is my whole class. I think the problem may be somewhere in my gpa full property. in my program class where I call the functions I have code to ask for studentID, firstName, lastName, and gpa. When I run the program and enter in a gpa of 5 it still returns the number 5 with a letter grade of A. I need it to return a number 4 with a letter grade of A. Any help would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Text;

namespace UnitTest1
{
  public class Student
  {
    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double Gpa { get; set; }

    private string letterGrade;

    public string LetterGrade
    {
        get
        {
            if (GPA >= 0 && GPA <= 0.99)
            {
                return "F";
            }
            else if (GPA >= 1 && GPA <= 1.29)
            {
                return "D";
            }
            else if (GPA >= 1.3 && GPA <= 1.69)
            {
                return "D+";
            }
            else if (GPA >= 1.7 && GPA <= 1.99)
            {
                return "C-";
            }
            else if (GPA >= 2 && GPA <= 2.29)
            {
                return "C";
            }
            else if (GPA >= 2.3 && GPA <= 2.69)
            {
                return "C+";
            }
            else if (GPA >= 2.7 && GPA <= 2.99)
            {
                return "B-";
            }
            else if (GPA >= 3 && GPA <= 3.29)
            {
                return "B";
            }
            else if (GPA >= 3.3 && GPA <= 3.69)
            {
                return "B+";
            }
            else if (GPA >= 3.7 && GPA <= 3.99)
            {
                return "A-";
            }
            else if (GPA == 4)
            {
                return "A";
            }
            return LetterGrade;
        }
        //All of this code sets the gpa number to a letter grade

    }

    private double gPA;

    public double GPA
    {
        get
        {
            if (Gpa <= 0)
            {
                return 0;
            }
            else if (Gpa >= 4)
            {
                return 4;
            }
            else
            {
                return Gpa;
            }
        }
        set
        {
            if (Gpa <= 0)
            {
                Gpa = 0;
            }
            else if (Gpa >= 4)
            {
                Gpa = 4;
            }
            Gpa = value; // <<=== remove me please
        }
    }

    public Student(int studentID, string firstName, string lastName, double gpa)
    {
        StudentID = studentID;
        FirstName = firstName;
        LastName = lastName;
        Gpa = gpa;
    }

    public void PrintTranscript()
    {
        Console.WriteLine("");
        Console.WriteLine("{0, -20} {1, 26}", "University of Coding", $"Student ID: {StudentID}");
        Console.WriteLine("{0, -20} {1, 23}", "123 Sea Sharp Street", "Date: 02/15/2018");
        //The coordinates in these two lines sets the text in a certain spot in the output screen

        Console.WriteLine("Cedarville, OH 45314");
        Console.WriteLine($"Student: {FirstName} {LastName}");
        Console.WriteLine("");
        Console.WriteLine($"Current GPA {Gpa}");
        Console.WriteLine("");
        Console.WriteLine($"Letter Grade: {LetterGrade}");
        //This code outputs what the user input
    }
  }
}

Upvotes: 2

Views: 914

Answers (2)

Priyan Perera
Priyan Perera

Reputation: 560

I don't think you need a setter at all here and you are outputting the wrong property.

public double GPA
{
    get
    {
        if (Gpa <= 0)
        {
            return 0;
        }
        else if (Gpa >= 4)
        {
            return 4;
        }
        else
        {
            return Gpa;
        }
    }
}

Output line

Console.WriteLine($"Current GPA {GPA}");

Upvotes: 0

D Stanley
D Stanley

Reputation: 152596

You can get rid of the Gpa property (you already have a backing field and are using custom get and set methods), and your getter and setter can be simplified:

private decimal gPA;
public decimal GPA
{
    get
    {
        return gPA;
    }
    set
    {
        if (value <= 0)  // use value, not gPA here
        {
            gPA = 0;
        }
        else if (value >= 4)
        {
            gPA = 4;
        }
        else
        {
            gPA = value; 
        }
    }
}

I would also switch to decimal if you want to use exact equality comparisons, and use < instead of <= in cases where it's what you intend (e.g. < 4 is more appropriate than <= 3.99)

Upvotes: 1

Related Questions