码云地址: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道题也只做上了一道,还有子类父类和数组那块太模糊了, 还有可能我的思路不是很清晰,想程序时总走弯路,老多的地方不会了,要不就是自己给自己挖个坑。争取以后好好努力把,加油!
原文地址:https://www.cnblogs.com/bhma/p/9787150.html
时间: 2024-10-08 11:25:51