Experiment 1 java basic programming (preview report)

Posted by addie on Fri, 21 Jan 2022 10:55:57 +0100

Object oriented -- Java experiment report

Experiment 1: Java basic programming

Experiment 1 java basic programming (preview report)

<center> 
<strong>full name:</strong> 
<u>XXX</u> 
&emsp;&emsp;
<strong>Class:</strong> 
<u>XXXXX</u> 
&emsp;&emsp;
<strong>Student No.:</strong>
<u>XXXXXXXXXXXX</u></center>

1, Experimental objectives

(1) Be able to use integrated development tools to develop simple java programs and understand the syntax rules of Java language;
(2) Be able to use java language to solve practical problems, and further be familiar with and master the programming method of Java language.

2, Experimental environment

IntelliJ IDEA Community Edition 2021.1 x64 + openjdk-16.0.1

3, Experimental preparation

  • Xiao Ming brought 20 yuan to the store to buy a book. What kind of goods can he buy with the rest of the money? How many can I buy? How much money is left?

**Problem analysis: * * the price of a book is 10 yuan. In the process of calculating the quantity of other commodities that can be purchased after purchasing the book, I think we can enumerate the possible quantity of each commodity item by item in the way of enumeration. And use judgment statements to ensure that the value of the possible quantity does not exceed the remaining amount. And calculate the balance by calculating the value of existing goods.

money =20
for(rubber)
    for(ruler)
        for(milk)
            for(book)
            {sum=n*price(rubber)+m*price(ruler)+i*price(milk)+j*price(book);
                if sum<money-price(book):
    					balance=money-price(book)-sum;
    					Output quantity and balance;
            }
  • The students' scores are divided into excellent (90-100 points), good (80-89 points), medium (70-79 points), pass (60-69 points) and fail (< 60 points) by using switch sentences.

**Problem analysis: * * use input statements to enter scores, and then divide by ten to rank.

switch (score){
    case 10:
    case 9:"excellent";break;//90-100 points
    case 8:"good";break;//80-89 points
    case 7:"in";break;//70-79 points
    case 6:"pass";break;//60-69 points
        default:"fail,";break;//< 60 points
}

Pay attention to the use of break statements in time. (in switch statements, break jumps out of statements after executing a case.)

  • Define a calculator class, which contains four static methods of addition, subtraction, multiplication and division, and define a test class to call the corresponding methods for four operations.

**Problem analysis: * * the two experimental target points of this topic realize calculator class and test class respectively. Calculator class also includes four static methods: addition, subtraction, multiplication and division. Therefore, it should have the following structure.

//Calculator class structure
class Calculator {
	public static int add(int n, int m) ;
    public static int sub(int n, int m) ; 
    public static int mult(int n, int m) ;
    public static int div(int n, int m) ;
}

In terms of testing classes, I have two schemes: one is to test all functions directly, and the other is to prefer a more realistic calculator mode.

//Direct test
class test {
    public static void testCa() {
        Calculator cA = new Calculator();
        input n,m;
        System.out.println("add:"+cA.add(n, m));
        System.out.println("mult:"+cA.mult(n, m));
        System.out.println("sub:"+cA.sub(n, m));
        System.out.println("div:"+cA.div(n, m));
    }
}
//Provide user selection
class test {
    public static void testCa() {
        Calculator cA = new Calculator();
        Scanner cin = new Scanner(System.in);
        input m,n;
        System.out.println("please input your choice:(+ - * /)");
        int choice = cin.next().charAt(0);
        switch (choice) {
            case '+':
                System.out.println(cA.add(n, m));
                break;
            case '*':
                System.out.println(cA.mult(n, m));
                break;
            case '-':
                System.out.println(cA.sub(n, m));
                break;
            case '/':
                System.out.println(cA.div(n, m));
                break;
        }
    }
}
  • Print the number of daffodils within 100-999 (the number of daffodils is the sum of the third power of the number on each digit is equal to the number itself, such as 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3).

**Problem analysis: * * daffodils number = the sum of the third power of the number on each digit;

It can be analyzed as: nmi=n*n*n+m*m*m+i*i*i, then it is the result of finding each number mod(10), then its structure is

        while (N > 0) {
            int temp = N % 10;
            sum = sum + temp * temp * temp;
            N /= 10;
        }

Define the boolean type function isFlower(int n) to output according to its return value, then the complete structure should be:

    public static boolean isFlower(int n) {
        int sum = 0;
        int N = n;
        while (N > 0) {
            int temp = N % 10;
            sum = sum + temp * temp * temp;
            N /= 10;
        }
        return sum == n;
    }
  • In the class, a static method is defined in printStar to print a tree. This method does not return value. It needs a shaping parameter number to define the height of the tree. This method is called in the main method, and the result of the following figure is obtained when the number value is 5 and 10.

**Problem analysis: * * analyze its graphic structure. When number=5, its structure is as follows:

    *  //On the i-th floor, there are number-i '' on the left side of *
   ***  //There are 2*i-1 '*' in layer i
  *****
 *******
*********

Therefore, its function should have the following structure:

    public static void printStar(int number) {
        for (int i = 1; i <= number; i++) {
                output number-i individual' ';
                Output 2*i-1 individual'*';
            System.out.println();
        }
    }

4, Problems encountered in Preview

  the master of basic knowledge is not enough, so it may not be the optimal strategy in problem analysis. Due to different understanding of the problem, it is also quite different from other people's schemes. But the experiment is to find and solve problems. And because it is an experiment, it is more full of uncertainty, so it is still unknown which scheme is right. It is precisely because of the uncertainty of the experiment that the experiment is attractive and the practice produces true knowledge.

Topics: Java Back-end