Question

Language: Java 30 pts. ) - This assignment revisits the student parsing program from earlier in...

Language: Java

30 pts. ) - This assignment revisits the student parsing program from earlier in the quarter, but challenges you to restructure the component pieces of the program to create a cleaner, more succinct Main(). You will generate a Student class of object and load an Array List

with student objects, then report the contents of that Array List. To do so, you must

perform the following:

A)(10 /30 pts.)-

Generate a class file “myStudent.java” (which will generate myStudent.class upon compile).

myStudent.

java will have the following public methods:

1 ) readData(String fileName)-Reads a .txt file ‘students.txt’

2 ) list(String gender) - List of students whose gender is ‘gender’

3) ageAvg (String gender) - Print the average age (one digit after the decimal point)

of students whose gender is gender’

4) ageBetween (String gender, int min, int max) - Generates a list of students whose

gender is ‘gender’ and age is greater than or equal to ‘min’ and less than ‘max’.

B) (10 /30 pts.) - Once your myStudent.class is functional, generate a file “Assg07_Classes.java”. Assg07_Classes.java should contain only a Main()

method that performs the following:

1 ) Instantiate a new object of the myStudent() class.

2 ) Calls the new object and instructs it to use its .readData() method, passing an argument of a file name.

3 ) Calls the new object and instructs it to use its list() method, passing an argument of a gender.

4 ) Print the result of a call to the new object’s. ageAvg() method, passing an argument of a gender.

5) Call the new object and instruct it to use its .age Between() method, passing an argument of

a gender, a min age and max age

C) (5 /30 pts.) - The main() method of Assg07_Classes.java may not contain more than

5 lines of code.

1 ) This total does not include comments and documentation

2) This total does not include statements mentioned at the above section

B) 3) ~ 5).

3)

This total does not include optional criteria or formatting improvements to your output..









Contents of student.txt:

John m 18

William m 22

Susan f 21

Jack m 19

Jennifer f 18

Sophia f 21

Emma f 19

Olivia f 26

Ava f 23

Chloe f 17

Rachael f 22

Mia f 18

Isabella f 29

Zoe f 17

Lily f 20


0 0
Add a comment Improve this question Transcribed image text
Answer #1

As the instance variable in myStudent clas is a ArrayList of Students I added Student.java also

Student.java

public class Student {

private String name;

private String gender;

private int age;

//constructor

public Student(String name, String gender, int age)

{

this.name = name;

this.gender = gender;

this.age = age;

}

//setters

//method that sets the name

public void setName(String name)

{

this.name = name;

}

//method that sets the gender

public void setGender(String gender)

{

this.gender = gender;

}

//method that sets the age

public void setAge(int age)

{

this.age = age;

}

//getters

//method that returns the name

public String getName()

{

return this.name;

}

//method that return the gender

public String getGender()

{

return this.gender;

}

//method that returns the age

public int getAge()

{

return this.age;

}

//method that displays the details of a student

public void display()

{

System.out.println(String.format("%-15s %-5s %d" , getName(), getGender(), getAge()));

}

}

myStudent.java

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

public class myStudent {

//member variable

private ArrayList<Student> students = new ArrayList<>();

//method that read the student details from a file

public void readData(String fileName)

{

try

{

//open the file

FileReader fr = new FileReader(fileName);

BufferedReader br = new BufferedReader(fr);

String line;

//run a loop until end of file is reached

while((line = br.readLine())!=null)

{

//for each line in the file, split the line based on space

String[] splittedValues = line.split(" ");

//create a Student object with the details and add it to array list

Student s = new Student(splittedValues[0], splittedValues[1], Integer.parseInt(splittedValues[2]));

students.add(s);

}

br.close();

}

//display error message if exception raised

catch(Exception ex)

{

System.out.println("Error in reading the file.");

}

}

//method that generates a list of students of specific gender

public void list(String gender)

{

System.out.println("Students with gender " + gender + " are: ");

//iterate through each student in the list

for(int i = 0; i < students.size(); i++)

{

//if the gender matches, display it

if(students.get(i).getGender().equals(gender))

students.get(i).display();

}

}

//method that prints the average age of students with specific gender

public void ageAvg(String gender)

{

int sum = 0, count = 0;

//iterate through each student in the list

for(int i = 0; i < students.size(); i++)

{

//if the gender matches

if(students.get(i).getGender().equals(gender))

{

//add the age to sum

sum = sum + students.get(i).getAge();

count++;

}

}

//compute and display the average age

double avg = (double)(sum)/(double)(count);

System.out.printf("\nThe average age of students with gender %s is %.1f\n", gender, avg);

}

//method that generates a list of students of specific gender and age between min and max

public void ageBetween(String gender, int min, int max)

{

System.out.println("\nStudents with gender " + gender + " and age between " + min + " and " + max + " are: ");

//iterate through each student in the list

for(int i = 0; i < students.size(); i++)

{

//if the gender matches

if(students.get(i).getGender().equals(gender))

{

//check if student age is between min and max, if yes display it

if(students.get(i).getAge() >= min && students.get(i).getAge() <= max)

students.get(i).display();

}

}

}

}

Assg07_Classes.java

public class Assg07_Classes {

public static void main(String[] args) {

//Instantiate a new object of the myStudent() class.

myStudent s = new myStudent();

//Calls the new object and instructs it to use its .readData() method, passing an argument of a file name.

s.readData("students.txt");

//Calls the new object and instructs it to use its list() method, passing an argument of a gender

s.list("f");

//Print the result of a call to the new object’s. ageAvg() method, passing an argument of a gender

s.ageAvg("f");

//Call the new object and instruct it to use its .age Between() method, passing an argument of a gender, a min age and max age

s.ageBetween("f", 20, 25);

}

}

Output:

Console 3 <terminated> Assq07 Classes Java Applicationl C:Program FilesJavare1.8.0 151binavaw.exe ( Students with gender f are: Susan Jennifer Sophia Emma Olivia Ava Chloe Rachael Mia Isabella Zoe Lily 21 21 19 26 23 17 29 17 20 The average age of students with gender f is 20.9 Students with gender f and age between 20 and 25 are: Susan Sophia Ava Rachael Lily 21 21 23 20

Let me know if you have any concerns with the above solution.

Add a comment
Know the answer?
Add Answer to:
Language: Java 30 pts. ) - This assignment revisits the student parsing program from earlier in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    Description: This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format. FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART The above six fields are separated by a tab key. A user can input a keyword indicating what group of students he is searching for. For example, if...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • Using Python: Par 1. You will define Student class with the following attributes: CWID: the student’s...

    Using Python: Par 1. You will define Student class with the following attributes: CWID: the student’s CWID FirstName: the student’s first name LastName: the student’s last name Gender: the student’s gender (‘M’ or ‘F’) BirthDate: the student’s date of birth (e.g. ‘03/14/1999’) ClassID: the class id that the student took ClassDate: the date when the student took the class (e.g. ‘01/26/2018’) Grade: the student’s grade for the class In addition, you will do the following tasks: Implement a set of...

  • JAVA program Note: you can't change anything is already written Student.java public class Student {   ...

    JAVA program Note: you can't change anything is already written Student.java public class Student {    private String name;    private String major;    private double gpa;    public Student(String name, String major, double gpa) {        // TO-DO: Assign the given parameters to the data fields. Use the this keyword.    }    public double getGPA() {        // TO-DO: return this.gpa    }    public String getName() {        // TO-DO: return this.name    }...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT