if the name is null or an empty string Methods public String getName returns

If the name is null or an empty string methods public

This preview shows page 10 - 13 out of 13 pages.

, if the name is null or an empty string. Methods: public String getName() //returns the person's name public String toString() //also returns the person's name Note that all instance variables in Person are private. They can only be accessed through public methods (such as “getters”). Write a subclass of Person called Student with the following API: Constructor: public Student(String name, long studentNumber) The constructor should throw a DuplicateIDException (a checked exception) if there is already a student with that student number. (Hint: use an ArrayList class variable to keep track of all student numbers.) Methods: public String toString() //returns the the Person's toString() with //the studentNumber included in parentheses public long getStudentNumber() //returns the studentNumber
Image of page 10
11 Write the code for the Student class.
Image of page 11
12 Q9: Consider the following classes and interface: (10 marks) public class Foo { private int i; public Foo(int i) { this.i = i; } @Override public String toString() { } public static void main(String[] args) { Foo x = new Foo(2); Foo y = new FooBar(3); Foo z = new FooBar(); System.out.println("x: " + x); System.out.println("y: " + y); System.out.println("z: " + z); } } public interface Bar { } public class FooBar extends Foo implements Bar{ public FooBar(int i) { super(i); } //Add a no-arg constructor that makes the default value of i = 5 public FooBar() { } }
Image of page 12
13
Image of page 13

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture