JAVA面向对象编程题

1.鱼类

class Fish {
    private String name;
    private int age;
    private double weight;
    private double length;

    public Fish(){

    }
    public Fish(String name,int age,double w,double h){
        this.name=name;
        this.age=age;
        this.weight=w;
        this.length=h;
    }
    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 double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }

    public void swim(){
        System.out.println("鱼会游泳");
    }
    public void breath(){
        System.out.println("鱼用腮呼吸");
    }
    public void eat(){
        System.out.println("鱼会吃饵料");
    }
    public void sayHi(){
        System.out.println("姓名:"+name);
        System.out.println("年龄:"+age);
        System.out.println("重量:" + weight);
        System.out.println("长度:" + length);
    }
}
public class Test9 {
    public static void main(String[] args) {
        Fish fish = new Fish("金鱼",2,2,2);
        fish.eat();
        fish.breath();
        fish.swim();
        fish.sayHi();
    }
}

2.僵尸类

/*
 * 铁桶僵尸(zombie) 血量 方法:被打一次掉2血 直到被打死
 * 帽子僵尸 血量 方法:被打一次掉5血 直到被打死
 * 封装一个 打僵尸的方法
 */
class Zombie{
    private int blood;
    public Zombie(){

    }
    public Zombie(int blood){
        this.blood=blood;
    }
    public int getBlood(){
        return blood;
    }
    public void setBlood(int blood){
        this.blood=blood;
    }
    public void play(){
        System.out.println("僵尸被打了");
    }
}
class TTZombie extends Zombie{
    public TTZombie(){

    }
    public TTZombie(int blood){
        super(blood);
    }
    public void play(){
        while(true){
            if(this.getBlood()<=0){
                System.out.println("铁桶僵尸被打死了");
                break;
            }
            this.setBlood(this.getBlood()-2);
            System.out.println("铁桶僵尸剩余血量:" + this.getBlood());

        }
    }
}
class MZZombie extends Zombie{
    public MZZombie() {

    }
    public MZZombie(int blood) {
        super(blood);
    }
    @Override
    public void play() {
        if(this.getBlood() <= 0) {
            return ;
        }
        this.setBlood(this.getBlood() - 5);
        System.out.println("帽子僵尸剩余血量:" + this.getBlood());
        play();
    }
}

public class Test9 {
    public static void main(String[] args) {
        TTZombie zombie = new TTZombie(30);
        zombie.play();
        MZZombie zombiee=new MZZombie(40);
        zombiee.play();
    }
}

3.教师学生类

class Man{
    private String name;
    private int age;
    public Man(){

    }
    public Man(String name,int age){
        this.age=age;
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setName(){
        this.name=name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void eat() {
        System.out.println("人类需要吃饭");
    }
    @Override
    public String toString(){
        return "姓名"+name+"年龄"+age;
    }
}

class Teacher extends Man{
    public Teacher(){

    }
    public Teacher(String name,int age){
        super(name,age);
    }
    public void teach(){
        System.out.println(this.getName()+"会讲课");
    }
    public String toString(){
        return super.toString();
    }
}

class Students extends Man{
    private int number;
    public Students(){

    }
    public Students(String name,int age,int number){
        super(name,age);
        this.number=number;
    }
    public int getNumber(){
        return number;
    }
    public void setNumber(int number){
        this.number=number;
    }
    public void study(){
        System.out.println(this.getName()+"在上课");
    }
    public String toString(){
        return super.toString()+"学号:"+number;
    }
}

public class Test9 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("胡歌",15);
        Students student = new Students("李科", 22, 20144);
        System.out.println(teacher);
        System.out.println(student);
    }
}

4.交通工具(Vehicle)类

/**
 * 请定义一个交通工具(Vehicle)的类,其中有:
 * 属性:速度(speed),车的类型(type)等等
 * 方法:移动(move()),设置速度(setSpeed(double s)),加速speedUp(double s),减速speedDown(double s)等等.
 * 最后在测试类Vehicle中的main()中实例化一个交通工具对象,
 * 并通过构造方法给它初始化speed,type的值,并且打印出来。另外,调用加速,减速的方法对速度进行改变。
 */
class Vehicle{
    public double speed;
    public String type;

    public Vehicle(){

    }
    public Vehicle(double d,String a){
        speed=d;
        type=a;
    }
    public void move(){
        System.out.println(type + " 速度 " + speed + " 迈----");
    }
    public void setSpeed(double s){
        speed=s;
    }
    public void speedUp(double num){
        speed+=num;
    }
    public void speedDown(double num){
        if(speed>=num) speed-=num;
        else speed=0;
    }

    @Override
    public String toString() {
        return "姓名:"+type+"速度"+speed+"迈----";
    }
}
public class Test9{
    public static void main(String[] args) {
        Vehicle v = new Vehicle(80000,"兰博基尼");
        v.move();
        System.out.println(v);
        v.setSpeed(80);// set速度为 80
        v.move();
        v.speedUp(50);//速度加50
        v.move();
    }
}

5.镜像字符串

import java.util.Scanner;

public class Test9 {
    public static Boolean count(String str1,String str2){
        int p1=0;
        int p2=0;
        char[] st1=str1.toCharArray();
        char[] st2=str2.toCharArray();
        String ns1=new StringBuffer(str1).reverse().toString();
        if(ns1.equals(str2))
            return true;
        else return false;
    }
    public static void main(String[] args) throws Exception{
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入字符串");
        String str1=scanner.next();
        String str2=scanner.next();
        if(str1.length()-str2.length()!=0){
            System.out.println("no");
            return ;
        }
        if(str1==null||str1.length()<=0||str2==null||str2.length()<=0){
            System.out.println("error");
        }
        Boolean num=count(str1,str2);
        if(num.equals(true)){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
        scanner.close();
    }
}

6.家电类

import java.util.Scanner;
interface Appliance{
    int getwieght();
}
class TV implements Appliance{
    private int weight;
    TV(int w){
        weight = w;
    }
    public int getwieght() {
        return weight;
    }
}
class WashMachine implements Appliance{
    private int weight;
    WashMachine(int w){
        weight = w;
    }
    public int getwieght() {
        return weight;
    }
}

class AirConditioner implements Appliance{
    int weight;
    AirConditioner(int w){
        weight = w;
    }
    public int getwieght() {
        return weight;
    }
}

class Trunk{
    TV tv[];
    private int numoftv;

    WashMachine wa[];
    private int numofwa;

    AirConditioner air[];
    private int numofair;

    private int num;

    Trunk(int data[][],int a,int b,int c,int n){

        numoftv = 0;
        numofwa = 0;
        numofair = 0;

        tv = new TV[a];
        wa = new WashMachine[b];
        air = new AirConditioner[c];

        for(int i=0;i<n;i++) {
            if(data[i][0]==1) {
                tv[numoftv] = new TV(data[i][1]);
                numoftv++;
            }else if(data[i][0] == 2) {
                wa[numofwa] = new WashMachine(data[i][1]);
                numofwa++;
            }else {
                air[numofair] = new AirConditioner(data[i][1]);
                numofair++;
            }
        }
    }
    int getsum() {
        int sum = 0;
        for(int i=0;i<numoftv;i++)
            sum+=tv[i].getwieght();
        for(int i=0;i<numofwa;i++)
            sum+=wa[i].getwieght();
        for(int i=0;i<numofair;i++)
            sum+=air[i].getwieght();
        return sum;
    }
}

public class Test9{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int numtv=0,numwa=0,numair=0;
        int data[][] = new int[n][2];
        for(int i=0;i<n;i++) {
            for(int j=0;j<2;j++) {
                int a = sc.nextInt();
                if(j==0) {
                    if(a==1)
                        numtv++;
                    else if(a==2)
                        numwa++;
                    else
                        numair++;
                }
                data[i][j] = a;
            }

        }
        Trunk t = new Trunk(data,numtv,numwa,numair,n);
        System.out.println(t.getsum());
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值