Ask Your Question
4

How can the GPA Calculator in Flutter be made functional?

asked 2022-07-16 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2021-10-16 14:00:00 +0000

huitzilopochtli gravatar image

To make the GPA calculator functional, you can follow these steps in Flutter:

  1. Create a class called “Course” with attributes such as “name”, “grade”, and “credit hours”.

  2. Create a list of type “Course” to store every inputted course.

  3. Create a form where the user can input the course name, grade, and credit hours.

  4. When the form is submitted, create a new “Course” object and append it to the list of courses.

  5. Calculate the total credit hours and quality points by iterating through the list of courses.

  6. Display the calculated GPA to the user.

Here is an example implementation of the above steps:

class Course {
  String name;
  String grade;
  int creditHours;

  Course({this.name, this.grade, this.creditHours});
}

List<Course> courses = [];

class GPAcalculator extends StatefulWidget {
  @override
  _GPAcalculatorState createState() => _GPAcalculatorState();
}

class _GPAcalculatorState extends State<GPAcalculator> {
  final _formKey = GlobalKey<FormState>();
  String _courseName;
  String _grade;
  int _creditHours;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("GPA Calculator")),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              TextFormField(
                decoration: InputDecoration(labelText: 'Course Name'),
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter course name';
                  }
                  return null;
                },
                onSaved: (value) {
                  _courseName = value;
                },
              ),
              TextFormField(
                decoration: InputDecoration(labelText: 'Grade'),
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter grade';
                  }
                  return null;
                },
                onSaved: (value) {
                  _grade = value;
                },
              ),
              TextFormField(
                decoration: InputDecoration(labelText: 'Credit Hours'),
                keyboardType: TextInputType.number,
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Please enter credit hours';
                  }
                  return null;
                },
                onSaved: (value) {
                  _creditHours = int.parse(value);
                },
              ),
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 16.0),
                child: RaisedButton(
                  onPressed: () {
                    if (_formKey.currentState.validate()) {
                      _formKey.currentState.save();
                      Course newCourse = Course(
                          name: _courseName,
                          grade: _grade,
                          creditHours: _creditHours);
                      courses.add(newCourse);
                      _formKey.currentState.reset();
                      setState(() {});
                    }
                  },
                  child: Text('Submit'),
                ),
              ),
              Divider(),
              Text('Results:'),
              Text('Total Credit Hours: ${_totalCreditHours()}'),
              Text('Quality Points: ${_qualityPoints()}'),
              Text('GPA: ${_calculateGPA()}')
            ],
          ),
        ),
      ),
    );
  }

  int _totalCreditHours() {
    int total = 0;
    for (var course in courses) {
      total += course.creditHours;
    }
    return total;
  }

  double _qualityPoints() {
    double total = 0.0;
    for (var course in courses) {
      switch (course.grade) {
        case 'A':
          total += (4.0 * course.creditHours);
          break;
        case 'B':
          total += (3.0 * course.creditHours);
          break;
        case 'C':
          total += (2.0 * course.creditHours);
          break;
        case 'D':
          total += (1.0 * course.creditHours);
          break;
        default:
          break;
      }
    }
    return total;
  }

  double _calculateGPA() {
    return (_qualityPoints() / _totalCreditHours());
  }
}

Note: This is just an example implementation and can be modified according to your specific requirements.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-07-16 11:00:00 +0000

Seen: 11 times

Last updated: Oct 16 '21