JAVA programming help Does my JAVA code below meet these coding standards? CODIN
ID: 3725517 • Letter: J
Question
JAVA programming help
Does my JAVA code below meet these coding standards?
CODING STANDARDS
• All classes should be public
• Every class should have a print method
• Every class must have a default constructor
• Use packages for every lab staring lab 5
• all properties (instance or static) should be declared private
• class name must start with uppercase
• must have public instance methods
• main() should be in its own class and can have method for input or for automating testing
• one class per .java file
• must add a default constructor in each class that is used for creating objects
• structure of class
o Instance and static variables
o Constructors
o Instance methods
o Static methods
_________________________________________________________________________________________________________
public class Driver
{
public static void main(String[] args)
{
Student arrStudent[] = new Student[40];
int studentCount=0;
arrStudent = Util.readFile("C:\Users\JamesJr\OneDrive\Documents\School\CURRENT QUARTER\Winter 2018\Java 35A\Assignments\Assignment 5\StudentScores.txt", arrStudent);
// find number of lines from file. which will show number of students in array.
studentCount= Util.studentCount;
Statistics s = new Statistics();
// print student data fetched from file
for(int i = 0; i<studentCount; i++)
{
arrStudent[i].printData();
}
// print statistics of students
s.printStatistics(arrStudent,studentCount);
}
}
______________________________________________________________________________________________________________
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Util
{
static int studentCount;
static Student[] readFile(String filename, Student[] stu)
{
int i = 0;
try
{
FileReader file = new FileReader(filename);
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
boolean firstLineSkipped = false;
while (!eof)
{
String line = buff.readLine();
if (line == null)
{
eof = true;
}
else
{
if (!firstLineSkipped)
{
firstLineSkipped = true;
continue;
}
stu[i] = new Student();
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens())
{
stu[i].setSID(Integer.parseInt(st.nextToken()));
int[] arr = new int[5];
for (int j = 0; j < 5; j++)
{
arr[j] = Integer.parseInt(st.nextToken());
}
stu[i].setScores(arr);
}
}
i++;
}
buff.close();
}
catch (IOException e)
{
System.out.printf("Error -- " + e.toString());
}
studentCount = i-1;
return stu;
}
}
_____________________________________________________________________________________________________
public class Statistics
{
private int[] lowscores= new int[5];
private int[] highscores= new int[5];
private float[] avgscores= new float[5];
void findlow(Student[] a, int c)
{
// finds low score.
int i=0;
for(i=0;i<5; i++)
{
int[] temp= a[0].getScores();
int min = temp[i];
for(int j=0; j< c; j++)
{
int[] s= a[j].getScores();
if(min > s[i])
{
min = s[i];
}
}
lowscores[i]= min;
}
}
//find high score
void findhigh(Student[] a, int c)
{
int i=0;
for(i=0;i<5; i++)
{
int[] temp= a[0].getScores();
int max = temp[i];
for(int j=0; j< c; j++)
{
int[] s= a[j].getScores();
if(max < s[i])
{
max = s[i];
}
}
highscores[i]= max;
}
}
//find average score.
void findavg(Student[] a, int c)
{
int i=0;
for(i=0;i<5; i++)
{
int sum =0;
for(int j=0; j<c; j++)
{
int[] s= a[j].getScores();
sum= sum + s[i];
}
avgscores[i]= sum/c;
}
}
//print statistics for all students and all quizes
public void printStatistics(Student[] a, int c)
{
findlow(a,c);
findhigh(a,c);
findavg(a,c);
System.out.printf(" Lowest score : " + lowscores[0] + " " + lowscores[1] + " " + lowscores[2] + " " + lowscores[3] + " " + lowscores[4] + " ");
System.out.printf(" High score : " + highscores[0] + " " + highscores[1] + " " + highscores[2] + " " + highscores[3] + " " + highscores[4] + " ");
System.out.printf(" Average score : " + avgscores[0] + " " + avgscores[1] + " " + avgscores[2] + " " + avgscores[3] + " " + avgscores[4] + " ");
}
}
____________________________________________________________________________________________________
public class Student
{
private int SID;
private int scores[] = new int[5];
//get and set functions for scores and ID
public int getSID()
{
return this.SID;
}
public void setSID(int id)
{
this.SID= id;
}
public int[] getScores()
{
return this.scores;
}
public void setScores(int[] s)
{
this.scores= s;
}
//print all data of a student
public void printData()
{
System.out.printf(" "+ SID +" "+ scores[0] +" "+ scores[1] +" "+ scores[2] + " "+ scores[3] +" "+ scores[4] +" ");
}
}
Explanation / Answer
• All classes should be public - Yes. All are public
• Every class should have a print method - Yes, except main() method. But it is calling the print methods indirectly. So it is printing some statements.
• Every class must have a default constructor - No. Noe of the classes satisfied this rule.
• Use packages for every lab staring lab 5 - No. Packages statement is not declared.
• all properties (instance or static) should be declared private - No. In Driver class and Util class this property not satisfied.
• class name must start with uppercase - Yes. All satisfied this one.
• must have public instance methods - No. Driver class and Util class doesn't have public instanc methods.
• main() should be in its own class and can have method for input or for automating testing - Yes
• one class per .java file - Yes
• must add a default constructor in each class that is used for creating objects - No. None of them have default constructor.
• structure of class
o Instance and static variables - Only Util class has static variable. Remaining classes don't have.
o Constructors - None of the classes have constructors.
o Instance methods - Util class and Driver classes don't have any instance methods.
o Static methods - Util class and Driver classes have static methods. Remaining doesn't
Please comment if you need any modification.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.