一: * 1,声明Shape类,声明一个计算面积的方法getArea * 2,声明ShapeOpr类,其中有一个方法printArea,接收参数Shape,用于得到某形状的面积,并直接输出 * 3,声明测试类
public class Shape {
private int r;
public Shape() {
}
public Shape(int r) {
this.r = r;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
//获取圆面积
public double getArea(){
double s =3.14 *r*r;
return s;
}
}
/**
* @author 犀角
* @date 2020/2/24 20:08
* @description
*/
public class ShapeOpr extends Shape{
public void printArea(Shape shape){
// shape.setR(3);
System.out.println("圆半径为:"+shape.getR()+",圆面积为:"+shape.getArea());
}
}
/**
* @author 犀角
* @date 2020/2/24 20:08
* @description
*/
public class ShapeOpr extends Shape{
public void printArea(Shape shape){
// shape.setR(3);
System.out.println("圆半径为:"+shape.getR()+",圆面积为:"+shape.getArea());
}
}
二、 1,声明一个实体类Student,属性;name,age,no(学号),重写equals方法,姓名和学号相等时即是同一个学生
2,声明一个学生的操作类,有一个方法:从一个学生数组中,根据姓名和学号判断给定的学生是否存在 3,声明测试类,测试
package com.Demo05;
import java.util.Objects;
/**
*/
public class Student {
private String name;
private int age;
private String no;
public Student() {
}
public Student(String name, int age, String no) {
this.name = name;
this.age = age;
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Student student = (Student) o;
return this.getName().equals(student.getName()) &&
this.getNo().equals(student.getNo());
}
@Override
public int hashCode() {
return Objects.hash(name,no);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", no='" + no + '\'' +
'}';
}
}
package com.Demo05;
import java.util.List;
/**
* 2,声明一个学生的操作类,有一个方法:
* * 从一个学生数组中,根据姓名和学号判断给定的学生是否存在
*/
public class StudentOpr extends Student {
public boolean isExist(Student student, List<Student> students){
for (Student s : students) {
if (s.equals(student)){
System.out.println(student.getName()+"学生存在,学号为:"+student.getNo());
return true;
}
}
System.out.println("该学生不存在!");
return false;
}
}
package com.Demo05;
import java.util.ArrayList;
import java.util.List;
/**
* @author 犀角
* @date 2020/2/24 20:46
* @description
*/
public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student("张三",23,"123");
Student s2 = new Student("李四", 25, "234");
Student s3 = new Student("王五", 26, "456");
Student s4 = new Student("奥特曼", 56, "4569");
System.out.println(s1.toString());
List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
StudentOpr studentOpr = new StudentOpr();
studentOpr.isExist(s1, students);
// System.out.println(exist1);
studentOpr.isExist(s4, students);
// System.out.println(exist);
Student s5 = new Student("张三",29,"123");
studentOpr.isExist(s5,students);
}
}
三、抽象类
编程题一: * (1)定义一个抽象类Weapon,该抽象类有两个抽象方法attack(),move():这两个方法分别表示武器的攻击方式和移动方式。 * (2)定义3个类:Tank,Flighter,WarShip都继承自Weapon,分别用不同的方式实现Weapon类中的抽象方法。 * (3)写一个类Army,代表一支军队: * 这个类有一个属性是Weapon数组w(用来存储该军队所拥有的所有武器); * 该类还提供一个构造方法,在构造方法里通过传一个int类型的参数来限定该类所能拥有的最大武器数量,并用这一大小来初始化数组w。 * 该类还提供一个方法addWeapon(Weapon wa),表示把参数wa所代表的武器加入到数组w中。 * 在这个类中还定义两个方法attackAll()和moveAll(),让w数组中的所有武器攻击和移动。 * (4)写一个主方法去测试以上程序。
public abstract class Weapon {
/**
* 攻击方式
*/
public abstract void attack();
/**
* 移动方式
*/
public abstract void move();
/**
* 武器名称
*/
public abstract void name();
}
package com.Demo08;
/**
* @author 犀角
* @date 2020/2/25 21:19
* @description
*/
public class Tank extends Weapon {
@Override
public void attack() {
System.out.println("tank attack!");
}
@Override
public void move() {
System.out.println("tank move!");
}
@Override
public void name() {
System.out.println("坦克");
}
}
package com.Demo08;
/**
* @author 犀角
* @date 2020/2/25 21:19
* @description
*/
public class Flighter extends Weapon {
@Override
public void attack() {
System.out.println("flight attack!");
}
@Override
public void move() {
System.out.println("flight move!");
}
@Override
public void name() {
System.out.println("飞机");
}
}
package com.Demo08;
/**
* @author 犀角
* @date 2020/2/25 21:19
* @description
*/
public class WarShip extends Weapon {
@Override
public void attack() {
System.out.println("warShip attack!");
}
@Override
public void move() {
System.out.println("warShip move!");
}
@Override
public void name() {
System.out.println("战舰");
}
}
package com.Demo08;
/**
* @author 犀角
* @date 2020/2/25 21:25
* @description
*/
public class Army {
private Weapon[] w;
public Army() {
}
public Army(int num) {
this.w = new Weapon[num];
}
//往武器库中添加武器
public void addWeapon(Weapon wa) {
//判断数组是否满
boolean full = true;
for (int i = 0; i < w.length; i++){
if (w[i] == null){
w[i] = wa;
System.out.println("武器装备添加成功");
full =false;
break;
}
}
//如果满了,输出武器库满了
if (full){
System.out.println("武器库满了!");
}
}
//让所有的武器攻击
public void attackAll(){
for (Weapon weapon : w) {
weapon.attack();
}
}
//让所有武器移动
public void moveAll(){
for (Weapon weapon : w) {
weapon.move();
}
}
//打印所有武器的名称
public void printName(){
for (Weapon weapon : w) {
weapon.name();
}
}
}
package com.Demo08;
/**
* @author 犀角
* @date 2020/2/25 22:19
* @description
*/
public class Test {
public static void main(String[] args) {
Army army = new Army(3);
Tank tank = new Tank();
Flighter flighter = new Flighter();
WarShip warShip = new WarShip();
army.addWeapon(tank);
army.addWeapon(flighter);
army.addWeapon(warShip);
army.attackAll();
army.moveAll();
army.printName();
}
}
四、接口
* 编程题二: * 利用接口做参数,写个计算器,能完成加减乘除运算。 * (1)定义一个接口Computable含有一个方法int compute(int n, int m) * (2)设计四个类分别实现此接口,完成加减乘除运算。 * (3)设计一个类Computor,类中含有方法:public int useCom(Computable com, int one, int two),此方法能够用传递过来的对象调用compute方法完成运算,并输出运算的结果。 * (4)设计一个主类Test,调用Computor中的方法useCom来完成加减乘除运算
public interface Computable {
int computer(int n,int m);
}
package com.Demo09;
/**
* @author 犀角
* @date 2020/2/25 22:34
* @description
*/
public class Addition implements Computable {
@Override
public int computer(int n, int m) {
return m + n;
}
}
package com.Demo09;
/**
* @author 犀角
* @date 2020/2/25 22:37
* @description
*/
public class Division implements Computable{
@Override
public int computer(int n, int m) {
return m/n;
}
}
package com.Demo09;
/**
* @author 犀角
* @date 2020/2/25 22:36
* @description
*/
public class Multiplication implements Computable {
@Override
public int computer(int n, int m) {
return m * n;
}
}
package com.Demo09;
/**
* @author 犀角
* @date 2020/2/25 22:35
* @description
*/
public class Subtraction implements Computable {
@Override
public int computer(int n, int m) {
return m - n;
}
}
package com.Demo09;
/**
* @author 犀角
* @date 2020/2/25 22:39
* @description 此方法能够用传递过来的对象调用compute方法完成运算,并输出运算的结果。
*/
public class Computer {
public int useCom(Computable com, int one, int two){
int result = com.computer(one, two);
return result;
}
}
package com.Demo09;
/**
* @author 犀角
* @date 2020/2/25 22:41
* @description
*/
public class Test {
public static void main(String[] args) {
Addition addition = new Addition();
Division division = new Division();
Multiplication multiplication = new Multiplication();
Subtraction subtraction = new Subtraction();
Computer computer = new Computer();
System.out.println(computer.useCom(addition, 1, 2));
System.out.println(computer.useCom(division, 2, 6));
System.out.println(computer.useCom(subtraction, 7, 88));
System.out.println(computer.useCom(multiplication, 3, 4));
}
}