201521123117 Java Programming 5th Week of study summary

Source: Internet
Author: User
Tags comparable

1. Summary of this week's study

1.1 Trying to summarize knowledge points about polymorphism and interfaces using mind Mapping

2. Written work

Q1 code reading: Child compressed in-package source code

Can the Child.java file in the 1.com.parent package be compiled and passed? Which sentence will be wrong? Try to correct the error. and analyze the output results.

A: cannot compile through, sentence System. out.println (i) will be faulted. To correct it, change the parent class's private int i=1 to protected int i=1 to execute

The output results are as follows:

1

2

2

1

1

2

1

2. Can the Outofparentpackage.java in another package be compiled and passed? What errors are prompted? Analyze the cause.

A: cannot compile through, prompting the type Parent is not visible and the public type outofparentpackage must are defined in its OW n file, because the parent class does not have any access to the specified word, the default is the package access permission, and the Outofparentpackage is outside the parent package, so it is not accessible

3. Answer: protected What should I do if I want to access the properties or methods of the adornment?

A: In the same package, protected-modified properties or methods can be accessed directly by the classes in the same package, and in different packages, the protected-decorated properties or methods can be inherited or indirectly inherited from the subclasses of the class to which they belong.

Q2abstract Advanced: Reading the design and use of Guessgame abstract class source code

1.Guess transformation before the code is very simple, and the modified code using abstract classes, abstract methods look very complex, then what is the benefit of such a transformation?

A: After using abstract classes, abstract methods, the code appears more resilient, adding java.util.Scanner classes that are used to scan the input text of the utility. After the transformation of abstract classes, abstract methods can be written on their own, output and input is not limited to scanning input and output text.

2. What should I do if I want to transform the game into a graphical interface?

A: Create a container-build-add the build to the container-set the layout (PS: from Baidu), the design of the graphical interface is placed in the main function called.

3. Combining this example, when do you think you should use it abstract ?

A: The concrete implementation of a class or method is not deterministic enough, abstract it, and then write it according to the actual situation to make the code more flexible.

4. Important: In this case, what is the change and what is the same? Attempt to combine abstract, inheritance and other concepts to illustrate.

A: In this example, the change is the code in the environment, unchanged is the function of code implementation is to complete the guessing game of the overall architecture. Abstract is an abstraction, an abstract class, or an abstract method that enhances the elasticity of the code, making it easier to write later based on the external environment of the code. Abstract classes that are not inherited have no meaning, and only abstract classes are implemented and applied, so that the abstraction class has a meaning.

Q3comparable and Comparator

1. Describe the purpose of the comparable interface. Why does a class implement the comparable interface so that it can be sorted directly using Arrays.sort?

A: The comparable interface forces the overall ordering of objects that implement each of its classes. After implementing the comparable interface in a class (the comparable interface has public int compareTo(T o); methods to compare the order of this object with the specified object.) If the object is less than, equal to, or greater than the specified object, it returns a negative integer, 0, or a positive integer, respectively. Look again at the prototype of Arrays.sort: public static <t extends Comparable< Super t>> void Sort (List

2. Why do I need an comparator interface with the comparable interface?

A: Because you can only implement CompareTo () once in a class, it is impossible to say that the code that often modifies the class implements the sort you want, so if you want to sort our class objects in the order specified in the CompareTo () method, you can use the comparator interface at this point.

Q4 interface-oriented case study: Read the Case-studentdao.zip case study

1. Draw a class diagram that describes the role of each class and interface.

For:

which
(1) Studentdao interface: Declares public Student Readstudent,public Boolean writestudent and public void diplayallstudent () three abstract methods. That is to write student data, read student data, and display all student information.
(2) Studentdaoarrayimpl class: Public Studentdaoarrayimpl set A students array of length size; public Student readstudent Enter a name, If the name of the student in the students array is equal to the name entered, the student is returned, otherwise null;public Boolean writestudent enters a student, and if there is a vacancy (null) in the array, the student is added to the students array, and returns True, otherwise returns false;public void Diplayallstudent () output for all students.
(3) Studen class: Used to create the object, the name of the student, create the name of the object and overwrite the ToString method. Methods are: GetName (), SetName (), toString (), and constructors.
(4) Studendaolistimpl class: Set a ArrayList with Student object and name List;public Student readstudent Enter a name, if the name of the student in the list is equal to the name entered, Return the student, otherwise return null;public Boolean writestudent Enter a student, join list, and return all students of the list true;public void Diplayallstudent ().

What is the difference between 2.StudenDaoListImpl and Studentdaoarrayimpl?

A: Studendaolistimpl differs from Studentdaoarrayimpl's individual private attributes, and Studendaolistimpl is implemented with ArrayList, Studentdaoarrayimpl is implemented in arrays (the number of students is determined at the outset and cannot be changed).

Q5 what is interface-oriented programming? What are the benefits of interface-oriented programming?
Discuss the analysis with the code of Test.java in topic 3 and 4.

A: Object-oriented: In the system analysis and architecture, distinguish between hierarchy and dependency, each level is not directly to its upper layer to provide services (that is, not directly instantiated in the upper layer), but by defining a set of interfaces, only the upper layer exposes its interface function, the upper layer for the lower layer is only the interface dependency, not the specific class. The benefits of Interface programming: interface-oriented programming allows the interface to be separated from the implementation, thus greatly improving the flexibility of the program. Different parts or levels of developers can work in parallel, like the creation of a hard disk without the CPU, and do not have to wait for the display, as long as the interface is consistent, reasonable design, can be developed in parallel, thereby improving efficiency.

Q6 pair Programming: Object-oriented design (big job 2-very important)
Content: Use Java code to complete the object-oriented design work done last week, you need to have a preliminary interface. Realize the function as simple as possible, few but good, only contains the necessary functions, do not pursue high Daquan.
Write: Class diagram (as concise as possible, not too many sub-categories, two can), system common function description, key code and interface
Form: Two people rely on the code cloud cooperation to complete. Please post your study number, name and division of duties here.
Note: After a few more classes to talk about Java Graphics interface programming, the time to upgrade the system to a graphical interface. The business logic part of the system should not change much, the change is the input and output part. So when coding, input (Scanner) and output (System.out) code, please do not bind it to a business handler function to die.
Choose to add points: give two people in the code cloud on the same project record, extra points. Note: Two people create a new project on the Code cloud

3. Code on the cloud on the submission of records and PTA Experiment Summary 3.1. Codes Cloud code submission record 3.2. PTA Experiment

Lab 1: The key is to implement the comparable interface. by calling Arrays.sort (), implement the ordering, and note that the CompareTo method's override should be able to sort the name in ascending order first, and then sort the age in ascending order if name is the same.
Lab 2: Comparator: Sorts our class objects in a different order than specified in the CompareTo () method. In this problem, we need to write the Namecomparator class and the Agecomparator class to implement the comparator interface

201521123117 Java Programming 5th Week of study summary

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.