CSC1110 Exercise 11

String Formatting

Recall that we can use the printf() function to add formatting to a String output using flags such as %f, %d, and %s as placeholders for values that we can add formatting instructions to. For example, to display pi to five decimal places, we would do the following

System.out.printf("%.5f", Math.PI);

If we want to format a String but not print it to the console, we can use the String.format() method that uses the same syntax and structure as printf(), but returns a formatted String instead, so the above formatting could be done like this:

String.format("%.5f, Math.PI);

The Java documentation on formatting can be a bit tough to read, but can be found here

The Course Enumeration

The Course enumeration is a collection of courses that an MSOE Software Engineering student would be taking. Each course has a code, a title, and a number of credits.

The Student class

The Student class contains a first and last name of the user, a List of Courses they will be taking, and a total number of credits, based on their courses. Use the UML diagram below to implement the Student class

The toString method

The toString() method will be formatted to force each course code to take up 8 spaces, each title to take up 25 spaces and be right justified, and each credit to take 2 spaces. A sample output from the Driver class would look like this

Jones, Sean
Course                       Title Credits
======== ========================= =======
CSC-1110      Software Development  4
COM-1001           College Writing  3
MTH-1110                Calculus I  4
PHY-1110                 Physics I  4
==========================================
                                   15

Jones, Sean
Course                       Title Credits
======== ========================= =======
CSC-1110      Software Development  4
COM-1001           College Writing  3
==========================================
                                    7

The Exercise

Using the information above, implement the Student class, add a new Student object to the Driver class with your Spring class schedule, and print that Student to the console.

Submission Instructions:

Commit and push your code to your GitHUb repository.