封装 作业

public class LocalDate {
public static void main(String[] args) {

    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime plusDaysResult = localDateTime.plusDays(20L);
    System.out.println("当前时间是 : " + localDateTime + "\n"
            + "当前时间加20天后为 : " + plusDaysResult + "\n");


}

}

int main()
{
int x,y;
printf("请输入第一个数:\n");
scanf("%d",&x);
printf("请输入第二个数:\n");
scanf("%d",&y);
prinf("相加:%d\n",x+y);
prinf("相减:%d\n",x-y);
prinf("相乘:%d\n",x*y);
prinf("相除:%.2f\n",x/(y*1.0));//把y*1.0转换成float类型的
printf("取余:%d",x%y);
}
package lxx;
import java.util.Scanner;

    /**
 * @author kangjiafu
 * @date 2021/5/5 20:40
 */
public class main {
        public static void main(String[] args) {
            int totalDay = 0;
            int dayOfWeek;
            int day = 0;
            int dayOfYear = 0;
            Scanner cs = new Scanner(System.in);
            System.out.print("请输入年:");
            int year = cs.nextInt();
            System.out.print("请输入月:");
            int month = cs.nextInt();
            boolean bool = false;

            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                bool = true;
            }

            for (int i = 1900; i < year; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                    totalDay += 366;
                } else {
                    totalDay += 365;
                }
            }
            for (int i = 1; i <= month; i++) {
                switch (i) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        day = 31;
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        day = 30;
                        break;

                    case 2:
                        if (bool) {
                            day = 29;
                            break;
                        } else {
                            day = 28;
                            break;
                        }


                }
                if (i < month) {
                    dayOfYear += day;

                }

                totalDay += dayOfYear;
                dayOfWeek = (1 + totalDay) % 7;
                System.out.println("星期天\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");

                for (int i1 = 0; i < dayOfWeek; i++) {
                    System.out.print("\t");
                }
                for (int i1 = 1; i <= day; i++) {
                if ((totalDay + i) % 7 == 6) {
                        System.out.print(i + "\n");
                    } else {
                        System.out.print(i + "\t");
                    }
                }
            }
        }
    }


public class Cat {
    String name;
    String color;
    int age;
    public void xingMing(){
        System.out.println("妮妮");
    }
    public void hanJiao(){
        System.out.println("喵");
    }
}
public class CatTest {
    public static void main(String[] args){
        Cat c=new Cat();

        c.name="妮妮";
        c.color="灰色";
        c.age=2;
        System.out.println("该猫的颜色为:"+c.color);
        System.out.println("该猫的年龄为:"+c.age+"岁");
        
        c.xingMing();
        c.hanJiao();
    }
}
public class main {
    private String chepaihao;
    private int chesu;
    private double zaizhong;
    public String getChepaihao() {
        return chepaihao;
    }
    public void setChepaihao(String chepaihao) {
        this.chepaihao = chepaihao;
    }
    public int getChesu() {
        return chesu;
    }
    public void setChesu(int chesu) {
        this.chesu = chesu;
    }
    public double getZaizhong() {
        return zaizhong;
    }
    public void setZaizhong(double zaizhong) {
        this.zaizhong = zaizhong;
    }
    public main()
    {
        chepaihao="XX231";
        chesu=100;
        zaizhong=100;
    }
    public main(String chepaihao, int chesu, double zaizhong) {
        super();
        this.chepaihao = chepaihao;
        this.chesu = chesu;
        this.zaizhong = zaizhong;
    }
    public void jiasu(int sudu)
    {
        if(sudu<0)
        {
            System.out.println("无法加速");
        }
        else
        {
            chesu+=sudu;
        }
    }
    public void jiansu(int sudu)
    {
        if(sudu<0)
        {
            System.out.println("无法减速");
        }

        else
        {

            if((chesu-sudu)<0)
            {
                System.out.println("减速失败");
            }
            else
            {
                chesu-=sudu;
            }

        }
    }


}

public class che {
    public static void main(String[] args) {
        main che1=new main();
        che1.setChepaihao("豫k5651");
        che1.jiasu(30);
        System.out.println("车牌号="+che1.getChepaihao());
        System.out.println("当前车速="+che1.getChesu());
        System.out.println("载重="+che1.getZaizhong());

        main che2=new main("豫j0394",120,200);
        che2.jiansu(20);
        System.out.println("车牌号="+che2.getChepaihao());
        System.out.println("当前车速="+che2.getChesu());
        System.out.println("载重="+che2.getZaizhong());

    }
}

6.封装的作用

(1)便于使用者正确使用系统,防止错误修改属性

(2)降低了构建大型系统的风险

(3)提高程序的可重用性

(4)降低程序之间的耦合度

7.C

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 从控制台输入一个日期,求此日期20天后的日期值(时间类型为 LocalDateTIme ,做完后再使用 java....
    康嘉富阅读 1,117评论 0 0
  • 从控制台输入一个日期,求此日期20天后的日期值(时间类型为 LocalDateTIme ,做完后再使用 java....
    陈笑扬阅读 1,202评论 0 0
  • 从控制台输入一个日期,求此日期20天后的日期值(时间类型为 LocalDateTIme ,做完后再使用 java....
    王艺博阅读 3,051评论 0 0
  • 目录: 设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒...
    加油小杜阅读 4,042评论 0 1
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 12,755评论 28 53

友情链接更多精彩内容