Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BCS345hw4 {
- public static void main(String args[]) throws IOException {
- Student[]students;
- students = new Student[25];
- final int SIZE = 5;
- int[]scores = new int[SIZE];
- int i = 0;
- File myFile = new File("hw4data.txt");
- Scanner scanner = new Scanner(myFile);
- System.out.printf("%2s%15s%15s%24s%28s%10s\n", "ID","First Name","Last Name","Scores", "Average","Grade" );
- while(scanner.hasNext() && i < students.length)
- {
- Student student = new Student();
- student.setID(scanner.nextInt());
- student.setFName(scanner.next());
- student.setLName(scanner.next());
- for(int j = 0; j < scores.length; j++) {
- scores[j] = scanner.nextInt();
- student.setScores(scores);
- }
- System.out.println(student);
- i++;
- }
- scanner.close();
- }
- import java.util.Arrays;
- /*
- * Talha Riaz
- * Professor Li BCS 345
- * Assignment 4 @duedate 10/30/17
- */
- public class Student{
- public int ID;
- private String firstName;
- private String lastName;
- private int scores[];
- float average;
- char grade;
- /*
- * Default constructor with no arguments
- * sets all variables to default values
- */
- public Student()
- {
- ID = 0;
- firstName = "No_Name";
- lastName = "No_Name";
- scores = new int[] {0,0,0,0,0};
- }
- /**
- * overloaded constructor for class Student
- * uses variables as parameters to set values of all variables
- * @param ID
- * @param firstName
- * @param lastName
- */
- public Student(int I, String fName,String lName,int[]array)
- {
- ID = I;
- firstName = fName;
- lastName = lName;
- scores = array;
- }
- /**
- * Sets value of ID = I and stores it in I
- * @param I
- * @return I
- */
- public void setID(int I) {
- ID = I;
- }
- /**
- * sets value of firstName and stores it in F
- * @param F
- * @return
- */
- public void setFName(String F) {
- firstName = F;
- }
- /*
- * sets value of lastName stores it in L
- * @param L
- * @return L
- */
- public void setLName(String L) {
- lastName = L;
- }
- /**
- * Sets the scores, stores them in a variable score
- * @param x
- * @return
- */
- public void setScores(int[]x) {
- for(int i = 0; i < x.length; i++)
- {
- scores[i] = x[i];
- }
- }
- /**
- *
- * @return value of ID
- */
- public int getID() {
- return ID;
- }
- /**
- *
- * @return value of firstName
- */
- public String getFName() {
- return firstName;
- }
- /**
- *
- * @return value of lastName
- */
- public String getLName() {
- return lastName;
- }
- public int[] getScores()
- {
- return scores;
- }
- /**
- * calculates average of test scores
- * returns the average with the lowest test score dropped
- * @return average
- */
- public float getAverage() {
- float average = 0;
- float total = 0;
- int lowest = scores[0];
- for(int i = 0; i < scores.length; i++)
- {
- if(scores[i] < lowest) {
- lowest = scores[i];
- }
- }
- for(int i = 0; i < scores.length; i++)
- {
- if(scores[i] > lowest) {
- total += scores[i];
- }
- }
- average = total/(scores.length - 1);
- return average;
- }
- /**
- * Calculates the grade based on the average
- * @return grade
- */
- public char getGrade()
- {
- average = getAverage();
- if(average >= 90 && average <= 100) {
- grade = 'A';
- }
- else if(average >= 80 && average < 90) {
- grade = 'B';
- }
- else if(average >= 70 && average < 80) {
- grade = 'C';
- }
- else if(average >= 60 && average < 70) {
- grade = 'D';
- }
- else {
- grade = 'F';
- }
- return grade;
- }
- /**
- * prints out information about the student object
- */
- public String toString() {
- String str = String.format("%5d%10s%15s",ID,firstName,lastName);
- for(int i = 0; i < scores.length; i++)
- {
- str += String.format("%8d", scores[i]);
- }
- str += String.format("%13.2f%9c\n", getAverage(), getGrade());
- return str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement