131 - 员工类
Description
假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资(都是整数):
(1)经理(Manager)——每月获得一份固定的工资
(2)销售人员(Salesman)——在基本工资的基础上每月还有销售提成
(3)一般工人(Worker)——则按他每月工作的天数计算工资
在Employee中提供函数getSalary()计算每个雇员一个月的工资,并在子类中重写。
在main函数中构造Employee的三个变量,分别指向Manager、Salesman、Worker的对象,调用getSalary函数,输出三个对象的工资
Input
经理的月工资
销售人员的基本工资 销售人员的提成
工人的工作天数 工人每天的工资
Output
经理的工资
销售人员的工资
工人的工资
Sample Input
12000
3000 5000
22 200
Sample Output
12000
8000
4400
HINT
MyAnswer
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Employee e1,e2,e3;
Scanner scan = new Scanner(System.in);
int mangersalary = scan.nextInt();
e1 = new Manager(mangersalary);
int salesmanbase = scan.nextInt();
int salesmanextra = scan.nextInt();
e2 = new Salesman(salesmanbase,salesmanextra);
int days = scan.nextInt();
int workerbase = scan.nextInt();
e3 = new Worker(days,workerbase);
System.out.println(e1.getSalary());
System.out.println(e2.getSalary());
System.out.println(e3.getSalary());
}
}
class Employee {
int salary;
public int getSalary(){
return 0;
}
}
class Manager extends Employee{
Manager(int s){
this.salary = s;
}
public int getSalary() {
return salary;
}
}
class Salesman extends Employee{
int base;
int extra;
Salesman(int a,int b){
this.base = a;
this.extra = b;
this.salary = a+b;
}
@Override
public int getSalary() {
return salary;
}
}
class Worker extends Employee{
int days;
int base;
Worker(int d,int b){
this.base = b;
this.days = d;
this.salary = d*b;
}
public int getSalary(){
return salary;
}
}
132 - 员工类-2
Description
修改题目131的员工类,使得Employee类为抽象类,getSalary()方法是抽象方法
Input
经理的月工资
销售人员的基本工资 销售人员的提成
工人的工作天数 工人每天的工资
Output
经理的工资
销售人员的工资
工人的工资
Sample Input
12000
3000 5000
22 200
Sample Output
12000
8000
4400
HINT
MyAnswer
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Employee e1,e2,e3;
Scanner scan = new Scanner(System.in);
int mangersalary = scan.nextInt();
e1 = new Manager(mangersalary);
int salesmanbase = scan.nextInt();
int salesmanextra = scan.nextInt();
e2 = new Salesman(salesmanbase,salesmanextra);
int days = scan.nextInt();
int workerbase = scan.nextInt();
e3 = new Worker(days,workerbase);
System.out.println(e1.getSalary());
System.out.println(e2.getSalary());
System.out.println(e3.getSalary());
}
}
abstract class Employee {
int salary;
public abstract int getSalary();
}
class Manager extends Employee{
Manager(int s){
this.salary = s;
}
public int getSalary() {
return salary;
}
}
class Salesman extends Employee{
int base;
int extra;
Salesman(int a,int b){
this.base = a;
this.extra = b;
this.salary = a+b;
}
@Override
public int getSalary() {
return salary;
}
}
class Worker extends Employee{
int days;
int base;
Worker(int d,int b){
this.base = b;
this.days = d;
this.salary = d*b;
}
public int getSalary(){
return salary;
}
}
133 - Shape类-2
Description
定义一个形状类Shape,提供计算周长getPerimeter()和面积getArea()的函数
定义一个子类正方形类Square继承自Shape类,拥有边长属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
定义一个子类长方形类Rectangle继承自Square类,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
定义一个子类圆形类Circle继承自Shape,拥有半径属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
在main函数中,分别构造三个Shape类的变量,分别指向一个Square、Rectangle、Circle对象,并输出他们的周长、面积.
Input
正方形类的边长
长方形类的长宽
圆类的半径
Output
正方形的周长、面积
长方形的周长、面积
圆形的周长、面积
Sample Input
1
1 2
2
Sample Output
4.00 1.00
6.00 2.00
12.57 12.57
HINT
MyAnswer
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Shape s1,s2,s3;
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
s1 = new Square(length);
System.out.printf("%.2f ",s1.getPerimeter());
System.out.printf("%.2f\n",s1.getArea());
length = scan.nextDouble();
double wide = scan.nextDouble();
s2 = new Rectangle(length,wide);
System.out.printf("%.2f ",s2.getPerimeter());
System.out.printf("%.2f\n",s2.getArea());
double radius = scan.nextDouble();
s3 = new Circle(radius);
System.out.printf("%.2f ",s3.getPerimeter());
System.out.printf("%.2f\n",s3.getArea());
scan.close();
}
}
abstract class Shape{
public abstract double getPerimeter();
public abstract double getArea();
}
class Square extends Shape {
double length;
Square(double length){
this.length = length;
}
@Override
public double getPerimeter(){
return 4*length;
}
@Override
public double getArea() {
return length*length;
}
}
class Rectangle extends Square{
double width;
Rectangle(double length,double width){
super(length);
this.width = width;
}
@Override
public double getArea() {
return length*width;
}
@Override
public double getPerimeter() {
return 2*(length+width);
}
}
class Circle extends Shape {
double r;
Circle(double rr){
this.r = rr;
}
@Override
public double getPerimeter() {
return 2*Math.PI*r;
}
@Override
public double getArea() {
return Math.PI*r*r;
}
}
134 - Shape类-3
Description
修改题目133,将Shape类改为抽象类
Input
正方形类的边长
长方形类的长宽
圆类的半径
Output
正方形的周长、面积
长方形的周长、面积
圆形的周长、面积
Sample Input
1
1 2
2
Sample Output
4.00 1.00
6.00 2.00
12.57 12.57
HINT
MyAnswer
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Shape s1,s2,s3;
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
s1 = new Square(length);
System.out.printf("%.2f ",s1.getPerimeter());
System.out.printf("%.2f\n",s1.getArea());
length = scan.nextDouble();
double wide = scan.nextDouble();
s2 = new Rectangle(length,wide);
System.out.printf("%.2f ",s2.getPerimeter());
System.out.printf("%.2f\n",s2.getArea());
double radius = scan.nextDouble();
s3 = new Circle(radius);
System.out.printf("%.2f ",s3.getPerimeter());
System.out.printf("%.2f\n",s3.getArea());
scan.close();
}
}
abstract class Shape{
public abstract double getPerimeter();
public abstract double getArea();
}
class Square extends Shape {
double length;
Square(double length){
this.length = length;
}
@Override
public double getPerimeter(){
return 4*length;
}
@Override
public double getArea() {
return length*length;
}
}
class Rectangle extends Square{
double width;
Rectangle(double length,double width){
super(length);
this.width = width;
}
@Override
public double getArea() {
return length*width;
}
@Override
public double getPerimeter() {
return 2*(length+width);
}
}
class Circle extends Shape {
double r;
Circle(double rr){
this.r = rr;
}
@Override
public double getPerimeter() {
return 2*Math.PI*r;
}
@Override
public double getArea() {
return Math.PI*r*r;
}
}
137 - 学生、大学生、研究生类-2
Description
修改题目125(学生类-本科生类-研究生类)
为学生类添加属性成绩,添加相应的get和set函数,添加函数getGrade()表示获得等级,该函数应当为抽象函数。
本科生和研究生的等级计算方式不同,如下所示
本科生标准 研究生标准
[80--100) A [90--100) A
[70--80) B [80--90) B
[60--70) C [70--80) C
[50--60) D [60--70) D
50 以下 E 60 以下 E
main函数中构造两个学生Student变量,分别指向本科生和研究生对象,调用getGrade()方法输出等级
Input
本科生类信息,学号、姓名、性别、专业、成绩
研究生类信息,学号、姓名、性别、专业、导师、成绩
Output
本科生等级
研究生等级
Sample Input
2 chen female cs 90
3 li male sc wang 80
Sample Output
A
B
HINT
MyAnswer
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Student s1,s2;
Scanner scan = new Scanner(System.in);
int no = scan.nextInt();
String name = scan.next();
String sex = scan.next();
String major = scan.next();
int score = scan.nextInt();
s1 = new CollegeStudent(no, name, sex, major,score);
no = scan.nextInt();
name = scan.next();
sex = scan.next();
major = scan.next();
String supervisor = scan.next();
score = scan.nextInt();
s2 = new GraduateStudent(no, name, sex, major, supervisor, score);
s1.getGrade();
s2.getGrade();
scan.close();
}
}
abstract class Student {
int no;
String name;
String sex;
int score;
Student(int no, String name, String sex, int score){
this.name = name;
this.no = no;
this.sex = sex;
this.score = score;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setName(String name) {
this.name = name;
}
public void setNo(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void print(){
System.out.println("no: "+getNo());
System.out.println("name: "+getName());
System.out.println("sex: "+getSex());
}
public abstract void getGrade();
}
class CollegeStudent extends Student {
String major;
CollegeStudent(int no, String name, String sex, String major, int score){
super(no,name,sex,score);
this.major = major;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public void print(){
super.print();
System.out.println("major: "+major);
}
public void getGrade(){
if(score>=80){
System.out.println("A");
}else if(score>=70){
System.out.println("B");
}else if(score>=60){
System.out.println("C");
}else if(score>=50){
System.out.println("D");
}else{
System.out.println("E");
}
}
}
class GraduateStudent extends CollegeStudent {
String supervisor;
GraduateStudent(int no, String name, String sex, String major, String supervisor, int score) {
super(no, name, sex, major, score);
this.supervisor = supervisor;
}
public String getSupervisor() {
return supervisor;
}
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public void print() {
super.print();
System.out.println("supervisor: " + supervisor);
}
public void doResearch() {
System.out.println(getName() + " is doing research");
}
public void getGrade() {
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 70) {
System.out.println("C");
} else if (score >= 60) {
System.out.println("D");
} else {
System.out.println("E");
}
}
}
138 - 打球过程
Description
利用模板方法来构造相关类实现下述过程:
各种球类的玩法虽然不同,但是球类比赛的过程是类似的,都包含如下几个步骤:
1球员报道-->2比赛开始-->3比赛-->4比赛结束-->5公布比赛成绩,且其中1 2 4步相同 第3步根据球类不同,玩法不同,第5步根据得分不同,公布方式结果不同
构造类BallMatch表示球类比赛,包含方法compete表示真个比赛过程
构造各个比赛过程的函数checkin,start,play,end,annouceResult
打印信息如下:
now checking in
now starting
now playing football
now ending
now annoucing result: 2-3
构造类FootballMatch和BasketBallMatch,实现具体的比赛过程。
在main函数中,读入整数i,如果为1,则构造一个足球比赛过程,如果为2则构造一个篮球比赛过程
打印比赛过程
Input
比赛类型 比分
Output
比赛过程信息
Sample Input
1 2-3
Sample Output
now checking in
now starting
now playing football
now ending
now annoucing result: 2-3
HINT
MyAnswer
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
BallMatch bm;
int i;
Scanner scan = new Scanner(System.in);
i = scan.nextInt();
String score = scan.next();
switch (i){
case 1:
bm = new FootballMatch(score);
bm.compete(score);
break;
case 2:
bm = new BasketBallMatch(score);
bm.compete(score);
break;
}
}
}
abstract class BallMatch{
public final void compete(String res){
checkin();
start();
play();
end();
annouceResult(res);
}
public void checkin(){
System.out.println("now checking in");
}
public void start(){
System.out.println("now starting");
}
public abstract void play();
public void end(){
System.out.println("now ending");
}
public void annouceResult(String result){
System.out.println("now annoucing result: "+result);
}
}
class FootballMatch extends BallMatch{
String result;
FootballMatch(String res){
this.result = res;
}
public void play(){
System.out.println("now playing football");
}
}
class BasketBallMatch extends BallMatch{
String result;
BasketBallMatch(String res){
this.result = res;
}
public void play(){
System.out.println("now playing basketball");
}
}