The fifth after-school experiment

Source: Internet
Author: User

Lab 5: Arrays

This experiment is the third time on the machine, belongs to the verification design experiment, through this experiment the student will grasp the following content:

1. Know how to declare, create, and initialize arrays of various types

2. Understand the concept of two-dimensional arrays and be able to declare, create, and initialize two-dimensional arrays of various types

3. Mastering access Methods for one-dimensional or two-dimensional array elements

4. Mastering the Declaration, creation, and initialization of irregular two-dimensional arrays and accessing methods learn about the use of methods for array manipulation in the arrays class and the system class

[Experimental Task One]: Verify the source program in the courseware example and identify the place that is not understood (authentication type).

1, the experimental requirements:

1) Debug the source code in the example of the courseware and analyze the results of the program running.

2) The experimental report includes the expected results of the running procedure, the actual running results, the analysis of the results, and the areas that you do not understand when reading the program.

Passarray.java,

Package procedures;

Passarray.java

Passing arrays and individual array elements to methods

public class Passarray {

public static void Main (string[] args) {

int a[] = {1, 2, 3, 4, 5};//define an array A and attach the initial value to him

String output = "The values of the original array are:\n";

for (int i = 0; i < a.length; i++)//for cycle

Output + = "" + a[i]; Give output +A[I] and then assign to output

Output + = "\n\neffects of passing Array" + "element call-by-value:\n"

+ "a[3] before Modifyelement:" + a[3];

Modifyelement (a[3]);//When a function references one of the values, it does not change the original array.

Output + = "\na[3] after Modifyelement:" + a[3];

Output + = "\ n Effects of passing entire array by reference";

Modifyarray (a); Array a passed call-by-reference

Output + = "\n\nthe values of the modified array are:\n";

for (int i = 0; i < a.length; i++)

Output + = "" + a[i];

SYSTEM.OUT.PRINTLN (output);

}

public static void Modifyarray (int b[]) {

for (int j = 0; J < B.length; J + +)

B[J] *= 2;//to each member multiplied by 2

}

public static void modifyelement (int e) {

E *= 2;//to E multiplied by 2 after assigning the value to E

}

}

Package procedures;

Import java.util.*;

public class Testarrays

{

public static void Main (string[] args)

{

Define a array of a

Int[] A = new int[]{3, 4, 5, 6};

Defines a a2 array

int[] A2 = new int[]{3, 4, 5, 6};

A and A2 arrays are of equal length and each element is equal in turn, outputting true

System.out.println ("a array and A2 arrays are equal:" + arrays.equals (A, a2));

Create a new B array by copying the A array

Int[] B = arrays.copyof (A, 6);

System.out.println ("A and b arrays are equal:" + arrays.equals (A, b));

Output b array of elements, output [3, 4, 5, 6, 0, 0]

SYSTEM.OUT.PRINTLN ("The element of array B is:" + arrays.tostring (b));

Assigns the 3rd element of the B array (including) to the 5th element (not included) to a 1

Arrays.fill (b, 2, 4, 1);

Output b array of elements, output [3, 4, 1, 1, 0, 0]

SYSTEM.OUT.PRINTLN ("The element of array B is:" + arrays.tostring (b));

Sort the B array

Arrays.sort (b);

Output b array of elements, output [0, 0, 1, 1, 3, 4]

SYSTEM.OUT.PRINTLN ("The element of array B is:" + arrays.tostring (b));

}

}

Package procedures;

Import java.io.*;

public class Qipan

{

Define a two-dimensional array to act as a chessboard

Private string[][] board;

Define the size of the board

private static int board_size = 15;

public void Initboard ()

{

Initializing an array of checkers

board = new String[board_size][board_size];

Assign each element "╋" to draw a chessboard on the console

for (int i = 0; i < board_size; i++)

{

for (int j = 0; J < Board_size; J + +)

{

BOARD[I][J] = "╋";

}

}

}

How to output a checkerboard in the console

public void Printboard ()

{

Print each array element

for (int i = 0; i < board_size; i++)

{

for (int j = 0; J < Board_size; J + +)

{

Print array elements without wrapping

System.out.print (Board[i][j]);

}

Outputs a newline character after each line of array elements is printed

System.out.print ("\ n");

}

}

public static void Main (string[] args) throws Exception

{

Qipan GB = new Qipan ();

Gb.initboard ();

Gb.printboard ();

This is the method for getting keyboard input

BufferedReader br = new BufferedReader (new InputStreamReader (system.in));

String inputstr = null;

System.out.println ("Please enter the coordinates of your chess, should be 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 delimiter

string[] Posstrarr = Inputstr.split (",");

Converts 2 strings to the coordinates of a user's chess

int xPos = Integer.parseint (posstrarr[0]);

int yPos = 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 involves

1. The validity of the coordinates, can only be a number, not beyond the Board range

2. If the point of chess under, 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, should be in X, y format:");

}

}

}

Package procedures;

public class NUM2RMB

{

Private string[] Hanarr = {"0", "one", "II", "three", "Restaurant",

"WU", "Lu", "Qi", "ba", "JIU"};

Private string[] Unitarr = {"Ten", "Hundred", "thousand", "Million", "100,000", "Million"};

/**

* Turn a four-bit numeric string into a kanji string

* @param numstr a four-bit numeric string that needs to be converted

* @return four-bit numeric string to be converted into a Chinese character string.

*/

private string Tohanstr (String numstr)

{

String result = "";

int numlen = Numstr.length ();

Iterate through each digit of a numeric string in turn

for (int i = 0; i < Numlen; i++)

{

convert char numbers to int numbers because their ASCII values are exactly 48

So the char number minus 48 gets the int type number, for example ' 4 ' is converted to 4.

int num = 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)

return result;

int Index=result.length ()-1;

while (Result.charat (index) = = ' 0 ') {

index--;

}

if (Index!=result.length ()-1)

Return result.substring (0,index+1);

else {

return result;

}

}

public static void Main (string[] args)

{

NUM2RMB nr = new NUM2RMB ();

SYSTEM.OUT.PRINTLN ("only supports integers (0~ million)");

Test to turn a four-bit numeric string into a Chinese character string

System.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 ("1258054"));

}

}

[Experimental Task II]: analysis of the following procedures, write out the results of the operation. (verification type) 1) The test report includes the answers to the questions that are required to run the results.

2) Ask to answer the question first, then the result verification.

Package Lalala;

public class Arraytest {

public static void Main (string[] args) {

int I, J;

int a[] = {2, 1, 4, 8, 9, 5, 3};

for (i = 0; i < a.length-1; i++) {

int k = i;

for (j = i; J < A.length; J + +) {

if (A[j] < a[k]) {

K = J;

}

}

int temp = A[i];

A[i] = a[k];

A[K] = temp;

}

for (i = 0; i < a.length; i++)

System.out.print (A[i] + "");

System.out.println ();

}

}

The array elements before the start of the loop are: 2,1,4,8,9,5,3

After the first loop: I =1 j=7 k=1 array elements sequentially 1 2 4 8 9 5 3

After the second cycle: I =2 j=7 k=1 array elements sequentially 1 2 4 8 9 5 3

After the third cycle: I =3 j=7 k=6 array elements sequentially 1 2 3 8 9 5 4

After the fourth cycle: I =4 j=7 k=6 array elements sequentially 1 2 3 4 9 5 8

After the fifth cycle: I =5 j=7 k=5 array elements sequentially 1 2 3 4 5 9 8

After the sixth cycle: I =6 j=7 k=6 array elements sequentially 1 2 3 4 5 8 9

[Experimental task three]: design and write a Java program according to the topic Requirements (design Experiment)

1, the experimental requirements:

1) The requirements of the experimental report include source code, each line of source code comments, running results, compilation error analysis and other content.

2. Experimental content:

1) int myarray[] = {1, 2, 3, 4, 5, 6};

2) int yourarray[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

3) Use the System.arraycopy method to write a program that copies all the contents of the MyArray array to the Yourarray array, places it at the beginning of the Yourarray array, and outputs each element of the Yourarray array.

Package Lalala;

public class Arraytest {

public static void Main (string[] args) {

int muarray[] = {1,2,3,4,5,6};

int youarray[] = {10,9,8,7,6,5,4,3,2,1};

System.arraycopy (Muarray, 0, Youarray, 0, muarray.length);

for (int i=0;i<muarray.length;i++) {

System.out.print (Muarray[i]);}

System.out.print ("");

for (int i=0;i<youarray.length;i++)

System.out.print (Youarray[i]);

}

}

[Lab task four]: design and write a Java program (integrated design) according to the requirements of the topic (optional)

1, the experimental requirements:

1) The requirements of the experiment report include the design idea, program flowchart, source code, running result, compilation error analysis and so on.

2. Experimental content:

1) There is an integer array, containing the elements in order: 10,7,9,2,4,5,1,3,6,8, write a program that uses the Arrays.sort method to sort the array and output each element of it.

Package Lalala;

Import Java.util.Arrays;

public class Arraytest {

public static void Main (string[] args) {

int array[]={10,7,9,2,4,5,1,3,6,8};

Arrays.sort (Array);

for (int i=0;i<array.length;i++)

{

System.out.print (array[i]+ "");

}

}

}

The fifth after-school experiment

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.