Java uses interface to implement addition, subtraction, multiplication and division calculator

Java uses interface to implement addition, subtraction, multiplication and division calculator

class Test{ public static void main(String[] args) { fun i=new fun(); jiafa s1=new jiafa(); jianfa s2=new jianfa(); chengfa s3=new chengfa(); chufa s4=new chufa(); System.out.print(“Addition result:”);i.func(s1 ,6,3); System.out.print(“Subtraction result:”); i.func(s2 ,6,3); System.out.print(“Multiplication result:”); i.func(s3 ,6,3); System.out.print(“Division result:”); i.func(s4 ,6,3); } } interface Ijisuanqi{ int jisuan(int a,int b); } class fun{ public void func(Ijisuanqi per,int a,int b){ System.out.println(per.jisuan(a,b)); } } class jiafa implements Ijisuanqi{ public int jisuan(int a,int b){ return a+b; } } class jianfa implements Ijisuanqi{ public int jisuan(int a,int b){ return a-b; } } class Chengfa implements Ijisuanqi{ public int jisuan(int a,int b){ return a*b; } } class chufa implements Ijisuanqi{ public int jisuan(int a, int b) { if (b == 0) { return -1; } else { return a / b; } } } Run results:

Javadouble addition, subtraction, multiplication and division

Javadouble addition, subtraction, multiplication and division

When double type values ​​are added together, the result may have a precision errorFor this reason, Java provides a high-precision calculation method: java.math.* provides the BigDecimal class import org.junit.Test; import java.math.BigDecimal; import java.math.MathContext; /** * @author ceshi * @Title: BigDecimalUtil * @ProjectName BigDecimalUtil * @Description: TODO * @date 2018/7/2719:30 */ public class BigDecimalUtil { @Test public void test(){ System.out.println(add(0.02,0.03)); System.out.println(subtraction(0.05,0.03,2)); System.out.println(multiplication(0.2,0.3)); System.out.println(division(0.02,0.03,2)); System.out.println(divisionRounding(0.5,0)); } /** *double addition * @param a * @param b * @return */ public double add(double a, double b) { BigDecimal b1=new BigDecimal(a); BigDecimal b2 = new BigDecimal(b); return b1.add(b2).doubleValue(); } /** *double subtraction * @param a * @param b * @param setPrecision Set precision * @return */ public static double subtraction(double a, double b,int setPrecision) { BigDecimal b1 = new BigDecimal(a); BigDecimal b2 = new BigDecimal(b); return b1.subtract(b2,new MathContext(setPrecision)).doubleValue(); } /** * The result of double multiplication is kept to two decimal places * @param a * @param b * @return */ public static double multiplication(double a, double b) { BigDecimal b1 = new BigDecimal(a); BigDecimal b2 = new BigDecimal(b); return b1.multiply(b2).doubleValue(); } /** *double division * @param a * @param b * @param The number of digits to retain in accurate results * @return */…

Addition, subtraction, multiplication and division in java

Several uses of DecimalFormat! http://blog.sina.com.cn/s/blog_4fcb75bd01008bz7.html About common methods of java.math.BigDecimal classhttp://www.2cto.com/kf/201204/128096 .html Precision issues with float operations in javahttp://www.jspspace.com/service/Art-3177-5.html Issues with double operations in javahttp:// www.blogjava.net/mphome/archive/2007/12/10/166783.html An exception that BigDecimal is not divisible by java.lang.ArithmeticException: Non-terminating decimal expansionhttp://hi.baidu.com/yanghlcn/item/0698ee1cee21bf0fe65c36b7 Non-terminating decimal expansion; no exact http://hi.baidu.com/melove1006/item/2786b87cb192fb326cc37c72 2011-10-19 19:14 Format numbers using DecimalFormat http://hi.baidu.com/ihsauqaxblbdmwq/item/87b4c8e94c53e4206cabb840

Insert image description here

[Sword Pointer Offer Simple Part 1] Addition without addition, subtraction, multiplication and division (java)

Foreword This question examines bit operations Problem description Write a function to find the sum of two integers. It is required that the four arithmetic operations “, -, *, /” are not allowed in the function body. symbol. Analysis Adding two numbers is a simple matter, but it cannot use four arithmetic operations. ; it becomes not so easy. If the four arithmetic operations cannot be used, then you can only use bit operations that are partial to the underlying principles of the computer, because the computer itself is doing this kind of calculation. Let’s start explaining. Suppose we want to add 5+7,5+7=12 in decimal. What about binary bit operations? First of all, ,5 corresponds to binary 101,7 corresponds to binary 111,Binary addition encoding should be divided into two steps: 1. Add the corresponding positions ,Not counting carry,Then you can get 010,No carry here means no carry of 1,Just 1+1=0,If the carry in binary should be 1, ;1=10. The 010 obtained in this way can be regarded as 101^111, Two exclusive ORs – the same is 0 and the difference is 1 2. Considering the carry value, then it is 1010, The result of 1010 is equivalent to doing the…

A small calculator that can calculate addition, subtraction, multiplication and division using java programming

package com.liu.method;import java.util.Scanner;//Small calculator for calculation of addition, subtraction, multiplication and divisionpublic class Count {public static void main(String[] args) {Scanner scanner =new Scanner(System.in);for(int i=0;i<=1000;i++){System.out.println("Please enter the first number:");double x=scanner.nextDouble(); System.out.println("Please enter the second number:");double y=scanner.nextDouble();System.out.println("Please enter the symbol:");//Enter + — * / calculate the addition separately Subtraction, multiplication and division, char ch=scanner.next().charAt(0);//Calculate the number you want to calculate, and the calculation through switch judgment is + – * /switch(ch){case '+&# 39; :System.out.println("The operation result is: "+addition(x,y));//Call the addition method to calculate the addition operation break;case '-':System.out.println ("The operation result is: "+subtraction(x,y));break;case '*':System.out.println("The operation result is: "+multiplication(x,y)); break;case '/':System.out.println("The operation result is: "+divisionMethod(x,y));break;}System.out.println("Please enter end to end this time Calculation: ");String str=scanner.next();//Use break to exit the calculation program if(str.equals("end")){System.out.println("This calculation is completed");break;} }scanner.close();}public static double addition(double x,double y){return x+y;}public static double subtraction(double x,double y){return x-y;}public static double multiplication(double x, double y){return x*y;}public static double divisionMethod(double x,double y){return x/y;}}Run result: Please enter the first one Number: 23 Please enter the second number: 34 Please enter the symbol: * The operation result is: 782.0 Please Enter end to end this calculation: end This calculation is completed Process finished with exit code 0

Java time addition and subtraction_Let’s talk about several common operations on dates in Java—value acquisition, conversion, addition, subtraction, and comparison

In the development process of Java, it is inevitable to get entangled with the Date type,I am going to summarize the date-related operations frequently used in the project,JDK version 1.7,If it can help everyone save a few minutes to get up and move, #xff0c; Go make a cup of coffee , it will be great, Hehe. Of course, “I only provide feasible solutions” and do not guarantee best practices. Discussions are welcome. 1. Date value In the era of the old version of JDK, there were many codes that used the java.util.Date class to obtain date values. However, Since the Date class is not convenient for internationalization, in fact, starting from JDK1.1, it is more recommended to use the java.util.Calendar class for time and date processing. The operations of the Date class will not be introduced here – let us go straight to the topic – how to use the Calendar class to obtain the current date and time. Since Calendar’s constructor method is protected, we will create a Calendar object through the getInstance method provided in the API. 1 //There are multiple overloaded methods to create Calendar objects 2 Calendar now = Calendar.getInstance(); //Default 3 //Specify time zone and…

Technology Sharing

Java addition, subtraction, multiplication and division problems

package a; import java.text.DecimalFormat; public class bb { public static void main(String[] args) {  public static void operation() {                                                                                                                                                                                        int) (Math.random( ) * 4);                                                                                                                                                                                                                                                                                                                   int b = (int) (Math.random() * 100) ;                                                                                                                                                                                                                                                                                          . “+a + “+” + b + “=” + (a + b));               break;                                                       //Subtraction System.out.println(” “);< ;                                                                                                                              System.out.println(" "); System.out.println( i + “. “+a + “*” + b + “=” + (a * b)); break; case 3: if (b == 0) {// If b = 0 is calculated I-; Break; } else {// If it is an integer output If (a % b == 0) { System.out.println (“”); System.out.println (i + “.” + A + “/” + b + “=” + (a / b));                                                                                                                                                                                                                                                                        ; System.out.println(” “);                                                                                                                                                            .                   String ss = new DecimalFormat(“0.00”).format(num);                                                                                        String ss = new DecimalFormat(“0.00”). format(num); br>} } Break; default: // All end Break; } } }…

Development notes: Write a simple calculator in Java that can calculate addition, subtraction, multiplication and division, receive user input and receive data in a loop

Preface: This article is compiled for you by the editor of Programming Notes#. It mainly introduces the knowledge related to writing a simple calculator in Java, which can calculate addition, subtraction, multiplication and division, receive user input and can receive data in a loop. I hope it will be useful to you. It has certain reference value. package com.lyn.Methon;import java.util.Scanner;public class CalDemo { //Write a calculator that can calculate addition, subtraction, multiplication and division, and be able to receive data in a loop and receive user input //Write 4 methods, addition, subtraction, multiplication and division //Use loop + switch for user interaction //Pass the two numbers that need to be operated and output the result public static void main(String[] args) { while(true){ System.out.println(“Please enter the calculation Type (+,-,*,/), or use quit to exit”); Scanner scanner = new Scanner(System.in); String cal = scanner.nextLine(); if (cal .equals(“quit”)){ System.out.println(“Calculator has exited”); break; }else { System.out.println(“Input to be made The two calculated numbers are separated by carriage return: “); switch (cal) { case “+”: Scanner num1 = new Scanner(System.in); double addNum1 = num1 .nextDouble(); Scanner num2 = new Scanner(System.in); double addNum2 = num2.nextDouble(); double add = add(addNum1,addNum2); System.out .println(addNum1 + “+” + addNum2 +…

20db23509bee844cfce99b96fa3f2d57.png

JAVA complex calculator multiple combo boxes_addition, subtraction, multiplication and division calculator super list box content quick saving

Function: is Then a number of questions are generated and then multi-threaded calculates the results of addition, subtraction, multiplication and division. Save results to desktop txt document in one click. It’s still the same saying, “take it away for reference” and just pass it by. .If (Super list box 1. Get the number of table items () ≠ 0) File number = Open the file (get the specific directory (3) + To text (time_get Beijing time ()) + “.txt”, #rewrite, ) . Counting loop head (super list box 1. Get the number of table items (), bureau_count) Write text (file number, to text(bureau_count) + “piece” + “(” + Super list box 1. Get the title (Bureau_Count- 1, 1) + “)” + “(” + Super list box 1. Get the title (Bureau_Count- 1 , 2)+ “)” + “=” + bureau_count- 1, 3) + “ ” + 1, 4) + xff0b; “Except results” + Super list box 1. Get the title (bureau_count- 1, 6) + #line break) . End of count loop () Close file (file number) Information box (“Total time spent:” + to text (get startup time () - startup time) + “Milliseconds”, 0, , ) . Otherwise Information box (“Please…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索