Page 1 of 1

Please help me with this lab. Student Class - Student.java package lab10; public class Student { private String studentI

Posted: Sat May 14, 2022 4:26 pm
by answerhappygod
Please help me with this lab.
Please Help Me With This Lab Student Class Student Java Package Lab10 Public Class Student Private String Studenti 1
Please Help Me With This Lab Student Class Student Java Package Lab10 Public Class Student Private String Studenti 1 (105.66 KiB) Viewed 59 times
Student Class - Student.java
package lab10;
public class Student
{
private String studentId;
private String firstName;
private String lastName;
private double[] Tests;
/**
* default Student constructor --
*/
public Student()
{
studentId = new String("None");
firstName = new String("None");
lastName = new String("None");
//create space for 4 scores
Tests = new double[4];
for (int i = 0; i < Tests.length; i++)
Tests = 0.0;
}
/**
* parameterized Student constructor --
* initializes instance variables to user-specified
values
* @param id the student id to be stored
* @param first the student's first name to be
stored
* @param last the student's last name to be
stored
* @param scores an array of Test scores to be stored in
Tests
*/
public Student(String id, String first, String last,
double[] scores)
{
studentId = id;
firstName = first;
lastName = last;
//create space for the scores
Tests = new double[scores.length];
for (int i = 0; i < Tests.length; i++)
Tests = scores;
}
/**
* setStudentId method --
* stores the student id passed to the method in the
instance
* variable studentId
* @param studentId the student id to be
stored
*/
public void setStudentId(String studentId)
{
this.studentId = new String(studentId);
}
/**
* setFirstName method --
* stores the first name passed to the method in the
instance
* variable firstName
* @param firstName the first name to be
stored
*/
public void setFirstName(String firstName)
{
this.firstName = new String(firstName);
}
/**
* setLastName method --
* stores the last name passed to the method in the
instance
* variable lastName
* @param lastName the last name to be
stored
*/
public void setLastName(String lastName)
{
this.lastName = new String(lastName);
}
/**
* setTests method --
* stores the Test scores passed to the method in the
instance
* variable Tests
* @param scores an array of Test scores to be stored in
Tests
*/
public void setTests(double[] scores)
{
// check to see if scores is a different length than
Tests
if (scores.length != Tests.length)
{
// if the lengths are different then re-create the Tests
array so it is the same size as scores
Tests = new double[scores.length];
}
for(int i = 0; i < scores.length; i++)
Tests = scores;
}
/**
* getId method --
* @return the student's id
*/
public String getStudentId()
{
return studentId;
}
/**
* getFirstName method --
* returns the value of the instance variable
firstName
* @return the student's first name
*/
public String getFirstName()
{
return firstName;
}
/**
* getLastName method --
* returns the value of the instance variable
lastName
* @return the student's last name
*/
public String getLastName()
{
return lastName;
}
/**
* getTests method --
* returns a reference to a copy of the array containing
the Test scores
* @return a reference to an array containing the Test
scores
*/
public double[] getTests()
{
double[] temp = new double[Tests.length];
// copy the values into a new array
for (int i = 0; i < Tests.length; i++)
temp = Tests;
// return the copy of the array
return temp;
}
/**
* calcAverage method --
* calculates the average of the Test scores
* @return the average of the Tests
*/
public double calcAverage()
{
double sum = 0.0;
for (int i = 0; i < Tests.length; i++)
sum += Tests;
return (sum / Tests.length);
}
/**
* toString method --
* returns the state of the object as a
string
* @return the id, name, and Test scores along with
appropriate
* labels, as a string
*/
public String toString()
{
String studentInfo = new String();
studentInfo = "Student Id: " + studentId + "\nName: "
+
firstName
+ " " + lastName + "\nTests: ";
for (int i = 0; i < Tests.length; i++)
studentInfo = studentInfo + Tests + " ";
return studentInfo;
}
}
public class Course
{
//instance
variables
private Student students[];
private int numStudents;
/**
* Default constructor --
* Creates storage for at most 30 students and sets the
number of
* students in the course to 0.
*/
public Course() {
this.students = new Student[30];
this.numStudents = 0;
}
/**
* getNumStudents --
* Returns the number of students currently in the
course.
* @return numStudents
*/
public int getNumStudents() {
return numStudents;
}
/**
* insertStudent --
* Adds a student to the course if there's room. If the
course is full,
* it doesn't do anything.
* @param newStudent - a reference to the student to be
added
*/
public void insertStudent(Student student)
{
if (numStudents < students.length) {
students[numStudents] = student;
numStudents++;
}
}
/**
* calcTestAverage --
* Calculates the average for a particular
Test.
* @param TestNum - the Test number to calculate the
average for
* @return the average for the specified
Test
*/
public double calcTestAverage(int TestNum){
double sum = 0.0;
for(int i = 0; i < numStudents; i++)
sum += students.getTests()[TestNum - 1];
return sum/numStudents;
}
/**
* findLowestTest --
* Determines the index of the student in the students
array with the lowest score
* for a particular Test.
*
* @param TestNum - an integer representing the Test
number to find the lowest
* Test score for
* @return the index of the student with the lowest Test
score for the
* specified Test
*/
public int findLowestTest(int TestNum)
{
int
lowestTestIndex = -1;
if
(TestNum >= 1 && TestNum <= 4) {
double
lowestScore = 100.0;
for
(int i = 0; i < numStudents; i++) {
if
(students[i].getTests()[TestNum - 1] < lowestScore)
{
lowestScore
= students[i].getTests()[TestNum - 1];
lowestTestIndex
= i;
}
}
}
return
lowestTestIndex;
}
// courseData.txt -
Lab #11 - Arrays of Objects – The Student and Course Classes (Continued) Due Date: Friday 11:59 pm - late code accepted up to 3 days late with a late penalty Goals: Practice using arrays of objects • Explore the use of the Scanner class to read from an input file Important Background Information: Files retain data for future use and hold considerably more data than can be easily be entered from the keyboard, printed on paper, or displayed on the screen. Typical file operations include: • creating a file and filling it (known as "writing to a file") getting the contents of a file (known as "reading from a file") • overwriting a file (keeping the same name but changing its contents -- a variation on writing to a file) In this lab, we are going to read from a file. Exceptions: In order to use a file, you need to modify the header of the main method. The new one is: public static void main(String[] args) throws Exception The addition of throws Exception states that the main method contains code that may cause an error (exception). When files are used, the possibility of an unrecoverable error increases significantly since you are now expanding your interaction with your computing environment. As an example, if your program tries to read from a file that doesn't exist, your program will crash. Since these types of errors (exceptions) are more severe, you must either handle them (topic covered in a future course) or acknowledge that the error may occur. We simply acknowledge this. Reading from a File (File Input): In the “real world”, the input to most programs comes from a file, not the user. Reading from a file means there can be a great deal of data supplied to the program and because this data is already stored in a file, it can be read (looked at, used, etc.) repeatedly without having to type it in. You will create a File object for the file you want to read from and then pass this object to the Scanner constructor so that you can use the Scanner class to read the information from the file. Include the following import statements to make the necessary libraries available: import java.io.*; // makes the file class available import java.util.*; // makes the Scanner class available Start Eclipse & Open an Existing Project We will be working again with the Student and Course files from Lab #10. Open the lab 10 project to continue. Create a new application class, Lab11App, in the lab10 project. Add a Javadoc comment block at the top that includes a title, description (to be filled in at the end) and your names as the authors. The package for each file should be lab10. Writing Java Statements Lab#11 - CSC 120 - Arrays of Objects (Continued) - Page 1 of 6

Write syntactically correct Java statements to complete each of the following. Pay attention to which class the statements should go in. Remember to comment each method in the Course class and each code segment in the application class. 1. Download the input file, courseData.txt. This file can be found at Blackboard Labs > Lab 11 > courseData.txt. Save it to your workspace]\lab 10. Open and take a look at the data stored in the course Data.txt file. How many students are in the file? In what order is the data stored for an individual student? In the Lab11App class: You should refer to the Scanner handout as needed. 2. Include the necessary import statements to access the File and Scanner classes. 3. Create the main method - be sure to acknowledge that an exception may occur. 4. Instantiate a Course object, referenced by the Course, using the default constructor. 5. Instantiate a Scanner object, referenced by fileScan, using the parameterized constructor. You will need to create a new File object that refers to the course Data.txt file you copied to the project directory in your workspace. Refer to the Reading a File document. 6. You will use the nextLine() method of the Scanner class to read each line of the file. The nextLine() method reads and returns a reference to a String. If you need to convert that String to an integer, use the parseInt() method of the Integer class. If you need to convert that String to a double, use the parseDouble() method of the Double class. Read the first line in the file which contains the number of students in the file and store the value appropriately. 7. Write a loop that will iterate through the students in the file (ie. the number you read in #6). 8. Write code to read the id, last name and first name from the file and store them appropriately. Recall that each of these will be references to Strings. 9. Write code to read the four exam grades and store them in an array of doubles. Be sure to use a loop to do this. This loop will be nested inside the loop that iterates through the students in the file. Recall that the loop control variables must be different (i vs. j). 10. Create a new Student object using the parameterized constructor passing the values read from the file. 11. Insert this reference to a Student object into the Course. 12. After all student data has been read from the file, call the close method on the Scanner object. fileScan.close(); Lab#11 - CSC 120 - Arrays of Objects (Continued) - Page 2 of 6

13. You should now have students in the course. To display them in a dialog box, type the following statement: System.out.println("Students in the course:\n" + the Course); Verify that all students are there. Notice that the students are listed in the order they were in the file. See expected output on page 4 and 5. In the Student class: 14. Write a compare To method that accepts a reference to a Student object. The method should compare the id of this student to the id of the student whose reference was passed to the method. You should think about the data type of the id and how you can compare variables of this type. (Hint: See the compare To method of the String class.] The method should return; the negative value returned from the String class compare To method if the id of this student is BEFORE the id of the other student O if the ids are the same • the positive value returned from the String class compareTo method if the id of this student is AFTER the id of the other student In the Course class: 15. Write a findStudent method that accepts a reference to a Student object. The method should locate the student in the array whose id matches the id of the student whose reference was passed to the method. Be sure to call the compare To method written in #14. If the student is found, the method should return the position where the Student reference is stored in the array. If the student is not found, the method should return -1. Write appropriate Javadoc for this method. In the Lab11App class: 16. Call the findStudent method for the following students in the Course. 123-45-6789 765-43-2100 999-99-9999 666-66-6666 Think about what needs to be passed to the method and how the argument will be used to locate the student in the array. You may want to create a "dummy" Student object with the appropriate id to use an argument. What will be returned? You should display the value returned for each of the test cases along with an following message: "Position of the student with id xxx-xx-xxxx:y" Where XXX-XX-XXxx is the id and y is the position. See expected output on page 5. In the Course class: Lab#11 - CSC 120 - Arrays of Objects (Continued) - Page 3 of 6

17. Write a deleteStudent method that accepts a reference a Student object. The method should locate the student and remove him/her from the course. You will need to shift students to fill in the gap keeping the current order. Don't forget to decrement numStudents accordingly. This method should return true if the student was successfully deleted and false the student was not successfully deleted. Write appropriate Javadoc for this method. In the Lab11App class: 18. Call the delete Student method for the following students in the Course: 765-43-2100 999-99-9999 123-45-6789 666-66-6666 Think about what needs to be passed to the method and what is returned. You should use a similar testing concept as was needed for #16. 19. After each call to the delete Student method, display the number of students in the course with the following message: "Delete student xxx-xx-xxxx. Number of students in the course: y." Where xxx-xx-xxxx is the id of the student just deleted, and y is the number of students in the course. See expected output on page 5. 20. After testing the delete Student method with each of the id's listed above, display the students in the Course to verify that they have been deleted from the Course. See expected output on page 5. 21. Be sure to fill in the description in the Javadoc at the beginning of Lab11App with an explanation of what the program does, as well as any Javadoc for new methods. Expected Output: 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Students in the course: Student Id: 123-45-6789 Name: Homer Simpson Exams: 80.0 55.0 65.0 40.0 Student Id: 135-79-2468 Name: Marge Simpson Exams: 80.0 85.0 30.0 85.0 Student Id: 111-22-3333 Name: Bart Simpson Exams: 65.0 60.0 50.0 60.0 Student Id: 555-33-1111 Name: Lisa Simpson Exams: 99.0 100.0 99.5 95.5 Student Id: 222-34-5555 Name: Maggie Simpson Exams: 30.0 30.0 30.0 25.0 Student Id: 876-54-3210 Name: Ned Flanders Exams: 70.0 75.5 79.9 72.25 Student Id: 321-54-9876 Name: Maude Flanders Lab#11 - CSC 120 - Arrays of Objects (Continued) - Page 4 of 6

22 Exams: 60.0 62.0 64.0 66.0 23 Student Id: 765-43-2100 24 Name: Todd Flanders 25 Exams: 77.0 79.0 82.0 66.0 26 Student Id: 888-44-2222 27 Name: Nelson Muntz 28 Exams: 0.0 0.0 0.0 0.0 29 Student Id: 333-44-5555 30 Name: Ralph Wiggum 31 Exams: 70.0 70.0 60.0 70.0 32 Student Id: 858-31-2424 33 Name: Moe Szyslak 34 Exams: 75.0 77.0 73.0 80.0 35 Student Id: 999-99-9999 36 Name: Montgomery Burns 37 Exams: 100.0 100.0 100.0 50.0 38 Position of the student with id 123-45-6789: 0 39 Position of the student with id 765-43-2100: 7 40 Position of the student with id 999-99-9999: 11 41 Position of the student with id 666-66-6666: -1 42 Delete student 765-43-2100. Number of students in the course : 11 43 Delete student 999-99-9999. Number of students in the course: 10 44 Delete student 123-45-6789. Number of students in the course: 9 9 45 Delete student 666-66-6666. Number of students in the course: 9 46 | Students in the course: 47 Student Id: 135-79-2468 48 Name: Marge Simpson 49 Exams: 80.0 85.0 80.0 85.0 50 Student Id: 111-22-3333 51 Name: Bart Simpson 52 Exams: 65.0 60.0 50.0 60.0 53 Student Id: 555-33-1111 54 Name: Lisa Simpson 55 Exams: 99.0 100.0 99.5 95.5 56 Student Id: 222-34-5555 57 Name: Maggie Simpson 58 Exams: 30.0 30.0 30.0 25.0 59 Student Id: 876-54-3210 60 Name: Ned Flanders 61 Exams: 70.0 75.5 79.9 72.25 62 Student Id: 321-54-9876 63 Name: Maude Flanders 64 Exams: 60.0 62.0 64.0 66.0 65 Student Id: 888-44-2222 66 Name: Nelson Muntz 67 Exams: 0.0 0.0 0.0 0.0 68 Student Id: 333-44-5555 69 Name: Ralph Wiggum 70 Exams: 70.0 70.0 60.0 70.0 71 Student Id: 858-31-2424 72 Name: Moe Szyslak 73 Exams: 75.0 77.0 73.0 80.0 Lab#11 - CSC 120 - Arrays of Objects (Continued) - Page 5 of 6

22 Exams: 60.0 62.0 64.0 66.0 23 Student Id: 765-43-2100 24 Name: Todd Flanders 25 Exams: 77.0 79.0 82.0 66.0 26 Student Id: 888-44-2222 27 Name: Nelson Muntz 28 Exams: 0.0 0.0 0.0 0.0 29 Student Id: 333-44-5555 30 Name: Ralph Wiggum 31 Exams: 70.0 70.0 60.0 70.0 32 Student Id: 858-31-2424 33 Name: Moe Szyslak 34 Exams: 75.0 77.0 73.0 80.0 35 Student Id: 999-99-9999 36 Name: Montgomery Burns 37 Exams: 100.0 100.0 100.0 50.0 38 Position of the student with id 123-45-6789: 0 39 Position of the student with id 765-43-2100: 7 40 Position of the student with id 999-99-9999: 11 41 Position of the student with id 666-66-6666: -1 42 Delete student 765-43-2100. Number of students in the course : 11 43 Delete student 999-99-9999. Number of students in the course: 10 44 Delete student 123-45-6789. Number of students in the course: 9 9 45 Delete student 666-66-6666. Number of students in the course: 9 46 | Students in the course: 47 Student Id: 135-79-2468 48 Name: Marge Simpson 49 Exams: 80.0 85.0 80.0 85.0 50 Student Id: 111-22-3333 51 Name: Bart Simpson 52 Exams: 65.0 60.0 50.0 60.0 53 Student Id: 555-33-1111 54 Name: Lisa Simpson 55 Exams: 99.0 100.0 99.5 95.5 56 Student Id: 222-34-5555 57 Name: Maggie Simpson 58 Exams: 30.0 30.0 30.0 25.0 59 Student Id: 876-54-3210 60 Name: Ned Flanders 61 Exams: 70.0 75.5 79.9 72.25 62 Student Id: 321-54-9876 63 Name: Maude Flanders 64 Exams: 60.0 62.0 64.0 66.0 65 Student Id: 888-44-2222 66 Name: Nelson Muntz 67 Exams: 0.0 0.0 0.0 0.0 68 Student Id: 333-44-5555 69 Name: Ralph Wiggum 70 Exams: 70.0 70.0 60.0 70.0 71 Student Id: 858-31-2424 72 Name: Moe Szyslak 73 Exams: 75.0 77.0 73.0 80.0 Lab#11 - CSC 120 - Arrays of Objects (Continued) - Page 5 of 6

Use code Post to submit the following (each student must submit the work individually). Late code accepted up to 3 days late with a late penalty: 0 O Lab11App.java should be uploaded to the assignment “Lab 11 - LabllApp.java" Course.java should be uploaded to the assignment "Lab 11 - Course.java" Student.java should be uploaded to the assignment “Lab 11 - Student.java" 100% of your lab grade will be automatically graded by codePost. Did you comment each logical grouping of statements with descriptive single line comments? Did you appropriately use if statements and if/else statements in the correct type of scenario? Use simple if statements where appropriate, use nested if-else statements where appropriate. Did you use loops where instructed to and where appropriate? Did you use the appropriate type of loop for a particular scenario? Did you write the simplest and most efficient code possible? o Does your code have appropriate line spacing for readability? Is the Javadoc complete and sufficient? o o o Lab#11 - CSC 120 - Arrays of Objects (Continued) - Page 6 of 6