第二次过程性考核
Posted qmmm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二次过程性考核相关的知识,希望对你有一定的参考价值。
码云仓库链接:https://gitee.com/QM0829/codes
7-5 jmu-Java-03面向对象基础-01-构造函数与toString (3 分)
定义一个有关人的Person
类,内含属性:String name
、int age
、boolean gender
、int id
,所有的变量必须为私有(private
)。
注意:属性顺序请严格按照上述顺序依次出现。
1.编写无参构造函数:
- 打印"This is constructor"。
- 将name,age,gender,id按照
name,age,gender,id
格式输出
2.编写有参构造函数
依次对name,age,gender
赋值。
3.覆盖toString函数:
按照格式:类名 [name=, age=, gender=, id=]
输出。建议使用Eclipse自动生成.
4.对每个属性生成setter/getter方法
5.main方法中
- 首先从屏幕读取n,代表要创建的对象个数。
- 然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。
- 然后将刚才创建的所有对象
逆序
输出。 - 接下来使用无参构造函数新建一个Person对象,并直接打印该对象。
输入样例:
3
a 11 false
b 12 true
c 10 false
输出样例:
Person [name=c, age=10, gender=false, id=0]
Person [name=b, age=12, gender=true, id=0]
Person [name=a, age=11, gender=false, id=0]
This is constructor
null,0,false,0
Person [name=null, age=0, gender=false, id=0]
import java.util.Scanner; class Person{ private String name = null; private int age = 0; private boolean gender = false; private int id = 0; public Person() { System.out.println("This is constructor"); System.out.println(name+","+age+","+gender+","+id); System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]"); } public Person(String n, int a, boolean g) { this.name = n; this.age = a; this.gender = g; } public String toString() { System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+0+"]"); return name; } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub @SuppressWarnings("resource") Scanner reader = new Scanner(System.in); int number = reader.nextInt(); Person[] per = new Person[number]; for(int i=0; i<per.length; i++) { String name = reader.next(); int age = reader.nextInt(); boolean genter = reader.nextBoolean(); per[i] = new Person(name,age,genter); } for(int x=per.length-1; x>=0;x--){ per[x].toString(); } per.toString(); @SuppressWarnings("unused") Person s = new Person(); } }
import java.util.Scanner; class Person{ private String name = null; private int age = 0; private boolean gender = false; private int id = 0; public Person() { System.out.println("This is constructor"); System.out.println(name+","+age+","+gender+","+id); System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]"); } public Person(String n, int a, boolean g) { this.name = n; this.age = a; this.gender = g; } public String toString() { System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+0+"]"); return name; } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub @SuppressWarnings("resource") Scanner reader = new Scanner(System.in); int number = reader.nextInt(); Person[] per = new Person[number]; for(int i=0; i<per.length; i++) { String name = reader.next(); int age = reader.nextInt(); boolean genter = reader.nextBoolean(); per[i] = new Person(name,age,genter); } for(int x=per.length-1; x>=0;x--){ per[x].toString(); } per.toString(); @SuppressWarnings("unused") Person s = new Person(); } }
7-6 集体评分 (5 分)
程序填空题。请补充以下代码,完成题目要求。(注意:需要提交完整代码) 有一个团队由5个人组成。他们每个人给指导老师一个分数,去掉最高分,去掉最低分,剩下的3个分数的平均分就是该团队对指导老师的评分。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] grade = new int[5]; for(int i=0; i<grade.length; i++){ grade[i] = in.nextInt(); } RR rr = new RT(grade); double dd = rr.mark(); System.out.printf("%.2f",dd); } } abstract class RR{ int[] grade; public RR(int[] grade){ this.grade = grade; } public abstract double mark(); } class RT extends RR{ }
输入格式:
在一行中给出5个不超过10的正整数(从小到大排列)。
输出格式:
输出集体评分,保留小数点后两位。
输入样例:
1 2 4 6 9
import java.util.Scanner; abstract class RR{ int[] grade; public RR(int[] grade){ this.grade = grade; } public abstract double mark(); } class RT extends RR{ public RT(int[] grade) { super(grade); } public double mark(){ double x = (grade[1]+grade[2]+grade[3])/3; return x; } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub @SuppressWarnings("resource") Scanner in = new Scanner(System.in); int[] grade = new int[5]; for(int i=0; i<grade.length; i++){ grade[i] = in.nextInt(); } RR rr = new RT(grade); double dd = rr.mark(); System.out.printf("%.2f",dd); } }
7-7 程序填空题3 (5 分)
参照输出样例补全以下程序,使程序输出结果与输出样例一致。
public class Main { public static void main(String[] args) { Son son = new Son(); son.method(); } } class Parent { Parent() { System.out.println("Parent‘s Constructor without parameter"); } Parent(boolean b) { System.out.println("Parent‘s Constructor with a boolean parameter"); } public void method() { System.out.println("Parent‘s method()"); } } class Son extends Parent { //补全本类定义 }
输入格式:
无
输出格式:
无
输入样例:
输出样例:
Parent‘s Constructor with a boolean parameter
Son‘s Constructor without parameter
Son‘s method()
Parent‘s method()
class Parent { Parent() { System.out.println("Parent‘s Constructor without parameter"); } Parent(boolean b) { System.out.println("Parent‘s Constructor with a boolean parameter"); } public void method() { System.out.println("Parent‘s method()"); } } class Son extends Parent { //补全本类定义 Son(){ super(true);//调用父类含有参数的构造方法 System.out.println("Son‘s Constructor without parameter"); } public void method() { System.out.println("Son‘s method()"); super.method();//调用父类的method方法 } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Son son = new Son(); son.method(); } }
7-8 求两点之间距离 (20 分)
定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数。 定义一个函数Distance(), 用于求两点之间的距离。
输入格式:
输入有两行: 第一行是第一个点的x坐标和y坐标; 第二行是第二个点的x坐标和y坐标。
输出格式:
输出两个点之间的距离,保留两位小数。
输入样例:
0 9 3 -4
输出样例:
13.34
import java.util.Scanner; public class Main { public static void main(String[] args) { @SuppressWarnings("resource") Scanner reader = new Scanner(System.in); int x1 = reader.nextInt(); int y1 = reader.nextInt(); int x2 = reader.nextInt(); int y2 = reader.nextInt(); Point spot = new Point(x1, y1); Point spot2 = new Point(x2, y2); double m = Point.Distance(spot, spot2); System.out.printf("%.2f", m); } } class Point { double x; double y; public Point(double x, double y) { this.x = x; this.y = y; } public static double Distance(Point a, Point b) { double c = 0; double i = Math.pow((a.x - b.x), 2); double j = Math.pow((a.y - b.y), 2); c = Math.sqrt(i + j); return c; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { @SuppressWarnings("resource") Scanner reader = new Scanner(System.in); int x1 = reader.nextInt(); int y1 = reader.nextInt(); int x2 = reader.nextInt(); int y2 = reader.nextInt(); Point spot = new Point(x1, y1); Point spot2 = new Point(x2, y2); double m = Point.Distance(spot, spot2); System.out.printf("%.2f", m); } } class Point { double x; double y; public Point(double x, double y) { this.x = x; this.y = y; } public static double Distance(Point a, Point b) { double c = 0; double i = Math.pow((a.x - b.x), 2); double j = Math.pow((a.y - b.y), 2); c = Math.sqrt(i + j); return c; } }
以上是关于第二次过程性考核的主要内容,如果未能解决你的问题,请参考以下文章