面向对象基础-封装 作业题 (一些常见的日期类API的使用)

第一题:圆类

(1)声明一个圆的图形类,包含实例变量/属性:半径

(2)在测试类的main中,创建2个圆类的对象,并给两个圆对象的半径属性赋值,最后显示两个圆的半径值、周长和面积值

提示:圆周率可以使用Math.PI

圆类:Circle

public class Circle {
    private double radius; // 半径

    public Circle() {
    }
    public Circle(double radius) {
        this.radius = radius;
    }
    public double perimeter(){
        return 2 * Math.PI * radius;
    }
    public double area(){
        return Math.PI * radius * radius;
    }

    @Override
    public String toString() {
        return "圆{" +
                "半径=" + radius +
                "周长=" + perimeter() +
                "面积=" + area() +
                '}';
    }
}

测试类:CircleTest

public class CircleTest {
    public static void main(String[] args) {
        Circle circle1 = new Circle(3);
        Circle circle2 = new Circle(2);
        Circle circle3 = new Circle(1);
        System.out.println(circle1);
        System.out.println(circle2);
        System.out.println(circle3);

    }
}

第二题:学生类

(1)声明一个学生类,包含实例变量/属性:姓名和成绩

(2)在测试类的main中,创建2个学生类的对象,并给两个学生对象的姓名和成绩赋值,最后输出显示

学生类:Student

public class Student {
    private String name; // 姓名
    private int grade;  // 成绩

    public Student() {
    }

    public Student(String name, int grade) {
        this.name = name;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "学生{" +
                "姓名='" + name + '\'' +
                ", 成绩=" + grade +
                '}';
    }
}

测试类:StudentTest

public static void main(String[] args) {
        Student s1 = new Student("张三",99);
        Student s2 = new Student("李四",88);
        Student s3 = new Student("王五",77);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }

第三题:MyInt类

(1)声明一个MyInt类,

包含一个int类型的value属性

包含一个方法boolean isNatural()方法,用于判断value属性值是否是自然数。自然数是大于等于0的整数。

包含一个方法int approximateNumberCount()方法,用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。

包含一个方法boolean isPrimeNumber()方法,用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数,并且value是大于1的自然数,那么value就是素数。

包含一个方法int[] getAllPrimeNumber()方法,用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。

(2)测试类的main中调用测试

MyInt类:

public class MyInt {
    private int value;

    public boolean isNatural(int value){
        return value >= 0;
    }

    public int approximateNumberCount(int value){
        int count = 0;
        for(int i = 1; i <= value; i++){
            if(value % i == 0){
                count++;
            }
        }
        return count;
    }

    public boolean isPrimeNumber(int value){
        if(value <= 0){
            return false;
        }
        boolean flag = true;
        for(int i = 2; i <= value-1; i++){
            if(value % i == 0){
                return false;
            }
        }
        return flag;
    }
    public int[] getAllPrimeNumber(int value){
        int []arr = new int[10];
        int count = 0;
        for(int i = 1; i <= value; i++){
            if(value % i == 0){
                arr[count++] = i;
            }
        }
        return arr;
    }

}

 测试类:MyIntTest

public class MyIntTest {
    public static void main(String[] args) {
        MyInt myInt = new MyInt();
        System.out.println(myInt.isNatural(12));
        System.out.println(myInt.approximateNumberCount(12));
        System.out.println(myInt.isPrimeNumber(11));
        System.out.println(Arrays.toString(myInt.getAllPrimeNumber(12)));

    }
}

第四题:MyDate日期类-1

声明一个日期类MyDate,包含属性:年、月、日,并在MyDate类中声明几个方法:

1、boolean isLeapYear():判断当前日期的是闰年吗

2、void set(int y, int m, int d):修改年,月,日为新日期

3、void puls(int years, int months, int days):加上years年,months月,days天后的日期

并在测试类Exercise4的main方法中创建对象,并调用测试

MyDate类:

public class MyDate {
    public int year;
    public int month;
    public int day;


    public MyDate() {
    }

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public boolean isLeapYear(){
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    public void set(int y, int m, int d){
        this.year = y;
        this.month = m;
        this.day = d;
    }
    public void puls(int years, int months, int days){
        LocalDate ld = LocalDate.of(this.year, this.month, this.day);
        ld = ld.plusYears(years);
        ld = ld.plusMonths(months);
        ld = ld.plusDays(days);
        this.year = ld.getYear();
        this.month = ld.getMonthValue();
        this.day = ld.getDayOfMonth();

    }

    @Override
    public String toString() {
        return "日期{" +
                "年份=" + year +
                ", 月份=" + month +
                ", 天数=" + day +
                '}';
    }
}

测试类:MyDateTest

public class MyDateTest {
    public static void main(String[] args) {
        MyDate myDate = new MyDate(2004,2,4);
        System.out.println("修改前"+myDate);
        System.out.println(myDate.isLeapYear());
        myDate.set(2000,9,23);
        System.out.println("修改后"+myDate);
        myDate.puls(24,4,12);
        System.out.println("相加后"+myDate);
    }
}

第五题:MyDate日期类-2

声明一个日期类MyDate,

包含属性:年、月、日

boolean isLeapYear():判断是否是闰年

String monthName():根据月份值,返回对应的英语单词

int totalDaysOfMonth():返回这个月的总天数

int totalDaysOfYear():返回这一年的总天数

int daysOfTheYear():返回这一天是当年的第几数

在测试类的main方法中,创建MyDate对象,赋值为当天日期值,调用方法测试。

MyDate2类:

public class MyDate2 {
    public int year;
    public int month;
    public int day;


    public MyDate2() {
    }

    public MyDate2(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public boolean isLeapYear(){
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    public String monthName(){
        String []months = {"January","February","March","April","May","June",
                "July","August","September","October","November","December"};
        return  months[this.month-1];
    }
    public int totalDaysOfMonth(){
        YearMonth yearMonth = YearMonth.of(this.year, this.month);
        return yearMonth.lengthOfMonth();
    }
    public int totalDaysOfYear(){
        YearMonth yearMonth = YearMonth.of(this.year, this.month);
        return yearMonth.lengthOfYear();
    }
    public int daysOfTheYear(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(this.year, this.month, this.day);
        return calendar.get(Calendar.DAY_OF_YEAR);
    }
}

测试类:MyDate2Test

public class MyDate2Test2 {
    public static void main(String[] args) {
        MyDate2 myDate = new MyDate2(2024, 11, 17);
        System.out.println("是否是闰年:"+myDate.isLeapYear());
        System.out.println(myDate.month+"月份的英文单词是:"+myDate.monthName());
        System.out.println(myDate.month+"月有:"+myDate.totalDaysOfMonth()+"天");
        System.out.println(myDate.year+"年有:"+myDate.totalDaysOfYear()+"天");
        System.out.println(myDate.year+"年"+myDate.month+"月"+myDate.day+"日是这一年中的第:"+myDate.daysOfTheYear()+"天");
    }
}

第六题:数学计算工具类

声明一个数学计算工具类MathTools,包含如下方法:

1、int add(int a, int b):求a+b

2、int subtract(int a,int b):求a-b

3、int mutiply(int a, int b):求a*b

4、int divide(int a, int b):求a/b

5、int remainder(int a, int b):求a%b

6、int max(int a, int b):求a和b中的最大值

7、int min(int a, int b):求a和b中的最小值

8、boolean equals(int a, int b):判断a和b是否相等

9、boolean isEven(int a):判断a是否是偶数

10、boolean isPrimeNumer(int a):判断a是否是素数

11、int round(double d):返回d的四舍五入后的整数值

声明一个Test06测试类,并在main方法中调用测试

MathTools类:

public class MathTools {
    int add(int a, int b){
        return a+b;
    }
    int subtract(int a,int b){
        return a-b;
    }
    int mutiply(int a, int b) {
        return a*b;
    }
    int divide(int a, int b) {
        return a/b;
    }
    int remainder(int a, int b) {
        return a%b;
    }
    int max(int a, int b) {
        return a > b ? a : b;
    }
    int min(int a, int b) {
        return a < b ? a : b;
    }
    boolean equals(int a, int b) {
        return a == b;
    }
    boolean isEven(int a){
        return a % 2 == 0;
    }
    boolean isPrimeNumer(int a) {
        for (int i = 2; i < a; i++) {
            if(a%i == 0){
                return false;
            }
        }
        return true;
    }
    int round(double d) {
        d += 0.5;
        return (int)d;
    }

}

测试类:MathToolsTest

public class MathToolsTest {
    public static void main(String[] args) {
        MathTools mathTools = new MathTools();
        int a = 12,b = 6;
        System.out.println(a+" + "+b+" = "+mathTools.add(a,b));
        System.out.println(a+" - "+b+" = "+mathTools.subtract(a,b));
        System.out.println(a+" * "+b+" = "+mathTools.mutiply(a,b));
        System.out.println(a+" / "+b+" = "+mathTools.divide(a,b));
        System.out.println(a+" % "+b+" = "+mathTools.remainder(a,b));
        System.out.println(a+","+b+"中的最大值是:"+mathTools.max(a,b));
        System.out.println(a+","+b+"中的最小值是:"+mathTools.min(a,b));
        System.out.println(a+","+b+"是否相等:"+mathTools.equals(a,b));
        System.out.println(a+"是否是偶数:"+mathTools.isEven(a));
        System.out.println(a+"是否是素数:"+mathTools.isPrimeNumer(a));
        System.out.println("12.8四舍五入后是:"+mathTools.round(12.8));

    }
}

第七题:常识工具类

声明一个常识工具类CommonsTools,包含如下方法:

1、String getWeekName(int week):根据星期值,返回对应的英语单词

2、String getMonthName(int month):根据月份值,返回对应的英语单词

3、int getTotalDaysOfMonth(int year, int month):返回某年某月的总天数

4、int getTotalDaysOfYear(int year):获取某年的总天数

5、boolean isLeapYear(int year):判断某年是否是闰年

声明一个Test08测试类,并在main方法中调用测试

CommonsTools类:

public class CommonsTools {
    String getWeekName(int week){
        String []weeks ={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
        return weeks[week-1];
    }
    public String getMonthName(int month){
        String []months = {"January","February","March","April","May","June",
                "July","August","September","October","November","December"};
        return  months[month-1];
    }
    public int getTotalDaysOfMonth(int year, int month){
        YearMonth yearMonth = YearMonth.of(year,month);
        return yearMonth.lengthOfMonth();
    }
    public int getTotalDaysOfYear(int year){
        YearMonth yearMonth = YearMonth.of(year,1);
        return yearMonth.lengthOfYear();
    }
    public boolean isLeapYear(int year){
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}

测试类:CommonsToolsTest

public class CommonsToolsTest {
    public static void main(String[] args) {
        CommonsTools commonsTools = new CommonsTools();
        System.out.println(commonsTools.getMonthName(12));
        System.out.println(commonsTools.getWeekName(3));
        System.out.println(commonsTools.getTotalDaysOfYear(2024));
        System.out.println(commonsTools.getTotalDaysOfMonth(2024,11));
        System.out.println(commonsTools.isLeapYear(2024));
    }
}

第八题:学生对象数组

(1)定义学生类Student

声明姓名和成绩实例变量

String getInfo()方法:用于返回学生对象的信息

(2)测试类的main中创建一个可以装3个学生对象的数组,从键盘输入3个学生对象的信息,并且按照学生成绩排序,显示学生信息

学生类:Student2

public class Student2 {
    private String name; // 姓名
    private int grade;  // 成绩

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public String getInfo() {
        return "Student2{" +
                "name='" + name + '\'' +
                ", grade=" + grade +
                '}';
    }
}

测试类:Student2Test

public class Student2Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Student2 []students = new Student2[3];
        for (int i = 0; i < students.length; i++) {
            Student2 st = new Student2();
            System.out.println("请输入第"+(i+1)+"个学生的名字:");
            st.setName(sc.next());
            System.out.println("请输入第"+(i+1)+"个学生的成绩:");
            st.setGrade(sc.nextInt());
            students[i] = st;
        }
        // 按照学生成绩排序
        for (int i = 0; i < students.length-1; i++) {
            for (int j = 0; j < students.length-1-i; j++) {
                if (students[j].getGrade() > students[j+1].getGrade()) {
                    Student2 temp = students[j];
                    students[j] = students[j+1];
                    students[j+1] = temp;
                }
            }
        }
        System.out.println("排序后:");
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].getInfo());
        }
    }
}

第九题:员工管理类-1

 1、声明一个Employee员工类,

 包含属性:编号(id)、姓名(name)、薪资(salary)、年龄(age),此时属性不私有化

 包含方法:

 (1)void printInfo():可以打印员工的详细信息

 (2)void setInfo(int i, String n, double s, int a):可以同时给id,name,salary,age赋值

 2、声明一个EmployeeManager类,包含如下方法:

(1)public void print(Emplyee[] all):遍历打印员工数组中的每个员工的详细信息

(2)public void sort(Employee[] all):将all员工数组按照年龄从高到低排序

(3)public void addSalary(Employee[] all, double increament):将all员工数组的每一个员工的工资,增加increament

 3、声明Test05测试类

(1)public static void main(String[] args):在main方法中,创建Employee[]数组,并创建5个员工对象放到数组中,并为

员工对象的属性赋值

(2)创建EmployeeManager对象,

(3)调用print方法,显示员工信息

(4)调用sort方法对员工数组进行按照年龄排序,并调用print方法,显示员工信息

(5)调用addSalary方法给每一个员工加薪1000元,并调用print方法,显示员工信息

Employee类:

public class Employee {
    int id;     // 编号
    String name; // 名字
    double salary;//薪资
    int age;      // 年龄


    public void setInfo(int i, String n, double s, int a){
        this.id = i;
        this.name = n;
        this.salary = s;
        this.age = a;
    }
    public String printInfo() {
       return ("员工{" +
                "编号=" + id +
                ", 名字='" + name + '\'' +
                ", 薪资=" + salary +
                ", 年龄=" + age +
                '}');
    }

}

EmployeeManager类:

public class EmployeeManager {
    public void print(Employee[] all){
        for(Employee e:all){
            System.out.println(e.printInfo());
        }
    }
    public void sort(Employee[] all){
        for (int i = 0; i < all.length - 1; i++) {
            for (int j = 0; j < all.length - i - 1; j++) {
                if(all[j].age < all[j + 1].age){
                    Employee temp = all[j];
                    all[j] = all[j + 1];
                    all[j + 1] = temp;
                }
            }
        }
    }

    public void addSalary(Employee[] all, double increament){
        for(Employee e:all){
            e.salary += increament;
        }
    }
}

测试类:EmployeeTest:

public class EmployeeTest {
    public static void main(String[] args) {
        EmployeeManager em = new EmployeeManager();
        Employee[] arr = new Employee[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new Employee();
        }
        arr[0].setInfo(1001,"张三",14999.99,23);
        arr[1].setInfo(1002,"刘二",18888.88,24);
        arr[2].setInfo(1003,"李四",12999.28,22);
        arr[3].setInfo(1004,"王五",16888.88,34);
        arr[4].setInfo(1005,"赵六",13999.99,25);
        em.print(arr);
        em.sort(arr);
        System.out.println("排序后:");
        em.print(arr);
        em.addSalary(arr,1000);
        System.out.println("加薪后:");
        em.print(arr);
    }
}

第十题:员工管理类-2

声明一个Employee员工类,包含属性:编号(id)、姓名(name)、薪资(salary)、年龄(age),包含如下方法:

String getInfo():返回员工的详细信息,每一个

void setInfo(int i, String n, double s, int a):可以同时给id,name,salary,age赋值

声明一个EmployeeManager员工管理类,包含:

Employee[]类型的allEmployees,长度指定为5

int类型的实例变量total,记录allEmployees数组实际存储的员工数量

boolean addEmployee(Employee emp):添加一个员工对象到allEmployees数组中,如果数组已满,则不添加并提示数组已满

Employee[] getEmployees():返回total个员工对象

在测试类的main中添加6个员工对象,并且遍历输出

Employee2类:

public class Employee2 {
    int id;     // 编号
    String name; // 名字
    double salary;//薪资
    int age;      // 年龄

    public Employee2() {
    }

    public Employee2(int id, String name, double salary, int age) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.age = age;
    }

    public void setInfo(int i, String n, double s, int a){
        this.id = i;
        this.name = n;
        this.salary = s;
        this.age = a;
    }
    public String printInfo() {
       return ("员工{" +
                "编号=" + id +
                ", 名字='" + name + '\'' +
                ", 薪资=" + salary +
                ", 年龄=" + age +
                '}');
    }
}

EmployeeManager2类:

public class EmployeeManager2 {
    Employee2[] allEmployees = new Employee2[5];
    int total;

    public boolean addEmployee2(Employee2 emp){
        if(total == allEmployees.length){
            System.out.println("数组已满,不能在添加数据");
            return false;
        }else {
            allEmployees[total++] = emp;
        }
        return true;
    }
    public void print2(){
        for(Employee2 e:allEmployees){
            System.out.println(e.printInfo());
        }
    }
    public Employee2[] getEmployees(){
        Employee2[] all = new Employee2[total];
        for(int i=0; i<total; i++){
            all[i] = allEmployees[i];
        }
        return all;
    }
}

测试类:Employee2Test:
 

public class Employee2Test {
    public static void main(String[] args) {
        EmployeeManager2 em2 = new EmployeeManager2();
        em2.addEmployee2(new Employee2(1001,"张三",14999.99,23));
        em2.addEmployee2(new Employee2(1002,"刘二",18888.88,24));
        em2.addEmployee2(new Employee2(1003,"李四",12999.28,22));
        em2.addEmployee2(new Employee2(1004,"王五",16888.88,34));
        em2.addEmployee2(new Employee2(1005,"赵六",13999.99,25));
        em2.addEmployee2(new Employee2(1001,"王麻子",14999.99,21));
        em2.print2();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值