Jeep Basic Java Programming day07+: Array job

Keywords: Java

Learning content

day07 homework

  1. What is method overload?
    Methods Overload:
    In the same class, the method name is the same and the parameter list is different. It has nothing to do with the return value type.
    The parameter list is different:
    A: Different number of parameters
    B: Different parameter types
    C: The order of parameters is different (heavy load, but not used in development)

  2. Which of the following is method overload (CD)
    A. void method(){}, int method(){}
    B. void method(int a, int b){}, void method(int c, int d){}
    C. int method(float a){}, int method(double b){}
    D. void method(int a){}, int method(byte b){}

  3. Keyboard input a date xx XX year xx month xx day, judging this day is the year's date?
    Requirements: Implemented using a one-dimensional array

import java.util.Scanner;

 public class test32 {

    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
                System.out.print("Please enter the year:");
                int year = sc.nextInt();
                System.out.print("Please enter month:");
                int month = sc.nextInt();
                System.out.print("Please enter the date:");
                int day = sc.nextInt();
                int days = 0;
                int[] month1 = {0,31,60,91,121,152,182,213,244,274,305,335,366};
                int[] month2 = {0,31,59,90,120,151,181,212,243,273,304,334,365};
                if(year % 4 == 0 && year % 100 != 0 | | year % 400 == 0 ){
                       days = month1[month - 1] + day;
                } else {
                       days = month2[month - 1] + day;
                }
                System.out.print(days);
  }
}
  1. In one competition, 10 judges scored the players to find out their scoring situation.

Requirements: Keyboard input 10 judges'scores. Score: Remove the highest score, the lowest score, and the remaining mean score.

import java.util.Scanner;

 public class test42 {

          public static void Avg() {
           
            Scanner sc = new Scanner(System.in);
            int[] arr = new int[10];
            for(int i = 0;i < arr.length;i++){
                   arr[i] = sc.nextInt();
            }
            int max = arr[0];
            int min = arr[0];
            for(int i = 1;i < arr.length;i++){
                   if(max < arr[i]) {
                          max = arr[i];
                   }
                   if(min > arr[i]) {
                          min = arr[i];
                   }
            }
            
            int[] arr1 = new int[arr.length];
            for(int i = 0;i < arr.length; i++){
                   if(max != arr[i] && min != arr[i]) {
                          arr1[i] = arr[i];
                   }
            }
 
            int sum = 0;
            for(int i = 0;i < arr1.length;i++){
                   sum += arr1[i];
            }            
            
            System.out.println(sum / ((arr.length - 2)*1.0));
     }
     
     public static void main(String[] args) {
            Avg();
     }
}
  1. Can the following statement be compiled and run properly, if not, please explain the reason; otherwise, the output result is?
    Why?
int[] array1 = new int[5];
int[] array2 = {1,2,3};
array1 = array2;
System.out.println(array1[0]);     
System.out.println(array1[4]);     

Answer: 1;
Error reporting, label overflow.

  1. Define an array int[] array = {1,5,7,23,3,2,18} to invert the array elements.
public class test5 {

        public static void main(String[] args) {
              arr();
       }
       public static void arr(){
              int[] array = {1,5,7,23,3,2,18};
              for(int i = 0;i < array.length / 2; i++){
                     int temp = array[i];
                     array[i] = array[array.length-1-i];
                     array[array.length-1-i] = temp;
              }
              for(int i = 0; i < array.length; i++) {
                     System.out.print(array[i] + " ");
              }
       }
}
  1. The company's annual sales sum, according to the quarterly and monthly statistics of a company as follows: Units (10,000 yuan)
    First quarter: 22,66,44
    Second quarter: 77,33,88
    Third quarter: 25, 45, 65
    Fourth quarter: 11,66,99
    Requirements: The data are stored in a two-dimensional array, and then the total annual sales are exported.
public class Test8 {
     public static int Sum(){
     int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};     

     int sum = 0;                                                          
     for (int i = 0;i < arr.length ;i++ ) {                
            for (int j = 0;j < arr[i].length ;j++ ) {     
                   sum = sum + arr[i][j];                            
   }
  }
   System.out.println(sum);
     return sum;
            }
            public static void main(String[] args) {
                   Sum();
     }
}

Posted by cmos on Wed, 09 Oct 2019 19:11:26 -0700