1. Read and run the sample Passarray.java, observe and analyze the results of the program output, summarize it, and then compare it with what is covered on the next page of the slide.
Source:
//Passarray.java//passing arrays and individual array elements to methods Public classPassarray { Public Static voidMain (string[] args) {intA[] = {1, 2, 3, 4, 5 }; String Output= "The values of the original array are:\n"; for(inti = 0; i < a.length; i++) Output+= " " +A[i]; Output+ = "\n\neffects of passing Array" + "element call-by-value:\n" + "a[3] before Modifyelement:" + a[3]; Modifyelement (a[3]); Output+ = "\na[3" after Modifyelement: "+ a[3]; Output+ = "\ n Effects of passing entire array by reference"; Modifyarray (a); //array a passed call-by-referenceOutput+ = "\n\nthe values of the modified array are:\n"; for(inti = 0; i < a.length; i++) Output+= " " +A[i]; SYSTEM.OUT.PRINTLN (output); } Public Static voidModifyarray (intb[]) { for(intj = 0; J < B.length; J + +) B[j]*= 2; } Public Static voidModifyelement (inte) {e*= 2; }}
Results:
Analysis:
The first output is the original array of a[i], then the output reference and the value passed by value, and the final output doubles the value.
2. Read the Qipan.java sample program to learn how to draw a Gobang disk using two-dimensional arrays and circular statements.
Source:
ImportJava.io.*; Public classqipan{//define a two-dimensional array to act as a chessboard Privatestring[][] board; //define the size of the board Private Static intBoard_size = 15; Public voidInitboard () {//initializing an array of checkersboard =NewString[board_size][board_size]; //assign each element "╋" to draw a chessboard on the console for(inti = 0; i < board_size; i++) { for(intj = 0; J < Board_size; J + +) {Board[i][j]= "╋"; } } } //How to output a checkerboard in the console Public voidPrintboard () {//Print each array element for(inti = 0; i < board_size; i++) { for(intj = 0; J < Board_size; J + +) { //print array elements without wrappingSystem.out.print (Board[i][j]); } //outputs a newline character after each line of array elements is printedSystem.out.print ("\ n"); } } Public Static voidMain (string[] args)throwsException {qipan GB=NewQipan (); Gb.initboard (); Gb.printboard (); //This is the method for getting keyboard inputBufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); String Inputstr=NULL; System.out.println ("Please enter the coordinates of your chess, in x, y format:"); //Br.readline (): Whenever you enter a line on the keyboard press ENTER, the content you just entered will be read by BR. while((Inputstr = Br.readline ())! =NULL) { //separates the user-entered string into 2 strings with a comma (,) as a delimiterstring[] Posstrarr = Inputstr.split (","); //converts 2 strings to the coordinates of a user's chess intXPos = Integer.parseint (posstrarr[0]); intYPos = Integer.parseint (posstrarr[1]); //assign the corresponding array element as "". GB.BOARD[XPOS-1][YPOS-1] = ""; /*The computer randomly generates 2 integers, which are assigned to the board array as the coordinates of a computer's chess game. Also involved 1. The validity of the coordinates can only be a number, not beyond the Checkerboard Range 2. If the point of the chess, can not repeat chess. 3. After each play, you need to scan who wins .*/Gb.printboard (); System.out.println ("Please enter the coordinates of your chess, in x, y format:"); } }}
Results:
3. Write a program to convert an integer to a kanji reading string. For example, "1123" is converted to "1123". Further, can you change the amount represented in the numbers to "Chinese character expression?" For example, the "¥123.52" converted to "one Bai San Yuan Wu angle of the three points."
Source:
Public classnum2rmb{PrivateString[] Hanarr = {"0", "one", "II", "three", "the" , "WU", "Lu", "Qi", "ba", "JIU"}; PrivateString[] Unitarr = {"Ten", "Hundred", "thousand", "Million", "100,000", "Million"}; /*** Turn a four-bit numeric string into a kanji string *@paramNumstr a four-bit numeric string that needs to be converted *@returnA four-bit numeric string is converted into a character string. */ Privatestring Tohanstr (String numstr) {string result= ""; intNumlen =numstr.length (); //iterate through each digit of a numeric string in turn for(inti = 0; i < Numlen; i++ ) { //convert char numbers to int numbers because their ASCII values are exactly//so the char number minus 48 gets the int type number, for example ' 4 ' is converted to 4. intnum = Numstr.charat (i)-48; //if it is not the last digit, and the number is not 0, you need to add units (thousand, Hundred, Ten) if(I! = numLen-1 && num! = 0) {result+ = Hanarr[num] + unitarr[numlen-2-i]; } //Otherwise, do not add units Else { //whether the previous number is "0" and not "0" when added if(Result.length () >0 && hanarr[num].equals ("0") && Result.charat (Result.length ()-1) = = ' 0 ') Continue; Result+=Hanarr[num]; } } //only single digit, direct return if(Result.length () ==1) returnresult; intIndex=result.length ()-1; while(Result.charat (index) = = ' 0 ') {Index--; } if(Index!=result.length ()-1) returnResult.substring (0,index+1); Else { returnresult; } } Public Static voidMain (string[] args) {NUM2RMB nr=NewNUM2RMB (); System.out.println ("Only supports integers (0~ million)"); //test to turn a four-bit numeric string into a Chinese character stringSystem.out.println (Nr.tohanstr ("0")); System.out.println (Nr.tohanstr ("1")); System.out.println (Nr.tohanstr ("10")); System.out.println (Nr.tohanstr ("15")); System.out.println (Nr.tohanstr ("110")); System.out.println (Nr.tohanstr ("123")); System.out.println (Nr.tohanstr ("105")); System.out.println (Nr.tohanstr ("1000")); System.out.println (Nr.tohanstr ("1100")); System.out.println (Nr.tohanstr ("1110")); System.out.println (Nr.tohanstr ("1005")); System.out.println (Nr.tohanstr ("1105")); System.out.println (Nr.tohanstr ("1111")); System.out.println (Nr.tohanstr ("10000")); System.out.println (Nr.tohanstr ("10001")); System.out.println (Nr.tohanstr ("10011")); System.out.println (Nr.tohanstr ("10111")); System.out.println (Nr.tohanstr ("11111")); System.out.println (Nr.tohanstr ("11000")); System.out.println (Nr.tohanstr ("11100")); System.out.println (Nr.tohanstr ("11110")); System.out.println (Nr.tohanstr ("101110")); System.out.println (Nr.tohanstr ("1001110")); }}
Results:
4. Randomly generate 10 numbers, populate an array, then display the contents of the array with a message box, then calculate the array elements and display the results in a message box. Request to design ideas, program flowchart, source code, results, programming summary, etc. published to the blog Park.
Design ideas: You can use Math.random () *1000 to generate random numbers, and then into the array, for addition calculation, and then output.
Flow chart:
Source:
Importjavax.swing.*; Public classRandom { Public Static voidMain (String args[]) {string output= "10 x 1000 or less random number: \ n"; intSum=0; intA []=New int[10]; for(inti = 0;i<10;i++) {A[i]=(int) (Math.random () *1000); Output+= " "+A[i]; Sum+=A[i]; } Output+ = "\ n 10 number of and is:" +sum; Joptionpane.showmessagedialog (NULL, output, "result", Joptionpane.plain_message); }}
Results:
Array of hands brain