7-5 jmu-java-03 Object-oriented foundation -01-constructors and 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; The 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]; Initializes an array of objects for (int i=0; i<per.length; i++) {//Enter String name = Reader.next () from the keyboard via a loop ; int age = Reader.nextint (); Boolean genter = Reader.nextboolean (); Per[i] = new Person (name,age,genter); } for (int x=per.length-1; x>=0;x--) { //by looping from backward to forward per[x].tostring (); } per.tostring (); @SuppressWarnings ("unused") person s = new person (); }}
7-6 Group Ratings
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];//Declaration array
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);//Call parent class with parameter constructor
}
Public double mark () {
Arrays.sort (grade);//Sort the array in ascending order
Return (double) (grade[1]+grade[2]+grade[3])/3;//Averaging
}
}
7-7 Program Blanks 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);//Call the parent class with a parameter construct () True or false to
System.out.println ("Son ' s Constructor without parameter");
}
public void Method () {
System.out.println ("Son ' s Method ()");
Super.method ();
}
}
7-8 find distance between two points
Import Java.util.Scanner;
Class point{
Double X;//x coordinates
Double y;//y coordinates
Point (double x,double y) {
This.x=x;
This.y=y;
}
public double distance (point a) {
//And the distance from the other point
//math.abs Absolute Value, sqrt radical, POW power
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);//Topic only two points are entered, so no for loop
String Readline=read.nextline ();//Gets the console row assigned to the string ReadLine
String[] Data=readline.split ("");//Assign string ReadLine to string group data with space delimited
Point Pointa=new Point (integer.valueof (data[0]), integer.valueof (data[1]));//Create object point a
Readline=read.nextline ();
Data=readline.split ("");//Previous declaration so don't use string
Point Pointb=new Point (integer.valueof (data[0]), integer.valueof (data[1]));//Create object point B
System.out.printf ("%.2f", Pointa.distance (POINTB));//Reserved Two-bit output
}
}
Learning content |
Line of code |
Blog Word |
Classes and objects, subclasses, and inheritance |
132 |
850 |
|
|
|
Summary: Class Liuhao usually practice less lead to process assessment will not be the time to make full use of the classroom practice good
Code Cloud Link: https://gitee.com/hlxwzyyyyzyq/events
The second process of assessment