栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

PTA 设计两个有继承关系的类Person, Student( (100 分)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PTA 设计两个有继承关系的类Person, Student( (100 分)

设计个Person类,包含几个字段:姓名,年龄,身份证号。 一个学生Student类,继承Person类。Student类还包含新的字段(学号,语文成绩,数学成绩,英语成绩)和获取平均成绩和总成绩两个方法。

裁判测试程序样例:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 读入一些信息
        String name = sc.next(); 
        String ID   = sc.next();
        int age     = sc.nextInt();

        // 测试Person类
        Person p = new Person(name, ID, age);
        p.show();

        System.out.println("============================");

        // 继续读入一些信息
        String stuID  = sc.next();
        int chinese   = sc.nextInt();
        int math      = sc.nextInt();
        int english   = sc.nextInt();
        sc.close();

        // 测试Student类
        Student stu = new Student(p, stuID, chinese, math, english);        
        stu.show();

    }
}

 

输入样例:

在这里给出一组输入。例如:

张三 422456202009094356 19 20201001 98 97 94

输出样例:

在这里给出相应的输出。例如:

姓名:张三
ID:422456202009094356
年龄:19
============================
姓名:张三
ID:422456202009094356
年龄:19
学号:20201001
总成绩:289
语文成绩:98
数学成绩: 97
英语成绩:94
平均成绩:96

 

class Person {
	String name;

	String ID;

	int age;

	public Person(String name,String ID,int age) {

		this.age=age;

		this.ID=ID;

		this.name=name;

		}
	public Person() {//
		
	}
	public void show() {

		System.out.println("姓名:"+name+"nID:"+ID+"n年龄:"+age);
	}
}

class Student extends Person {
		String name;

		String ID;

		int age;

		String stuID;

		int chinese;

		int math;

		int english;
	
	public Student(Person p, String stuID,int chinese,int math,int english) {	

		   this.name=p.name;

		   this.age=p.age;

		   this.ID=p.ID;

		   this.stuID=stuID;

		   this.chinese=chinese;

	       this.math=math;

		   this.english=english;
			
	
	}
	public void show() {

		System.out.println("姓名:"+name+"nID:"+ID+"n年龄:"+age);
		
		System.out.println("学号:"+stuID);

		sum();

		System.out.println("语文成绩:"+chinese+ "n数学成绩: "+math+ "n英语成绩:"+english);

		avg();	
		
	}
	void sum() {

		int sum11;

		sum11=(chinese+math+english);

		System.out.println("总成绩:"+sum11);
	}
	void avg() {


		int avg1;

		avg1=(chinese+math+english)/3;

		System.out.println("平均成绩:"+avg1);
	}


	

}

 有没有别的方法??求解

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489728.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号