Accessors/Mutators needed ?
I wrote a student grade calculation program. Now, our prof. gave us a sample program that tracks an employee and their salary and information. When I'm comparing mine to his, I notice I have no accessor/mutator (get/set) statements like he does. Am I doing something wrong? Am I missing something or should I be using the get/set commands anywhere? My program and output runs just fine. Thanks for the help. I am very new and this is my first time creating an object template.
Here is my StudentRecord program
// ****************************************************************************
// Title: ClassRecord.java
// Class: CSC-201 (Summer 2007)
// Assignment: HW #2
// Author: Me
// Last Modified:
//
// Description: A basic program that allows a user to enter information about
// a student's grades to determine their overall numeric score for
// the class as well as the final letter grade attained in the
// course.
//
// Assumptions: - Students are identified by student ID #
// - User is aware that tests are only 10 points each
// - User is aware that midterm/Final exam are 100 points each
// - User has to rerun the program to recalculate changes
//
// Deficiencies: - Does not check to see if students scored higher than the
//assumed maximum points for each assignment
//- Limited to hard-coded number of quizzes/tests
//- Limited to initial weights given for each quiz/test
//- Does not use a students name
//
// ****************************************************************************
// ****************************************************************************
// CONSTRUCTORS
// ****************************************************************************
/* Default constructor */
/* Comprehensive constructor */
// ****************************************************************************
// MUTATOR METHODS
// ****************************************************************************
// ****************************************************************************
// ACCESSOR METHODS
// ****************************************************************************
// ****************************************************************************
// PUBLIC INTERFACE METHODS
// ****************************************************************************
// Precondition:
// Postcondition:
// ****************************************************************************
// PRIVATE HELPER METHODS
// ****************************************************************************
// Precondition:
// Postcondition:
// ****************************************************************************
// REQUIRED PACKAGES
// ****************************************************************************
import java.util.*;
publicclass StudentRecord{
// ****************************************************************************
// INSTANCE VARIABLES
// ****************************************************************************
privateint studentID, quizOne, quizTwo, midTerm, finalExam, overallScore;
privatechar letterGrade;
publicvoid readInput()
{
Scanner keyboard =new Scanner(System.in);
System.out.println("Please enter the Student's ID#: ");
studentID = keyboard.nextInt();
System.out.println("Please enter the score for Quiz 1: ");
quizOne = keyboard.nextInt();
System.out.println("Please enter the score for Quiz 2: ");
quizTwo = keyboard.nextInt();
System.out.println("Please enter the score for the Midterm: ");
midTerm = keyboard.nextInt();
System.out.println("Please enter the score for the Final Exam: ");
finalExam = keyboard.nextInt();
}
publicvoid calculation()
{
overallScore = (quizOne + quizTwo) * 5 / 4 + (midTerm / 4) +
finalExam / 2;
if (overallScore >= 90)
letterGrade ='A';
elseif ((overallScore >= 80) && (overallScore < 90))
letterGrade ='B';
elseif ((overallScore >= 70) && (overallScore < 80))
letterGrade ='C';
elseif ((overallScore >= 60) && (overallScore < 70))
letterGrade ='D';
else
letterGrade ='F';
}
publicvoid writeOutput()
{
System.out.println("Student# " + studentID +" grade report");
System.out.println("Quiz one = " + quizOne);
System.out.println("Quiz two = " + quizTwo);
System.out.println("Midterm = " + midTerm);
System.out.println("Final = " + finalExam);
System.out.println("Overall Score = " + overallScore);
System.out.println("Final Letter Grade = " + letterGrade);
}
}
Here is the DriverStudentRecord that I use to test it.
publicclass DriverStudentRecord{
publicstaticvoid main(String[] args){
StudentRecord testStudent =new StudentRecord();
testStudent.readInput();
testStudent.calculation();
testStudent.writeOutput();
}
}
Now here is a copy of my prof.'s program
// ****************************************************************************
// Title: Employee.java
// Class: CSC-201 (Fall 2006)
// Assignment: ICE #1
// Author: Ric Heishman
// Last Modified: 08/06/06
//
// Description: Template for an employee object.
//
// Assumptions: 1) employee number is a 5-digit integer.
// 2) salary cannot be negative.
// 3) first name limited to 10 characters.
// 4) last name limited to 15 characters.
//
// Deficiencies: None
//
// <-- text in file should be no wider than 80 characters so text doesn't wrap
//when printed in portrait format -->
// ****************************************************************************
// ****************************************************************************
// REQUIRED PACKAGES
// ****************************************************************************
import java.text.NumberFormat;
import java.util.Locale;
publicclass Employee{
// **************************************************************************
// INSTANCE VARIABLES
// **************************************************************************
privateint employeeNumber;// class invariant: 5-digit limit
privatedouble salary;
private String firstName,// class invariant: 10 character limit
lastName,// class invariant: 15 character limit
hireDate;
// **************************************************************************
// CONSTRUCTORS
// **************************************************************************
/* Default constructor */
public Employee(){
this(0,
0.0,
"firstname",
"lastname",
"hiredate");
}
/* Comprehensive constructor */
public Employee(int employeeNumber,
double salary,
String firstName,
String lastName,
String hireDate){
setEmployeeNumber(employeeNumber);
setSalary(salary);
setFirstName(firstName);
setLastName(lastName);
setHireDate(hireDate);
}
// **************************************************************************
// MUTATOR METHODS
// **************************************************************************
publicvoid setEmployeeNumber(int employeeNumber){
if (employeeNumber >= 0 && employeeNumber <= 99999)
this.employeeNumber = employeeNumber;
else{
System.out.println("Employee number must be between 0 -> 99999");
System.exit(1);
}
}
publicvoid setSalary(double salary){
if (salary >= 0.0)
this.salary = salary;
else
this.salary = 0.0;// potential problem ?
}
publicvoid setFirstName(String firstName){
if (checkStringLength(firstName, 1))
this.firstName = firstName;
else{
System.out.println("First Name must be < 10 characters");
System.exit(1);
}
}
publicvoid setLastName(String lastName){
if (checkStringLength(lastName, 2))
this.lastName = lastName;
else{
System.out.println("Last Name must be < 15 characters");
System.exit(1);
}
}
publicvoid setHireDate(String hireDate){
this.hireDate = hireDate;
}
// **************************************************************************
// ACCESSOR METHODS
// **************************************************************************
publicint getEmployeeNumber(){
return employeeNumber;
}
publicdouble getSalary(){
return salary;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getHireDate(){
return hireDate;
}
// **************************************************************************
// PUBLIC INTERFACE METHODS
// **************************************************************************
// Precondition: requires Employee object to be compared to calling object
// Postcondition: returns "true" if all fields of calling object and
//comparison object are identical
publicboolean equals(Employee employee){
if (this.employeeNumber == employee.employeeNumber &&
this.firstName == employee.firstName &&
this.lastName == employee.lastName &&
this.hireDate == employee.hireDate &&
this.salary == employee.salary)
returntrue;
else
returnfalse;
}
// Precondition: Employee object has been instantiated
// Postcondition: returns Sting representing current state of object
public String toString(){
return"Employee Number: " + employeeNumber +"\nEmployee Name: " +
firstName +" " + lastName +"\nSalary: " + formatSalary(salary) +
"\nHire Date: " + hireDate;
}
// **************************************************************************
// PRIVATE HELPER METHODS
// **************************************************************************
// Precondition: requires a string to be tested and an integer value
//representing a particular type of test for string length:
//(1) - maximum length of 10
//(2) - maximum length of 15
// Postcondition: returns "true" if string length < particular maximum length
privateboolean checkStringLength(String string,
int typeOfTest){
int maximumLength = 0;
switch (typeOfTest){
case 1:
maximumLength = 10;
break;
case 2:
maximumLength = 15;
break;
default:
System.out.println("Improper value for \"typeOfTest\" variable");
}
if (string.length() < maximumLength)
returntrue;
else
returnfalse;
}
// Precondition: requires double value to be formatted as US currency
// Postcondition: returns String of input value formatted as US currency
private String formatSalary(double salary){
Locale locale = Locale.US;
return NumberFormat.getCurrencyInstance(locale).format(salary);
}
}

