20172319 2018.04.11 "Java Programming Tutorial" 7th Week class Quiz (write-up blog)

Source: Internet
Author: User
Tags comparable

20172319 2018.04.11 "Java Programming Tutorial" 7th Week Classroom Quiz
    • Program: Program design and data structure
    • Class: 1723
    • Student: Tang Caiming
    • Study No.: 20172319
    • Instructor: Wang Zhiqiang
    • Date: 2018.04.11
    • Compulsory/elective: compulsory
Directory
    • Test content
    • Test requirements
    • Experimental steps
      • Pre-preparation
      • Demand analysis
    • Code implementation and interpretation
    • Program run results and code
    • Testing process and problems encountered
    • Resources
Test content
    • Complete the Programming Project PP7.4: Specific reference to the Java Programming Tutorial (eighth edition)
    • Implementation of the Compareble interface
Test requirements
    • Compile and run the project PP7.4; and (class code, test code, run result code) send to blue ink cloud
    • Managed code to [email protected], detail reference using open source China managed code
    • Back to Catalog
Preliminary preparation of the experimental procedure
    • Knock on the textbook example 7.3 (rationaltester2.java-test) and Example 7.4 (rationalnumber2.java-Class), compile and run to see if it is wrong, if wrong, control the textbook to two code one after the troubleshooting, you can start programming project.
Demand analysis
    1. Need to understand the basics of interfaces
    2. Learn how to implement interfaces
    3. Understand the comparable interface and the CompareTo method it contains (the only method)
    • Back to Catalog
Code implementation and interpretation
    The class that implements the interface for the
    1. is to use the reserved word implements on the class declaration head, followed by the interface name. Add implements COMPARABLE<RATIONALNUMBER2> to the end of the head of the Rationalnumber class; declares that you want to implement the comparable interface.
      public class RationalNumber2 implements COMPARABLE<RATIONALNUMBER2>
    2. if a class declares to implement an interface, it must provide implementation code that implements all the methods in the interface. Therefore, to implement the interface completely requires code in the RationalNumber2 class that implements all the methods in the interface. public int CompareTo (RationalNumber2 op2)
    3. The
    4. Converts the value of the object in the RationalNumber2 to a floating-point value, and then compares it with a defined error standard (reason: a good way to refer to a floating-point numerical comparison in WEEK5 learning summary).

        Double n1, N2, difference;  N1 = (double) numerator/denominator;  N2 = (double) op2.getnumerator ()/Op2.getdenominator ();  difference = Math.Abs (N2-N1);  if (Difference < 0.0001) return-1;     else if (Difference > 0.0001) return 1;
       else return 0;  
    5. Test program code

public class rationaltester2{//----------------------------------------------------------------------//Creates   Some rational number objects and performs various//operations on them.      ----------------------------------------------------------------------public static void Main (string[] args) {      RationalNumber2 r1 = new RationalNumber2 (1, 3);      RationalNumber2 r2 = new RationalNumber2 (1, 9);      RationalNumber2 R3, R4, R5, R6, R7;      System.out.println ("First rational Number:" + R1);      System.out.println ("Second Rational Number:" + R2);      if (R1.islike (R2)) System.out.println ("R1 and R2 are equal.");      Else System.out.println ("R1 and R2 is not equal.");        if (R1.compareto (r2) = = 1) System.out.println ("R1 > R2");        else if (R1.compareto (R2) ==-1) System.out.println ("R1 < R2");      Else System.out.println ("r1 = R2");      R3 = R1.reciprocal (); System.out.println("The reciprocal of R1 is:" + R3);      R4 = R1.add (R2);      R5 = R1.subtract (R2);      R6 = r1.multiply (R2);      R7 = r1.divide (R2);      System.out.println ("R1 + R2:" + R4);      System.out.println ("R1-R2:" + R5);      SYSTEM.OUT.PRINTLN ("R1 * R2:" + R6);   System.out.println ("R1/R2:" + R7); }}
    • Back to Catalog
Program run results and code




    • Back to Catalog
Testing process and problems encountered
    • Issue 1: The unknown of the conditional statement and the incorrect use of the relational operator result in an output error.
    • Solution: Learn the conditional statements, then debug the code, find the unreasonable place to modify.
    • Question 2: The knowledge of class writing is still vague and unclear, which leads to the obstruction of the initial programming work.
    • Solution: Find data, in the class carefully analyze the role of each statement, define the appropriate method name for easy invocation.
    • Back to Catalog
Resources
    • Java Programming and Data Structure Tutorial (second edition)
    • Tutorial on Java Programming and data Structure (second edition) Learning Guide
    • Construction method, toString (), this, return
    • Back to Catalog

20172319 2018.04.11 "Java Programming Tutorial" 7th Week class Quiz (write-up blog)

Related Article

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.