Java programming 자바 프로그래밍 실습 : 다형성 polymorphism 추상 클래스 interface을 활용한 예제
2021-1 성균관대학교 소프트웨어학과 타메르 교수님의 자바 프로그래밍 실습 수업을 들으면서 수행한 예제입니다.
Requirements
아래 요구사항들을 사용해서 구현하라
- interface
- abstract class
- inheritance
- polymorphism
- getter and setter methods
- this and super
Task1
아래와 같은 field를 가진 superclass인 Place와 Company, University subclass를 만들어라.
output
source code
public class Task1Place {
String title;
int nRooms;
String location;
public Task1Place(String title, int nRooms, String location) {
setTitle(title);
setnRooms(nRooms);
setLocation(location);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getnRooms() {
return nRooms;
}
public void setnRooms(int nRooms) {
this.nRooms = nRooms;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String toString() {
return "Company: " + getTitle()
+ "\nNumber of rooms: " + getnRooms()
+ "\nLocation: " + getLocation() ;
}
}
public class Task1University extends Task1Place {
int rank;
int nStudents;
int nTeachers;
public Task1University(String title, int nRooms, String location, int rank, int nStudents, int nTeachers) {
super(title, nRooms, location);
setRank(rank);
setnStudents(nStudents);
setnTeachers(nTeachers);
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public int getnStudents() {
return nStudents;
}
public void setnStudents(int nStudents) {
this.nStudents = nStudents;
}
public int getnTeachers() {
return nTeachers;
}
public void setnTeachers(int nTeachers) {
this.nTeachers = nTeachers;
}
public String toString() {
return super.toString()
+ "\nRank: " + getRank()
+ "\nNumber of Students: " + getnStudents()
+ "\nNumber of Teachers: " + getnTeachers();
}
}
public class Task1Company extends Task1Place {
int nEmployees;
double annualincome;
int nDepartments;
public Task1Company(String title, int nRooms, String location, int nEmployees, double annualincome, int nDepartments) {
super(title, nRooms, location);
setnEmployees(nEmployees);
setAnnualincome(annualincome);
setnDepartments(nDepartments);
}
public int getnEmployees() {
return nEmployees;
}
public void setnEmployees(int nEmployees) {
this.nEmployees = nEmployees;
}
public double getAnnualincome() {
return annualincome;
}
public void setAnnualincome(double annualincome) {
this.annualincome = annualincome;
}
public int getnDepartments() {
return nDepartments;
}
public void setnDepartments(int nDepartments) {
this.nDepartments = nDepartments;
}
public String toString() {
return super.toString()
+ "\nNumber of Employees: " + getnEmployees()
+ "\nAnnual Income: " + getAnnualincome()
+ "\nNumber of Department: " + getnDepartments();
}
}
public class Task1 {
public static void main(String[] args) {
// declare objects
Task1Company company = new Task1Company("Samsung Electronics", 100, "Seoul", 200, 10000000, 20 );
Task1University university = new Task1University("SKKU", 200, "Suwon", 89, 100000, 2000);
// print
System.out.println(company);
System.out.println("=========================");
System.out.println(university);
}
}
Task2
아래와 같은 필드와 메소드를 가진 슈퍼 클래스와 서브 클래스를 만들고,
professor, accountant, graudate, undergraduate 객체를 하나씩 만들어 getDetail 메소드를 통해 정보를 출력하라
output
source code
public class Task2Person {
String fullname;
int age;
public Task2Person(String fullname, int age) {
setFullname(fullname);
setAge(age);
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void getDetail() {
System.out.println("Full name: " + getFullname()
+ "\nAge: " + getAge());
}
}
public class Task2Staff extends Task2Person{
String department;
double salary;
public Task2Staff(String fullname, int age, String department, double salary) {
super(fullname, age);
setDepartment(department);
setSalary(salary);
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void getDetail() {
super.getDetail();
System.out.println("Salary: " + getSalary()
+ "\nDepartment: " + getDepartment());
}
}
public class Task2Student extends Task2Person{
String major;
double tuitionFee;
public Task2Student(String fullname, int age, String major, double tuitionFee) {
super(fullname, age);
setMajor(major);
setTuitionFee(tuitionFee);
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getTuitionFee() {
return tuitionFee;
}
public void setTuitionFee(double tuitionFee) {
this.tuitionFee = tuitionFee;
}
public void getDetail() {
super.getDetail();
System.out.println("Major:" + getMajor()
+ "\nTuition Fee: " + getTuitionFee());
}
}
public class Task2Professor extends Task2Staff{
int nResearchers;
String mainCourse;
// constructor
public Task2Professor(String fullname, int age, String department, double salary, int nResearchers,
String mainCourse) {
super(fullname, age, department, salary);
setnResearchers(nResearchers);
setMainCourse(mainCourse);
}
public int getnResearchers() {
return nResearchers;
}
public void setnResearchers(int nResearchers) {
this.nResearchers = nResearchers;
}
public String getMainCourse() {
return mainCourse;
}
public void setMainCourse(String mainCourse) {
this.mainCourse = mainCourse;
}
public void getDetail() {
super.getDetail();
System.out.println("Number of Researchers: " + getnResearchers()
+ "\nMain Course: " + getMainCourse());
}
}
public class Task2Accountant extends Task2Staff{
int officeNo;
public Task2Accountant(String fullname, int age, String department, double salary, int officeNo) {
super(fullname, age, department, salary);
setOfficeNo(officeNo);
}
public int getOfficeNo() {
return officeNo;
}
public void setOfficeNo(int officeNo) {
this.officeNo = officeNo;
}
public void getDetail() {
super.getDetail();
System.out.println("Office Number: " + getOfficeNo());
}
}
public class Task2Graduate extends Task2Student {
String researchInterest;
public Task2Graduate(String fullname, int age, String major, double tuitionFee, String researchInterest) {
super(fullname, age, major, tuitionFee);
setResearchInterest(researchInterest);
}
public String getResearchInterest() {
return researchInterest;
}
public void setResearchInterest(String researchInterest) {
this.researchInterest = researchInterest;
}
public void getDetail() {
super.getDetail();
System.out.println("Research Interest: " + getResearchInterest());
}
}
public class Task2Undergraduate extends Task2Student{
String year;
public Task2Undergraduate(String fullname, int age, String major, double tuitionFee, String year) {
super(fullname, age, major, tuitionFee);
setYear(year);
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public void getDetail() {
super.getDetail();
System.out.println("Year: " + getYear());
}
}
public class Task2 {
public static void main(String[] args) {
Task2Person[] persons = new Task2Person[4];
// create instances of four different subclasses using polymorphism
persons[0] = new Task2Professor("Frank George", 56, "Computing", 10000.00, 45, "Java");
persons[1] = new Task2Accountant("Jack Sparrow", 25, "International", 2300.0, 305);
persons[2] = new Task2Graduate("Firuz Juraev", 29, "Computing", 16000.0, "Healthcare");
persons[3] = new Task2Undergraduate("Mike Shinoda", 19, "COmputing", 12000.0,"Freshman");
// call getDetail of all instances using for loop
for (Task2Person currentPerson : persons) {
System.out.println("===========================");
currentPerson.getDetail();
}
}
}
Task3
Payable 이라는 인터페이스 클래스를 만들고 아래와 같은 추상 클래스와 각 서브 클래그를 만들어라.
오버라이딩 해야 하는 calculatePayment 메소드는 다음과 같이 만들어라.
output
source code
public interface Task3Payable {
String model = "";
double calculatePayment();
void getDetail();
}
public abstract class Task3PublicTransport implements Task3Payable{
String model;
public Task3PublicTransport(String model) {
setModel(model);
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public void getDetail() {
System.out.println( "=========================="
+ "\nTransport type: " + getClass().getName()
+ "\nModel: " + getModel());
}
}
public class Task3Cab extends Task3PublicTransport{
double baseFare;
double farePerKm;
double distance;
public Task3Cab(String model, double baseFare, double farePerKm, double distance) {
super(model);
setBaseFare(baseFare);
setFarePerKm(farePerKm);
setDistance(distance);
}
public double getBaseFare() {
return baseFare;
}
public void setBaseFare(double baseFare) {
this.baseFare = baseFare;
}
public double getFarePerKm() {
return farePerKm;
}
public void setFarePerKm(double farePerKm) {
this.farePerKm = farePerKm;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
@Override
public double calculatePayment() {
return getBaseFare() + (getDistance() * getFarePerKm());
}
@Override
public void getDetail() {
super.getDetail();
System.out.println("Base Fare: " + baseFare + "\nDistance: " + distance
+ "\nFare per km: " + farePerKm
+ "\ntotal Fare: " + calculatePayment());
}
}
public class Task3Bus extends Task3PublicTransport{
int nStations;
double farePerStation;
public Task3Bus(String model, int nStations, double farePerStation) {
super(model);
setnStations(nStations);
setFarePerStation(farePerStation);
}
public int getnStations() {
return nStations;
}
public void setnStations(int nStations) {
this.nStations = nStations;
}
public double getFarePerStation() {
return farePerStation;
}
public void setFarePerStation(double farePerStation) {
this.farePerStation = farePerStation;
}
@Override
public double calculatePayment() {
return getnStations() * getFarePerStation();
}
@Override
public void getDetail() {
super.getDetail();
System.out.println("Fare per stations: " + getFarePerStation()
+ "\nNumber of stations: " + getnStations()
+ "\ntotal Fare: " + calculatePayment());
}
}
public class Task3Subway extends Task3PublicTransport{
double oneTimePayment;
public Task3Subway(String model, double oneTimePayment) {
super(model);
setOneTimePayment(oneTimePayment);
}
public double getOneTimePayment() {
return oneTimePayment;
}
public void setOneTimePayment(double oneTimePayment) {
this.oneTimePayment = oneTimePayment;
}
@Override
public double calculatePayment() {
return getOneTimePayment();
}
@Override
public void getDetail() {
super.getDetail();
System.out.println("One-tiem Payment: " + getOneTimePayment()
+ "\ntotal Fare: " + calculatePayment());
}
}
public class Task3 {
public static void main(String[] args) {
// create instances
Task3Cab cab = new Task3Cab("BMW", 10,5,7);
Task3Bus bus = new Task3Bus("Mercedes-Benz", 10, 1.5);
Task3Subway subway = new Task3Subway("Shinkansen", 80);
// call getDetail methods of instances
cab.getDetail();
bus.getDetail();
subway.getDetail();
}
}
'프로그래밍 언어 > Java' 카테고리의 다른 글
Week8) Java Programming Lab : More on Grapical User Interface (GUI) (0) | 2021.04.12 |
---|---|
Week7) Java Programming Lab : Grapical User Interface (GUI) AWT and Swing APIs (0) | 2021.04.05 |
Week6) Java Programming Lab : Polymorphism (0) | 2021.03.29 |
Java programming 자바 프로그래밍 실습 : 은행 계좌 ATM (0) | 2021.03.24 |
Week5) Java Programming Lab : Inheritance (0) | 2021.03.23 |