Lab 05 - Review Random by Mary (Optional) | CS 163/4: Java Programming (CS 1) | CS 163/4: Java Programming (CS 1) | CS 163/4: Java Programming (CS 1) HomeTopicsResourcesPoliciesGradingLabsPracticals | Canvas CS 163/4: Java Programming (CS 1) Computer Science HomeTopicsResourcesPoliciesGradingLabsPracticalsCanvas Lab 05 - Review Random by Mary (Optional) Introduction Today’s lab will help prepare you for your first midterm and reinforce your learning. You will work with many of the concepts you have learned thus far in the semester. If you have any concerns or struggle with any of the tasks today, feel free to ask a TA. Java Docs, zyBooks, and lecture notes will also be valuable resources. Step 1: Constructors, Classes, and Objects Create a class, constructor, and main method from scratch. Class This class will be called Student, and should simulate storing data about a CSU student, like their name, credits, ID number, transfer status, and class status. Declare the class in the file. Class Variables Then, add class variables to hold the various data, including: the student’s name the student’s number of credits (0-120+) the student’s id number (a nine digit number ranging from 800,000,000+) the student’s transfer status (Hint: Think about what variable type this should be, if it only has two possible values, yes or no, true or false) the student’s class status (Possible values: “new”, “Freshman”, “Sophomore”, “Junior”, “Senior”, “Graduated!”) Constructor - Student(String name, int credits, int id) Create a constructor for the student class. It should take in parameters for the student’s name, credits, and ID number. The constructor should initialize the class variables for name, credits and ID number with the parameter values. Then, if the number of credits is 0, the student is not a transfer student (Yes, this program oversimplifies factors like credit earned in high school). Therefore, assign the value of the class variable representing whether a student is a transfer depending on the number of credits. Every student when initially created as an object should have the status be assigned to “new”. Step 2: Method to Progress Students - progressStudent(int semesters) As students progress through college, they earn more credits and their class level changes. Create a method that keeps track of these methods. You will be writing this method from scratch, so here are some specifications: It should depend on an instance of the class to be created since it modifies class variables. Think about what keyword you should use in the signature because of this. It should be named progressStudent. It should take in one parameter, an int named semesters that represents how much time (in semesters) has passed in the student’s career at CSU. The method should increment the class variable representing credits. The number of credits earned should be the number of semesters multiplied by 15, an average course load for a student to graduate in 4 years with no summer courses and no previously earned credit. Then, the method should check the amount of total credits and change the status variable as follows: 120 or more credits: “Graduated!” 90 or more credits: “Senior” 60 or more credits: “Junior” 30 or more credits: “Sophomore” below 30 credits: “Freshman” Step 3: To String Method - toString() The toString() method will be used by the compiler when including an object within a print line arguments, for example: Student s = new Student("Bill Nye", 42, 800000132);
System.out.println(s);
This allows to easily print data about an object of a class without accessing many class variables ourselves each time. Your method signature should be as follows: public String toString(){ }
This method should return a String that looks like the following example, with each category separated by a tab character and values “yes” and “no” substituted for the true/false values of the class variable transfer. Student: Bill Nye ID: 800000132 Credits: 0 Status: new Transfer: no
Step 4: Read Student File Method - readStudentFile(String filename) Write a method that reads in student data from a file and returns the formatted data for each student into a String. This method should have the following signature: public static String readStudentFile(String filename){}
Initialize a scanner to read from the file using the filename given to the method. Initialize another variable to the first possible ID number, 8000000000. The scanner should then read from the file. Using String methods, break the data into the applicable categories to create a Student object that can be called with the toString method and then added to the returned string. The data in the file will be a line for each student, in the format name.credits. The first student created will have the ID number 800000000, and each ID number after that will be one more than the previous student’s ID number. In your returned string, each student’s string representation should be separated by a newline. Step 5: Write Your Own Test Method - testMethod() Write your own test method to check your work. Create it with the following method signature: public static String testMethod(){}
The return values of your test should be concatenated into a single string to be returned from the method. First, create a new Student named Steve Irwin with the ID number 800,000,111. Add that student’s string representation to the string to be returned, along with two new line characters following it. Then progress the Student by 2 semesters using your progressStudent method, followed by two new line characters. Progress the Student by 6 semesters now, followed by two new line characters. Now, add the result of calling readStudentFile() with the file “students” to the string to be returned. Now call the testMethod() from main within a print line statement and check that your methods behave as expected. Computer Science Department 279 Computer Science Building 1100 Centre Avenue Fort Collins, CO 80523 Phone: (970) 491-5792 Fax: (970) 491-2466 Department Courses Degrees Faculty & Staff Prospective Students CS 163/4: Java Programming (CS 1) Computer Programming in Java: Topics include variables, assignment, expressions, operators, booleans, conditionals, characters and strings, control loops, arrays, objects and classes, file input/output, interfaces, recursion, inheritance, and sorting. Apply to CSU Contact CSU Disclaimer Equal Opportunity Privacy Statement © 2021 Colorado State University - Computer Science Department, Fort Collins, CO 80523