java面向对象作业3

题目3:

设计,并定义一个学员类:Student,要求有以下属性:

	学员编号(String) 
	姓名(String)
	性别(String)
	身高(double)
	年龄(int)

使用封装的原则,并提供无参和全参的构造方法。

1 定义MainApp类,并包含main()方法。

2 在main方法,运行程序,应分别提示用户输入学员编号、姓名等信息。

【提示:键盘输入字符串。String s = sc.next();】

例如控制台显示:

请输入学员编号:
xxx
请输入学员姓名:
xxx

数据接收到程序中,并定义变量存储;

创建Student对象,通过构造方法将所有数据存储到Student对象中;

在main方法中获取并打印对象中的每个属性值。

参考代码:

public class Student {
    private String id;
    private String name;
    private String gender;
    private double height;
    private int age;
    public Student(){}
    public Student(String id, String name, String gender, double height, int age) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.height = height;
        this.age = 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 String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

import java.util.Scanner;
public class MainApp {
    public static void main(String[] args) {
        System.out.println("请输入您的编号");
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        System.out.println("请输入您的姓名");
        Scanner sc1 = new Scanner(System.in);
        String s2 = sc.nextLine();
        System.out.println("请输入您的性别");
        Scanner sc2 = new Scanner(System.in);
        String s3 = sc.nextLine();
        System.out.println("请输入您的身高");
        Scanner sc3 = new Scanner(System.in);
        double s4 = sc.nextDouble();
        System.out.println("请输入您的年龄");
        Scanner sc4 = new Scanner(System.in);
        int s5 = sc.nextInt();
        Student student = new Student(s1,s2,s3,s4,s5);
        System.out.println(student.getId());
        System.out.println(student.getName());
        System.out.println(student.getGender());
        System.out.println(student.getHeight());
        System.out.println(student.getAge());
    }
}
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页