The second process of assessment

Source: Internet
Author: User

7-5 jmu-java-03 Object-oriented foundation -01-Constructors and ToString (25 points)

Define a class of people Person , including attributes:
String name,,, int age boolean gender int id , all variables must be private ( private ). Note: The order of the attributes should appear in the order listed above.

1. Write the parameterless constructor:
    • Print "This is constructor".
    • Output Name,age,gender,id as name,age,gender,id formatted
2. Writing a parametric constructor

name,age,genderassign values in turn.

3. Overwrite the ToString function:

By format: 类名 [name=, age=, gender=, id=] output. It is recommended to use Eclipse Auto-generation.

4. Generate Setter/getter method for each attribute in the 5.main method
    • First reads n from the screen, which represents the number of objects to be created.
    • Then enter the N line name age gender, calling the parameter constructor written above 2 to create a new object.
    • Then output all the objects that you just created 逆序 .
    • Next, use the parameterless constructor to create a new person object and print the object directly.
Input Sample:
3a 11 falseb 12 truec 10 false
Sample output:
Person [name=c, age=10, gender=false, id=0]Person [name=b, age=12, gender=true, id=0]Person [name=a, age=11, gender=false, id=0]This is constructornull,0,false,0Person [name=null, age=0, gender=false, id=0]
程序设计思路:创建一个有关人Person的类,实例化类构造函数,进行调用
知识点:
创建类,有参函数与无参函数的构造,调用
1 ImportJava.util.Scanner;2 classperson{3   PrivateString name; All variables must be private ( private )4   Private intAge ;5   Private Booleangender;6   Private intID;7    PublicPerson () {8      This. Name = "C";9      This. Age = 10;Ten      This. Gender = "false"; One      This. id = 0; A   } -    Public voidToString (String N,intABooleanGinti) { -      This. Name =N; the      This. Age =A; -      This. Gender =G; -      This. ID =i; -System.out.println ("person[name=" + This. name+ ", age=" + This. age+ ", gender=" + This. gender+ ", id=" + This. id+ "]"); +   } -}

The above content for the test to write their own, after the review with the students to get answers

1 ImportJava.util.Scanner;2 3 classperson{4     PrivateString name =NULL;5     Private intAge = 0;6     Private BooleanGender =false;7     Private intID = 0;8     9      PublicPerson () {TenSystem.out.println ("This is constructor"); OneSystem.out.println (name+ "," +age+ "," +gender+ "," +ID); ASYSTEM.OUT.PRINTLN ("person [name=" +name+ ", age=" +age+ ", gender=" +gender+ ", id=" +id+ "]"); -     } -      the      PublicPerson (String N,intABooleang) { -          This. Name =N; -          This. Age =A; -          This. Gender =G; +     } -      +      PublicString toString () { ASYSTEM.OUT.PRINTLN ("person [name=" + This. name+ ", age=" + This. age+ ", gender=" + This. gender+ ", id=" +0+ "]"); at         returnname; -     } - } -  -  Public classFirst { -  in      Public Static voidMain (string[] args) { -Scanner reader =NewScanner (system.in); to         intNumber =reader.nextint (); +Person[] per =NewPerson[number];//initializing an array of objects -          for(inti=0; i<per.length; i++) {//input from the keyboard by looping theString name =Reader.next (); *             intAge =reader.nextint (); $             BooleanGenter =Reader.nextboolean ();Panax NotoginsengPer[i] =NewPerson (name,age,genter); -         } the          for(intX=per.length-1; x>=0;x--) {//output by loop from back to forward + per[x].tostring (); A         } the          + per.tostring (); -person s =NewPerson (); $     } $  -}

Operation Result:

7-6 Group Rating (10 points)

Procedural blanks. Please fill in the following code to complete the topic requirements. (Note: The full code needs to be submitted) there is a team of 5 people. Each of them gives the instructor a score, removes the highest score, removes the lowest score, and the average score for the remaining 3 points is the team's rating of the instructor.

Input format:

Give 5 positive integers (from small to large) that do not exceed 10 in a row.

Output format:

Output collective rating, reserved two digits after the decimal point.

Input Sample:
1 2 4 6 9
Sample output:
4.00
 A classRtextendsrr{ atRT (int[] grade) { -         Super(grade);//calling the parent class has a parameter constructor -     } -      Public Doublemark () { -Arrays.sort (grade);//sort the array in ascending order -         return(Double) (Grade[1]+grade[2]+grade[3])/3;//averaging in     } -}
7-7 Program Blanks 3 (5 points)

The reference output sample complements the following program to make the program output consistent with the output sample.

  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 {//complement the class definition}
Sample output:
 parent ' s Constructor with a Boolean Parameterson ' s Constructor without Parameterson ' s Method () Parent's method () 
program idea: Call the parent class, note the order, in the exam did not take into account the use of super (), must write in the subclass constructor of the first sentence
Knowledge Point: parameter value
 class  Son  Extends   Parent { void   Parent () {System.out.println (" Son ' s Constructor without parameter ");  public  void   method () {System.out.println (" Son ' s Method () "
class extends Parent {    Son () {        Super(true);    // calling the parent class has a parameter construct        System.out.println ("Son ' s Constructor without parameter");    }      Public void method () {        System.out.println("Son ' s Method ()");         Super . Method ();    }

Operation Result:

7-8 the distance between two points (10 points)

Defines a point class with two data members: X and Y, representing X and y coordinates, and several member functions. Defines a function distance (), which is used to find the distance between two points.

Input format:

The input has two lines: the first line is the x-coordinate and the y-coordinate of the first point, and the second line is the x-coordinate and y-coordinate of the second point.

Output format:

Outputs a distance of two points, preserving two decimal places.

Input Sample:

0 9 3-4

Sample output:

13.34

ImportJava.util.*;Importjava.math.*; Public classmain{ Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); DoubleX1 =input.nextdouble (); DoubleY1 =input.nextdouble (); Doublex2 =input.nextdouble (); Doubley2 =input.nextdouble (); System.out.println (String.Format ("%.2f", Math.sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (y1-y2))) ; }}

Summary: In recent weeks of study, the attitude of the class is not correct, resulting in many methods to define the structure of the fuzzy, in the class also did not make full use of time to review, the periodic examination reminded themselves in a timely manner,

After the study must be corrected, a lot of thinking, pay attention to accumulation, look forward to the next assessment of their own progress.

Learning content Number of lines of code Blog words
Construction method
Subclass and Parent class
Second assignment

The second process of assessment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.