JAVA class implementation scores

2011-04-17  来源:本站原创  分类:Java  人气:65 

Title:
1, write a fraction of class to achieve score of addition, subtraction, multiplication, and division;
2, the following points from small to large order: 1 / 2, 2 / 3, 48/62, 70/20, 9 / 25

My solution is as follows, we discuss the discussion:

package com.gzitc.exam;
/**
 *  Finding the greatest common divisor, least common multiple
 * @author e606#qq.com
 *
 */
public class MathUtil {
        /**
         *  Finding the greatest common divisor two numbers, you can use the division algorithm .
         *  Always with a large number divided by a smaller number, and then use the remainder to replace a larger number . Divisible by when the divisor is the greatest common divisor .
         * @param a
         * @param b
         * @return
         */
        public static long gY(long a, long b) {

                long r = 1;

                if(a < b) {
                        long t = a;
                        a = b;
                        b = t;
                }

                r = a % b;

                while( r != 0) {
                        long t = r;
                        r = b % r;
                        b = t;
                }

                return b;
        }
        /**
         *  Seeking Common multiple
         * @param a
         * @param b
         * @return
         */
        public static long gB(long a, long b) {
                long gb = 0;
                gb = a * b / gY(a, b);
                return gb;
        }
}
package com.gzitc.exam;

/**
 *  Score an entity class
 * @author e606#qq.com
 *
 */
public class Fraction {
        /**
         *  Molecular
         */
        private long sun;
        /**
         *  The denominator
         */
        private long mum;
        /**
         *  Instantiate one instance
         * @param sun
         * @param mum
         */
        public Fraction(long sun, long mum) {
                this.sun = sun;
                this.mum = mum;
        }

        public long getSun() {
                return sun;
        }

        public void setSun(long sun) {
                this.sun = sun;
        }

        public long getMum() {
                return mum;
        }

        public void setMum(long mum) {
                this.mum = mum;
        }

        @Override
        public String toString() {
                return sun + "/" + mum;
        }
        /**
         *  Determine whether the score is greater than 0
         * @return
         */
        public int isUpZero() {
                if(sun * mum > 0) {
                        return 1;
                } else {
                        return -1;
                }
        }
}

package com.gzitc.exam;

public class FractionUtil {
        /**
         *  The addition
         * @param fraction1
         * @param fraction2
         * @return
         */
        public static Fraction plus(Fraction a,Fraction b) {
                long r_mum = MathUtil.gB(a.getMum(), b.getMum());
                long r_sun = a.getSun() * r_mum / a.getMum() + b.getSun() * r_mum / b.getMum();
                return new Fraction(r_sun, r_mum);
        }
        /**
         *  Subtraction
         * @param a
         * @param b
         * @return
         */
        public static Fraction minus(Fraction a,Fraction b) {
                long r_mum = MathUtil.gB(a.getMum(), b.getMum());
                long r_sun = a.getSun() * r_mum / a.getMum() - b.getSun() * r_mum / b.getMum();
                return new Fraction(r_sun, r_mum);
        }
        /**
         *  Multiplication
         * @param a
         * @param b
         * @return
         */
        public static Fraction multiplied(Fraction a,Fraction b) {
                long r_sun = a.getSun() * b.getSun();
                long r_mum = a.getMum() * b.getMum();
                Fraction r_fraction = new Fraction(r_sun, r_mum);
                return r_fraction;
        }
        /**
         *  Division operation
         * @param a
         * @param b
         * @return
         */
        public static Fraction divided(Fraction a,Fraction b) {
                long r_sun = a.getSun() * b.getMum();
                long r_mum = a.getMum() * b.getSun();
                Fraction r_fraction = new Fraction(r_sun, r_mum);
                return r_fraction;
        }

        /**
         *  To sort the array of scores from small to large
         * @param arr
         * @return
         */
        public static void sort(Fraction arr[]) {
                int length = arr.length;
                // Bubble sort
                for(int i = 0; i < length-1; i++) {
                        for(int j = i; j < length; j++) {
                                // Determine if subtraction new score is greater than 0, if greater than 0, Description arr[i] Greater than arr[j], Swap locations .
                                if(FractionUtil.minus(arr[i], arr[j]).isUpZero() > 0) {
                                        Fraction temp = arr[i];
                                        arr[i] = arr[j];
                                        arr[j] = temp;
                                }
                        }
                }
        }

}

package com.gzitc.exam;

public class Test {
        public static void main(String args[]) {
                Fraction a,b;
                a = new Fraction(5,7);
                b = new Fraction(3,7);
                Fraction arr[] = {new Fraction(1,2), new Fraction(2,3), new Fraction(48,62),
                                new Fraction(70,20), new Fraction(9,25)};

                System.out.println(a+"+"+b+"="+FractionUtil.plus(a, b));
                System.out.println(a+"-"+b+"="+FractionUtil.minus(a, b));
                System.out.println(a+"*"+b+"="+FractionUtil.multiplied(a, b));
                System.out.println("("+a+")/("+b+")="+FractionUtil.divided(a, b));

                System.out.print(" Array :");
                for(int i = 0; i < arr.length; i++) {
                        System.out.print(arr[i]+"  ");
                }
                System.out.print(", Results from small to large :");
                // Call the sort method
                FractionUtil.sort(arr);

                for(int i = 0; i < arr.length; i++) {
                        System.out.print(arr[i]+" , ");
                }
        }
}
相关文章
  • JAVA class implementation scores 2011-04-17

    Title: 1, write a fraction of class to achieve score of addition, subtraction, multiplication, and division; 2, the following points from small to large order: 1 / 2, 2 / 3, 48/62, 70/20, 9 / 25 My solution is as follows, we discuss the discussion: p

  • Java MD5 implementation of the two kinds of 2010-01-23

    First public class MD5Code { /* * Following these S11-S44 is actually a 4*4 The matrix, in the original C It is used in the implementation of #define Implementation of, here they are becoming static * final Represents a read-only, in the same process

  • How to define the Java component implementation? 2009-11-18

    SCA in line with the application components are used to implement each type of implementation. SCA defines a number of technical parameters and the Tuscany implementation of the types of projects has also increased a little. SCA Java component define

  • Java multi-threaded implementation of the Callable interface 2010-04-01

    import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** *//** * Callable And Future interface * Callable Is similar to the Runnable interface ,

  • Java program implementation of the principle 2010-05-02

    <o:p> </ o: p> First of all, look at what is a computer program. The role of the computer is that it can help people complete the calculation function, which consists of hardware and software components, hardware, including the central process

  • In Java, the implementation of Oracle stored procedure page 2010-09-13

    package com.ztf.test; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import oracle.jdbc.driver.OracleTypes; public class TestProcedure ( / ** * @

  • java HashMap implementation principle [Z] 2010-10-12

    In-depth collection of Java Learning Series: HashMap implementation principle 1. HashMap Overview: HashMap Hash table based on asynchronous Map interface implementation. This implementation provides all optional map operations, and allows the use of

  • java simple implementation of remote communication technologies and 2010-11-12

    Transfer from this article: http://staratsky.javaeye.com/blog/345593 In a distributed service framework, one of the most basic question is how the remote service communications, and in many areas of Java can be the underlying remote communication tec

  • java simple implementation of encryption and decryption 2010-11-28

    There are a variety of encryption algorithms: some cite here only a few: 1: Message Digest: (digital fingerprint): either an arbitrary length of a block of data to calculate, generate a unique fingerprint. MD5/SHA1 Send your information to other peop

  • Several JAVA singleton Implementation 2011-01-08

    1. Hungry class of Chinese and single cases package pattern.singleton; // Ehan single classes. at class initialization time , Has its own instance of the public class Singleton1 { // Private default constructors private Singleton1() {} // Has its own

  • JAVA multi-threaded implementation of the puzzle 2009-07-30

    In a multi-threaded operation, how to make data sharing, by setting the static variable, the operation of the variable method of realization of synchronization mechanism public class TradeDetail extends BaseCollect implements Runnable { private stati

  • Factory Method Pattern java source implementation (design patterns lying learning Memorandum) 2010-04-02

    public class LeiFeng { public void Sweep(){ System.out.println(" Sweeping the floor "); } public void Wash(){ System.out.println(" Laundry "); } public void BuyRice(){ System.out.println(" Buy m "); } } public class Undergrad

  • Tomcat SSL mutual authentication Java articles Implementation 2010-05-15

    Two-way authentication, the client connect to the server, the client certificate authentication server, the server client certificate authentication, the link should be on both sides to verify each other's digital certificates to ensure that it is au

  • Efficient algorithm for all combinations (Java Edition implementation) 2010-07-17

    The blog to see an algorithm, using Java to achieve a Algorithm description: Algorithm Description: When n is greater than 2, n the number of all combinations, a total of (2 ^ n) -1 species. When the right combination of n elements for all the time,

  • Java source implementation strategy mode (study design pattern lying Memorandum) 2010-04-03

    /** * <h1> The Strategy pattern </h1> * QS: Do a shopping checkout software, marketing under customer's purchase of goods and the number of The first time the program * @author xangqun * */ public class Program { /** *PS: * <pre> If shop

  • Java implementation of the order of sub-class constructor 2008-12-07

    Java sub-class construct a complete implementation of the order of package test; public class Test { public static void main(String[] args) { new Son(); } public static void print(String msg) { System.out.println(msg); } public static int getInt(Stri

  • Native2Ascii and Ascii2Native the Java implementation 2010-07-30

    /** * native2ascii.exe Java code implementation. * * @author * @version 1.0 */ public class Native2AsciiUtils { /** * prefix of ascii string of native character */ private static String PREFIX = "\\u"; /** * Native to ascii string. It's same as

  • thumbnails and screen grab java implementation 2010-12-07

    import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.Robot; import java.awt.Toolkit; import j

  • JAVA foundation: In order to build Web Services Struts application 2009-04-01

    Here, I will briefly introduce the next Struts Architecture and Model - View - Controller (MVC). Struts encourages the Model - View - Controller design paradigm to build up basic applications. Struts view through the ActionForm with Struts controller

  • some java problem 2009-05-07

    36. Question: sendRedirect Why can we not go to the address mms protocol? response.sendRedirect ( "mms: / / missiah.adsldns.org: 9394"); Answer: java platform implementation of the protocol currently has no mms, you can check system java.protoco