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:
public class Passarray {
public static void Main (string[] args) {
int a[] = {1, 2, 3, 4, 5};
String output = "The values of the original array are:\n";
for (int i = 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-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;
}
public static void modifyelement (int e) {
e *= 2;
}
Run results
Analysis
The passing of a reference to an array is used to change the stored value of the original data, while the transfer of the value is passed by value, changing only one copy of the original data.
The biggest key to passing the array type method parameters by reference and by value is:
When using the former, if there is code in the method that changes the value of the array element, the original array element is actually modified directly.
Using the latter does not have the problem, and the method body modifies only one copy of the original array element.
2. what is the output of the following code? Why does it have this result?
int[] A = {5, 7, 20};
Int[] B = new Int[4];
System.out.println ("b array is of length:" + b.length);
b = A;
System.out.println ("b array is of length:" + b.length);
Output results
The length of the b array is: 4
The length of the B array is: 3
Reason
code b = A; indicates that the address of array A is assigned to B, so array B is equivalent to array A, array A has 3 elements, so the length of array B is 3;
3. Write a program to convert an integer to a kanji reading string. For example, "1123" is converted to "1123".
Source Code
Import Java.util.Scanner;
public class Zhuanhuan {
Static String Wei []={"", "ten", "Hundred", "thousand", "Million"};
Static String Han []={"0", "one", "two", "three", "four", "five", "six", "seven", "eight", "Nine"};
Static String output= "";
public static void Main (string[] args) {
System.out.println ("Enter a number");
Scanner sc=new Scanner (system.in);
int N=sc.nextint ();
int C=weishu (n);
Shu (n,c);
}
public static int Weishu (int x) {//number of digits
int a=10,n=1;
Double b=x/a;
if (b<1)
return n;
Else
{n=2;
while (b>1)
{
a=a*10;
n++;
b=x/a;
}
return n;
}
}
public static void Shu (int x,int y) {//separates the bits of the number into the array
int a=1;
int j=0;
int c[]=new Int[y];
for (int i=1;i<y;i++)
{
a=a*10;
}
while (a>0)
{
int b=x/a;
C[j]=b;
j + +;
x=x%a;
A=A/10;
}
int n=0;
for (int i=0;i<y;i++)//convert numbers to Kanji
{
int k=c[i];
if (k==0)
{
n++;
if (n>1)
{
n--;
Continue
}
OUTPUT+=HAN[K];
Continue
}
Output+=han[k]+wei[y-i-1];
}
SYSTEM.OUT.PRINTLN (output);
}
}
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.
Design ideas:
1. First define a real-length array;
2. Looping 10 times produces 10 random numbers coexisting into the array;
3. define a string type variable;
4. connect the elements in the array to a variable of type string;
5. End With message box output;
Source
public class Randomint {
public static void Main (string[] args) {
int sum=0;
String output= "";
int a []=new int[10];
Random r=new random ();
for (int i=0;i<10;i++)
{
a[i]= (int) r.nextint ();
Output+=a[i]+ "";
}
for (int i=0;i<10;i++)
Sum+=a[i];
Joptionpane.showmessagedialog (NULL, "array element:" +output);
Joptionpane.showmessagedialog (NULL, "The and of the elements of the array is:" +sum);
}
}
Flow chart
Java Classroom exercises-hands-on brain-arrays