JAVA(DAY1-DAY10)

DAY 1 环境安装及测试

package week1;

/**
 * This is the first code
 * @author hyf
 */

public class Day1 {
 public static void main(String[] args) {
  System.out.println("Hello,world");
 }
}

运行效果:
在这里插入图片描述

DAY 2 基本运算操作

2.1 加、减、乘、除、整除、取余.
2.2 熟悉 println 的中阶用法.

package week1;

/**
 * This is the second code
 * @author hyf
 */
public class Day2 {
    public static void main(String[] args) {
        int temFirstint,temSecondint,temResult;
        double temFirstdouble,temSeconddouble,temResultdouble;
        temFirstint=15;
        temSecondint=3;
        temFirstdouble=1.2;
        temSeconddouble=2.5;
        //加
        temResult=temFirstint+temSecondint;
        System.out.println(" "+temFirstint+"+"+temSecondint+"="+temResult);
        //减
        temResult=temFirstint-temSecondint;
        System.out.println(" "+temFirstint+"-"+temSecondint+"="+temResult);
        //乘
        temResult=temFirstint*temSecondint;
        System.out.println(" "+temFirstint+"*"+temSecondint+"="+temResult);
        //除
        temResult=temFirstint/temSecondint;
        System.out.println(" "+temFirstint+"/"+temSecondint+"="+temResult);
        //取余
        temResult=temFirstint%temSecondint;
        System.out.println(" "+temFirstint+"%"+temSecondint+"="+temResult);
        //加
        temResultdouble=temFirstdouble+temSeconddouble;
        System.out.println(" "+temFirstdouble+"+"+temSeconddouble+"="+temResultdouble);
        //减
        temResultdouble=temFirstdouble-temSeconddouble;
        System.out.println(" "+temFirstdouble+"-"+temSeconddouble+"="+temResultdouble);
        //乘
        temResultdouble=temFirstdouble*temSeconddouble;
        System.out.println(" "+temFirstdouble+"*"+temSeconddouble+"="+temResultdouble);
        //除
        temResultdouble=temFirstdouble/temSeconddouble;
        System.out.println(" "+temFirstdouble+"/"+temSeconddouble+"="+temResultdouble);
        //取余
        temResultdouble=temFirstdouble%temSeconddouble;
        System.out.println(" "+temFirstdouble+"%"+temSeconddouble+"="+temResultdouble);

    }
}

在这里插入图片描述

DAY 3 基本if语句

3.1 if-else
3.2 函数调用
3.3 方法头部的注释

package week1;
/**
 * @author  hyf
 */

public class Day3 {
    public static void main(String[] args) {


        int a, b, result;
        a = 5;
        if (a >= 0) {
            b = a;
        } else {
            b = -a;
        }
        System.out.println("The absoulte value of " + a + " is " + b);
        a = -5;
        if (a >= 0) {
            b = a;
        } else {
            b = -a;
        }
        System.out.println("The absoulte value of " + a + " is " + b);
        a=6;
        System.out.println("The absoulte value of " + a + " is " + abs(a));
        a=-5;
        System.out.println("The absoulte value of " + a + " is " + abs(a));
    }

    /**
     * *****************************
     * The absolute value of the given parameter
     * @param parValue The given value
     * @return
     * ******************************
     */
    public static  int abs(int m){
        if(m>=0){
            return m;
        }
        else {
            return -m;
        }

}}

在这里插入图片描述

DAY 4 闰年的计算

4.1 if语句嵌套
4.2 闰年
4.3布尔类型

package week1;

/**
 * @author hyf
 */
public class Day4 {
    /**
     * ********************
     * The entrance of the program
     */
    public static void main(String[] args) {
//Test is leap year
        int temyear=2022;
        System.out.println(""+temyear+"is");
        if(!isleapyear(temyear)){
            System.out.println("NOT");
        }//of if
        System.out.println("a leapyear.");
        temyear=2020;
        System.out.println(""+temyear+"is");
        if (!isleapyear(temyear)) {
            System.out.println("NOT");
        }//of if
        System.out.println("a leapyear.");

    //Test is leap year2
    int temyear2=2022;
        System.out.println(""+temyear2+"is");
        if(!isleapyearv2(temyear2)){
        System.out.println("NOT");
    }//of if
        System.out.println("a leapyear.");
    temyear2=2020;
        System.out.println(""+temyear2+"is");
        if (!isleapyearv2(temyear2)) {
        System.out.println("NOT");
    }//of if
        System.out.println("a leapyear.");
}
/**
 * given the two function to judge if it is LeapYear
 */
public static boolean isleapyear(int a){
    if(a%4!=0){
        return false;

    }else return true;
}
public static boolean isleapyearv2(int b){
    if(b%4!=0){
        return false;
    }
    else if(b%400==0)
    {return true;}
    else if(b%100==0){
        return true;
    }
    else{
        return true;
    }
}
}

在这里插入图片描述

DAY 5 基本switch 语句

1.switch,case,break,default的用法
2.单元测试单独使用一个方法,main方法里的代码越少越好。

package week1;

/***
 * This is the first code
 * @author hyf
 */
public class Day5 {
    /***
     * The entrance of the program.
     */
    public static void main(String[] args) {
        ScoreToLevelTest();
    }
    public static char ScoreToLeve(int paraScore){
        //E press error,and F press Fail
        char resultLevel='E';
        //Divide by 10,the result ranges from 0 to 10
        int tempDigitalLevel=paraScore/10;
        switch (tempDigitalLevel){
            case 10:
            case 9:
                resultLevel='A';
                break;
            case 8:
                resultLevel='B';
                break;
            case 7:
                resultLevel='C';
                break;
            case 6:
                resultLevel='D';
                break;
            case 5:
            case 4:
            case 3:
            case 2:
            case 1:
            case 0:
                resultLevel='F';
                break;
            default:
                resultLevel='E';
        }//of switch
        return resultLevel;
    }
    /***
     * ********************
     *
     * Method unit test
     * ********************
     */
    public static void ScoreToLevelTest(){
        int temscore=100;
        System.out.println("Score "+temscore+" to level is "+ScoreToLeve(temscore));
        temscore=90;
        System.out.println("Score "+temscore+" to level is "+ScoreToLeve(temscore));
        temscore=80;
        System.out.println("Score "+temscore+" to level is "+ScoreToLeve(temscore));
        temscore=50;
        System.out.println("Score "+temscore+" to level is "+ScoreToLeve(temscore));
    }
}

在这里插入图片描述

DAY 6 基本for语句

6.1循环语句是程序的核心
6.2算法的复杂度一般根据循环语句来计算。

package week1;

import weka.core.SystemInfo;

/***
 * This is the sixth code.
 * @author hyf
 */
public class Day6 {
    /***
     * the entrance of the program.
     */
    public static void main(String[] args) {
        forStatemenTest();
    }
    /***
     * *************
     * Method unit test.
     * **************
     */
    public static void forStatemenTest(){
        int temN=10;
        System.out.println("1 add to "+temN+" is "+addToN(temN));
        temN=0;
        System.out.println("1 add to "+temN+" is "+addToN(temN));
        int temStepLenth=1;
        temN=10;
        System.out.println("1 add to "+temN+" with step length "+temStepLenth+" is "+addToNWithStepLength(temN,temStepLenth));
        temStepLenth=2;
        System.out.println("1 add to "+temN+" with step length "+temStepLenth+" is "+addToNWithStepLength(temN,temStepLenth));

    }// of forStatementTest
    /***
     * *************
     * Add from 1 to N
     * @return The sum
     */
    public static int addToN(int paraN){
        int resultSum=0;
        for(int i=1;i<=paraN;i++){
            resultSum+=i; //resultSum=resultSum+i
        }
        return resultSum;
    }//of addToN
    /***
     * Add 1 to n with step length
     */
    public static int addToNWithStepLength(int paraN,int paraStepLength){
        int resultSum=0;
        for(int i=1;i<=paraN;i+=paraStepLength){
            resultSum+=i;
        }
        return resultSum;
    }//of addTonWithStepLength
}//of class Day6

在这里插入图片描述

DAY 7 矩阵元素相加

7.1 矩阵的赋值
7.2 二重循环

package week1;
import  java.util.Arrays;

/***
 * This is the seventh code
 * @author hyf
 */
public class Day7 {
    /***
     * ******************
     * The entrance of the program
     * *****************
     */
    public static void main(String[] args) {
        matrixElementSumTest();
        matrixAdditionTest();
    }// of main
    /***
     * **************************
     * Sum the elements of a matrix
     * @param paraMatrix The given matrix
     * @return The sum of all its elements
     * **************************
     */
    public static int matrixElementSum(int [][] paraMatrix){
        int resultSum=0;
        for(int i=0;i<paraMatrix.length;i++){
            for(int j=0; j<paraMatrix[0].length;j++){
                resultSum+=paraMatrix[i][j];
            }//of for j
        }// of for i
        return resultSum;
    }// of matrixElementSum
    /***
     * **********************
     * Unit test for respective method
     * **********************
     */
    public static void matrixElementSumTest(){
        int [][]tempMatrix=new int[3] [4];
        for(int i=0;i<tempMatrix.length;i++){
            for(int j=0;j<tempMatrix[0].length;j++){
                tempMatrix[i][j]=i*10+j;
            }// of j
        }//of i
        System.out.println("The matrix is:\r\n"+Arrays.deepToString(tempMatrix));
        System.out.println("The matrix element sum is: "+matrixElementSum(tempMatrix));
    }//of matrixElementSumTest
    /***
     * *************
     * Add two matirx.
     * *************
     */
    public  static int [][] matrixAddition(int[][] paraMatrix1,int[][] paraMatrix2){
        int[][]resultMatrix=new int[paraMatrix1.length][paraMatrix1[0].length];
        for(int i=0;i<paraMatrix1.length;i++){
            for(int j=0;j<paraMatrix1[0].length;j++){
                resultMatrix[i][j]=paraMatrix1[i][j]+paraMatrix2[i][j];
            }//of for j
        }// of for i
        return resultMatrix;
    }//or for matrixAddtion
    /***
     * unit test for respective method.
     */
    public static void matrixAdditionTest(){
        int [][] temMatrix=new int[3][4];
        for(int i=0;i<temMatrix.length;i++){
            for (int j=0;j<temMatrix[0].length;j++){
                temMatrix[i][j]=i*10+j;
            }//of for j
        }//of for i
        System.out.println("The matrix is:\r\n"+Arrays.deepToString(temMatrix));
        int[][] tempNewMatrix=matrixAddition(temMatrix,temMatrix);
        System.out.println("The new matrix is\r\n"+Arrays.deepToString(tempNewMatrix));
    }//of matrixAdditionTest
}// of class Day7

在这里插入图片描述

DAY 8 矩阵相乘

8.1 三重循环是多数程序的极限.
8.2 非法输入检查是程序正常运行的基本保障. 如果检查所有的非法输入, 会导致大量代码行, 这在商业代码中是必须的.

package week2;
import weka.core.pmml.Array;

import java.util.Arrays;
/***
 * 矩阵乘法
 * @author hyf
 */
public class Day8 {
    /**
     *********************
     * The entrance of the program.
     *
     * @param args Not used now.
     *********************
     */
    public static void main(String args[]) {
        matrixMultiplicationTest();
    }// Of main

    /**
     *********************
     * Matrix multiplication. The columns of the first matrix should be equal to the
     * rows of the second one.
     *
     * @param paraFirstMatrix  The first matrix.
     * @param paraSecondMatrix The second matrix.
     * @return The result matrix.
     *********************
     */
    public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
        int m = paraFirstMatrix.length;
        int n = paraFirstMatrix[0].length;
        int p = paraSecondMatrix[0].length;

        // Step 1. Dimension check.
        if (paraSecondMatrix.length != n) {
            System.out.println("The two matrices cannot be multiplied.");
            return null;
        } // Of if

        // Step 2. The loop.
        int[][] resultMatrix = new int[m][p];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < p; j++) {
                for (int k = 0; k < n; k++) {
                    resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
                } // Of for k
            } // Of for j
        } // Of for i

        return resultMatrix;
    }// Of multiplication

    /**
     *********************
     * Unit test for respective method.
     *********************
     */
    public static void matrixMultiplicationTest() {
        int[][] tempFirstMatrix = new int[2][3];
        for (int i = 0; i < tempFirstMatrix.length; i++) {
            for (int j = 0; j < tempFirstMatrix[0].length; j++) {
                tempFirstMatrix[i][j] = i + j;
            } // Of for j
        } // Of for i
        System.out.println("The first matrix is: \r\n" + Arrays.deepToString(tempFirstMatrix));

        int[][] tempSecondMatrix = new int[3][2];
        for (int i = 0; i < tempSecondMatrix.length; i++) {
            for (int j = 0; j < tempSecondMatrix[0].length; j++) {
                tempSecondMatrix[i][j] = i * 10 + j;
            } // Of for j
        } // Of for i
        System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempSecondMatrix));

        int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
        System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));

        System.out.println("Trying to multiply the first matrix with itself.\r\n");
        tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
        System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
    }// Of matrixMultiplicationTest
}

在这里插入图片描述

DAY 9 While 语句

9.1 while语句本质上比for更基础,因此可以替代for,但for更方便。
9.2 break语句又出现了,上次是在switch语句里,都表示跳出当前代码块。

package week2;

/***
 * @author  hyf
 */
public class Day9 {
    /***
     * The entrance of the program
     */
    public static void main(String[] args) {
        whileStatementTest();
    }//of main
    /***
     * The sum not exceeding a given
     */
    public static void whileStatementTest(){
        int tempMax=100;
        int tempValue=0;
        int tempSum=0;
        // Approach 1
        while(tempSum<=tempMax){
            tempValue++;
            tempSum+=tempValue;
            System.out.println("temvalue= "+tempValue+",tempsum="+tempSum);
        }//of while
        tempSum-=tempValue;
        System.out.println("The sum not exceeding "+tempMax+" is "+tempSum);
        //Approach 2
        System.out.println("\r\n Alternative approach.");
        tempValue=0;
        tempSum=0;
        while(true){
            tempValue++;
            tempSum+=tempValue;
            System.out.println("temValue= "+tempValue+",tempSum="+tempSum);
            if(tempMax<tempSum){
                break;
            }//of if
        }//of while
        tempSum-=tempValue;
        System.out.println("The sum not exceeding"+tempMax+"is"+tempSum);
    }//of whileStatementTest
}//of Day9

在这里插入图片描述

DAY 10 综合任务

学生成绩存放于一个矩阵中,其中行表示学生,列表示科目。要求:
1.进行学生成绩的随机生成,在 [ 50 , 100 ] [50,100] [50,100]
2.找出成绩最好的,成绩最差的。但有挂科的同学不参与评比。

package week2;
import weka.core.Debug;
import weka.core.pmml.Array;

import java.util.Arrays;
import java.util.Random;

/***
 * This is the tenth code
 * @author hyf
 */
public class Day10 {
    /***
     * ****************************
     * The entrance of the program
     * ****************************
     */
    public static void main(String[] args) {
        task1();
    }//of main
    /***
     * *********************
     * Method unit test
     * *********************
     */
    public static void task1(){
        //step1 Generate the data with n students and m courses
        //Set these values by yourself
        int n=10;
        int m=3;
        int lowerBound=50;
        int upperBound=100;
        int threshold=60;

        //Here we have to use an object to generate random number.
        Random tempRandom=new Random();
        int [][]data=new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                data[i][j]=lowerBound+tempRandom.nextInt(upperBound-lowerBound);
            }//of for j
        }//of for i
        System.out.println("The dta is:\r\n"+ Arrays.deepToString(data));

        //Step2.Compute the total score of each student.
        int[] totalScores=new int[n];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(data[i][j]<threshold){
                    totalScores[i]=0;
                    break;
                }//of if
                totalScores[i] +=data[i][j];
            }//of for j
        }//of for i
        System.out.println("The total scores are:\r\n"+Arrays.toString(totalScores));

        //Step3 Find the best and worst student
        //Typical initialization for index:invalid value.
        int tempBestIndex=-1;
        int tempWorstIndex=-1;
        //Typical initialization for best and worst values.
        //They must be replaced by valid values.
        int tempBestScore=0;
        int tempWorstScore=m*upperBound+1;
        for(int i=0;i<n;i++){
            //Do not consider failed students.
            if(totalScores[i]==0){
                continue;
            }//of if
            if(tempBestScore<totalScores[i]){
                tempBestScore=totalScores[i];
                tempBestIndex=i;
            }//of if
            //Attention:This if statement cannot be combined with the last one
            //using "else if".because a student can be both the best and the worst.
            if(tempWorstScore>totalScores[i]){
                tempWorstScore=totalScores[i];
                tempWorstIndex=i;
            }//of if
        }//of for i

        //Step 4 out put the student number and the score.
        if(tempBestIndex==-1){
            System.out.println("Cannot find the best student. All student have failed.");
        }
        else{
            System.out.println("The best student is NO."+tempBestIndex+" with scores:"+Arrays.toString(data[tempBestIndex]));
        }
        if(tempWorstIndex==-1){
            System.out.println("Cannot find the worst student. All student have failed.");
        }
        else{
            System.out.println("The worst student is NO."+tempWorstIndex+" with scores:"+Arrays.toString(data[tempWorstIndex]));
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值