The answer provided is a part of registration system where a user can view the registration
details for all the students. It makes use of advanced arrays and inheritance as per requirements of the question.
package com.company;
import java.util.Scanner;
// Base Class Vehicle
class Person{
// Private Fields
private String first_name;
private String middle_name;
private String last_name;
private String year_of_birth;
// Parameterized Constructor
public Person(String first_name, String middle_name, String last_name, String year_of_birth) {
this.first_name = first_name;
this.middle_name = middle_name;
this.last_name = last_name;
this.year_of_birth = year_of_birth;
}
// public method to print details
public void printDetails() {
System.out.println("\t\tFirst name: " + first_name);
System.out.println("\t\tMiddle name: " + middle_name);
System.out.println("\t\tLast name: " + last_name);
System.out.println("\t\tYear of birth: " + year_of_birth);
}
}
// Derived Class Car
class Student extends Person {
// Private field
private String registration_number;
private String program;
// Parameterized Constructor
public Student(String first_name, String middle_name, String last_name, String year_of_birth,
String registration_number, String program) {
super(first_name, middle_name, last_name, year_of_birth); //calling parent class constructor
this.registration_number = registration_number;
this.program = program;
}
public void StudentDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("\t\tRegistration number: " + registration_number);
System.out.println("\t\tProgram : " + program);
}
}
class Main {
public static void main(String[] args) {
//An array to store the data of cars available for sale
String[][] students_registration = {
{"Kelly", "daniel", "Nyamai", "1998","COM/B/01-00068/2018","Computer Science"},
{"Martin", "Charles", "puth", "1996", "MEG/D/03-3465/2016","Mechanical Engeneering"},
{"Charity","Nekesa ", "moreen"," 2000","MED/B/01-00012/2019","Nursing and midwifery"},
{"wambui","Everlyn","Muriuki"," 2001", "BCM/b/00124/2020","Bachelor of Commerce"}
};
//MEnu
System.out.println("\nWelcome to Havard university registration sysytem\n");
System.out.println("1.View all the students registration details");
System.out.println("2.Exit the program");
System.out.print("Enter your option: ");
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
if(option == 1)
{
for(int i = 0; i<students_registration.length;i++)
{
System.out.print(i+1);
Student my_student = new Student(students_registration[i][0],students_registration[i][1],
students_registration[i][2], students_registration[i][3], students_registration[i][4],
students_registration[i][5]); //creation of car Object
my_student.StudentDetails(); //calling method to print details
}
}
else if(option==2)
{
System.out.println("Program exited successfully");
}
else
{
System.out.println("Invalid option please try again");
}
}
}
Comments
Leave a comment