NullPointerException on a 2D array of objects - java

I have a 2D Array called sectionArray which is of type Student class. Each student has a name and an array[] of 5 grades for exam scores. These names and scores are divided into to sections in a file that must be read. I keep getting a nullPointer on my sectionArray no matter what I change. Thank you for any suggestions.
import java.util.*;
import java.io.*;
public class ProgressReport {
private Student[][] sectionArray;// each student has a name,
// grade, average,
// and the array of scores
private File file;
private Scanner inputFile;
public ProgressReport() throws IOException {
sectionArray = new Student[2][];
file = new File("Lab5A.in.txt");
inputFile = new Scanner(file);
}
/**
*
* #return the values in the sectionArray
*/
public Student[][] getSectionArray() {
return sectionArray;
}
/**
* initialize sectionArray and set the values
*
* #param sectionArray
* passed 2D array of Students
*/
public void setSectionArray(Student[][] sectionArray) {
for (int i = 0; i < sectionArray.length; i++) {
for (int j = 0; j < sectionArray[i].length; j++) {
this.sectionArray[i][j] = sectionArray[i][j];
}
}
}
/**
* reads from the file and creates new Students
*
* #throws IOException
*/
public void readInputFile() throws IOException {
int colNum = 0;
int section = 0;
String name = " ";
int[] grades = new int[5];
while (inputFile.hasNext()) {
colNum = inputFile.nextInt();// gets size of row
sectionArray[section] = new Student[colNum];// initialize array
// iterates through colNum amount of times
for (int j = 0; j < colNum; j++) {
name = inputFile.next();// gets next name in column
for (int i = 0; i < 5; i++) {
// stores scores for that name
grades[i] = inputFile.nextInt();
}
// creates new Student with name and grades
sectionArray[section][j] = new Student(name, grades);
section++;
}
}
// passes the values in sectionArray
setSectionArray(sectionArray);
}
}
My student class looks like this:
public class Student {
private String name = " "; // Store the name of the student
private char grade; // Store the letter grade
private double average; // Store the average score
private int[] scores; // Store the five exam scores
public Student() {
grade = ' ';
average = 0.0;
}
public Student(String name, int[] score) {
this.name = name;
scores = new int[5];
for (int i = 0; i < scores.length; i++) {
scores[i] = 0;
}
for (int i = 0; i < scores.length; i++) {
this.scores[i] = score[i];
}
}
// getters
public String getName() {
return name;
}
public char getGrade() {
return grade;
}
public double getAverage() {
return average;
}
// think about changing this to return a different format
public int[] getScores() {
return scores;
}
// setters
public void setScore(int[] scores) {
}
public void setName(String name) {
this.name = name;
}
public void setGrade(char grade) {
this.grade = grade;
}
public void setAverage(double average) {
this.average = average;
}
/**
* determine the average of the five test scores for each student
*/
public void calculateAverage() {
double total = 0;
double average = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
average = total / scores.length;
setAverage(average);
}
/**
* Determine the student's letter grade based on average of test scores
*/
public void calculateGrade() {
double average = 0;
average = getAverage();
if (average <= 100 && average >= 90) {
setGrade('A');
} else if (average <= 89 && average >= 80) {
setGrade('B');
} else if (average <= 79 && average >= 70) {
setGrade('C');
} else if (average <= 69 && average >= 60) {
setGrade('D');
} else if (average <= 59 && average >= 0) {
setGrade('F');
}
}
public String toString() {
return getName() + " " + getAverage() + " " + getGrade();
}
}

The array you are trying to write is initialized with sectionArray = new Student[2][];. This will create a matrix (2D array) with 2 columns and only one row, and then you try to set your new values on this array. If you already know the size of the matrix you are going to read from the file, then initialize it with the correct values.
Anyway, I did not get why you are trying to use a 2D array for this. If I understood correctly the purpose of your code, you should be using a List instead to store the read data, and since it has a dynamically increasing size you wouldn't have to bother with controlling the indexes like you do with the array. Take a look at this tutorial to learn how to use lists.

Related

Cannot work around ArithmeticException? / by zero?

I'm having trouble working around an ArithmeticException I'm getting from my getAverageScore() method in my Student class. I'm trying to write a program that reads the following text file scores.txt:
34 c081 c082 c083 c084
S2023 99 75 85 62
S2025 -1 92 67 52
S1909 100 83 45 -1
The c numbers such as c081 are course codes at the school, and the s numbers such as s2023 are student numbers. The figures in the middle represent their scores, the -1 also means they weren't enrolled in the unit.
I want to be able to write a program where I can give output as the student with the highest average across their classes, with that average and their student number. I want to be able to make sure the -1 values aren't included in that class average, so that if a student has one of those, their average is only calculated for the classes they did actually take.
This is the Student class:
import java.util.ArrayList;
public class Student {
private String studentNumber;
private ArrayList<Integer> scores = new ArrayList<Integer>();
public Student(String studentText) {
String[] parts = studentText.split(studentText, ' ');
this.studentNumber = parts[0];
for (int i = 1; i < parts.length - 1; i++) {
scores.add(Integer.parseInt(parts[i + 1]));
}
}
public String getStudentNumber() {
return this.studentNumber;
}
public float getAverageScore() {
int sum = 0;
int numberOfCoursesNotTaken = 0;
for (int i = 0; i <this.scores.size(); i++) {
if (i != -1) {
sum += this.scores.get(i);
System.out.println(sum);
} else {
numberOfCoursesNotTaken++ ;
System.out.println(numberOfCoursesNotTaken);
}
}
return sum / (this.scores.size() - numberOfCoursesNotTaken);
}
}
I'm calling this method from the following class:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class MySchool {
public static void main(String[] args) {
int numberOfStudents;
ArrayList<Student> allStudents = new ArrayList<Student>() ;
try {
File scoresFile = new File("Scores.txt");
Scanner scoresFileReader = new Scanner(scoresFile);
String headerRow = scoresFileReader.nextLine();
char c = headerRow.charAt(0);
numberOfStudents = Character.getNumericValue(c);
for (int studentI = 0; studentI < numberOfStudents; studentI++) {
String studentText = scoresFileReader.nextLine();
System.out.println(studentText);
Student student = new Student(studentText);
allStudents.add(student);
}
scoresFileReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred");
e.printStackTrace();
}
float highestAverage = 0;
String highestScoringStudentNumber = null;
for (Student student : allStudents) {
if (student.getAverageScore() > highestAverage) {
highestAverage = student.getAverageScore();
highestScoringStudentNumber = student.getStudentNumber();
}
}
System.out.println("Highest scoring student: " + highestScoringStudentNumber);
}
}
However, the return line of getAverageScore() in Student gives the following error: Exception in thread "main" java.lang.ArithmeticException: / by zero
at Student.getAverageScore(Student.java:34)
at MySchool.main(MySchool.java:42)
I thought I'd solved this issue using the numberOfCoursesNotTaken variable? Could anyone help me?
I really recommend removing invalid values as soon as they are found. You could replace your Student constructor by the following:
public Student(String studentText) {
String[] parts = studentText.split(" "); // previously (studentText, ' ')
this.studentNumber = parts[0];
for (int i = 1; i < parts.length - 1; i++) {
int score = Integer.parseInt(parts[i]); // NOT i+1
if (score >=0) this.scores.add(score);
}
}
While you are at it, you can greatly simplify your average-score calculation (now that there are no -1s):
public float averageScore() {
if (scores.isEmpty()) {
return 0; // avoids divide-by-zero
} else {
int total = 0;
for (int score : scores) total += score;
return total / (float) scores.size();
}
}
Student class -> getAverageScore : in the for loop, you're checking if i != -1, instead of if scores.get(i) != -1.

Problem with finding the highest exam range

I have an assignment that requires me to create a method called getExamRange that looks at an array which includes the exam scores of several students, takes the lowest and highest scores, and subtracts the minimum exam score from the maximum exam score. I also have to create a getMostImprovedStudent which run the getExamRange method on an array of Students and returns the student with the highest exam range. I'm having trouble getting the correct results when the code is run. What is causing this problem?
Here is the code for the Student.java class:
import java.util.*;
public class Student
{
private static final int NUM_EXAMS = 4;
private String firstName;
private String lastName;
private int gradeLevel;
private double gpa;
private int[] exams;
private int numExamsTaken;
/**
* This is a constructor. A constructor is a method
* that creates an object -- it creates an instance
* of the class. What that means is it takes the input
* parameters and sets the instance variables (or fields)
* to the proper values.
*
* Check out StudentTester.java for an example of how to use
* this constructor.
*/
public Student(String fName, String lName, int grade)
{
firstName = fName;
lastName = lName;
gradeLevel = grade;
exams = new int[NUM_EXAMS];
numExamsTaken = 0;
}
public int getExamRange()
{
Arrays.sort(exams);
int examScore1 = exams[0];
int examScore2 = 0;
int lastPos = 0;
for(int i = 0; i < exams.length - 1; i++)
{
lastPos++;
}
examScore2 = exams[lastPos];
return examScore2 - examScore1;
}
public String getName()
{
return firstName + " " + lastName;
}
public void addExamScore(int score)
{
exams[numExamsTaken] = score;
numExamsTaken++;
}
// This is a setter method to set the GPA for the Student.
public void setGPA(double theGPA)
{
gpa = theGPA;
}
/**
* This is a toString for the Student class. It returns a String
* representation of the object, which includes the fields
* in that object.
*/
public String toString()
{
return firstName + " " + lastName + " is in grade: " + gradeLevel;
}
}
and here is the code for the Classroom.java class:
public class Classroom
{
Student[] students;
int numStudentsAdded;
public Classroom(int numStudents)
{
students = new Student[numStudents];
numStudentsAdded = 0;
}
public Student getMostImprovedStudent()
{
int highestScore = 0;
int score = 0;
int location = 0;
int finalLocation = 0;
for(Student s: students)
{
score = s.getExamRange();
location++;
if(score > highestScore)
{
highestScore = score;
finalLocation = location;
}
}
return students[finalLocation - 1];
}
public void addStudent(Student s)
{
students[numStudentsAdded] = s;
numStudentsAdded++;
}
public void printStudents()
{
for(int i = 0; i < numStudentsAdded; i++)
{
System.out.println(students[i]);
}
}
}
Here are the directions for the assignment which state what the methods are supposed to do:
Taking our Student and Classroom example from earlier, you should fill in the method getMostImprovedStudent, as well as the method getExamRange. The most improved student is the one with the largest exam score range.
To compute the exam score range, you must subtract the minimum exam score from the maximum exam score.
For example, if the exam scores were 90, 75, and 84, the range would be 90 - 75 = 15.
Firstly let us look at the getExamRange function
public int getExamRange(){
if(exams == null ||exams.length == 0){
return 0;
}
int min = exams[0];
int max = exams[0];
for (int i : exams
) {
if(i<min){
min=i;
}
if(i>max){
max=i;
}
}
return max - min;
}
and now on getMostImprovedStudent
public Student getMostImprovedStudent()
{
if(students == null ||students[0] == null || students.length=0){
return null;
}
int highestScore = students[0].getExamRange();
int score = 0;
Student mostImprovedStudent = students[0]
for(int i=0;i<students.length;i++)
{
if(students[i]!=null){
score = students[i].getExamRange();
if(score > highestScore)
{
highestScore = score;
mostImprovedStudent = students[i];
}
}
}
return mostImprovedStudent;
}

addScore method is over writing current array values instead of appending to the current list

I am a novice java student. I have a project for a java class where I am to write a Golfer class, Score class and a tester for the Golfer class to test all methods. The specific problems I am having is :
When I call the addScore method, the method overwrites the old data instead of adding to the existing. I need to get the program to add the scores in the array in addition to the previous score.
The findScore method is private and used in the public method getScore, however, when I run the program, I get the null value regardless of the parameter in the call. I need to return the index of an array based on the date entered.
The following is excerpts from the code and not the entire program.
public class Golfer {
/**String representing the golfer's name*/
private String name;
/**String representing the golf course where the golfer's hadicap is kept*/
private String homeCourse;
/**Unique integer that identifies every golfer*/
private int idNum;
/**Array storing all the golfer's scores*/
private Score[] scores;
public static int nextIDNum;
/**Default constructor, sets all instance field to a default value. Creates Array.
*/
public Golfer() {
name = "";
homeCourse = "";
scores = new Score[0];
nextIDNum = 1000;
}
/**Constructor sets name and homeCourse from parameters and uses the static variable nextIDNum to retrieve the next available ID number. Creates Array.
*/
public Golfer(String golferName, String home) {
setName(golferName);
setHomeCourse(home);
setNextIDNum(nextIDNum);
scores = new Score[10];
}
/**Creates a Score object from the parameters that represent the course, course rating, course slope, date and score.  Adds the newly created Score object to the Array of Scores. 
#param golfCourse A String representing the golf course name
#param rating A double representing the golf course rating
#param slope An int representing the golf course slope
#param scoreDate A String representing the date the course was played
#param score An int representing what was scored on the course
*/
public void addScore(String golfCourse, double rating, int slope, String scoreDate, int score) {
Score[] golfScores = new Score[scores.length + 1];
for (int i = 0; i < scores.length; i++) {
golfScores[i] = scores[i];
}
golfScores[golfScores.length - 1] = new Score(golfCourse, rating, slope, scoreDate, score);
scores = golfScores;
}
/**Deletes a score from the Array based on score date,  Assumes only one score per day.
#param golfDate A string representing the date of the golf score
#return true if score found and deleted,
#return false if score not found.
*/
public boolean deleteScore (String golfDate) {
for (int i = 0; i < scores.length; i++) {
if (findScore(golfDate) > 0) {
scores[i] = null;
}
return true;
}
return false;
}
/**Returns a score object based on the score date. If not found returns null
#param golfDate The date of the golf score
#return Scores[i] The score on the parameterized date
#return null A null value if the score was not found.
*/
public Score getScore(String golfDate) {
for (int i = 0; i < scores.length; i++) {
if (findScore(golfDate) > 0) {
return scores[i];
}
}
return null;
}
/**Given a parameter representing the score's date, finds the score on a given date and returns the Array index of a score. Return constant NOTFOUND if not found.
#param golfDate A string representing the date of the score
#return i An array index representing the score
#return NOTFOUND A constant set to -1 if the score isn't found
*/
private int findScore(String golfDate) {
final int NOTFOUND = -1;
for (int i = 0; i < scores.length; i++) {
if (scores[i].equals(golfDate)) {
return i;
}
}
return NOTFOUND;
}
}
The score class:
public class Score {
private String courseName;
private int score;
private String date;
private double courseRating;
private int courseSlope;
public Score(String course, double rating, int slope, String golfDate, int scr) {
setCourseName(course);
setScore(scr);
setDate(golfDate);
setCourseRating(rating);
setCourseSlope(slope);
}
public Score() {
courseName = "";
score = 0;
date = "";
courseRating = 0.0;
courseSlope = 0;
}
public void setCourseName(String course) {
courseName = course;
}
public String getCourseName() {
return courseName;
}
public void setScore(int golfScore) {
if ((golfScore < 40) && (golfScore > 200)) {
golfScore = 9999;
System.out.println("Error: golf score must be between 40 and 200.");
}
score = golfScore;
}
public int getScore() {
return score;
}
public void setDate(String golfDate) {
date = golfDate;
}
public String getDate() {
return date;
}
public void setCourseRating(double rating) {
if ((rating < 60) && (rating > 80)) {
rating = 9999;
System.out.println("Error: the course rating must be between 60 and 80.");
}
courseRating = rating;
}
public double getCourseRating() {
return courseRating;
}
public void setCourseSlope(int slope) {
if ((slope < 55) && (slope > 155)) {
slope = 9999;
System.out.println("Error: The course slope must be between 55 and 155.");
}
courseSlope = slope;
}
}
The tester class:
public class GolferTester {
public static void main (String []args) {
String course1 = "Augusta National";
String course2 = "Bayhill CC";
String course3 = "TPC Sawgrass";
String player1 = "Sam Snead";
String player2 = "Arnold Palmer";
String player3 = "Jack Nicklaus";
int score1 = 66;
int score2 = 201;
int score3 = 72;
int slope1 = 60;
int slope2 = 156;
int slope3 = 77;
double rating1 = 65.2;
double rating2 = 81.8;
double rating3 = 70.9;
String date1 = "01/01/2017";
String date2 = "06/01/2016";
String date3 = "12/22/2016";
Golfer golfer1 = new Golfer(player1, course1);
Golfer golfer2 = new Golfer(player2, course2);
Score s1 = new Score(course1, rating1, slope1, date1, score1);
Score s2 = new Score(course2, rating2, slope2, date2, score2);
s1.setScore(score1);
s1.setDate(date1);
s1.setCourseRating(rating1);
s1.setCourseSlope(slope1);
s1.setCourseName(course1);
s2.setScore(score3);
s2.setDate(date3);
s2.setCourseRating(rating3);
s2.setCourseSlope(slope3);
s2.setCourseName(course3);
golfer1.addScore(s1.getCourseName(), s1.getCourseRating(), s1.getCourseSlope(), s1.getDate(), s1.getScore());
golfer2.addScore(s2.getCourseName(), s2.getCourseRating(), s2.getCourseSlope(), s2.getDate(), s2.getScore());
System.out.println(golfer1);
System.out.println("");
System.out.println(golfer2);
System.out.println("");
s1.setScore(score2);
s1.setCourseRating(rating2);
s1.setCourseSlope(slope2);
golfer1.addScore(s1.getCourseName(), s1.getCourseRating(), s1.getCourseSlope(), s1.getDate(), s1.getScore());
System.out.println(golfer1);
deleteScore(s1.getDate());
System.out.println(s1.getDate());
}
}
Any help would be appreacited
Don't use an array for a list of elements that you know will grow/shrink. Use ArrayList, which has add()
Your getScore method is returning null always

Simple prisoner's dilemma genetic algorithm

I have used an existing genetic algorithm from
here
and reworked it I but don't know what I'm doing wrong
This is the error that I get
Exception in thread "main" java.lang.NullPointerException at
simpleGa.Algorithm.crossover(Algorithm.java:69) at
simpleGa.Algorithm.evolvePopulation(Algorithm.java:34) at
simpleGa.GAprisonerdilemma.main(GAprisonerdilemma.java:41)
I can't figure out exactly where the mistake is. Read a lot about NullPointerException but couldn't figure it out
package simpleGa;
public class Population {
public static Individual[] individuals;
/*
* Constructors
*/
// Create a population
public Population(int populationSize, boolean initialise) {
individuals = new Individual[populationSize];
// Initialise population
if (initialise) {
// Loop and create individuals
for (int i = 0; i < size(); i++) {
Individual newIndividual = new Individual();
newIndividual.generateIndividual();
saveIndividual(i, newIndividual);
}
for(int i=0;i<size();i++)
{
if(i%2==1){Individual individual1=individuals[i-1];
Individual individual2=individuals[i];
if(individuals[i-1].getGene(i-1)==0 && individuals[i].getGene(i)==0){
individuals[i-1].fitness=individual1.fitness+1;
individuals[i].fitness=individual2.fitness+1;
}
if(individuals[i-1].getGene(i-1)==1 && individuals[i].getGene(i)==1){
individuals[i-1].fitness=individual1.fitness+2;
individuals[i].fitness=individual2.fitness+2;
}
if(individuals[i-1].getGene(i-1)==0 && individuals[i].getGene(i)==1){
individuals[i-1].fitness=individual1.fitness+3;
individuals[i].fitness=individual2.fitness+0;
}
if(individuals[i-1].getGene(i-1)==1 && individuals[i].getGene(i)==0){
individuals[i-1].fitness=individual1.fitness+0;
individuals[i].fitness=individual2.fitness+3;
}
}}}
}
/* Getters */
public Individual getIndividual(int index) {
return individuals[index];
}
public Individual getFittest() {
Individual fittest = individuals[0];
// Loop through individuals to find fittest
for (int i = 1; i < size(); i++) {
if (fittest.getFitness() <= getIndividual(i).getFitness()) {
fittest = getIndividual(i);
}
}
return fittest;
}
/* Public methods */
// Get population size
public int size() {
return individuals.length;
}
// Save individual
public void saveIndividual(int index, Individual indiv) {
individuals[index] = indiv;
}
}
package simpleGa;
public class Individual {
static int defaultGeneLength = 1000;
private long[] genes =new long [defaultGeneLength];
// Cache
public static int fitness = 0;
// Create a random individual
public void generateIndividual() {
for (int i = 0; i < size(); i++) {
long gene = Math.round(Math.random());
genes[i] = gene;
}
}
/* Getters and setters */
// Use this if you want to create individuals with different gene lengths
public static void setDefaultGeneLength(int length) {
defaultGeneLength = length;
}
public long getGene(int i) {
return genes[i];
}
public void setGene(int index, long value) {
genes[index] = value;
fitness = 0;
}
/* Public methods */
public int size() {
return genes.length;
}
public static int getFitness() {
return fitness;
}
public void setFitness(int i) {
fitness=i;
}
#Override
public String toString() {
String geneString = "";
for (int i = 0; i < size(); i++) {
geneString += getGene(i);
}
return geneString;
}
}
package simpleGa;
public class Algorithm {
/* GA parameters */
private static final double uniformRate = 0.5;
private static final double mutationRate = 0.015;
private static final int tournamentSize = 5;
private static final boolean elitism = true;
/* Public methods */
// Evolve a population
public static Population evolvePopulation(Population pop) {
Population newPopulation = new Population(pop.size(), false);
// Keep our best individual
if (elitism) {
newPopulation.saveIndividual(0, pop.getFittest());
}
// Crossover population
int elitismOffset;
if (elitism) {
elitismOffset = 1;
} else {
elitismOffset = 0;
}
// Loop over the population size and create new individuals with
// crossover
for (int i = elitismOffset; i < pop.size(); i++) {
Individual indiv1 = tournamentSelection(pop);
Individual indiv2 = tournamentSelection(pop);
Individual newIndiv = crossover(indiv1, indiv2);
newPopulation.saveIndividual(i, newIndiv);
}
// Mutate population
for (int i = elitismOffset; i < newPopulation.size(); i++) {
mutate(newPopulation.getIndividual(i));
}
for(int i=0;i<pop.size();i++)
{for(int j=0;j<pop.getIndividual(i).size();j++)
{if(i%2==1){Individual individual1=Population.individuals[i-1];
Individual individual2=Population.individuals[i];
if(Population.individuals[i-1].getGene(i-1)==0 && Population.individuals[i].getGene(i)==0){
Population.individuals[i-1].fitness=individual1.fitness+1;
Population.individuals[i].fitness=individual2.fitness+1;
}
if(Population.individuals[i-1].getGene(i-1)==1 && Population.individuals[i].getGene(i)==1){
Population.individuals[i-1].fitness=individual1.fitness+2;
Population.individuals[i].fitness=individual2.fitness+2;
}
if(Population.individuals[i-1].getGene(i-1)==0 && Population.individuals[i].getGene(i)==1){
Population.individuals[i-1].fitness=individual1.fitness+3;
Population.individuals[i].fitness=individual2.fitness+0;
}
if(Population.individuals[i-1].getGene(i-1)==1 && Population.individuals[i].getGene(i)==0){
Population.individuals[i-1].fitness=individual1.fitness+0;
Population.individuals[i].fitness=individual2.fitness+3;
} }}}``
return newPopulation;
}
// Crossover individuals
private static Individual crossover(Individual indiv1, Individual indiv2) {
Individual newSol = new Individual();
// Loop through genes
for (int i = 0; i < indiv1.size(); i++) {
// Crossover
if (Math.random() <= uniformRate) {
newSol.setGene(i, indiv1.getGene(i));
} else {
newSol.setGene(i, indiv2.getGene(i));
}
}
return newSol;
}
// Mutate an individual
private static void mutate(Individual indiv) {
// Loop through genes
for (int i = 0; i < indiv.size(); i++) {
if (Math.random() <= mutationRate) {
// Create random gene
long gene = Math.round(Math.random());
indiv.setGene(i, gene);
}
}
}
// Select individuals for crossover
private static Individual tournamentSelection(Population pop) {
// Create a tournament population
Population tournament = new Population(tournamentSize, false);
// For each place in the tournament get a random individual
for (int i = 0; i < tournamentSize; i++) {
int randomId = (int) (Math.random() * pop.size());
tournament.saveIndividual(i, pop.getIndividual(randomId));
}
// Get the fittest
Individual fittest = tournament.getFittest();
return fittest;
}
package simpleGa;
public class FitnessCalc {
/* Public methods */
// Set a candidate solution as a byte array
// To make it easier we can use this method to set our candidate solution
// with string of 0s and 1s
// Calculate inidividuals fittness by comparing it to our candidate solution
static int getFitness(Individual individual) {
int fitness = 0;
// Loop through our individuals genes and compare them to our cadidates
fitness=Individual.fitness;
return fitness;
}
}
// Get optimum fitness
}
package simpleGa;
import java.util.Scanner;
public class GAprisonerdilemma {
public static void main(String[] args) {
// Set a candidate solution
Scanner keyboard = new Scanner(System.in);
System.out.println("Input number of games!");
int k = keyboard.nextInt();
Individual.setDefaultGeneLength(k);
// Create an initial population
System.out.println("Input number of individuals in the population!");
int p = keyboard.nextInt();
Population myPop = new Population(p, true);
System.out.println("Input acceptable number of generations!");
int l = keyboard.nextInt();
// Evolve our population until we reach an optimum solution
int generationCount = 0;
int j=l+1;
System.out.println("Input requiered fitness value !");
int f = keyboard.nextInt();
int h=0;
// Evolve our population until we reach an optimum solution
for(int i=0;i<j;i++)
{
if(i==0){}
else{
if(myPop.getFittest().getFitness()>=f){if(h==0){h++;}
else{ System.out.println("Solution found!");
System.out.println("Generation: " + generationCount);
System.out.println( "Fitness(Points): " + myPop.getFittest().getFitness());
break;}
}else {myPop = Algorithm.evolvePopulation(myPop);
generationCount++;
System.out.println("Generation: " + generationCount + " Fittest: " + myPop.getFittest().getFitness());
}
if(i==j-1){ if(myPop.getFittest().getFitness()>=f)System.out.println("Solution found !");
else System.out.println("Solution not found closest solution is!");
System.out.println("Generation: " + generationCount);
System.out.println( " Fitness(Points): " + myPop.getFittest().getFitness());}
}
}
System.out.println("0 for betrays in that turn 1 for cooperates!");
System.out.println("Turns:");
System.out.println(myPop.getFittest());
}
}

Java read student records and calculate high low and average for each quiz

Update: Thank you so much for all your input! I figured out that my low returns all 0s because java initializes all integers 0 so I changed it to int lowScore [] = new int [] {100, 100, 100, 100, 100}. The problem with findAvg is that in main i created an array of student [40] but the file only contains 15 students, which is why I cannot use a.length to find average. I've added codes to count the number of lines.
I am working on an assignment about reading a txt file of student ID along with their 5 quiz scores. The assignment is asking to read those scores up to 40 students and calculate the high, low and average for each quiz.
Sample output:
Stud Quiz1 Quiz2 Quiz3 Quiz4 Quiz5
1234 90 100 90 98 80
1243 92 92 90 98 70
High Score: 92 100 90 98 80
Low Score: 90 92 90 98 70
Average Score: 91 96 90 98 75
I've created 4 classes: student class for ID and scores, statistics class to calculate high, low and average, util class for read file function, and driver class for the main.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
class Student {
private int SID;
private int scores[] = new int[5];
public int getSID() {
return SID;
}
public void setSID(int sID) {
SID = sID;
}
public int getScores(int index) {
return scores[index];
}
public void setScores(int[] scores) {
this.scores = scores;
}
public void printSID() {
System.out.println(SID);
}
public void printScores() {
for (int x : scores) {
System.out.println(x);
}
}
}
class Statistics {
private final int[] lowscores = new int[5];
private final int[] highscores = new int[5];
private final float[] avgscores = new float[5];
public void findlow(Student[] a) {
for (Student stu : a) {
for (int i = 0; i < 5; i++) {
lowscores[i] = Math.min(lowscores[i], stu.getScores(i));
}
}
}
public void findhigh(Student[] a) {
for (Student stu : a) {
for (int i = 0; i < 5; i++) {
highscores[i] = Math.max(highscores[i], stu.getScores(i));
}
}
}
public void findavg(Student[] a) {
int[] sum = new int[5];
for (Student stu : a) {
for (int i = 0; i < 5; i++) {
sum[i] += stu.getScores(i);
avgscores[i] = sum[i] / a.length;
}
}
}
public void printLow() {
for (int x : lowscores) {
System.out.println("Low Score " + x);
}
}
public void printHigh() {
for (int x : highscores) {
System.out.println("High Score " + x);
}
}
public void printAvg() {
for (float x : avgscores) {
System.out.println("Average " + x);
}
}
}
class Util {
static Student[] readFile(String filename, Student[] stu) {
try {
FileReader file = new FileReader(filename);
BufferedReader buff = new BufferedReader(file);
int count = 0;
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
break;
else {
System.out.println(line);
if (count > 0) {
StringTokenizer st = new StringTokenizer(line);
for (int i = 0; i < stu.length; i++) {
stu[i] = new Student();
}
stu[count - 1].setSID(Integer.parseInt(st.nextToken()));
int scores[] = new int[5];
int scoreCount = 0;
while (st.hasMoreTokens()) {
scores[scoreCount] = Integer.parseInt(st.nextToken());
scoreCount++;
stu[count - 1].setScores(scores);
}
}
}
count++;
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
return stu;
}
}
public class Driver{
public static void main(String[] args) {
Student lab4[] = new Student[40];
lab4 = Util.readFile("C:\\lab4.txt", lab4);
Statistics statlab4 = new Statistics();
statlab4.findlow(lab4);
statlab4.findhigh(lab4);
statlab4.findavg(lab4);
statlab4.printLow();
statlab4.printHigh();
statlab4.printAvg();
}
}
The program reads an input file lab4.txt which includes 1 line of header and 15 lines of student records. The program runs but does not calculate the high low and average correctly. I know my calculation for average might be wrong; but I don't know why high and low don't work.
Please help me. Thank you!
For the find average, you shouldn't divide by a.length every iteration.
public void findavg(Student []a) {
int []sum = new int [5];
for(Student stu: a) {
for(int i=0; i<5; i++) {
sum[i] += stu.getScores(i);
}
}
for(int i=0; i<5; i++) {
avgscores[i] = sum[i] / a.length;
}
}
The min and max seems fine.
Not going to rewrite your code for you, just going to ask some questions because I think some people are still a bit confused about your purpose:
Is the purpose of the findAvg method to find the average of all the students marks for a particular test? Or to find the student's average mark for all of their tests?
Moving the Statistics class inside the methods of the Student class (as done by some of the other answers) would work if the latter is your goal, but I wouldn't mind keeping them separate if you're going for the former.
If you're trying to find the highest mark of a group of students, then I'd get them all into an Array or ArrayList and then try an ascending sort (or do them both at the same time).
public float avgCalculate(Student [] a, int testnumber){
float sum = 0.0f;
for (Student s: a){
sum = sum + s.getScores[testnumber];
}
float average = (sum / a.length);
return average;
}
Code is completely untested, so make sure it works and get back to me. It should calculate the average scores of a bunch of students (a) on a specific test (test number). Hopefully you can extrapolate the rest of the code (you may need to modify your other classes a bit etc.) For the highs and lows consider using the Arrays.sort() library method.
Since you've mentioned the Statistics class is required, I've taken another crack at your issue. And I see that when you calculate the highs and lows, the first loop is the students. Instead, make the first loop for the appropriate array, like so:
public void findLow(Student[] a) {
for (int i = 0; i < 5; i++)
for (Student stu : a)
lowScores[i] = Math.min(stu.getScore(i), lowScores[i]);
}
What this then does is:
for each low score you want to find:
for each student that took the quiz:
if this student's score is lower, set that as the lowest
Apply the same logic to your findHigh method.
For your findAvg method, as previously stated, don't divide in every iteration:
public void findAvg(Student[] a) {
for (int i = 0; i < 5; i++) {
for (Student s : a) {
avgScores[i] += s.getScore(i);
}
avgScores[i] /= a.length;
}
}

Categories

Resources