20145307 Second Java Learning Experiment Report

Source: Internet
Author: User

20145307 Java Programming Experiment Report II: Java Object-oriented programming experiment requirements

1. Initial mastery of unit testing and TDD

2. Understanding and mastering the object-oriented three elements: encapsulation, inheritance, polymorphism

3. Initial mastery of UML modeling

4. Familiarity with S.O.L.I.D principles

5. Understanding Design Patterns

Experimental content

One, Unit test

1. Three kinds of code

Pseudo-code: Show Design Steps in a concise natural language

Percentile five-point system: If the score is less than 60, turn to "fail" if the score between 60 and 70, turn into "pass" if the score between 70 and 80, turn into "medium" if the score between 80 and 90, turn into "good" if the score between 90 and 100, turn into "excellent" other, turn to "error"

Product Code: A program or machine language used to implement a specific function;

Package EXP1;      public class ever{public static String percentage2fivegrade (int grade) {//If the score is less than 60, turn to "fail" if (Grade < 60)  Return "Failed";  If the score is between 60 and 70, turn to "pass" else if (Grade <) return "pass";  If the score is between 70 and 80, turn to "medium" else if (Grade <) return "medium";  If the score is between 80 and 90, turn to "good" else if (Grade < +) return "good";  If the score is between 90 and 100, turn to "excellent" else if (Grade <) return "excellent"; Other, turn to "error" Else return "error";}}

Test code: The code used to test the product code

Second, the arithmetic of plural

The original design code is as follows:

Import Java.util.scanner;class Complex {Double A, B;complex () {this.a = 0; this.b = 0;}    Complex (double a) {THIS.A = A; this.b = 0;}    Complex (double A, double b) {this.a = A; this.b = b;}    Complex Add (complex P1, complex p2) {Complex p = new complex (p1.a + p2.a, p1.b + p2.b); return p;}    Complex minus (complex P1, complex p2) {Complex p = new complex (p1.a-p2.a, p1.b-p2.b); return p;}    void print () {System.out.println ("The value of the complex number is:");    if (this.b! = 0) System.out.println (this.a + "+" + this.b + "I"); else System.out.println (THIS.A);}}    public class Complexx {public static void main (string[] args) {System.out.printf ("Enter two sets of numbers followed by each group representing a complex number:");    Scanner scanner=new Scanner (system.in);    int q,w,e,r,t;    Q=scanner.nextint ();    W=scanner.nextint ();    E=scanner.nextint ();    R=scanner.nextint ();    Complex C = new complex ();    Complex C1 = new Complex (q, W);    Complex C2 = new Complex (E, R);    C1.print ();    C2.print (); System.out.printf ("Input to select addition operation input and select subtraction operation ");    T=scanner.nextint ();    Switch (t) {case (1): System.out.println ("The sum of the two complex numbers is:");    System.out.println ((C.add (c1, C2). A + "+" + c.add (c1, C2).-B + "I"). ToString ());    Case (2): System.out.println ("The difference between these two complex numbers is:");    System.out.println ((C.minus (c1, C2). A + "+" + c.minus (c1, C2).-B + "I"). ToString ());} }}

Modify Plan

    public class complex{double real,img;    Public Complex () {this.real=0; this.img = 0;}    Public Complex (Double real,double img) {this.real=real; This.img =img;}      Public double Getreal () {return this.real;}   Public double GetImage () {return this.img;    } public double Getreal (Complex c) {return c.real;        } public double GetImage (Complex c) {return c.img;    } public void Setreal (double real) {this.real=real;    } public void SetImage (double img) {this.img =img;    } public Complex Addcomplex (Complex A,complex b) {Complex temp =new Complex ();    Temp.real=a.real+b.real;    Temp.img =a.img +b.img; return temp;}    Public Complex Deccomplex (Complex A,complex b) {Complex temp = new Complex ();    Temp.real = A.real-b.real;    temp.img = a.img-b.img; return temp;}    Public Complex Mulcomplex (Complex A,complex b) {Complex temp = new Complex (); Temp.real = a.real*b.real-a.img*b.img;    temp.img = A.real*b.img+a.img*b.real; return temp;}    Public Complex Divcomplex (Complex A,complex b) {Complex temp = new Complex ();    Temp.real= (a.real*b.real+a.img*b.img)/(B.REAL*B.REAL+B.IMG*B.IMG);    Temp.img = (a.img*b.real-a.real*b.img)/(B.REAL*B.REAL+B.IMG*B.IMG); return temp;}       public void Printcomplex () {System.out.println ("" "+this.real+" + "+this.img+" I ");}    Public String toString () {string fin= "";    if (img>0) {fin = real+ "+" +img+ "I";    } else if (img<0) {fin = real+ "" +img+ "I";    } else {fin = fin;   } return fin;

for Test:

public static void main(String[] args) {    Complex q = new Complex(6, 3);    q.printComplex();    Complex w = new Complex(2, 7);    w.printComplex();    Complex asd = new Complex();    asd = asd.addComplex(q, w);    asd.printComplex();    asd= asd.decComplex(q, w);    asd.printComplex();    asd = asd.mulComplex(q, w);    asd.printComplex();    asd = asd.divComplex(q, w);    asd.printComplex();}

Requirements Analysis & Summary

Design a simple arithmetic program that can perform complex numbers.

The first thing to do is to define the interface reference API:java.util.Scanner, to prevent the error more precisely define the double class, and then to clarify the underlying knowledge of the mathematical related operations.

After clear thinking, how to design, or a purpose, define the function, and then in the main function to refer to the previously defined functions, if you like, you can also define a print function to use, easy to output more functions in the later interface, it is also easy to manage. The Add,sub,multi function is already given in the previous code, and then only a reference is required.

20145307 Second Java Learning Experiment Report

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.