Nanjing University of Posts and Telecommunications java programming homework online programming fifth homework

Wang Liguo's "The 5th Assignment of Java Language Programming (2018)" in detail

Total score: 100

Multiple Choice Score: 50

 1. Which of the following tools is a Java compiler? ( )
A.javac.exe
B.java.exe
C.javap.exe
D.javadoc.exe
 2. Which of the following data types is not a basic data type in Java? ( )
A.boolean
B.char
C.int
D.String
 3. Suppose there is the following class definition: public class test{ public static void main(String[] args){ int a= 3, b = 4; swap(a,b); System.out.println("a=" +a + " b=" + b); } public static void swap(int a,int b){ int tmp = a; a = b; b = tmp; } } The result after the program runs is ( )
A.a=4 b=3
B.a=3 b=4
C.a=a b=b
D. No result output
 4. After executing the following code, the value of b is ( ) int a=0, b=0; do{ --b; a = a-1; }while(a>0);
A.0
B.1
C.-1
D. Infinite loop
 5. Which of the following statements about arrays in Java is incorrect ( ).
A. The elements in the array must be of the same type
B. Elements in an array are ordered
C. Array objects, which are of reference type
D. The size of the array can be changed arbitrarily
 6. In the body of the loop, if you want to end the loop, which statement can you use? ( ).
A.break
B.continue
C.final
D.finally
 7. Which of the following identifiers is illegal? ( )
A.statics
B.static_10
C.10static
D.$statics10
 8. With the definition of an array int[] a = new int[3], the following reference to the array element is wrong ( ).
A.a[0]
B.a[a.length-1]
C.int i=0;a[i]
D.a[a.length]-1
 9. int a=new int[2][3], then the array contains ( ) array elements.
A.2
B.3
C.6
D. not sure
 10. What is the value of count after the following code segment is executed ( ) int count = 1; for (int i = 1; i <= 5; i++) { count += i; } System.out.println(count);
A.5
B.1
C.15
D.16

Coding Question Score: 50

 Digital Encryption Score: 10/10
import java.util.Scanner;
/**
 * @Author liguo
 * @Description 输入一个四位数,将其加密后输出。
 * 方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,
 * 最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。
 * 例如输入1257,经过加9取余后得到新数字0146,再经过两次换位后得到4601。
 * 输入描述
 * 输入在一行中给出一个四位的整数x,即要求被加密的数。
 * 输出描述
 * 在一行中按照格式“The encrypted number is V”输出加密后得到的新数V。
 * 样例输入1:
 * 1257
 * 样例输出1:
 * The encrypted number is 4601
 * @Data 2018-04-27 
 */
public class Main {
    public static String jiaMi(int a) { int temp = String.valueOf( a ).length(); int b[] = new int[temp]; //整数转化为一个从高位到低位的数组 for (int i = temp - 1; i >= 0; i--) { b[i] = a % 10; a = a / 10; } // for ( int number : b) // System.out.print( number +" "); //将各个位数加上9并除以10取余 for (int i =0 ;i <b.length;i++) { b[i] = ((b[i] + 9) % 10); } //交换千分位和十分位 b[0]千分位,b[2]十分位 b[0] += b[2]; b[2] = b[0] - b[2]; b[0] -= b[2]; //交换百分位和个分位 b[1]百0分位,b[3]个位 b[1] += b[3]; b[3] = b[1] - b[3]; b[1] -= b[3]; // 转化为数组转为string类型的整数 StringBuffer str = new StringBuffer(); for(int i=0;i<b.length;i++){ str.append(b[i]); } return str.toString(); // for ( int number : b) // System.out.println( number ); } public static void main(String[] args) { Scanner in = new Scanner( System.in ); int temp = in.nextInt(); String result = jiaMi(temp); System.out.println("The encrypted number is "+result); } }
 Sequence Sort Score: 10 / 10
import java.util.Arrays;
import java.util.Scanner;


/**
 * @Author liguo
 * @Description 2.数列排序
题目描述
将一个具有20个元素的数组中的中间10个元素按从大到小顺序排序,要求使用冒泡法排序。
输入描述
在一行中输入20个小于50的整数,数据之间只能用1个空格间隔。
输出描述
直接输出变化后的数组,每个数输出占4列列宽。
样例输入1:
5 4 3 2 1 8 5 2 9 6 3 1 4 7 4 1 2 3 4 5
样例输出1:
5   4   3   2   1   9   8   7   6   5   4   3   2   1   4   1   2   3   4   5

 * @Data 2018-04-27
 */
public class Main {
    public static void maopaoSort(int n[]){ for (int i = 1; i <=n.length-1; i++) { for (int j = 0; j <= n.length-1-i; j++) { //后面大值就进行值交换把他们换到前面 if (n[j]<=n[j+1]){ n[j]+=n[j+1]; n[j+1] = n[j] - n[j+1]; n[j]-=n[j+1]; } } } } public static void main(String[] args) { Scanner in = new Scanner( System.in ); int []array1 = new int[20]; int []array2; // for (int value:array1) { // value = in.nextInt(); // } for (int i = 0; i <array1.length ; i++) { array1[i]=in.nextInt(); } array2= Arrays.copyOfRange(array1,5,15); //复制的数组从5-14.没有15 // System.out.println(Arrays.toString( array2 )); maopaoSort( array2 ); // System.out.println(Arrays.toString( array2 )); // arraycopy( array2,0,array1,5,10); for (int i =0 ,j = 5; i < array2.length; i++,j++) { array1[j] = array2[i]; } for (int value:array1) System.out.printf("%4d", value); } }
 6-4-4 Print Yang Hui's Triangle Score: 10/10
import java.util.Scanner;

/**
 * @Author liguo
 * @Description 输入一个整数,表示该三角形的行数

输出描述
对应行数的杨辉三角型,三角形中的每个元素请使用“5d”的格式字符串打印输出
样例输入1:
5
样例输出1:
1
1    1
1    2    1
1    3    3    1
1    4    6    4    1
样例输入2:
9
样例输出2:
1
1    1
1    2    1
1    3    3    1
1    4    6    4    1
1    5   10   10    5    1
1    6   15   20   15    6    1
1    7   21   35   35   21    7    1
1    8   28   56   70   56   28    8    1
 * @Data 2018-04-27
 */
public class Main{
    public static void yangHui(int n){ int a [][] = new int [n ][n]; a[0][0] = 1; for (int i = 1; i < n; i++) { a[i][0]= 1; a[i][i]=1; for (int j = 1; j < i; j++) { a[i][j] = a[i-1][j-1]+a[i-1][j]; } } //打印二维数组.显示杨辉三角形 for (int i = 0; i < n; i++) { for (int j = 0; j <=i ; j++) { System.out.printf("%5d",a[i][j]); } System.out.println(); } } public static void main(String[] args) { Scanner in = new Scanner( System.in ); int n = in.nextInt(); yangHui( n ); } }
 6-4-3 Construct the specified sequence Score: 10 / 10
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author liguo
 * @Description 题目描述
编写函数fun,求出a到b之内能被7或者11整除,但不能同时被7和11整除的所有正数,并将他们放在数组中,函数返回这些数的个数。
编写main函数,输入a,b的值并调用函数进行运算。

输入描述
从键盘输入a,b的值(1<=a<=b<1000),用以下格式字符串输入a,b的值:
在C语言中使用:scanf("%d%d",&a,&b);
在Java语言中使用Scanner对象的nextInt()方法获取a,b的值。

输出描述
用以下格式字符串输出数组中的元素的值:"%d "(注意:%d后面有一个空格)

样例输入1:
1 20 <回车>ii
样例输出1:
7 11 14
样例输入2:
50 100 <回车>
样例输出2:
55 56 63 66 70 84 88 91 98 99
 * @Data 2018-04-28 21:11
 */
public class Main {
    public static void fun(int a,int b){ boolean flag= true; int count = 0; // int []arr = new int[b-a]; ArrayList <Integer> arr = new ArrayList <>( b-a ); for (int i = a; i <= b; i++) { flag = (i%7==0||i%11==0); if (i%77==0) flag = false; if (flag) { arr.add( i ); } } arr.trimToSize(); arr.toArray( ); for (int value:arr) System.out.printf("%d ",value); } public static void main(String[] args) { Scanner in = new Scanner( System.in ); int a = in.nextInt(); int b = in.nextInt(); fun( a,b ); } }
 6-4-1 Average Score: 10 / 10
import java.util.Arrays;
import java.util.Scanner;
/**
 * @Author liguo
 * @Description
 * 编程从键盘上输入20个整数,求去掉最大值和最小值以后那些元素的平均值
输入描述
连续输入20个整数,用空格作为分隔
输出描述
C语言和Java语言中,用以下格式字符串输出结果:"count=%d,average=%.2f\n"
Python语言中,用以下格式字符串输出结果:"count={},average={:.2f}"
样例输入1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
样例输出1:
count=18,average=10.50
样例输入2:
90 80 70 100 50 60 70 100 75 85 85 90 80 70 65 50 60 70 80 90
样例输出2:
count=16,average=76.25
 * @Data 2018-04-29 0:12
 */
public class Main {
    public static void main(String[] args) { Scanner in = new Scanner( System.in ); int []arr = new int[20]; int count=20; int sum=0; double average = 0; //录入数据 for (int i = 0; i < 20; i++) { arr[i]=in.nextInt(); } Arrays.sort( arr ); //去掉出去除两头的最大最小值 for (int i = 1; i < 19; i++) { if (arr[i] == arr[0] | arr[i] == arr[19]) { arr[i] = 0; count--; } } //去掉两个首尾的最大值最小值 arr[0] = 0; arr[19]=0; count-=2; for (int value:arr) sum+= value; average = (double) sum/count; System.out.printf( "count=%d,average=%.2f\n",count,average); } }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325064744&siteId=291194637