AI悦创

V1

2022/09/22阅读：27主题：凝夜紫

# 02-Part 1-Birth Date Application(30 pts)

## Part 1: Birth Date Application: (30 pts)Use MOD % and / division to separate digits for Birthday date Birthdays.java

Ask for the user to enter a two people’s birthday as an 8 digit-number! The user should enter the birthday for each person in an 8-digit format ("YYYYMMDD"). Then your program should compare the two birthday dates to determine which person is older, or if they have the same age.

For example, President’s Obama's birthday is Aug 4,1961. So, it should be entered as 19610804 (YYYYMMDD). We can assume that the user enters good data with 8 digits for each birthday so you do not have to do any data validation.

• This program should separate each 8-digit-number into three separate numbers to represent the day, month, and the year, in that order. Only use the mathematical operators to separate the digits (will cover this during class).

Thus, in our example the program would assign 4 to a first variable from the right, let's say day, then assign 8 to a second variable, let's say month and 1961 to the number variable, let's say year. (Do not worry about leading zeroes for days and months.)

Next the program should print out the date of birth written in the following standard format MM/DD/YYYY for each person. So, the output for Obama’s date of birth should be as follow: 8/4/1961

Also, your program should be able to print which person is older or the other possibility that they are the same age. You should use control structure allowing program to make decision (smart). So, result should be decided by program and not you.

## Example (Comparing Obama’s to Beyoncé Birthday):

Enter the birthday in 8-digit format for person's # 1: (YYYYMMDD): 19610804

Enter the birthday in 8-digit format for person's # 2, (YYYYMMDD): 19810904

For this example, the following result will be printed:

Enter name for person #1: Obama

Enter name for person #2: Beyoncé

Obama’s date of birth is: 8/4/1961

Beyoncé’s date of birth is: 9/4/1981

Obama is Older than Beyoncé!

A. Program Execution (90%)

Meeting all of the require

## 答案

### 1. 整数分割

``public class HW {    public static void main(String[] args) {        int a = 25;        int theunit = a % 10;        int decade = a / 10;        int decadeplusunit = theunit + decade;        System.out.println(decadeplusunit);    }}``

### 2. 年月日分割

``public class HW {    public static void main(String[] args) {        int birthday = 19610804;        int year = birthday / 10000;        int month = birthday % 10000 / 100;        int day = birthday % 10000 % 100;        System.out.println(day);    }}``

### 3. 编写成对象

``public class HW {    public static void main(String[] args) {    }    public String getBirthday(int birthday) {        int year = birthday / 10000;        int month = birthday % 10000 / 100;        int day = birthday % 10000 % 100;        String birthday_format = month + "/" + day + "/" + year;        return birthday_format;    }}``

### 4. main

``import java.util.Scanner; // Scanner == java.util.Scannerpublic class Birthday {    public static void main(String[] args) {        Birthday birthday = new Birthday();  // 类的实例化        Scanner scanner = new Scanner(System.in);//        Scanner input = new Scanner(System.in);        // 获取姓名//        System.out.println("请输入第一个姓名:");        System.out.print("请输入第一个姓名:");         String name1 = scanner.next();         // String name1 = scanner.nextLine();        // 获取日期//        System.out.println("以 8 位格式输入" + name1 + "的生日:(YYYYMMDD):");        System.out.print("以 8 位格式输入" + name1 + "的生日:(YYYYMMDD):");        int birthday1 = scanner.nextInt();        String birthday1_format = birthday.getBirthday(birthday1);        System.out.print("请输入第一个姓名:");        String name2 = scanner.next();        System.out.print("以 8 位格式输入" + name2 + "的生日:(YYYYMMDD):");        int birthday2 = scanner.nextInt();        String birthday2_format = birthday.getBirthday(birthday2);        System.out.println("输出第一个人的姓名:" + name1);        System.out.println("输出第二个人的姓名:" + name2);        System.out.println(name1 + "的出生日期是:" + birthday1_format);        System.out.println(name2 + "的出生日期是:" + birthday2_format);        if (birthday1 < birthday2) {            System.out.println(name1 + "比" + name2 + "还老!");        }else if(birthday1 == birthday2){            System.out.println(name1 + "和" + name2 + "是同年同月同日生!");        }else {            System.out.println(name2 + "比" + name1 + "还老!");        }    }    public String getBirthday(int birthday) {        int year = birthday / 10000;        int month = birthday % 10000 / 100;        int day = birthday % 10000 % 100;        String birthday_format = month + "/" + day + "/" + year;        return birthday_format;    }}``

### 5. 标准代码

``import java.util.Scanner; // Scanner == java.util.Scannerpublic class Birthday {    public static void main(String[] args) {        Birthday birthday = new Birthday();  // 类的实例化        Scanner scanner = new Scanner(System.in);//        Scanner input = new Scanner(System.in);        // 获取日期        System.out.print("Enter the birthday in 8-digit format for person's # 1: (YYYYMMDD):");        int birthday1 = scanner.nextInt();        String birthday1_format = birthday.getBirthday(birthday1);        System.out.print("Enter the birthday in 8-digit format for person's # 2, (YYYYMMDD): ");        int birthday2 = scanner.nextInt();        String birthday2_format = birthday.getBirthday(birthday2);        // 获取姓名        System.out.print("Enter name for person #1:");         String name1 = scanner.next();         // String name1 = scanner.nextLine();        System.out.print("Enter name for person #2:");        String name2 = scanner.next();        System.out.println(name1 + "\'s date of birth is:" + birthday1_format);        System.out.println(name2 + "\'s date of birth is:" + birthday2_format);//        System.out.println(name1 + "的出生日期是:" + birthday1_format);//        System.out.println(name2 + "的出生日期是:" + birthday2_format);        if (birthday1 < birthday2) {            System.out.println(name1 + " is Older than " + name2 + "!");        }else if(birthday1 == birthday2){            System.out.println(name1 + "和" + name2 + "是同年同月同日生!");        }else {            System.out.println(name2 +" is Older than " + name1 + "!");        }    }    public String getBirthday(int birthday) {        int year = birthday / 10000;        int month = birthday % 10000 / 100;        int day = birthday % 10000 % 100;        String birthday_format = month + "/" + day + "/" + year;        return birthday_format;    }}``

::: details 公众号：AI悦创【二维码】

:::

::: info AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

:::

V1