THINK FIRST·CODE LATER

← All labs

People on campus: constructors and super

Problem

Write Person (private name and birth year, getName(), ageIn(int year), role() → Person, toString() → Person Mei (born 1990)), and two subclasses:

  • CampusStudent(name, birthYear, major, year) — role() → Student, toString() adds , CS, year 2;
  • Professor(name, birthYear, department, office) — role() → Professor, toString() adds , Math, office GEH-210.

Each subclass constructor must call super(name, birthYear), and each toString() must reuse super.toString().

CampusPeople.main reads the current year, then lines S …, P … or V name birthYear (a visitor, plain Person) until the end of input. Print every person, 2 students, 1 professors, 4 people (count with instanceof) and Oldest: Chen, 58 in 2026.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l14-5-data-1.txt

2026
S Lina 2005 CS 2
P Chen 1968 Math GEH-210
S Omar 2004 Finance 3
V Mei 1990
What your program must print for l14-5-data-1.txt
Student Lina (born 2005), CS, year 2
Professor Chen (born 1968), Math, office GEH-210
Student Omar (born 2004), Finance, year 3
Person Mei (born 1990)
2 students, 1 professors, 4 people
Oldest: Chen, 58 in 2026

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l14-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content l14-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.