PTA -- 题附代码(计算年龄) (学生数组类) (人类)

前言

  • 时间:2020.3.31
  • 内容:PTA练习题
  1. 7-1 计算年龄
  2. 7-2 类的定义与对象使用
  3. 7-3 定义类与创建对象
  • 备注:偷懒的小伙伴想直接复制上传的话,需要改类名为Main哦!!这些题都比较简单就不细说啦,和平常不同的只是把各种类和main放到了一块儿了。。

7-1 计算年龄 (10分)

package com.pro.test5;
/*7-1 计算年龄 (10分)
定义一个Birthday类,其成员变量有3个整形变量(出生的年月日):year,month,day;提供构造方法对这3个成员变量进行初始化;提供成员变量的get、set方法;成员函数有getAge(),功能是实现计算到2017年12月25日时该Birthday对象的年龄。
编写程序测试这个类。

输入格式:
输入出生的年、月、日(注:输入的年月日以换行隔开)

输出格式:
计算得到年龄
    
输入样例:
1995
12
23

输出样例:
age=22 */

import java.util.Scanner;

public class MainTest_51 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Birthday birth = new Birthday();
        while(sc.hasNext()){
            birth.year = sc.nextInt();
            birth.month = sc.nextInt();
            birth.day = sc.nextInt();
            System.out.println("age=" + birth.getAge());
        }
    }
    public static class Birthday{
        int year;
        int month;
        int day;
        public void setYear(int year) {this.year = year;}
        public void setMonth(int month) {this.year = month;}
        public void setDay(int day) {this.year = day;}
        public int getYear() {return year;}
        public int getMonth() {return month;}
        public int getDay() {return day;}
        public int getAge() {
            return 2017-this.year;
        }
        public Birthday(){
            this.year = 2000;
            this.month = 1;
            this.day = 1;
        }
    }
}

7-2 类的定义与对象使用 (10分)

package com.pro.test5;
/* 7-2 类的定义与对象使用 (10分)
请定义一个学生类(Student),包含学号、姓名、年龄(7-60岁)三个私有成员。从键盘输入学生的成员值后生成对象,并按要求输出相应的结果。

输入格式:
第一行一个整数k,代表后面要生成的学生人数。 从第二行开始的连续k行,每行3个数据,分别表示一个学生的学号、姓名和年龄。

输出格式:
输出每个学生的基本情况。如果数据不符合要求则输出"bad"

输入样例:
3
20174042001 zhangsan 20
20174042030 lisi 2
20174042050 wangwu 17

输出样例:
zhangsan 20174042001 20
bad
wangwu 20174042050 17 */

import java.util.Scanner;

public class MainTest_52 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int k = sc.nextInt();
            Student[] stu = new Student[k];
            for (int i=0; i<k; i++){
                String id = sc.next();
                String name = sc.next();
                int age = sc.nextInt();
                stu[i] = new Student(id,name,age);
            }
            for(int i=0; i<k; i++){
                if(stu[i].age <=7 || stu[i].age >=60)
                    System.out.println("bad");
                else
                    System.out.println(stu[i].name + " " + stu[i].id + " " + stu[i].age);
            }
        }
    }
    public static class Student{
        String id;
        String name;
        int age;
        Student(String id,String name,int age){
            setId(id);
            setName(name);
            setAge(age);
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        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;
        }
    }
}

7-3 定义类与创建对象 (10分)

package com.pro.test5;
/*7-3 定义类与创建对象 (10分)
定义一个类Person,定义name和age属性,定义有参的构造方法对name和age进行初始化。在测试类中创建该类的2个对象,姓名、年龄分别为lili、19和lucy、20,在屏幕打印出2个对象的姓名和年龄。

输入格式:
本题目无输入

输出格式:
在一行中输出一个人的姓名和年龄

输出样例:
this person is lili,her age is 19
this person is lucy,her age is 20*/

public class MainTest_53 {
    public static void main(String[] args) {
        Person per1 = new Person("lili",19);
        Person per2 = new Person("lucy",20);
        System.out.println(per1.toString());
        System.out.println(per2.toString());
    }
    public static class Person {
        private String name;
        private int age;
        public Person(String name,int age){
            setName(name);
            setAge(age);
        }
        @Override
        public String toString() {
            return "this person is " + name + ",her age is " + age;
        }
        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;
        }
    }
}

发布了19 篇原创文章 · 获赞 0 · 访问量 1988

猜你喜欢

转载自blog.csdn.net/hyidol/article/details/105224345
PTA