第二次过程性考核
7-5 jmu-Java-03面向对象基础-01-构造函数与toString
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 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 集体评分
import java.util.Scanner;
import java.util.Arrays;
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{
RT(int[] grade){
super(grade); //调用父类有参构造函数
}
public double mark(){
Arrays.sort(grade); //将数组 升序排序
return (double)(grade[1]+grade[2]+grade[3])/3; //求平均值
}
}
7-7 程序填空题3
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 求两点之间距离
import java.util.Scanner;
class Point{
double x; //x坐标
double y; //y坐标
Point(double x,double y){
this.x=x;
this.y=y;
}
public double distance(Point a){
//和另一个点的距离
//math.abs 绝对值,sqrt 根号,pow 幂
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); //题目只输入两个点,所以未用for循环
String readLine=read.nextLine(); //获取控制台一行 赋值给字符串readLine
String[] data=readLine.split(" "); //将字符串readLine以空格为分隔 赋值给字符串组data
Point pointA=new Point(Integer.valueOf(data[0]),Integer.valueOf(data[1])); //创建对象 点A
readLine=read.nextLine();
data=readLine.split(" "); //之前有声明 所以不用String
Point pointB=new Point(Integer.valueOf(data[0]),Integer.valueOf(data[1])); //创建对象 点B
System.out.printf("%.2f",pointA.distance(pointB)); //保留两位输出
}
}
学习内容 | 代码行 | 博客字 |
类与对象,子类和继承 | 130 | 900 |
总结:时间有限,平时不经常练,导致考试时大脑当机,吸取此次教训,平时应多加练习,巩固所学内容
码云链接:https://gitee.com/xiaoshishuya/codes