第二次過程性考核!
阿新 • • 發佈:2018-10-14
構造 間距 port sqrt 數組 碼雲 總結 圖片 關鍵字
碼雲地址:https://gitee.com/bhma/codes/s57mefv2y0483irwtncb938
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 @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 集體評分
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); System.out.println("Son‘s Constructor without parameter"); } public void method() { System.out.println("Son‘s method()"); super.method(); } }
設計思路:子類父類傳值,看懂程序。
運行結果:
7-8 求兩點之間距離
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); System.out.println(String.format("%.2f", Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2))) ); } }
設計思路:兩個坐標,構造函數。
運行結果:
學習內容 | 代碼(行) | 博客(字) |
類與子類對象繼承 | 60 | 500 |
final關鍵字 | 20 | 100 |
總結:這次考核讓我認清了自己,好多東西不會,4道題也只做上了一道,還有子類父類和數組那塊太模糊了, 還有可能我的思路不是很清晰,想程序時總走彎路,老多的地方不會了,要不就是自己給自己挖個坑。爭取以後好好努力把,加油!
第二次過程性考核!