RU beehive logo ITEC dept promo banner
ITEC 320
2018fall
nokie
ibarland

a first Ada program, revisited
hw03-mini: gradepoints_v2

due: Sep.28 (Fri) 15:00, D2L and hardcopy; program name: gradepoints_v2.adb

Updates: ignoring formatting and minor phrasing changes Last modified

This homework takes the first homework, and refactors it to use functions/procedures appropriately. It should be fairly easy; it is worth considerably fewer points than the other programming projects. It will help you learn about procedures and parameter modes.

Sample input file in2.txt:

Joe Jones
30
3.5
Jack Spratt
40
1.500

Sample output:

rucs% gradepoints_v2 < in2.txt Name: Joe Jones Hours: 30 GPA: 3.500 Grade Points: 105.00 Points above: 45.00 Name: Jack Spratt Hours: 40 GPA: 1.500 Grade Points: 60.00 Points below: 20.00

Code requirements

Program to re-write

Find the bug: There is an unintentional bug below, for you to find and fix. You'll notice it when you compare the output to the sample-input/outputs stated above (which are correct). (Even us profs, when we skip writing test-cases first, accidentally ship bugs in our code!)
-- Purpose: Calculate points surplus or deficit
--          based on hours taken and GPA

-- Input format: 
   --  Line 1: Name
   --  Lines 2 and 3: hours attempted and desired GPA

with Ada.Text_IO; use Ada.Text_IO;

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;

with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure homework_two is

   name: Unbounded_String;            -- Student name
   hours_attempted: Natural;          -- Hours taken
   gpa: Float;                        -- Current GPA

   earned_points: Float;              -- Points attained

   -- Requirements
   Required_GPA: constant := 2.0;     -- GPA required to graduate
   required_points: Float;            -- Points needed to graduate

   -- Points calculations
   surplus, deficit: Float;           -- Points above or below required

begin
   -- Read values
   get_line(name);
   get(hours_attempted);
   get(gpa);

   -- Calculation
   earned_points := Float(hours_attempted) * gpa;
   required_points := Required_GPA * Float(hours_attempted);

   -- Calculate both even though one will not be needed
   surplus := earned_points - required_points; 
   deficit := required_points - earned_points;

   -- Output
   put_line("Name: " & name);
   put_line("Hours:" & hours_attempted'img);

   put("GPA: ");
   put(item => gpa, fore => 1, aft => 3, exp => 0);
   new_line;

   put("Grade Points: ");
   put(earned_points, 1, 2, 0);
   new_line;

   -- Surplus or deficit
   if earned_points = Required_GPA then
      put_line("Exact: 0");
   elsif earned_points >= Required_GPA then
      put("Points above: ");
      put(surplus, fore => 1, aft => 2, exp => 0);
      new_line;
   else
      put("Points below: ");
      put(deficit, fore => 1, aft => 2, exp => 0);
      new_line;
   end if;

end homework_v2;

1 Certainly, you can pass parameters in or out – just no I/O :-)      
2 Just as in Java, if you mix Scanner#nextInt followed by Scanner#nextLine, you probably need to add an additional Scanner#nextLine after scanning the nextInt to consume the newline sitting there.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.