Hi, i was just wanting to know people's opinion on the format of my classes below, i haven't done much java programming and I just want to make sure that this program is easily understood. I will not be able to respond, because I have to wake up in 3 hours for school. So thanks in advance, I will be reading this tomorrow, to see your suggestions, thank you.
here is the scholar class, it is the class that i made to create a scholar(student object) that will be applying for a scholarship and the grades are entered in the ScholarTester class.
I could've done things differently and easier but the professor strictly called for it to be exactly as he stated. I came up with a better, less tedious way, but i couldn't use it. My way was to create a method to randomly generate grades and values to determine the major, but my prof. saw it and made me implement everything in ScholarTester. Also neither of the students will receive the scholarship, that will be determined in project 2 my professor said.
package project1;
import java.util.*;
/**
*
* @author Kevin
*/
public class Scholar implements Comparable {
// private variables
private String fullName;
private double gradePointAverage;
private int essayScore;
private int creditHours;
private boolean scienceMajor;
private double totalScore;
// constructor for a Scholar object
public Scholar(String myFullName, double myGradePointAverage, int myEssayScore,
int myCreditHours, boolean isScienceMajor){
this.fullName = myFullName;
this.gradePointAverage = myGradePointAverage;
this.essayScore = myEssayScore;
this.creditHours = myCreditHours;
this.scienceMajor = isScienceMajor;
}
// returns a Scholar object's fullname
public String getFullName(){
return fullName;
}
// returns a Scholar object's gpa
public double getGradePointAverage(){
return gradePointAverage;
}
// returns a Scholar object's essay score
public int getEssayScore(){
return essayScore;
}
/// returns a Scholar object's credit hours
public int getCreditHours(){
return creditHours;
}
// retruns if a Scholar object is a science major or not
public boolean getScienceMajor(){
return scienceMajor;
}
// sets a Scholar object's full name
public String setFullName(String lastName, String firstName){
fullName = lastName + ", " + firstName;
return fullName;
}
// sets a Scholar object's gpa
public double setGradePointAverage(double a){
gradePointAverage = a;
return gradePointAverage;
}
// sets a Scholar object's gpa
public int setEssayScore(int a){
essayScore = a;
return essayScore;
}
// sets a Scholar object's credit hours
public int setCreditHours(int a){
creditHours = a;
return creditHours;
}
// sets a Scholar's major to a science major or not a science major
public boolean setScienceMajor(boolean a){
scienceMajor = a;
return scienceMajor;
}
// calculates a Scholar object's score
public double getScore(){
totalScore = (gradePointAverage*5) + essayScore;
if (scienceMajor == true)
totalScore += .5;
if (creditHours >= 60 && creditHours <90)
totalScore += .5;
else if (creditHours >= 90)
totalScore += .75;
return totalScore;
}
// compares two scholar objects.
public int compareTo(Object obj){
Scholar otherObj = (Scholar)obj;
double result = getScore() - otherObj.getScore();
if (result > 0)
return 1;
else if (result < 0)
return -1;
return 0;
}
// returns the highest scoring of two Scholar objects
public static Scholar max(Scholar s1, Scholar s2){
if (s1.getScore() > s2.getScore())
System.out.println(s1.getFullName() + " scored higher than " +
s2.getFullName() + ".\n");
else
System.out.println(s2.getFullName() + " scored higher than " +
s1.getFullName() + ".\n");
return null;
}
// returns the highest of 3 student objects
public static Scholar max(Scholar s1, Scholar s2, Scholar s3){
if (s1.getScore() > s2.getScore() && s1.getScore() > s3.getScore())
System.out.println(s1.getFullName() + " scored the highest" +
" out of all three students.");
else if (s2.getScore() > s1.getScore() && s2.getScore() > s3.getScore())
System.out.println(s2.getFullName() + " scored the highest" +
" out of all three students.");
else if (s3.getScore() > s2.getScore() && s3.getScore() > s1.getScore())
System.out.println(s3.getFullName() + " scored the highest" +
" out of the three students.");
return null;
}
// toString method for a Scholar object
public String toString(){
return "Student name: " + fullName + " -" + " Grade Point Average: "
+ gradePointAverage + ". " + "Essay Score: " + essayScore + "."
+ " Credit Hours: " + creditHours + ". " + " Science major: "
+ scienceMajor + ".";
}
}
here's the ScholarTester class:
package project1;
import java.util.*;
/**
* This program creates Scholar objects that are applying for a scholarship
* @author Kevin
*/
public class ScholarTester {
public static void main(String [] args){
System.out.println("This program was written by Kevin Brown. \n");
System.out.println("--------Part 1--------\n");
// attributes for a scholar object named abraham
double abrahamGpa;
int abrahamEssayScore;
int abrahamCreditHours;
int scienceMajorValueAbraham;
boolean abrahamIsScienceMajor;
// random numbers used for each scholar object.
Random doubleGpa = new Random();
Random intGpa = new Random();
Random essayScore = new Random();
Random creditHours = new Random();
Random scienceMajor = new Random();
// randomly creates abraham's statistics
abrahamGpa = doubleGpa.nextDouble() + intGpa.nextInt(4);
abrahamEssayScore = essayScore.nextInt(6);
abrahamCreditHours = creditHours.nextInt(121);
scienceMajorValueAbraham = scienceMajor.nextInt(2);
if (scienceMajorValueAbraham == 0)
abrahamIsScienceMajor = true;
else
abrahamIsScienceMajor = false;
Scholar abraham = new Scholar("Lincoln, Abraham", abrahamGpa,
abrahamEssayScore, abrahamCreditHours,
abrahamIsScienceMajor);
System.out.println(abraham);
double georgeGpa;
int georgeEssayScore;
int georgeCreditHours;
int scienceMajorValueGeorge;
boolean georgeIsScienceMajor;
// randomly creates george's statistics
georgeGpa = doubleGpa.nextDouble() + intGpa.nextInt(4);
georgeEssayScore = essayScore.nextInt(6);
georgeCreditHours = creditHours.nextInt(131);
scienceMajorValueGeorge = scienceMajor.nextInt(2);
if (scienceMajorValueGeorge == 0)
georgeIsScienceMajor = true;
else
georgeIsScienceMajor = false;
// new scholar object is created from the existing attributes
Scholar george = new Scholar("Bush, George", georgeGpa, georgeEssayScore,
georgeCreditHours, georgeIsScienceMajor);
System.out.println(george + "\n");
System.out.println("--------Part 2--------\n");
System.out.println(abraham.getFullName() + " scored " + abraham.getScore() + ".");
System.out.println(george.getFullName() + " scored " + george.getScore() + ".\n");
System.out.println("--------Part 3--------\n");
/* comparing george bush and abraham lincoln's scores and printing them out to
show that the compareTo method is working*/
System.out.println(abraham.getFullName() + " scored "
+ abraham.getScore() + ".");
System.out.println(george.getFullName() + " scored "
+ george.getScore() + ".\n");
if(abraham.compareTo(george) == 1)
System.out.println(abraham.getFullName() + " scored higher than " +
george.getFullName() + ".\n");
else
System.out.println(george.getFullName() + " scored higher than " +
abraham.getFullName() + ".\n");
System.out.println("--------Part 4--------\n");
// prints out scores to show proof that the Scholar.max method is working right.
System.out.println(abraham.getFullName() + " scored "
+ abraham.getScore() + ".");
System.out.println(george.getFullName() + " scored "
+ george.getScore() + ".\n");
Scholar.max(abraham, george); // maximum score between abraham and george.
// variables for a scholar object named thomas.
double thomasGpa;
int thomasEssayScore;
int thomasCreditHours;
int scienceMajorValueThomas;
boolean thomasIsScienceMajor;
// randomly creates thomas' statistics
thomasGpa = doubleGpa.nextDouble() + intGpa.nextInt(4);
thomasEssayScore = essayScore.nextInt(6);
thomasCreditHours = creditHours.nextInt(131);
scienceMajorValueThomas = scienceMajor.nextInt(2);
if (scienceMajorValueThomas == 0)
thomasIsScienceMajor = true;
else
thomasIsScienceMajor = false;
// new scholar object created from existing attributes
Scholar thomas = new Scholar("Jefferson, Thomas", thomasGpa, thomasEssayScore,
thomasCreditHours, thomasIsScienceMajor);
System.out.println("New student added - " + thomas + "\n");
// returns all 3 students scores to show proof that the Scholar.max method is working right
System.out.println(thomas.getFullName() + " scored " + thomas.getScore());
System.out.println(abraham.getFullName() + " scored " + abraham.getScore());
System.out.println(george.getFullName() + " scored " + george.getScore() + "\n");
// highest score of all three scholars
Scholar.max(abraham, george, thomas);
}
}
Thanks for reading this long and boring project.