Computer Testing Questions (expression calculation-java), Huawei-java

Source: Internet
Author: User


PS: Self-written, self-tested OK for your reference.

/*
* Enter an expression (3*8 + 7-2) with no parentheses to output the result.
*/

/* This program does not consider fault tolerance Processing for the moment */

1 import java. util. iterator; 2 3 public class Calculator {4 5/* For example, in expression 3*8 + 7-2, dataNum = 4, opNum = 3 */6 private static int daNum = 0; // number of data in the expression 7 private static int opNum = 0; // number of operators in the expression 8 9 public static void main (String [] args) {10 11 System. out. println ("enter an arithmetic expression (for example, 3*8 + 7-2):"); 12 seconds s = new seconds (System. in); 13 // String str = s. nextLine (). toString (); 14 char [] ch = s. nextLine (). toChar Array (); 15 s. close (); 16 StringBuffer sbOP = new StringBuffer (); 17 StringBuffer sbDA = new StringBuffer (); 18 19 int dataIndex = 0; 20 int [] data = new int [32]; // limits the maximum number of data to be processed to 32 21 22 for (int I = 0; I <ch. length; I ++) 23 {24 if ('+' = ch [I]) | ('-' = ch [I]) | ('*' = ch [I]) | ('/' = ch [I]) 25 {26 sbOP. append (ch [I]); 27 opNum ++; 28 for (int j = dataIndex; j <I; j ++) 29 {30 sbDA. append (ch [j ]); 31} 32 data [daNum ++] = Integer. parseInt (sbDA. toString (); 33 sbDA. delete (0, sbDA. length (); // clear sbDA 34 dataIndex = I + 1; 35} 36} 37 for (int j = dataIndex; j <ch. length; j ++) 38 {39 sbDA. append (ch [j]); 40} 41 data [daNum ++] = Integer. parseInt (sbDA. toString (); 42 int [] da = new int [daNum]; // numeric array 43 for (int I = 0; I <daNum; I ++) 44 {45 da [I] = data [I]; 46 // System. out. println ("da:" + da [I] ); 47} 48 char [] op = sbOP. toString (). toCharArray (); // operator array 49 50 System. out. println ("=" + cal (da, op); 51} 52 53 private static int cal (int [] da, char [] op) {54 55 if (1 = daNum) 56 {57 return da [0]; 58} 59 60 // indicates the initialization, so the da value is valid 61 boolean [] flag = new boolean [daNum]; 62 for (int I = 0; I <daNum; I ++) 63 {64 flag [I] = true; 65} 66 67 while (1! = DaNum) 68 {69 // multiplication and division operation 70 for (int I = 0; I <opNum; I ++) 71 {72 if ('*' = op [I]) 73 {74 da [I + 1] = da [I] * da [I + 1]; 75 flag [I] = false; 76 daNum --; 77} 78 if ('/' = op [I]) 79 {80 da [I + 1] = da [I]/da [I + 1]; 81 flag [I] = false; 82 daNum --; 83} 84} 85 86 // addition/subtraction operation 87 int index = 0; 88 for (int I = 0; I <opNum; I ++) 89 {90 index = I + 1; 91 if ('+' = op [I]) 92 {93 while (! Flag [index]) 94 {95 index ++; 96} 97 da [index] = da [I] + da [index]; 98 flag [I] = false; 99 daNum --; 100} 101 if ('-' = op [I]) 102 {103 while (! Flag [index]) 104 {105 index ++; 106} 107 da [index] = da [I]-da [index]; 108 flag [I] = false; 109 daNum --; 110} 111} 112} 113 114 return da [opNum]; 115} 116}

 


JAVA machine testing questions

Question 1:
Import java. util. ArrayList;
Import java. util. List;

Class Vehicle {
Public void run (){
System. out. println ("this is the run method of the vehicle ");
}
}
Class Motor extends Vehicle {
Public void run (){
System. out. println ("this is the Motorrun method ");
}
}
Class Ship extends Vehicle {
Public void run (){
System. out. println ("this is the Shiprun method ");
}
}
Class Aeroplane extends Vehicle {
Public void run (){
System. out. println ("this is the Aeroplanerun method ");
}
}
Class Car extends Motor {
Public void run (){
System. out. println ("this is the Carrun method ");
}
}
Class Bus extends Motor {
Public void run (){
System. out. println ("this is the Busrun method ");
}
}
Public class Test {
Public static void main (String [] args ){

List <Vehicle> list = new ArrayList <Vehicle> ();
Vehicle v1 = new Vehicle ();
Vehicle v2 = new Motor ();
Vehicle v3 = new Ship ();
Vehicle v4 = new Aeroplane ();
Vehicle v5 = new Car ();
Vehicle v6 = new Bus ();
List. add (v1 );
List. add (v2 );
List. add (v3 );
List. add (v4 );
List. add (v5 );
List. add (v6 );
For (Vehicle vehicle: list ){
Vehicle. run ();
}
}
}

Java second-level computer test questions compilation and answers

Sweat, speechless, java is useless, and the most important thing is ability. I suggest you study hard and do not give any answers. It hurts yourself.

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.