第二次过程性考核
7-5 面向对象基础-构造函数与tostring
https://gitee.com/ztsxxny/codes/1qu783a6ys4m9zre0hdw212
import java.util.Scanner;
class Person{ //定义类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
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 集体评分
https://gitee.com/ztsxxny/codes/qh8rawd23mxp4o50lgcjb39
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) {
@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 程序填空
https://gitee.com/ztsxxny/codes/cgs1pdmbwq8nl3afu9hx547
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 {
Son() {
super(true); //调用父类有参构造 ()内true或false都可以
System.out.println("Son's Constructor without parameter");
}
public void method() {
System.out.println("Son's method()");
super.method();
}
}
7-8 求两点间距离
https://gitee.com/ztsxxny/codes/l39k8ts7ifn0cdmypahxu76
import java.util.Scanner;
class Point{
double x;
double y;
Point(double x,double y){
this.x=x;
this.y=y;
}
public double distance(Point a){
return Math.abs(Math.sqrt(Math.pow((this.x-a.x),2)+Math.pow((this.y-a.y),2)));
}
}
public class Main{
public static void main(String[] args){
Scanner read=new Scanner(System.in);
String readLine=read.nextLine();
String[] data=readLine.split(" ");
Point pointA=new Point(Integer.valueOf(data[0]),Integer.valueOf(data[1]));
readLine=read.nextLine();
data=readLine.split(" ");
Point pointB=new Point(Integer.valueOf(data[0]),Integer.valueOf(data[1]));
System.out.printf("%.2f",pointA.distance(pointB));
}
}