简体   繁体   中英

How do I print using toString using inheritance in Java

public class StuTest2
{
   public static final int NUMBER_OF_STUDENTS = 7;

   public static void main(String[] args)
   {
  Student[] stus = new Student[NUMBER_OF_STUDENTS];

  // Student has ID, name, and GPA
  stus[0] = new Student(123, "Suzy", 3.9);

  // Default for missing GPA will be 9.99 "special value
  stus[1] = new Student(234, "Tom");

  // Default name will be "Student #xxx" where
  // "xxx" is the actual ID number
  stus[2] = new Student(456);

  // A grad student also has a thesis topic
  stus[3] = new GradStudent(567, "Fred", 3.8, "Java");

  // Default thesis topic is "Undecided"
  stus[4] = new GradStudent(678, "Staci", 3.1);

  // Doctoral students earn a stipend
  stus[5] = new DoctoralStudent(789, "Mandy", 4.0, "Databases", 3550.00);

  // If missing, the default stipend is $3000.00
  stus[6] = new DoctoralStudent(890, "Ned", 3.7, "Cisco Networking");

  // Inside the loop, the toString method is called for each
  // student. All graduate students show the word "Graduate" in
  // front of the output from this method.
  for(Student stu : stus)
  {
  }
 }
}

class Student
  {
   private int id;
   private String name;
   private double gpa;

   public Student(int i, String n, double g)
   {
       id = i;
       name = n;
       gpa = g;
   }

   public Student(int i)
   {
       this(i, "Student #" + i);
   }

   public Student(int i, String n)
   {
       this(i, n, 9.99);
   }

   public int getId()
   {
       return id;
   }

   public String getName()
   {
       return name;
   }

   public double getGPA()
   {
       return gpa;
   }

   public String toString()
   {
       return System.out.println(stus.getId+", " + stus.getName
            + ", " + stus.getGPA);
   }

  }

class GradStudent extends Student
{
private String topic;

public GradStudent(int i, String n, double g, String t)
{
    super(i, n, g);
    topic = t;
}

public GradStudent(int i, String n, double g)
{
    this(i, n, g, "Undecided");
}

public String getTopic()
{
    return topic;
}

public String toString()
{
    return super.getTopic();
}
}

class DoctoralStudent extends GradStudent
{
private double stip;

public DoctoralStudent(int i, String n, double g, String t, double s)
{
    super(i, n, g, t);
    stip = s;
}

public DoctoralStudent(int i, String n, double g, String t)
{
    this(i, n, g, t, 3000.00);
}

public double getStip()
{
    return stip;
}

public String toString()
{
    return super.getStip();
}

}

I'm trying to print out while using the return super.toString(), but Iget errors saying cannot find symbol for stus, but I have it right before starting the student class. What gives? ps, sorry for the bad closings, trying to meet standards on here lol

Your "stus" variable is only in scope inside the main() method, so you can't access it outside of that method. Furthermore, "stus" is an array, so it doesn't even make sense to call getId on it. Further, notice that getId refers to a variable since it doesn't have parenthesis after it.

Keep in mind that in your toString() method, you're already "inside" a Student Object, so you can just call the getId() function directly:

 public String toString()
   {
       return getId() +", " + getName() + ", " + getGPA();
   }

Also note that I've removed the System.out.println() function in your toString method, since it doesn't return anything and therefore doesn't make sense to return anyway.

You've got a lot of incorrect syntax in your code, and I highly recommend starting much smaller. You'll have much better luck if you develop your program incrementally instead of trying to do the whole thing in one shot. I recommend starting over and compiling and testing with every single line you add.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM