201621123021 Java programming Fifth week of study summary

Source: Internet
Author: User
Tags comparable

201621123021 Java programming Fifth week of study summary 1. Study summary of this week 1.1 write down the key points of knowledge you think are important in this week's study
    • interface, comparator interface, comparable interface, HAS-A
1.2 Try to organize these keywords using mind mapping.

2. Written assignment 1. Object-oriented design job (team project, 2-3 people)

Content: Continue to improve the last big job.

1.1 Project Introduction Form
Student responsible for the task Blog Address
Jiang Zholin Store Git[https://gitee.com/tongcan/shoppingcart.git]
Dong Mingshu Good, ShoppingCart Git
1.2 System common function frame diagram

1.3 System General Class diagram

1.4 Shopping cart, commodity, System key code

Shopping Cart

import Java.math.bigdecimal;import java.math.biginteger;import java.util.ArrayList; Public classShoppingCart {Privatearraylist<good> goods =NewArraylist<>();  Public voidAdd (good good) {//buy Goods = = put items into the shopping cartGoods.add (good); } @Override PublicString toString () {return "shoppingcart{"+"goods="+ Goods +"}"; }     PublicBigDecimal sum () {BigDecimal res=NewBigDecimal (0);  for(Good good:goods) {res=Res.add (Good.getprice ()); }        returnRes; }     Public voidDeletegood (intindex) {Goods.remove (Index-1); }     Public voidDeletegood (Good O) {goods.remove (o); }}

Commodity

ImportJava.math.BigDecimal; Public classGood {PrivateBigDecimal Price; PrivateString name; Private intNumber ;  PublicGood (BigDecimal price, String name) { This. Price =Price ;  This. Name =name; }     PublicBigDecimal GetPrice () {returnPrice ; }     Public voidSetprice (BigDecimal price) { This. Price =Price ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"good{" + "price=" + Price + ", name=" + name + "\" + "}"; }    //Quantity Edit     Public intGetNumber () {returnNumber ; }     Public voidSetnumber (intNumber ) {         This. Number =Number ; }}

Business

ImportJava.math.BigDecimal; Public classGood {PrivateBigDecimal Price; PrivateString name; Private intNumber ;  PublicGood (BigDecimal price, String name) { This. Price =Price ;  This. Name =name; }     PublicBigDecimal GetPrice () {returnPrice ; }     Public voidSetprice (BigDecimal price) { This. Price =Price ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"good{" + "price=" + Price + ", name=" + name + "\" + "}"; }    //Quantity Edit     Public intGetNumber () {returnNumber ; }     Public voidSetnumber (intNumber ) {         This. Number =Number ; }}

1.5 Other: Compared with the previous system, the design of the system and other changes. Other impressions.
    • The system has become more and more powerful than last time. Through this big assignment I understand the importance of object-oriented.
2. Abstract: Reading Guessgame abstract class design and use of source code 2.1 guess the pre-transformation code is simple, and the modified code uses abstract classes, abstract methods, it seems more complex, what is the benefit of such a transformation?
    • The use of abstract classes, abstract methods can make subclasses more convenient to implement polymorphism.
2.2 GuessGame(改造后)There are abstract and non-abstract methods in Java, and what kind of methods in the abstract class you think should be declared as abstract, what method does not need to be declared as abstractcan be implemented directly.
    • Abstract classes can be used when we want a class to implement different functions.
    • Abstract classes are not applicable when we want to implement a particular function on a class.
2.3 Important: In this case, what is the change and what is the same? Try to describe it in conjunction with concepts such as abstract.
    • The change is that a class becomes an abstract class from a non-abstract class.
    • The constant is the game guessing the number function has not changed.
3. Comparable and Comparator3.1 combined with PTA 7-1 your code explains why a class implements the comparable interface, so you can use Arrays.sort to sort an array of that type directly.
 Public int Age ) {        Super();         this. Name = name;         this. Age = Age ;    }

    • Because the comparable interface contains the creation of arrays, you can use Arrays.sort to sort them.
3.2 Combined with the PTA 7-2 in your code description, with the comparable interface why still need comparator interface?
    • Because the subject needs to be two kinds of sorting, a sort by age, a sort by name, when the comparable interface is occupied, you also need to use the comparator interface for comparison.
3.3 Previous Jobs Shape, Rectangle, Cirlce, what method in the shape class should be declared abstract? Tell the reason.
    • The function of the shape to recalculate the perimeter getPerimeter() and the function to calculate the area getArea() should be declared abstract , because the area and perimeter are different in the way that the functionality is implemented in various graphs.
3.4 There are many shape type objects such as Rectangle、Circle。 Want to use Arrays.sortSort them out, write the corresponding code. And a brief description of which class should be implemented comparable interface is better?
 Public int CompareTo (Shape o) {    if(this. Getperimeter ()-o.getperimeter () <0)     return -1;     Else if (this. Getperimeter ()-o.getperimeter () >0)     return 1;     Else    return 0;} Array.Sort (Shape);

    • It should be better to implement the comparable interface in shape, which should be included in the shape class for both perimeter and area.
3.5 Take the 3.4 code you write as an example, outlining the benefits of programming for comparable interfaces and for parent-class programming.
    • interface-oriented programming and parent-oriented programming can improve the flexibility of the function, and title 3.4, I have a flexible choice of comparison methods for area and perimeter comparison procedures.
4. Interface-oriented case Analysis 4.1 a. Draw a class diagram. B. StudentDaoWhat is the use of interfaces?

    • B. StudentDao functions as the interface of the program, read, write, output function.
4.2 StudenDaoListImplAnd StudentDaoArrayImplWhat is the common point? What's the difference?
    • In common: All classes are used to store student information.
    • Difference: Storing data by way of a StudenDaoListImpl list, and StudentDaoArrayImpl storing data by array.
4.3 The benefits of interface-oriented programming are outlined in conjunction with the main function in Test.java.
    • Interface-oriented programming increases the flexibility of the program, such as the background source of data in Test.java may be different, but it can be used correctly.
5. What is interface-oriented programming? What are the benefits of interface-oriented programming?
    • Interface-oriented programming, regardless of the background data is the form of the program can be the correct processing of these data, such as the topic of Test.java, we have two data backstage one is the list of the other is an array, the interface can be handled correctly, to achieve the correct processing of student information.
    • The benefit of interface-oriented programming is to increase the breadth of the program's operation, allowing the data object to become a collection and then be processed uniformly.
3. Code cloud and PTA3.1. Codes Cloud Code submission record

3.2 PTA Problem set complete situation diagram

3.3 Count the amount of code completed this week
Week Time Total code Amount New Code Volume total number of files number of new files
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 437 437 7 7
5 905 468 13 6

201621123021 Java programming Fifth 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.