20220327 java面向对象代码题

1)计算平均分 使用成员变量、成员方法、构造方法和this关键字,先记录4名学生的语文、数学、英语这3科成绩,再计算每个人的平均分

public class Student {
    int number;
    String name;
    float math;
    float english;
    float chinese;

    public Student(int number, String name, float math, float english, float chinese) {
        this.number = number;
        this.name = name;
        this.math = math;
        this.english = english;
        this.chinese = chinese;
    }

    public void average(){
        float aver=(chinese +math+english)/3;
        System.out.println(number+"\t"+name+"\t"+chinese+"\t"+math+"\t"+english+"\t"+String.format("%.2f",aver));

    }

    public static void main(String[] args) {
        System.out.println("学号\t姓名\t语文\t数学\t英语  平均分");
        System.out.println("——————————————————————————————————————————————————");
        Student student1 = new Student(1, "张三", 91.5f, 98f, 89f); //张三的各科成绩
        student1.average(); //计算张三的平均分

        Student student2 = new Student(2, "李四", 96f, 98.5f, 93f); //李四的各科成绩
        student2.average(); //计算李四的平均分

        Student student3 = new Student(3, "王五", 97f, 100f, 98.5f); //王五的各科成绩
        student3.average(); //计算王五的平均分

        Student student4 = new Student(4, "钱六", 77f, 83f, 81f); //钱六的各科成绩
        student4.average(); //计算钱六的平均分
    }
}

2) 厘米与英寸互转 编写工具类,提供厘米与英寸之间的相互转换的工具方法

public class Length {
	static double ratio = 2.54; // 厘米与英寸之间的转换系数

	public static double transferCM(double cm) { // “厘米转换为英寸”的方法
		double inch = cm / ratio;
		return inch;
	}

	public static double transferINCH(double inch) { // “英寸转换为厘米”的方法
		double cm = inch * ratio;
		return cm;
	}

	public static void main(String[] args) {
		System.out.println("10厘米转成英寸的结果为:" + transferCM(10));
		System.out.println("10英寸米转成厘米的结果为:" + transferINCH(10));
	}
}

3)划火柴 定义一个点燃接口,再定义一个火柴类实现点燃接口,每根火柴对象只能调用一次点燃方法,这种对象该如何创建?

interface Light {
	public void light();
}

public class Match {
	public static void main(String[] args) {
		new Match() {
			public void light() {
				System.out.println("一根火柴被点燃了……");
			}
		}.light();
	}
}

 4)吃水果 创建一个抽象的水果类,类中有一个获取水果名称的抽象方法。创建人类,人类有个吃的方法,参数类型为水果类型,并可以在控制台打印吃了什么?

abstract class Fruits {
	public abstract String getName();

}

class Human {
	void eat(Fruits food) {
		System.out.println("吃了一个" + food.getName());
	}
}

public class Demo {
	public static void main(String[] args) {
		Human tom = new Human();
		tom.eat(new Fruits() {
			public String getName() {
				return "苹果";
			}
		});
		tom.eat(new Fruits() {
			public String getName() {
				return "香蕉";
			}
		});
	}
}

5) 定义getValue方法,获取三个数中的最值,能够通过字符串,指定获取最大值或者最小值

 public class Test2 {
      public static void main(String[] args) {
          boolean b = doCheck(2);
          System.out.println(b);

          boolean b1 = doCheck(3);
          System.out.println(b1);
      }

      // 定义doCheck方法,参数为(int iVar),返回值boolean类型
      public static boolean  doCheck(int num){
  //        3.doCheck方法内,定义变量boolean flag.
          boolean flag  ;

  //        4.doCheck方法内,判断num是否为偶数.
          if (num % 2== 0 ) {
              // 如果是偶数,使用for循环,初始化值i为0,i<=20进入循环,步进表达式i++
              for (int i = 0; i <= 20; i++) {
                  num-=i;
              }
              flag =  true;
          }else {
              // 否则是奇数,使用for循环,初始化值i为0,i<=20进入循环,步进表达式i++
              for (int i = 0; i <= 20; i++) {
                  num+=i;
              }
              flag =   false;
          }
          // 输出num的值
          System.out.println("num:"+ num);
          return flag;
      }
  }

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Royalreairman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值