JAVA inheritance, polymorphism exercises __java

Source: Internet
Author: User
Package personsalary;
	Abstract class Person {private int ID;
	private String name;
	private double salary;
	int holidays;
		Public person () {} public person (int id,string name,double salary,int holidays) {this.id = ID;
		THIS.name = name;
		This.salary = salary;
	This.holidays = holidays;
		Public double calculatesalary () {if (holidays==0) return salary;
		else if (holidays>0&&holidays<5) return salary*0.75;
	else return salary*0.5;

}} package personsalary;
	Public class Employee extends person {public employee () {super ();
	Public Employee (int id,string name,double salary,int holidays) {super (id,name,salary,holidays); Public double calculatesalary () {return super. Calculatesalary () +super. Calculatesalary () *0.1 +super.
	Calculatesalary () *0.3;

}} package personsalary;
	Public class Manager extends person {public Manager () {super (); } public Manager (int id,string name,double salary,int holidays) {super (ID,name,salary,holidays); Public double calculatesalary () {return super. Calculatesalary () +super. Calculatesalary () *0.2+ super.
	Calculatesalary () *0.5+500;

}} package personsalary;
	public class Director extends person {public Director () {super ();
	Public Director (int id,string name,double salary,int holidays) {super (id,name,salary,holidays); Public double calculatesalary () {return super. Calculatesalary () +super. Calculatesalary () *0.2 +super.
	Calculatesalary () *0.5+500;

}} package personsalary;

Import java.util.*;
		public class Test {public static void main (string[] args) {Scanner reader = new Scanner (system.in);
		System.out.println ("Please enter ID:");
		int ID = Reader.nextint ();
		System.out.println ("Please enter Name:");
		String name = Reader.next ();
		System.out.println ("Please enter Salary:");
		Double salary = reader.nextdouble ();
		System.out.println ("Please enter days of absence:");
		int holidays = Reader.nextint ();
		SYSTEM.OUT.PRINTLN ("Please choose Occupation:"); System.oUt.println ("1. Average employee 2 Manager 3 director");
		int choose = Reader.nextint ();
			if (choose==1) {Employee TCL = new Employee (id,name,salary,holidays); The wages of System.out.println (name+) are: "+TCL."
		Calculatesalary ());
			}else if (choose==2) {Manager TCL = new manager (id,name,salary,holidays); The wages of System.out.println (name+) are: "+TCL."
		Calculatesalary ());
			}else{Director TCL = new Director (id,name,salary,holidays); The wages of System.out.println (name+) are: "+TCL."
		Calculatesalary ()); }
	}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.