A student object consists of: FirstName: string LastName: string Birthdate: tupl
ID: 3738236 • Letter: A
Question
A student object consists of:
FirstName: string
LastName: string
Birthdate: tuple, such as (1,17,1998)
TechID: string
Grades: list of tuples, such as [(3127,”C”,4),(2496,”B”,3),(6703,”D”,3)]
So one student might look like this:
FirstName: “Bob”
LastName: “Smith”
Birthdate: (2,17,1998)
TechID: “43546578”
Grades: [(5930,”B”,4),(7660,”C”,4),(3122,”C”,3),(5438,”C”,2),
(6730,”C”,4),(4268,”C”,4),(5709,”D”,4),(8071,”B”,4)]
The data for each student can be found in the text files. Your program should read the data from each of the files, as appropriate. (Do not change the format of the input files.) Your getData function should read in *all* of the data, at one time. And once all of the data has been read into an appropriate structure, that data should be passed back to the main program. Your program should not go back-and-forth between reading data and returning data.
Create a list of students. Do not process the students one-by-one as they are created or as they are placed in the list. Create a student, add it to the list of student objects, repeat. Once the list of students is complete, go through the list to output the report on each student.
You will need three methods for processing a student object.
currentAge: How old is the student? Be sure to use the Python module.
Hint:
import datetime
now = datetime.datetime.now()
print (now.year)
currentGPA: What is the student’s GPA?
Explanation / Answer
Please find my implementation:
#!/usr/bin/python
import os.path
from datetime import date
studentList = [];
gradesList = [];
outputFileName = "output.txt";
inputFileOneName = "inputOne.txt";
inputFileTwoName = "inputTwo.txt";
class Student:
def __init__(self, firstName, lastName, birthDate, techID, grades):
self.firstName = firstName;
self.lastName = lastName;
self.birthDate = birthDate;
self.techID = techID;
self.grades = grades;
def currentAge(self):
birthDay = self.birthDate[0]
birthMonth = self.birthDate[1]
birthYear = self.birthDate[2]
today = date.today()
return today.year - birthYear - ((today.month, today.day) < (birthMonth, birthDay))
def currentGPA(self):
# I don't know how to calculte GPA from grages
# Give the formula, I will update the code accordingly
totalCredits = 0;
for record in self.grades:
totalCredits = totalCredits + record[2];
return "2.5 (" + str(totalCredits) + " credits)";
def report(self):
name = self.firstName + " " + self.lastName + "(#"+ str(self.techID) +")";
doB = str(self.birthDate[0]) +"/" + str(self.birthDate[1]) + "/" + str(self.birthDate[2]);
age = "Age : " + str(self.currentAge()) + " (" + doB +")";
gpa = "GPA : " + str(self.currentGPA());
return (" " + name + " " + age +" " +gpa +" ");
def readFromFileOne(fileName):
doesFileExists = False;
if os.path.isfile(fileName) :
doesFileExists = True;
# If the file exists then Load the existing data into the studentList
if doesFileExists :
fileInstance = open(fileName,'r');
fileText = fileInstance.read();
lineSplits = fileText.split(" ");
for line in lineSplits :
if line == "" :
continue;
wordSplits = line.split(",")
techID = int(wordSplits[0]);
firstName = wordSplits[1];
lastName = wordSplits[2];
doBString = wordSplits[3];
dobSplits = doBString.split("/");
dobTuple = (int(dobSplits[0]), int(dobSplits[1]), int(dobSplits[2]));
studentGrades = []
for record in gradesList:
if record[0] == techID:
studentGrades.append((record[1], record[2], record[3]));
student = Student(firstName, lastName, dobTuple, techID, studentGrades);
studentList.append(student);
def readFromFileTwo(fileName):
doesFileExists = False;
if os.path.isfile(fileName) :
doesFileExists = True;
# If the file exists then Load the existing data into the studentList
if doesFileExists :
fileInstance = open(fileName,'r');
fileText = fileInstance.read();
lineSplits = fileText.split(" ");
for line in lineSplits :
if line == "" :
continue;
wordSplits = line.split(",")
techID = int(wordSplits[0]);
classID = int(wordSplits[1]);
grade = wordSplits[2];
credits = int(wordSplits[3]);
gradesList.append((techID, classID, grade, credits));
def readFromFile():
readFromFileTwo(inputFileTwoName);
readFromFileOne(inputFileOneName);
# Starting the procedure
readFromFile();
fileInstance = open(outputFileName,'w');
for student in studentList:
ouputString = student.report();
fileInstance.write(ouputString);
fileInstance.close();
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.