4

So for my beginners programming class we had to code a program focusing on inheritance where we could create and enter information for different kinds of college students.

I have the program and the output working just fine, but my professor wants us to format the output like so:

When entering a new student information, he wants a student's info to be printed out neatly using a toString() method like this:

Choice:
Student Name:
Student ID:
Number of Credits:

However at the end of the program, he wants us to format the output using printf so that it looks like so:

Type of Student:
(tab)Student Name:
(tab)Student ID:
(tab)Number of Credits:
(tab)Different info based on type of student:
(tab)Tuition:

Here is my code for the original toString() method in the student superclass, the Undergrad and Grad subclasses just call this and then add on their specific attributes to the end:

public String toString()
{
  return "Student Name: " + getName() + "\n" + "Student ID: " + getId() + "\n" + "Number of Credits: " + getNumCredits();
}

And here is the code in the test class where I try to format that:

for(Student s: enrollment)
  {
     if(s instanceof Undergrad)
     {
        System.out.printf("Undergraduate Student: ");
        System.out.printf("%n\t" + s.toString());
        System.out.printf("%n\tTuition: " + s.calcTuition());
        System.out.printf("%n");
     }
     else
     {
        System.out.printf("%nGraduate Student: ");
        System.out.printf("%n\t" + s.toString());
        System.out.printf("%n\tTuition: " + s.calcTuition());
        System.out.printf("%n");
     }
  }            

However, this only tabs in the Name line of the toString() method and Tuition output. My professor wants us to use printf for this but I'm not sure how to make it apply to all three lines of the toString() method. I can't edit the toString() method itself because that would screw up the formatting of the non-final printouts. What am I doing wrong?

Abbey S
  • 111
  • 2
  • 15

2 Answers2

0

You can just add the tab to the return string:

return "\tStudent Name: " + getName() + "\n" + "\tStudent ID: " + getId() + "\n" + "\tNumber of Credits: " + getNumCredits();
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

You're struggling with the fact, that your Student class has too many responsibilities, namely knowing the Student's properties and formatting output for two different cases. Instead of defining toString inside Student, try to define a different class StudentPrinter that provides two methods: printFinal and printNonFinal. Both take an object of type Student and provide the needed output.

See also the Single Responsibility Principle.

blubb
  • 9,510
  • 3
  • 40
  • 82
  • I would but unfortunately the toString method is a required part of the homework. We're only allowed to make the 4 classes I have listed which include Student, Undergrad, Grad, and TestStudent... I normally would just tab each line in the to String method, but that would screw up the formatting for the first half of the assignment when my professor doesn't want there to be a tab there... – Abbey S Nov 03 '16 at 00:49
  • @AbbeyS: I was afraid you were going to mention such restrictions. It looks like your professor wants to keep you in a well-guarded environment. It's probably best if you ask a tutor, since he has the exact context necessary to give you the right hints. Very well-structured question, though, so welcome to StackOverflow! Keep posting :) – blubb Nov 03 '16 at 00:52