The second process of assessment

Source: Internet
Author: User
Tags square root

First question: 7-5 jmu-java-03 Object-oriented -01-constructors and ToString

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]

Program Design ideas: Follow the steps to do it. In the main function, note that n is the number of objects to be created, not the object. That is, to initialize the array, loop the input from the keyboard. Then use the loop to achieve reverse output.

Knowledge points used: referencing the scanner class, creating classes and parameterless constructors and parametric constructors, defining setter/getter methods, overriding the ToString function, initializing arrays, for loops

Operation Result:

The code is as follows:
Import Java.util.Scanner;    Implementation from keyboard input requires reference to scanner class person{//Create Person class, with Name,age,gender,id attribute private String name = NULL;    private int age = 0;    Private Boolean gender = false;        private int id = 0;        Public person () {//Create parameterless constructor 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) {//creates a parameter constructor this.name = n;        This.age = A;    This.gender = g;     } public String GetName () {///define Setter/getter method return name;     } public void SetName (String name) {this.name=name;     } public int Getage () {return age;     } public void Setage (int age) {this.age=age;     } public boolean Getgender () {return gender; } public void Setgender (Boolean gender) {This.gender=genDer     } public int getId () {return id;     } public void setId (int id) {this.id=id; The public String ToString () {//Overrides the ToString function System.out.println ("person [name=" +this.name+ ", age=" +this.age+ "        , gender= "+this.gender+", id= "+0+"] ");    return name;        }}public class Five {public static void main (string[] args) {Scanner reader = new Scanner (system.in);         int n = reader.nextint ();      Reads n from the keyboard, representing the number of input objects person[] per = new Person[n];            Initializes an array of objects for (int i=0; i<per.length; i++) {//Loops input String from the keyboard 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--) {//Output per[x].tostring () by looping in reverse order;        } per.tostring ();    person person = new person (); New Person object and Instantiate}}

The second question, 7-6 group rating (5 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.

Import Java.util.scanner;public class Main {public    static void Main (string[] args) {                Scanner in = new Scanner (Sys tem.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{}

The design of the program: implementation of the above output needs to inherit the parent class has the parameter constructor, with the super () method, then the average

Knowledge points to use: Super () method

Operation Result:


代码如下:
Import Java.util.Scanner; public class Sex {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{      //Inherit RR class public    RT (int[] grade) {        super (grade);        Inherited parent class has a parameter constructor    } public    double mark () {        double x = (grade[1]+grade[2]+grade[3])/3;     Average        return x;    }}


 
The third question, 7-7 procedural Questions 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}
Input Sample: Sample output:
Parent‘s Constructor with a boolean parameterSon‘s Constructor without parameterSon‘s method()Parent‘s method()

Program Design Ideas: the implementation of the above output requires super () to call the parent class has a parameter constructor, and then use Super.method () to let subclasses override the parent class method.

Knowledge points used: Super (true), subclass overriding parent class

Operation Result:

代码如下:
public class Seven {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 the constructor method        System.out.println ("Son ' s Constructor without parameter") with parameters;    }    public void Method () {        System.out.println ("Son ' s Method ()");        Super.method ();          Call the method methods of the parent class    }}

The fourth question, 7-8 to two points between the distance (20 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

Program Design ideas: Define a point class, declare the x, y variables, define a parameter constructor, and then define the distance method, calculate the distance between two points, respectively, the square and then the square of the cross-ordinate subtraction, in the main function to achieve from the keyboard input x1,x2,y1,y2 value, Then pass in the first and second coordinates of the argument constructor, and call the distance method to calculate the distance.

Knowledge points used: Defines a parameter constructor, Math.pow () returns the specified power of the base, and Math.sqrt returns the square root

Operation Result:

The code is as follows:
Import Java.util.scanner;public class Eight {public    static void Main (string[] args) {        Scanner reader = new Scanne R (system.in);        int x1 = Reader.nextint ();        int y1 = Reader.nextint ();        int x2 = Reader.nextint ();        int y2 = Reader.nextint ();        Point P1 = new Point (x1, y1);     The first coordinate        is passed to point p2 = new Point (x2, y2);     Pass        in the second coordinate double m = point.distance (P1, p2);     Call the distance method of point to calculate the distance        System.out.printf ("%.2f", m);    Format Output    }}class point {    double x;    Double y;    Public point (Double x, double y) {        this.x = x;        This.y = y;    }    public static double Distance (point A, point B) {        Double i = Math.pow ((a.x-b.x), 2);     Calculates the square and        double j = Math.pow ((A.Y-B.Y), 2) of the subtraction of the horizontal axis;     Calculates the square and return math.sqrt of the ordinate subtraction        (i + j);      Open Radical    }}

The evaluation of the code is mostly Baidu and ask classmates after writing, whether it is the C language or Java about the creation of classes, functions, methods, object instantiation of the content of this chapter I have been learning bad, and this stage is a little busy, so also did not do exercises, resulting in writing this code is very difficult, all need to learn from classmates to complete, So I will spend more time learning this part of the content, not like this one.

Code Cloud Address: https://gitee.com/tzy123/projects

Learning content Number of lines of code Blog words
Constructing methods and objects 95
Subclasses and inheritance, overloading 60
The second process of assessment 155 800



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.