2019-7-4 [JavaSE] 对象 5个练习题

任务一:老鼠类

创建一个老鼠类,老鼠有昵称和爱好,会说“我叫杰瑞,我爱好吃,一只小猫,有啥可怕”;创建几只具体的老鼠。(用不带返回值方法)
在这里插入图片描述

class Mouse{
	String name;
	String hobby;
	public void say() {
		System.out.println("我叫"+name+",我爱好"+hobby+",一致小猫,有啥可怕");
	}
}
public class TestMouse {

	public static void main(String[] args) {
		Mouse jerry = new Mouse();
		jerry.name = "杰瑞";
		jerry.hobby = "吃";
		jerry.say();
	}
}

任务二:游人类

请编写游人类。(用带返回值的方法)
在这里插入图片描述

//请编写游人类。
class Person{
	String name;
	int age;
	public String show() {
		return name+","+age;
	}
}
public class TestPerson {

	public static void main(String[] args) {
		Person zhangsan = new Person();
		zhangsan.name = "张三";
		zhangsan.age = 22;
		System.out.println(zhangsan.show());
	}
}

任务三:数学类

实现两个整数计算的四个方法:
加法
减法
乘法
除法
提示:
int add(int n1,int n2)
int subtract (int n1,int n2)
int multiply (int n1,int n2)
int divide(int n1,int n2)

import java.util.Scanner;

class MyMath{
	//任务三:
	public int add(int n1,int n2) {
		return n1 + n2;
	}
	public int substract(int n1,int n2) {
		return n1 - n2;
	}
	public int multiply(int n1,int n2) {
		return n1 * n2;
	}
	public int divde(int n1 , int n2) {
		return n1 / n2;
	}
}
public class TestMyMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyMath m = new MyMath();
		Scanner input = new Scanner(System.in);
		int n1,n2;
		String oper;
		System.out.println("输入两个数和一个运算符:");
		n1 = input.nextInt();
		n2 = input.nextInt();
		oper = input.next();
		System.out.println(new MyMath().calculation(n1, n2, oper));	
	}
}

任务四:两个整数计算

实现两个整数计算的一个方法,用一个方法实现加减乘除。
提示: int calculation(int n1,int n2,String operator)

import java.util.Scanner;

class MyMath{
	//任务四:
	public int calculation(int n1,int n2,String operator) {
		int sum = 0;
		switch(operator) {
		case "+":
			sum = n1 + n2;
			break;
		case "-":
			sum = n1 - n2;
			break;
		case "*":
			sum = n1 * n2;
			break;
		case "/":
			sum = n1 / n2;
			break;
		case "%":
			sum = n1 % n2;
			break;
		}
		return sum;
	}
}
public class TestMyMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyMath m = new MyMath();
		Scanner input = new Scanner(System.in);
		int n1,n2;
		String oper;
		System.out.println("输入两个数和一个运算符:");
		n1 = input.nextInt();
		n2 = input.nextInt();
		oper = input.next();
		System.out.println(new MyMath().calculation(n1, n2, oper));	
	}
}

任务五:Emloyee类

有属性:员工编号,员工名,员工电话,员工身高,员工工资,;
有方法:显示员工信息();
定义一个员工管理类,
有方法:计算员工总工资(员工); //计算多名员工的工资
计算员工平均身高(员工);//计算多名员工的平均身高
难点提示:对象数组。数组中的每个元素存储的是个对象。

//员工类
class Employee{
	int no;
	String name;
	String phone;
	double height;
	double salary;
	public void show() {
		System.out.println(no+","+name+","+phone+","+height+","+salary);
	}
}
//员工管理类
class EmployeeManager{
	public void sum(Employee []emps) {
		double sum = 0;
		for(Employee emp:emps) {
			sum += emp.salary;
		}
		System.out.println(sum);
	}
	public void avg(Employee [] emps) {
		double sum = 0;
		for(Employee emp:emps) {
			sum += emp.height;
		}
		System.out.println(sum/emps.length);
	}
}
//测试类
public class TestEmployee {

	public static void main(String[] args) {
		Employee guojing = new Employee();
		guojing.salary = 3000;
		guojing.height = 170.45;
		Employee yangkang = new Employee();
		yangkang.salary = 5000;
		yangkang.height = 175.5;
		Employee huangrong = new Employee();
		huangrong.salary = 8000;
		huangrong.height = 180;
		Employee [] emps = {guojing,yangkang,huangrong};
		EmployeeManager manager = new EmployeeManager();
		manager.sum(emps);
		manager.avg(emps);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值