Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a sequence of names, ages, gpa-s, and zip codes for u

ID: 3715166 • Letter: W

Question

Write a program that reads a sequence of names, ages, gpa-s, and zip codes for up to 50 students from an input file.  Store the data in an array that has capacity to hold 50 Student objects. Keep track of number of students in variable count. Class Student has name, of String type, and age of int type, gpa of type double,  and zip of String type.  Assume that each line of input has name, age, gpa, zip data in this order and separated by a space.

Design class StudentList that has the following static methods for an array of Student type.

static void printList(Student[] list, int n)             // prints all n students in the list


static int zipOfYoungest(Student[] list, int n)    // returns the zip code of youngest student


                                                                                  //in the list

static Student oldestStudent(Student[] list, int n) // returns the oldest student in the list


static int countNewBritain((Student[] list, int n) // returns the number of students in zip 06050


static void printListReversed(Student[] list, int n)  // prints students from n-th one


                                                                                       // down to the first one in the list.

Class Tester will have main method. In it read data from input file "inData.txt" into array studentList, assign variable count, and invoke each of the five methods. You should create an input file, and put data for 7 students in it.

First three students in the input file should be:

Sara 21 4.0 06050

Tommy 39 3.5 06111

Lisa 19 3.2 06050

and remaining four of your choice.

File should be in the same folder where all files from BlueJ for this program are located. Do not forget to append throws IOException to the main method header, and to use

import java.io.*;  

import java.util.*;

clauses in the  Tester class.

Explanation / Answer

Student.java

package students;

public class Student {

private String name;

private int age;

private double gpa;

private String zip;

public Student(String name, int age, double gpa, String zip) {

this.name = name;

this.age = age;

this.gpa = gpa;

this.zip = zip;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public double getGpa() {

return gpa;

}

public void setGpa(double gpa) {

this.gpa = gpa;

}

public String getZip() {

return zip;

}

public void setZip(String zip) {

this.zip = zip;

}

@Override

public String toString() {

return "Name: "+name+" Age: "+age+" GPA: "+gpa+" Zip: "+zip;

}

}

StudentList.java contains main method:

package students;

import java.io.*;

import java.util.*;

public class StudentList {

public static void main(String[] args) throws IOException {

Student[] students=new Student[20];

File file = new File("D:/workspace/Test/src/students/inData.txt");

Scanner input = new Scanner(file);

int i=0;

int n=7;

while(input.hasNextLine()){

String line=input.nextLine();

String[] values=line.split("\s");

String name=values[0];

int age=Integer.parseInt(values[1]);

double gpa=Double.parseDouble(values[2]);

String zip=values[3];

Student student=new Student(name, age, gpa, zip);

students[i]=student;

i++;

}

Student[] originalOrder=Arrays.copyOf(students, n);

printList(students, n);

System.out.println("Zip of Youngest: "+zipOfYoungest(students, n));

System.out.println("Oldest Student Details: "+oldestStudent(students,n));

System.out.println("Count New Britain: "+countNewBritain(students,n));

printListReversed(originalOrder,n);

input.close();

}

// prints all n students in the list

public static void printList(Student[] list, int n){

System.out.println(n +" Student Details: ");

for(int i=0;i<n;i++){

System.out.println(list[i].toString());

}

}

//returns the zip code of youngest student in the list

public static String zipOfYoungest(Student[] list, int n){

Student temp=list[0];

for(int i=0;i<n;i++){

for (int j = 1; j < (n - i); j++) {

if (list[j - 1].getAge() > list[j].getAge()) {

// swap elements

temp = list[j - 1];

list[j - 1] = list[j];

list[j] = temp;

}

}

}

return list[0].getZip();

}

// returns the oldest student in the list

public static Student oldestStudent(Student[] list, int n){

Student temp=list[0];

for(int i=0;i<n;i++){

for (int j = 1; j < (n - i); j++) {

if (list[j - 1].getAge() < list[j].getAge()) {

// swap elements

temp = list[j - 1];

list[j - 1] = list[j];

list[j] = temp;

}

}

}

return list[0];

}

// returns the number of students in zip 06050

public static int countNewBritain(Student[] list, int n) {

int countNewBritain=0;

for(int i=0;i<n;i++){

if(list[i].getZip().equals("06050")){

countNewBritain++;

}

}

return countNewBritain;

}

//prints students from n-th one down to the first one in the list.

public static void printListReversed(Student[] list, int n){

System.out.println("Students in reverse order:");

for(int i=n-1;i>=0;i--){

System.out.println(list[i].toString());

}

}

}

inData.txt

Sara 21 4.0 06050

Tommy 39 3.5 06111

Lisa 19 3.2 06050

John 25 3.8 06121

Anna 35 3.3 06050

Greg 22 3.5 06111

kate 29 4.0 06112

Sample output:

7 Student Details:
Name: Sara Age: 21 GPA: 4.0 Zip: 06050
Name: Tommy Age: 39 GPA: 3.5 Zip: 06111
Name: Lisa Age: 19 GPA: 3.2 Zip: 06050
Name: John Age: 25 GPA: 3.8 Zip: 06121
Name: Anna Age: 35 GPA: 3.3 Zip: 06050
Name: Greg Age: 22 GPA: 3.5 Zip: 06111
Name: kate Age: 29 GPA: 4.0 Zip: 06112
Zip of Youngest: 06050
Oldest Student Details:
Name: Tommy Age: 39 GPA: 3.5 Zip: 06111
Count New Britain: 3
Students in reverse order:
Name: kate Age: 29 GPA: 4.0 Zip: 06112
Name: Greg Age: 22 GPA: 3.5 Zip: 06111
Name: Anna Age: 35 GPA: 3.3 Zip: 06050
Name: John Age: 25 GPA: 3.8 Zip: 06121
Name: Lisa Age: 19 GPA: 3.2 Zip: 06050
Name: Tommy Age: 39 GPA: 3.5 Zip: 06111
Name: Sara Age: 21 GPA: 4.0 Zip: 06050

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote