Java Basic interface--operations

Source: Internet
Author: User

Use the interface to do the parameters, write a calculator, can complete the +-*/operation

(1) Define an interface compute contains a method int computer (int n,int m);

(2) The design of four classes to implement this interface, complete +-*/operation

(3) The design of a class Usecompute, containing methods:

public void usecom (Compute com, int one, int)

This method requires the ability to: 1. Call the computer method with the passed object to complete the operation.

2. The result of the output operation

(4) Design a test class, call the method usecom in Usecompute to complete the +-*/operation

1. Interface

Package Lianxi4;public interface Computer {int computer (int n,int m);}

2. Calculation method

Package Lianxi4;public class Jiafa implements computer {@Overridepublic int computer (int n, int m) {return m+n;}}

Package Lianxi4;public class Jianfa implements computer {@Overridepublic int computer (int n, int m) {//TODO auto-generated method stub Retu RN n-m;}}

Package Lianxi4;public class Chengfa implements computer {@Overridepublic int computer (int n, int m) {return n*m;}}

Package Lianxi4;public class Chufa implements computer {@Overridepublic int computer (int n, int m) {//TODO auto-generated method stub retur n n/m;}}

3. Definition method

Package Lianxi4;public class Usecomputer {//(3) Design a class usecompute, containing a method://public void usecom (Compute com, int one, int two)// This method requires the ability to: 1. Call the computer method with the passed object to complete the operation//                2. The result of the output operation is public void usecom (computer com, int one, int) {SYSTEM.OUT.PRINTLN (Com.computer (one, both));}}

4. Testing and Results

Package Lianxi4;public class Text {public static void main (string[] args) {usecomputer u=new usecomputer (); Jiafa a=new Jiafa (); System.out.print ("The result of the addition operation of the 3+4 is:"); U.usecom (A, 3, 4); Jianfa b=new Jianfa (); System.out.print ("7-2 of the subtraction operation result is:"); U.usecom (b, 7, 2); CHENGFA c=new Chengfa (); System.out.print (the result of the multiplication operation of the 3x4 is: "); U.usecom (c, 3, 4); Chufa d=new Chufa (); System.out.print ("9/3 of the division operation result is:"); U.usecom (d, 9, 3);}}

Java Basic interface--operations

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.