Set-Up ·
Create a new project in your Eclipse workspace named: Lab13 ·
In the src folder, create a package named: edu.ilstu ·
Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java
o StudentList.java
o students.txt
Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code.
Create a .java class called StudentListTester.java with a main method. In that class, write a program that tests all capabilities of the StudentList class. When writing the list to a file, use “studentout.txt”.
We have provided the students.txt file as an example data file.
The files need to be imported will be shown below
Student.java:
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* Student class maintains information about a particular
student
*
* @author Mary Elaine Califf
*/
public class Student
{
private String name;
private String major;
private double gpa;
private int hoursCompleted;
/**Default constructor
*
*/
public Student()
{
super();
this.name = "";
this.major = "";
this.gpa = 0.0;
this.hoursCompleted = 0;
}
/**Constructor
* @param name
* The student's name
* @param major
* The student's major
*/
public Student(String name, String major)
{
super();
this.name = name;
this.major = major;
this.gpa = 0.0;
this.hoursCompleted = 0;
}
/**Constructor
* @param name
* The student's name
* @param major
* The student's major
* @param gpa
* The student's cumulative gpa
* @param hoursCompleted
* Number of credit hours the student has completed
*/
public Student(String name, String major, double gpa, int
hoursCompleted)
{
super();
this.name = name;
this.major = major;
this.gpa = gpa;
this.hoursCompleted = hoursCompleted;
}
/**
* @return The student's major.
*/
public String getMajor()
{
return this.major;
}
/**
* @param major The major to set.
*/
public void setMajor(String major)
{
this.major = major;
}
/**
* @return The student's name.
*/
public String getName()
{
return this.name;
}
/**
* @param name The name to set.
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return The student's gpa.
*/
public double getGpa()
{
return this.gpa;
}
/**
* @return The number of credit hours the student has
completed.
*/
public int getHoursCompleted()
{
return this.hoursCompleted;
}
/**
* @return The student's class (Freshman, Sophomore, Junior,
Senior)
*/
public String getClassLevel()
{
String classLevel;
if (this.hoursCompleted < 30)
{
classLevel = "Freshman";
}
else if (this.hoursCompleted < 60)
{
classLevel = "Sophomore";
}
else if (this.hoursCompleted < 90)
{
classLevel = "Junior";
}
else
{
classLevel = "Senior";
}
return classLevel;
}
/**Update student's information to reflect completion of a
semester's work
* @param semHours
* Number of credit hours the student has just completed
* @param semGPA
* GPA for the semester just completed
*/
public void updateStudent(int semHours, double semGPA)
{
int oldHours = this.hoursCompleted;
this.hoursCompleted = oldHours + semHours;
this.gpa = (oldHours * this.gpa + semHours * semGPA) /
this.hoursCompleted;
}
/**Produces a string with information about a student in a nice
format with
* appropriate labeling
*/
public String toString()
{
DecimalFormat fmt = new DecimalFormat("0.000");
String myString = this.name + " is a " + getClassLevel() + "
"
+ this.major + "\n"
+ "who has completed " + this.hoursCompleted + " hours
" +
"with a " + fmt.format(this.gpa) + " gpa.\n";
return myString;
}
/** Reads student data in file format with name on first line,
hours and gpa
* on second line, and major on third line.
* @param input
* Scanner connected to input source for student information
*/
public void read(Scanner input)
{
this.name = input.nextLine();
this.hoursCompleted = input.nextInt();
this.gpa = input.nextDouble();
input.nextLine();
this.major = input.nextLine();
}
/**
* Prints student information in file format
* @param output
* PrintWriter connected to open output stream
*/
public void write(PrintWriter output)
{
output.println(this.name);
output.println(this.hoursCompleted + " " +
this.gpa);
output.println(this.major);
}
}
StudentList.java:
import java.io.*;
/**
* A class to maintain an array of students
*/
public class StudentList
{
// add appropriate instance variables here: need an
array of Students
// and a way to keep track of how many students are in
the list
// assume there will never be more than 100
students
// call your array stuArray
// provide a default constructor that sets up an empty
student array
// provide a private helper method that accepts a
Student's name and
// returns the index of the student in the
array or -1 if the student
// is not in the array. This method should
be called by the
// displayStudent method
/**Reads a list of students from a file
* @param fileName
* Name of the file to read from
*/
public void readList(String fileName)
{
// fill in code to read a list of
students from a file
// into the array -- this should
end with StudentList
// consisting of exactly those
students in the file--so
// the first student in the file
will be the first student
// in the array, and all operations
on the list will only
// affect the set of students in
the file until additional
// students are added using the
addStudent method below.
// Make sure you don't overfill the
array.
// For each student, create the
Student object using the default
// constructor and then use the
read method of Student to get
// the data from the file
}
/** Writes a list of students to a file
* @param fileName
* Name of the file to write to
*/
public void writeList(String fileName)
{
PrintWriter outfile = null;
try
{
outfile = new
PrintWriter(new FileWriter("output.txt"));
// use a for
loop to write all of the Student objects from
// the array to
output.txt by calling the Student's write
// method and
passing it outfile
outfile.close();
}
catch (IOException e)
{
System.out.println("An error occurred: "+e);
System.out.println("The list of students was not written");
}
}
/** Add a student to the end of the list
* @param aStudent
* The student to add
*/
public void addStudent(Student aStudent)
{
// write code to add a student to
the end of the list of students only if there is room
}
/**
* @param studentName
*/
public void displayStudent(String studentName)
{
// fill in missing pieces of the
following code and uncomment it
// add appropriate code to handle
an incorrect name
/*
int index;
index = // fill in code here
;
System.out.println(this.stuArray[index]);
*/
}
// Write a method to count the number of freshmen,
sophomores, juniors and seniors in the array
// and print the counts to the screen. The Student
class has a getClassLevel method that
// returns a String with the value "Freshman",
"Sophomore", "Junior" or "Senior". Use an array
// to store your counts.
// add an appropriate javadoc comment
public void printClassCounts()
{
}
}
students.txt
Ann Hawkins
16 3.6
English
Miles Kimball
33 3.8
Computer Science
Mary Ellen de Silva
94 2.97
Arts Technology
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
students.txt (input file)
Ann Hawkins
16 3.6
English
Miles Kimball
33 3.8
Computer Science
Mary Ellen de Silva
94 2.97
Arts Technology
_________________________
// Student.java
package edu.ilstu;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* Student class maintains information about a particular
student
*
* @author Mary Elaine Califf
*/
public class Student {
private String name;
private String major;
private double gpa;
private int hoursCompleted;
/**
* Default constructor
*
*/
public Student() {
super();
this.name = "";
this.major = "";
this.gpa = 0.0;
this.hoursCompleted = 0;
}
/**
* Constructor
*
* @param name
* The student's name
* @param major
* The student's major
*/
public Student(String name, String major) {
super();
this.name = name;
this.major = major;
this.gpa = 0.0;
this.hoursCompleted = 0;
}
/**
* Constructor
*
* @param name
* The student's name
* @param major
* The student's major
* @param gpa
* The student's cumulative gpa
* @param hoursCompleted
* Number of credit hours the student has
completed
*/
public Student(String name, String major, double gpa,
int hoursCompleted) {
super();
this.name = name;
this.major = major;
this.gpa = gpa;
this.hoursCompleted =
hoursCompleted;
}
/**
* @return The student's major.
*/
public String getMajor() {
return this.major;
}
/**
* @param major
* The major to set.
*/
public void setMajor(String major) {
this.major = major;
}
/**
* @return The student's name.
*/
public String getName() {
return this.name;
}
/**
* @param name
* The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return The student's gpa.
*/
public double getGpa() {
return this.gpa;
}
/**
* @return The number of credit hours the student has
completed.
*/
public int getHoursCompleted() {
return this.hoursCompleted;
}
/**
* @return The student's class (Freshman, Sophomore,
Junior, Senior)
*/
public String getClassLevel() {
String classLevel;
if (this.hoursCompleted < 30)
{
classLevel =
"Freshman";
} else if (this.hoursCompleted <
60) {
classLevel =
"Sophomore";
} else if (this.hoursCompleted <
90) {
classLevel =
"Junior";
} else {
classLevel =
"Senior";
}
return classLevel;
}
/**
* Update student's information to reflect completion
of a semester's work
*
* @param semHours
* Number of credit hours the student has just
completed
* @param semGPA
* GPA for the semester just completed
*/
public void updateStudent(int semHours, double semGPA)
{
int oldHours =
this.hoursCompleted;
this.hoursCompleted = oldHours +
semHours;
this.gpa = (oldHours * this.gpa +
semHours * semGPA)
/ this.hoursCompleted;
}
/**
* Produces a string with information about a student
in a nice format with
* appropriate labeling
*/
public String toString() {
DecimalFormat fmt = new
DecimalFormat("0.000");
String myString = this.name + " is
a " + getClassLevel() + " "
+ this.major + "\n" + "who has completed "
+ this.hoursCompleted + " hours " + "with a
"
+ fmt.format(this.gpa) + " gpa.\n";
return myString;
}
/**
* Reads student data in file format with name on first
line, hours and gpa
* on second line, and major on third line.
*
* @param input
* Scanner connected to input source for student
information
*/
public void read(Scanner input) {
this.name = input.nextLine();
this.hoursCompleted =
input.nextInt();
this.gpa =
input.nextDouble();
input.nextLine();
this.major =
input.nextLine();
}
/**
* Prints student information in file format
*
* @param output
* PrintWriter connected to open output stream
*/
public void write(PrintWriter output) {
output.println(this.name);
output.println(this.hoursCompleted
+ " " + this.gpa);
output.println(this.major);
}
}
_________________________
// StudentList.java
package edu.ilstu;
import java.io.*;
import java.util.Scanner;
/**
* A class to maintain an array of students
*/
public class
package edu.ilstu;
import java.io.*;
import java.util.Scanner;
/**
* A class to maintain an array of students
*/
public class StudentList
{
// add appropriate instance variables here: need an array of
Students
// and a way to keep track of how many students are in the
list
// assume there will never be more than 100 students
// call your array stuArray
private Student stdArray[]=null;
private int counter;
// provide a default constructor that sets up an empty student
array
public StudentList() {
stdArray=new Student[100];
counter=0;
}
// provide a private helper method that accepts a
Student's name and
// returns the index of the student in the array or -1 if the
student
// is not in the array. This method should be called by the
// displayStudent method
public int getStudentIndx(String name)
{
int indx=-1;
for(int i=0;i<counter;i++)
{
if(stdArray[i].getName().equalsIgnoreCase(name))
indx=i;
}
return indx;
}
/**Reads a list of students from a file
* @param fileName
* Name of the file to read from
*/
public void readList(String fileName)
{
// fill in code to read a list of students from a file
// into the array -- this should end with StudentList
// consisting of exactly those students in the file--so
// the first student in the file will be the first student
// in the array, and all operations on the list will only
// affect the set of students in the file until additional
// students are added using the addStudent method below.
// Make sure you don't overfill the array.
// For each student, create the Student object using the
default
// constructor and then use the read method of Student to get
// the data from the file
Scanner sc=null;
String name,major,str;
int hours;
double gpa;
try {
sc=new Scanner(new
File(fileName));
while(sc.hasNext())
{
if(stdArray.length!=counter)
{
name=sc.nextLine();
str=sc.nextLine();
String
arr[]=str.split(" ");
hours=Integer.parseInt(arr[0]);
gpa=Double.parseDouble(arr[1]);
major=sc.nextLine();
Student s=new
Student(name, major, gpa, hours);
addStudent(s);
}
}
} catch (FileNotFoundException e) {
System.out.print("** File not found
**");
}
}
/** Writes a list of students to a file
* @param fileName
* Name of the file to write to
*/
public void writeList(String fileName)
{
PrintWriter outfile = null;
try
{
outfile = new PrintWriter(new FileWriter(fileName));
// use a for loop to write all of the Student objects from
// the array to output.txt by calling the Student's write
// method and passing it outfile
for(int i=0;i<counter;i++)
{
stdArray[i].write(outfile);
}
outfile.close();
}
catch (IOException e)
{
System.out.println("An error occurred: "+e);
System.out.println("The list of students was not written");
}
}
/** Add a student to the end of the list
* @param aStudent
* The student to add
*/
public void addStudent(Student aStudent)
{
// write code to add a student to the end of the list of students
only if there is room
stdArray[counter]=aStudent;
counter++;
}
/**
* @param studentName
*/
public void displayStudent(String studentName)
{
// fill in missing pieces of the following code and uncomment
it
// add appropriate code to handle an incorrect name
int index;
index =getStudentIndx(studentName);
if(index==-1)
{
System.out.println("Student Not Found");
}
else
{
System.out.println(stdArray[index]);
}
}
// Write a method to count the number of freshmen,
sophomores, juniors and seniors in the array
// and print the counts to the screen. The Student class has a
getClassLevel method that
// returns a String with the value "Freshman", "Sophomore",
"Junior" or "Senior". Use an array
// to store your counts.
// add an appropriate javadoc comment
public void printClassCounts()
{
// int cntFreshMan=0,cntSophomore=0,cntJunior=0,cntSenior=0;
int cnts[]=new int[4];
for(int i=0;i<counter;i++)
{
if(stdArray[i].getClassLevel().equalsIgnoreCase("Freshman"))
{
cnts[0]++;
}
if(stdArray[i].getClassLevel().equalsIgnoreCase("Sophomore"))
{
cnts[1]++;
}
if(stdArray[i].getClassLevel().equalsIgnoreCase("Junior"))
{
cnts[2]++;
}
if(stdArray[i].getClassLevel().equalsIgnoreCase("Senior"))
{
cnts[3]++;
}
}
System.out.println("No of Freshman :"+cnts[0]);
System.out.println("No of Sophomore :"+cnts[1]);
System.out.println("No of Junior :"+cnts[2]);
System.out.println("No of Senior :"+cnts[3]);
}
}
{
// add appropriate instance variables here: need an array of
Students
// and a way to keep track of how many students are in the
list
// assume there will never be more than 100 students
// call your array stuArray
private Student stdArray[]=null;
private int counter;
// provide a default constructor that sets up an empty student
array
public StudentList() {
stdArray=new Student[100];
counter=0;
}
// provide a private helper method that accepts a
Student's name and
// returns the index of the student in the array or -1 if the
student
// is not in the array. This method should be called by the
// displayStudent method
public int getStudentIndx(String name)
{
int indx=-1;
for(int i=0;i<counter;i++)
{
if(stdArray[i].getName().equalsIgnoreCase(name))
indx=i;
}
return indx;
}
/**Reads a list of students from a file
* @param fileName
* Name of the file to read from
*/
public void readList(String fileName)
{
// fill in code to read a list of students from a file
// into the array -- this should end with StudentList
// consisting of exactly those students in the file--so
// the first student in the file will be the first student
// in the array, and all operations on the list will only
// affect the set of students in the file until additional
// students are added using the addStudent method below.
// Make sure you don't overfill the array.
// For each student, create the Student object using the
default
// constructor and then use the read method of Student to get
// the data from the file
Scanner sc=null;
String name,major,str;
int hours;
double gpa;
try {
sc=new Scanner(new
File(fileName));
while(sc.hasNext())
{
if(stdArray.length!=counter)
{
name=sc.nextLine();
str=sc.nextLine();
String
arr[]=str.split(" ");
hours=Integer.parseInt(arr[0]);
gpa=Double.parseDouble(arr[1]);
major=sc.nextLine();
Student s=new
Student(name, major, gpa, hours);
addStudent(s);
}
}
} catch (FileNotFoundException e) {
System.out.print("** File not found
**");
}
}
/** Writes a list of students to a file
* @param fileName
* Name of the file to write to
*/
public void writeList(String fileName)
{
PrintWriter outfile = null;
try
{
outfile = new PrintWriter(new FileWriter(fileName));
// use a for loop to write all of the Student objects from
// the array to output.txt by calling the Student's write
// method and passing it outfile
for(int i=0;i<counter;i++)
{
stdArray[i].write(outfile);
}
outfile.close();
}
catch (IOException e)
{
System.out.println("An error occurred: "+e);
System.out.println("The list of students was not written");
}
}
/** Add a student to the end of the list
* @param aStudent
* The student to add
*/
public void addStudent(Student aStudent)
{
// write code to add a student to the end of the list of students
only if there is room
stdArray[counter]=aStudent;
counter++;
}
/**
* @param studentName
*/
public void displayStudent(String studentName)
{
// fill in missing pieces of the following code and uncomment
it
// add appropriate code to handle an incorrect name
int index;
index =getStudentIndx(studentName);
if(index==-1)
{
System.out.println("Student Not Found");
}
else
{
System.out.println(stdArray[index]);
}
}
// Write a method to count the number of freshmen,
sophomores, juniors and seniors in the array
// and print the counts to the screen. The Student class has a
getClassLevel method that
// returns a String with the value "Freshman", "Sophomore",
"Junior" or "Senior". Use an array
// to store your counts.
// add an appropriate javadoc comment
public void printClassCounts()
{
// int cntFreshMan=0,cntSophomore=0,cntJunior=0,cntSenior=0;
int cnts[]=new int[4];
for(int i=0;i<counter;i++)
{
if(stdArray[i].getClassLevel().equalsIgnoreCase("Freshman"))
{
cnts[0]++;
}
if(stdArray[i].getClassLevel().equalsIgnoreCase("Sophomore"))
{
cnts[1]++;
}
if(stdArray[i].getClassLevel().equalsIgnoreCase("Junior"))
{
cnts[2]++;
}
if(stdArray[i].getClassLevel().equalsIgnoreCase("Senior"))
{
cnts[3]++;
}
}
System.out.println("No of Freshman :"+cnts[0]);
System.out.println("No of Sophomore :"+cnts[1]);
System.out.println("No of Junior :"+cnts[2]);
System.out.println("No of Senior :"+cnts[3]);
}
}
_____________________________
// StudentListTester.java
package edu.ilstu;
import java.util.Scanner;
public class StudentListTester {
public static void main(String[] args)
{
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
Student stdArr[]=new
Student[3];
for(int
i=0;i<stdArr.length;i++)
{
stdArr[i]=new
Student();
}
for(int
i=0;i<stdArr.length;i++)
{
System.out.println("Enter Student#"+(i+1)+" Data :");
stdArr[i].read(sc);
}
StudentList sl=new
StudentList();
for(int
i=0;i<stdArr.length;i++)
{
sl.addStudent(stdArr[i]);
}
stdArr[0].updateStudent(14,3.9);
System.out.println("Tony is found
at index :"+sl.getStudentIndx("Tony"));
System.out.println("\"Tony\"
Student Info :");
sl.displayStudent("Tony");
System.out.println();
sl.readList("students.txt");
sl.writeList("output.txt");
sl.printClassCounts();
}
}
_________________________
Output( console):
Enter Student#1 Data :
Kane Williams
12
3.5
Computer Science
Enter Student#2 Data :
Bean Johnson
8
3.2
Electronics
Enter Student#3 Data :
Tony
5
3.7
Mechanical
Tony is found at index :2
"Tony" Student Info :
Tony is a Freshman Mechanical
who has completed 5 hours with a 3.700 gpa.
No of Freshman :4
No of Sophomore :1
No of Junior :0
No of Senior :1
________________________
// output.txt (output file)
Kane Williams
26 3.715384615384615
Computer Science
Bean Johnson
8 3.2
Electronics
Tony
5 3.7
Mechanical
Ann Hawkins
16 3.6
English
Miles Kimball
33 3.8
Computer Science
Mary Ellen de Silva
94 2.97
Arts Technology
_______________Could you plz rate me well.Thank You
Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...
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)...
PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed....
A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...
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 *****************************************************/...
Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...
I need help for part B and C 1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...
FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...
DO NOT COPY AND PASTED!!!!!!!!!!!!! IF YOU DO NOT HOW TO DO IT, JUST DO NOT ANSWER MY QUESTIONS package scheduler; import java.util.List; public class Scheduler { /** * Instantiates a new, empty scheduler. */ public Scheduler() { } /** * Adds a course to the scheduler. * * @param course the course to be added */ public void addCourse(Course course) { } /** * Returns the list of courses that this scheduler knows about. * * This returned object...
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(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...