Question & Answer: rogramming and Logic I Project 3 – Student Assignment In this project, you will – input student data…..

Programming and Logic I Project 3 – Student Assignment In this project, you will – input student data from a file – create student objects – add student objects to an array – write the student object data to standard output. Download the input text file studentInput3.txt. This file contains four sets of data. Each set of data includes four lines that correspond to one student’s data. The first line is the student’s first and last name, the second line is the student’s id number, the third is the student’s year and the fourth contains the student’s major. Modify your RunStudent program from project 2 to create a Student array, read the sets of data, create a student object, add the object to the array, and print the array. There are many different ways to do this, but we will do it in the most basic way. The first question is: do we know how many sets of data are in the file? If we do, and if this number is not likely to change, a for loop and an array are fine to use. An array has a set size that cannot be changed. So, if we know we will only have 10 students, we can create a Student array of size 10 and use a for loop to traverse the input file. If we have a file, but do not know how many students are contained in the file, an array might not be our best bet. An ArrayList has room for 10 elements by default, but can grow and shrink. An ArrayList has methods to add elements that do the work for us if the list gets too large for the current size. But deep down inside, an ArrayList contains an array. For this project, we know that we will only have four students, so we will create an array of size 4. Since we have only four students, this means we will have four sets of data. This makes reading from the file easy: a for loop that executes four times and reads the four lines of data during each loop iteration. Reading the data from the file can be tricky. It would be best to get this part of the program working first before you proceed to the rest of the program. It is always important to make sure the data structures appropriately contain the data. The last step will be to create a method in the RunStudent class that writes the content of the array to standard output. Your program will do the following: – Open the input file for reading (hard code the name of the input file – studentInput3.txt). – Create an array of size 4 of type Student – Create a Student variable (and other variables as needed) – The for loop should execute four times and do the following: o Read the data for one student from the input file o Use the Student variable to create a Student object o Add the student object to the array (use the loop control variable as the index to the array) – Finally, after the loop has completed execution, call the method printStudentArray() and send to this method the Student array. This method will be created in the RunStudent class. It takes one parameter – a Student array – and prints the contents of the array. The printStudentArray method will be contained in the RunStudent class. The following is the method header: public static void printStudentArray(Student[] s) Notes – You only need ONE Student object. Declare the Student variable before the for loop begins and create a new Student object using a constructor method inside the loop – The student’s year is stored as a char but printed as the entire word. – printStudentArray should call the display method in the Student class – no need to reinvent the wheel here Upload Student.java and RunStudent.java

Expert Answer

 

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: rogramming and Logic I Project 3 – Student Assignment In this project, you will – input student data…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

Solution===============================================

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

//Barebone student class
class Student{
String firstName;
String lastName;
int ID;
char year;
String major;

public void display(){
System.out.println(“Student: “+firstName+” “+lastName+”, Id: “+ID+”, Year:”+year+”, Major: “+major);
}
}

public class RunStudent {
private Student students[];

public RunStudent(){
//Allocating memory for 4 students
students=new Student[4];
}

public void printStudentArray(Student[] s){
//Only one instance is so created, as requirement
//Instance named student
for(Student student:s){
student.display();
}
}

public void populateDataFromFile() throws FileNotFoundException{
Scanner in = new Scanner(new File(“studentInput3.txt”));
//Getting data from file to objects
for(int i=0;i<4;i++){
//Creating an object
Student student = new Student();
//Assigning values from file
String[] names=in.nextLine().split(” “);
student.firstName=names[0];
student.lastName=names[1];
student.ID=Integer.parseInt(in.nextLine());
student.year=in.nextLine().charAt(0);
student.major=in.nextLine().trim();

//Putting the object inside the array
students[i]=student;

}

in.close();
}

public static void main(String[] args) throws FileNotFoundException {
RunStudent run = new RunStudent();
run.populateDataFromFile();
run.printStudentArray(run.students);

}

}

SnapShot==================================================

Question & Answer: rogramming and Logic I Project 3 - Student Assignment In this project, you will - input student data..... 1

Input File Used:==========================================

Question & Answer: rogramming and Logic I Project 3 - Student Assignment In this project, you will - input student data..... 2

Still stressed from student homework?
Get quality assistance from academic writers!