Java封装练习题

第一题:

一、练习题目

编写程序描述狗

二、问题描述

使用面向对象的思想,编写自定义描述狗的信息。设定属性包括:品种,年龄,心情,名字;方法包括:叫,跑

三、要求:

1、设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问

2、限定心情只能有“心情好”和“心情不好”两种情况,如果无效输入进行提示,默认设置“心情好”。

3、设置构造函数实现对属性赋值

4、叫和跑的方法,需要根据心情好坏,描述不同的行为方式。

5、编写测试类,测试狗类的对象及相关方法(测试数据信息自定义)

 

 第二题:

一、练习题目

编写程序描述IT从业者

二、问题描述

以面向对象的思想,编写自定义类描述IT从业者。设定属性包括:姓名,年龄,技术方向,工作年限;方法包括:工作

三、要求:

1、设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问

2、限定IT从业人员必须年满15岁,无效信息需提示,并设置默认年龄为15

3、工作方法通过输入参数,接收工作单位和职务,输出个人工作信息

4、编写测试类,测试IT从业者类的对象及相关方法(测试数据信息自定义)

 

 第三题:

一、练习题目

编写程序描述卡车信息

二、问题描述

某公司要开发X出租公司车辆管理系统”,请用面向对象的思想设计卡车类。

设定:

属性:车牌号、车型、颜色、日租金、载重量

方法:租赁

三、要求:

1、设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问

2、租赁方法通过输入参数,接收租车人姓名和租赁时间,描述租赁状态,要求判断租赁时间的有效性。

3、设计构造函数实现属性赋值

4、编写测试类,测试卡车类的对象及相关方法(测试数据信息自定义)

 

 

第一题:
package fengzhuang;

public class homework1 {
    private String type;
    private int age;
    private String mood="心情好";
    public homework1(String type,int age,String mood)
    {
        this.type=type;
        this.age=age;
        this.mood=mood;
    }
    public void shoot()
    {
        if(this.mood.equals("心情好"))
        {
            System.out.println("汪汪汪!");
        }
        else
        {
            System.out.println("呜呜呜~");
        }
    }
    public void run()
    {
        if(this.mood.equals("心情好"))
        {
            System.out.println("跑得快!");
        }
        else
        {
            System.out.println("跑得慢~");
        }
    }
    public static boolean ismood(String c)
    {
        if(c.equals("心情好")||c.equals("心情不好"))
            return true;
        else
            return false;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getMood() {
        return mood;
    }
    public void setMood(String mood) {
        this.mood = mood;
    }
    
}


package fengzhuang;

import java.util.Scanner;

public class test {
    
    public static void main(String[] args) {
        System.out.println("依次输入名字,年纪,心情:");
        Scanner s=new Scanner(System.in);
        String a=s.next();
        int b=s.nextInt();
        String c=s.next();
        if(homework1.ismood(c))
        {
            homework1 dog=new homework1(a, b, c);
            dog.run();
            dog.shoot();
        }
        else
        {
            System.out.println("您输入的心情错误");
        }
    }
}
第二题:
package fengzhuang;

public class homework2 {
    private String name;
    private int age;
    private String field;
    private int years;
    
    public homework2(String name,int age,String field,int years)
    {
        this.name=name;
        this.age=age;
        this.field=field;
        this.years=years;
    }
    public static boolean isAge(int age)
    {
        return age>=15;
    }
    public void information(String workplace,String kind)
    {
        System.out.println("工作在"+workplace+"的"+age+"岁的"+kind+name+"的工作年限是"+years+",工作地点是"+workplace);
    }
    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 getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public int getYears() {
        return years;
    }

    public void setYears(int years) {
        this.years = years;
    }
    
}
package fengzhuang;

import java.util.Scanner;

public class test {
    
    public static void main(String[] args) {
        System.out.println("依次输入名字,年纪,工作方向,工作年限:");
        Scanner s=new Scanner(System.in);
        String a=s.next();
        int b=s.nextInt();
        String c=s.next();
        int d=s.nextInt();
        if(homework2.isAge(b))
        {
            homework2 people=new homework2(a, b, c, d);
            System.out.println("依次输入工作地点和工作职务:");
            String e=s.next();
            String f=s.next();
            people.information(e, f);
            
        }
        else
        {
            System.out.println("您输入年龄错误");
        }
        
        

    }
}
第三题:
package fengzhuang;

public class homework3 {
    private String id;
    private String type;
    private String color;
    private String price;
    private double weight;
    public homework3(String id,String type,String color,String price,double weight)
    {
        this.id=id;
        this.type=type;
        this.color=color;
        this.price=price;
        this.weight=weight;
    }
    public void zulin(String name,double hours)
    {
        if(hours>0){
        System.out.println(name+"租了一辆"+color+type+",车号是"+id+",租赁时间是"+hours+"h,日租价格是"+price+"载重为"+weight+"kg");
        }
        else
        {
            System.out.println("租赁时间不能为负数!");
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }
}
package fengzhuang;

import java.util.Scanner;

public class test {
    
    public static void main(String[] args) {
        System.out.println("依次输入:车牌号、车型、颜色、日租金(原)、载重量(kg)");
        Scanner s=new Scanner(System.in);
        String a=s.next();
        String b=s.next();
        String c=s.next();
        String d=s.next();
        Double e=s.nextDouble();
        homework3 zuChe=new homework3(a, b, c, d, e);
        System.out.println("依次输入借车人名字和借车时间(h):");
        String f=s.next();
        int g=s.nextInt();
        zuChe.zulin(f, g);
        
        

    }
}

 

posted @ 2021-08-11 09:12  热心市民陆女士  阅读(343)  评论(0编辑  收藏  举报