《Java程序设计》实验2 面向对象基础1

6-1 员工的工资

分数 15

全屏浏览题目

切换布局

作者 刘凤良

单位 天津仁爱学院

假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资(都是整数):

(1)经理(Manager)——每月获得一份固定的工资

(2)销售人员(Salesman)——在基本工资的基础上每月还有销售提成

(3)一般工人(Worker)——则按他每月工作的天数计算工资 在Employee中提供方法getSalary(),用于计算每个雇员一个月的工资,并在子类中重写。

Post_AppendCode的main方法中已经构造Employee的三个变量,分别指向Manager、Salesman、Worker的对象,调用getSalary方法,输出三个对象的工资。 要求:编码实现经理、销售人员、一般工人三个类。

方法接口定义:

 
 

public int getSalary(); //计算每个雇员一个月的工资

裁判测试程序样例:

 
 

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int managerSalary = scan.nextInt(); int salemanSalary = scan.nextInt(); int salemanRaise = scan.nextInt(); int workerEveryday = scan.nextInt(); int workerDays = scan.nextInt(); Employee e1 = new Manager(managerSalary); Employee e2 = new Salesman(salemanSalary, salemanRaise); Employee e3 = new Worker(workerEveryday, workerDays); System.out.println(e1.getSalary()); System.out.println(e2.getSalary()); System.out.println(e3.getSalary()); scan.close(); } } /* 请在这里填写答案 */

输入描述:

经理的月工资
销售人员的基本工资 销售人员的提成
工人的工作天数 工人每天的工资

输出描述:

经理的工资
销售人员的工资
工人的工资

输入样例:

12000
3000 5000
22 200

输出样例:

12000
8000
4400

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

abstract class Employee
{
	abstract public int getSalary();
}
class Manager extends Employee
{
	private int salary;
	Manager(int salary)
	{
		this.salary = salary;
	}
	public int getSalary()
	{
		return salary;
	}
}
class Salesman extends Employee
{
	private int salary;
	private int raise;
	Salesman(int salary,int raise)
	{
		this.salary = salary;
		this.raise = raise;
	}
	public int getSalary()
	{
		return salary + raise;
	}
}
class Worker extends Employee
{
	private int everyday;
	private int days;
	Worker(int everyday,int days)
	{
		this.everyday = everyday;
		this.days = days;
	}
	public int getSalary()
	{
		return days*everyday;
	}
}


6-2 动物自我介绍

分数 15

全屏浏览题目

切换布局

作者 刘凤良

单位 天津仁爱学院

基于继承关系编写一个动物体系,具体的动物包含小狗和小猫。每只动物都有名字和颜色,都能够做自我介绍(introduce)。此外,小狗有智商属性(整数),能接飞盘(catchFrisbee(),方法体内输出一行“catch frisbee”即可),小猫有眼睛颜色属性,能抓老鼠(catchMouse(),方法体内输出一行“catch mouse”即可)。各种小动物自我介绍时均介绍自己的姓名和颜色,此外,小狗应介绍自己的智商,小猫应介绍自己的眼睛颜色。小狗介绍时输出”My name is xxx, my color is xxx, my IQ is xxx”, 小猫介绍时输出“My name is xxx, my color is xxx, my eyecolor is xxx”
构造类TestAnimal,提供静态函数introduce(Animal),对参数动物自我介绍。提供静态函数action(Animal),根据参数对象的实际类型进行活动,如果是小狗,则让其接飞盘,如果是小猫,则让其抓老鼠。
Main函数中,根据动物类型构造动物,并调用TestAnimal中的方法进行自我介绍(introduce)和活动(action)

函数接口定义:

 
 

public void introduce(); //自我介绍 public void action(); //动物的活动

裁判测试程序样例:

 
 

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner s = new Scanner(System.in); int i = s.nextInt(); Animal a = null; if (i == 1) { a = new Cat(s.next(), s.next(), s.next()); } else if (i == 2) { a = new Dog(s.next(), s.next(), s.nextInt()); } TestAnimal.introduce(a); TestAnimal.action(a); } } /* 请在这里填写答案 */

输入描述:

动物类型 动物名称 动物颜色 动物其他属性 如
1 猫名称 猫颜色 猫眼睛颜色
2 狗名称 狗颜色 狗的智商

输出描述:

自我介绍
活动

输入样例:

1 Mikey white blue

输出样例:

My name is Mikey, my color is white, my eyecolor is blue
catch mouse

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

abstract class Animal
{
	private String name;
	private String color;
	Animal(String name,String color)
	{
		this.name = name;
		this.color = color;
	}
	public String getName()
	{
		return name;
	}
	public String getColor()
	{
		return color;
	}
	abstract public void introduce();
}
class Dog extends Animal
{
	private int IQ;
	Dog(String name,String color,int IQ)
	{
		super(name,color);
		this.IQ = IQ;
	}
	public void catchFrisbee()
	{
		System.out.println("catch frisbee");
	}
	public void introduce()
	{
		System.out.println("My name is "+super.getName()+", my color is "+super.getColor()+", my IQ is "+IQ);
	}
}
class Cat extends Animal
{
	private String eyeColor;
	Cat(String name,String color,String eyeColor)
	{
		super(name,color);
		this.eyeColor = eyeColor;
	}
	public void catchMouse()
	{
		System.out.println("catch mouse");
	}
	public void introduce()
	{
		System.out.println("My name is "+super.getName()+", my color is "+super.getColor()+", my eyecolor is "+eyeColor);
	}
}
class TestAnimal
{
	public static void introduce(Animal a)
	{
		a.introduce();
	}
	public static void action(Animal a)
	{
		if(a instanceof Dog)
		{
			Dog dog = (Dog)a;
			dog.catchFrisbee();
		}
		else
		{
			Cat cat = (Cat) a;
			cat.catchMouse();
		}
		
	}
}

6-3 模拟题: 重写父类方法equals

分数 15

全屏浏览题目

作者 sunyu

单位 西南石油大学

在类Point中重写Object类的equals方法。使Point对象x和y坐标相同时判定为同一对象。

裁判测试程序样例:

 
 

import java.util.Scanner; class Point { private int xPos, yPos; public Point(int x, int y) { xPos = x; yPos = y; } @Override /* 请在这里填写答案 */ } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Object p1 = new Point(sc.nextInt(),sc.nextInt()); Object p2 = new Point(sc.nextInt(),sc.nextInt()); System.out.println(p1.equals(p2)); sc.close(); } }

输入样例:

10 20
10 20

输出样例:

true

代码长度限制

16 KB

时间限制

4000 ms

内存限制

64 MB

public boolean equals(Object obj)
	{
		if(obj==null)
			return false;
		if(obj instanceof Point)
		{
			Point other = (Point)obj;
			if(this.xPos==other.xPos&&this.yPos==other.yPos)
				return true;
			else
				return false;
		}	
		else
			return false;
	}

6-4 图书和音像租赁

分数 15

全屏浏览题目

作者 刘凤良

单位 天津仁爱学院

图书和音像店提供出租服务,包括图书和DVD的出租。图书包括书名(String,一个词表示)和价格(double),DVD包括片名(String,一个词表示)。它们都是按天出租,但租金计算方式却不同,图书的日租金为图书价格的1%,DVD的日租金为固定的1元。构造图书和DVD类的继承体系,它们均继承自Media类,且提供方法getDailyRent()返回日租金,构造音像店类MediaShop,提供静态函数double calculateRent(Media[] medias, int days)。 在main函数中构造了Media数组,包含图书和DVD的对象,调用calculateRent方法得到并输出租金,保留小数点两位。

方法接口定义:

 
 

double getDailyRent(); static double calculateRent(Media[] medias, int days);

裁判测试程序样例:

 
 

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Media[] ms = new Media[n]; for (int i = 0; i < n; i++) { String type = sc.next(); if (type.equals("book")) { ms[i] = new Book(sc.next(), sc.nextDouble()); } else { ms[i] = new DVD(sc.next()); } } double rent = MediaShop.calculateRent(ms, sc.nextInt()); System.out.printf("%.2f", rent); } } /* 请在这里填写答案 */

输入样例:

5
book Earth 25.3
book Insights 34
dvd AI
dvd Transformer
book Sun 45.6
20

输出样例:

60.98

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

abstract class Media
{
	private String name;
	Media(String name)
	{
		this.name = name;
	}
	abstract double getDailyRent();
}
class Book extends Media
{
	private double price;
	Book(String name,double price)
	{
		super(name);
		this.price = price;
	}
	public double getDailyRent()
	{
		return price*0.01;
	}
}
class DVD extends Media
{
	DVD(String name)
	{
		super(name);
	}
	public double getDailyRent()
	{
		return 1;
	}
}
class MediaShop
{
	public static double calculateRent(Media[] medias, int days)
	{
		double sum=0;
		for(int i=0;i<medias.length;i++)
		{
			sum+=medias[i].getDailyRent();
		}
		return sum*days;
	}
}

7-1 快递计价器

分数 15

全屏浏览题目

切换布局

作者 Ma

单位 山东科技大学

现需要编写一个简易快递计价程序。具体来说:

1、抽象快递类Express,其包含一个属性int weight表示快递重量(单位为kg),一个方法getWeight()用于返回快递重量和一个抽象方法getTotal()用于计算快递运费。

2、两个类继承Express,分别是:
(a)顺路快递SLExpress:计价规则为首重(1kg)12元,每增加1kg费用加2元。
(b)地地快递DDExpress:计价规则为首重(1kg)5元,每增加1kg费用加1元。

3、Main:
接收用户通过控制台输入的N行信息,自动计算所有快递的运费总和。

输入样例:

6
SL 2
DD 2
SL 1
SL 1
SL 1
DD 3

输入解释:

第1行n表示需要计算的快递件数
第2至n+1表示每个快递信息,即选哪家快递公司 以及快递的重量(单位kg)

输出样例:

63

输出解释:

所有快递总运费。

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int n=in.nextInt();
		Express[] a = new Express[n];
		int sum=0;
		for(int i=0;i<n;i++)
		{
			String str = in.next();
			if(str.equals("SL"))
			{
				a[i] = new SLExpress(in.nextInt());
			}
			else
			{
				a[i] = new DDExpress(in.nextInt());
			}
			sum+=a[i].getTotal();
		}
		System.out.println(sum);
	}
}
abstract class Express
{
	private int weight;
	Express(int weight)
	{
		this.weight = weight;
	}
	public int getWeight()
	{
		return weight;
	}
	abstract public int getTotal();
}
class SLExpress extends Express
{
	SLExpress(int weight)
	{
		super(weight);
	}
	public int getTotal()
	{
		if(super.getWeight()<=1&&super.getWeight()>0)
			return 12;
		else 
			return 12+2*(super.getWeight()-1);
	}
}
class DDExpress extends Express
{
	DDExpress(int weight)
	{
		super(weight);
	}
	public int getTotal()
	{
		if(super.getWeight()<=1&&super.getWeight()>0)
			return 5;
		else 
			return 5+1*(super.getWeight()-1);
	}
}

7-2 周长计算器

分数 15

全屏浏览题目

作者 Ma

单位 山东科技大学

1、定义一个接口 Shape 用于表示图形,其包含一个 double length() 的方法用于求周长。
2、定义三角形类 Triangle 、长方形类 Rectangle 、圆形类Circle分别实现接口 Shape
3、定义测试类ShapeTest并使用 Shape接口定义变量shape,用其指向不同类形的对象,输出各种图形的周长。

提示:
1、计算圆周长时PI取3.14即可;
2、需要判断能否构成三角形(任意两个边的和大于第三边),不能构成三角形的话周长为0。

输入格式:

输入多组double型数据,具体如下:
1 //表示圆的半径;
2 3 //表示长方形的长度、宽度
4 5 6 //表示三角形的三边的长度
//若输入数据中有0或负数,则不表示任何图形,周长为0。

输出格式:

图形的周长。

输入样例:

1
2 3
4 5 6

输出样例:

6.28
10.00
15.00

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值