## Experiment 1 java basic programming (experimental report)

full name: XXX Class: XXX Student No.: XXX### 1, Experimental purpose

in the integrated development environment, compile and execute simple java programs according to the basic syntax of Java language, understand the operation mechanism of Java programs through this practice link, and master the basic structure and programming method of Java language

### 2, Experimental objectives

learn to use the declaration and use of java variables, master the use of other loop statements such as for loop, understand object-oriented programming, be able to use object-oriented programming to solve problems, understand the difference between static methods and ordinary methods, and be familiar with the modification of access qualifiers

### 3, Experimental content

#### 3.1 experimental environment

IntelliJ IDEA Ultimate Edition 2021.2 x64. + openjdk-17.0.1.

#### 3.2 specific experimental contents

##### Question one

- 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: after Xiaoming buys the number, the remaining money is 10 yuan. Next, by enumerating, calculate the number of remaining items he can buy, using the three-tier for cycle.

Select models: enumerations

public class FirstQuestion { public static void main (String[] args) { int money = 20; money = money - 10; System.out.println("After purchasing book: " + money); for (int bookCount = 0; bookCount * 10 <= money; bookCount++) for (int milkCount = 0; milkCount * 5 <= money; milkCount++) for (int eraserCount = 0; eraserCount <= money; eraserCount++) for (int rulerCount = 0; rulerCount * 2 <= money; rulerCount++) { int restMoney = money - bookCount * 10 - milkCount * 5 - eraserCount - rulerCount * 2; if (restMoney >= 0) System.out.printf("He can buy it%d This book, %d A milk, %d An eraser, %d Ruler remaining%d element\n", bookCount, milkCount, eraserCount, rulerCount, restMoney); } } }

- experimental result

He can buy 0 books, 0 milk, 0 eraser, 0 ruler, and the rest is 10 yuan

He can buy 0 books, 0 milk, 0 eraser, 1 ruler, and the rest is 8 yuan

He can buy 0 books, 0 milk, 0 eraser, 2 rulers, and the rest is 6 yuan

He can buy 0 books, 0 milk, 0 eraser, 3 rulers, and the rest is 4 yuan

He can buy 0 books, 0 milk, 0 eraser, 4 rulers, and the rest is 2 yuan

He can buy 0 books, 0 milk, 0 eraser, 5 rulers, and the rest is 0 yuan

He can buy 0 books, 0 milk, 1 eraser, 0 ruler, and the rest is 9 yuan

He can buy 0 books, 0 milk, 1 eraser, 1 ruler, and the rest is 7 yuan

He can buy 0 books, 0 milk, 1 eraser, 2 rulers, and the rest is 5 yuan

He can buy 0 books, 0 milk, 1 eraser, 3 rulers, and the rest is 3 yuan

He can buy 0 books, 0 milk, 1 eraser, 4 rulers, and the rest is 1 yuan

He can buy 0 books, 0 milk, 2 erasers, 0 ruler, and the rest is 8 yuan

He can buy 0 books, 0 milk, 2 erasers, 1 ruler, and the rest is 6 yuan

He can buy 0 books, 0 milk, 2 erasers, 2 rulers, and the rest is 4 yuan

He can buy 0 books, 0 milk, 2 erasers, 3 rulers, and the rest is 2 yuan

He can buy 0 books, 0 milk, 2 erasers, 4 rulers, and the rest is 0 yuan

He can buy 0 books, 0 milk, 3 erasers, 0 ruler, and the rest is 7 yuan

He can buy 0 books, 0 milk, 3 erasers, 1 ruler, and the rest is 5 yuan

He can buy 0 books, 0 milk, 3 erasers, 2 rulers, and the rest is 3 yuan

He can buy 0 books, 0 milk, 3 erasers and 3 rulers, leaving 1 yuan

He can buy 0 books, 0 milk, 4 erasers, 0 ruler, and the rest is 6 yuan

He can buy 0 books, 0 milk, 4 erasers, 1 ruler, and the rest is 4 yuan

He can buy 0 books, 0 milk, 4 erasers, 2 rulers, and the rest is 2 yuan

He can buy 0 books, 0 milk, 4 erasers, 3 rulers, and the rest is 0 yuan

He can buy 0 books, 0 milk, 5 erasers, 0 ruler, and the rest is 5 yuan

He can buy 0 books, 0 milk, 5 erasers, 1 ruler, and the rest is 3 yuan

He can buy 0 books, 0 milk, 5 erasers, 2 rulers, and the rest is 1 yuan

He can buy 0 books, 0 milk, 6 erasers, 0 ruler, and the rest is 4 yuan

He can buy 0 books, 0 milk, 6 erasers, 1 ruler, and the rest is 2 yuan

He can buy 0 books, 0 milk, 6 erasers, 2 rulers, and the rest is 0 yuan

He can buy 0 books, 0 milk, 7 erasers, 0 ruler, and the rest is 3 yuan

He can buy 0 books, 0 milk, 7 erasers, 1 ruler, and the rest is 1 yuan

He can buy 0 books, 0 milk, 8 erasers, 0 ruler, and the rest is 2 yuan

He can buy 0 books, 0 milk, 8 erasers, 1 ruler, and the rest is 0 yuan

He can buy 0 books, 0 milk, 9 erasers, 0 ruler, and the rest is 1 yuan

He can buy 0 books, 0 milk, 10 erasers, 0 ruler, and the rest is 0 yuan

He can buy 0 books, 1 milk, 0 eraser, 0 ruler, and the rest is 5 yuan

He can buy 0 books, 1 milk, 0 eraser, 1 ruler, and the rest is 3 yuan

He can buy 0 books, 1 milk, 0 erasers, 2 rulers, and the rest is 1 yuan

He can buy 0 books, 1 milk, 1 eraser, 0 ruler, and the rest is 4 yuan

He can buy 0 books, 1 milk, 1 eraser, 1 ruler, and the rest is 2 yuan

He can buy 0 books, 1 milk, 1 eraser, 2 rulers, and the rest is 0 yuan

He can buy 0 books, 1 milk, 2 erasers, 0 ruler, and the rest is 3 yuan

He can buy 0 books, 1 milk, 2 erasers, 1 ruler, and the rest is 1 yuan

He can buy 0 books, 1 milk, 3 erasers, 0 ruler, and the rest is 2 yuan

He can buy 0 books, 1 milk, 3 erasers and 1 ruler. The rest is 0 yuan

He can buy 0 books, 1 milk, 4 erasers, 0 ruler, and the rest is 1 yuan

He can buy 0 books, 1 milk, 5 erasers, 0 ruler, and the rest is 0 yuan

He can buy 0 book, 2 milk, 0 eraser, 0 ruler, and the rest is 0 yuan

He can buy a book, 0 milk, 0 eraser, 0 ruler, and the rest is 0 yuan

- error analysis

Note that there can be no negative money here

Note the conditions of I and F

##### Question two

- 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.

import java.util.Scanner; public class SecondQuestion { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a valid score"); try { int score = scanner.nextInt(); switch (score / 10) { case 9,10 -> System.out.println("excellent"); case 8 -> System.out.println("good"); case 7 -> System.out.println("secondary"); case 6 -> System.out.println("pass"); default -> System.out.println("fail,"); } } catch (Exception e) { System.out.println("Input error"); } } }

- experimental result

Please enter a valid score

2

fail,

Please enter a valid score

;;;

Input error

- error analysis

break forgot to write, resulting in a lot of output

The fraction divided by 10 is written as module 10

Input is not numeric

##### Question three

- 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.

public class ThirdQuestion { public static void main (String[] args) { Test.test(); } } class Calculator { public static double add (double o1, double o2) { return o1 + o2; } public static double sub (double o1, double o2) { return o1 - o2; } public static double multi (double o1, double o2) { return o1 * o2; } public static double div (double o1, double o2) throws ArithmeticException { return o1 / o2; } } class Test { public static void test () { double num1 = 1; double num2 = 0; System.out.printf("%f + %f = %f\n", num1, num2, Calculator.add(num1, num2)); System.out.printf("%f - %f = %f\n", num1, num2, Calculator.sub(num1, num2)); System.out.printf("%f × %f = %f\n", num1, num2, Calculator.multi(num1, num2)); try { System.out.printf("%f ÷ %f = %f\n", num1, num2, Calculator.div(num1, num2)); } catch (ArithmeticException e) { System.out.println("Calculation error, divisor is 0"); } } }

- experimental result

1.000000 + 0.000000 = 1.000000

1.000000 - 0.000000 = 1.000000

1.000000 × 0.000000 = 0.000000

1.000000 ÷ 0.000000 = Infinity

- error analysis

The divisor is 0, and an exception needs to be handled

##### Question 4

- 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).

public class ForthQuestion { public static void main (String[] args) { for (int i = 100; i <= 999; i++) { int[] nums = getNumbers(i); if ((Math.pow(nums[0], 3) + Math.pow(nums[1], 3) + Math.pow(nums[2], 3)) == i) System.out.println(i); } } private static int[] getNumbers (int num) { int[] nums = new int[3]; nums[0] = num / 100; nums[1] = (num - nums[0] * 100) / 10; nums[2] = num % 10; return nums; } }

- experimental result

153

370

371

407

- error analysis

When decomposing each digit, the algorithm needs to be clear

##### Question 5

- 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.

public class FifthQuestion { public static void main (String[] args) { printStar(6); } private static void printStar (int n) { for (int line = 1; line <= n; line++) { for (int i = 0; i < n - line; i++) { System.out.print(" "); } for (int i = 0; i < 2 * line - 1; i++) { System.out.print("*"); } System.out.println(); } } }

- experimental result

* *** ***** ******* ********* ***********

- error analysis

The output result of each row is related to the number of rows

### 4, Experimental summary

learned and used the declaration of java variables, mastered the use of other loop statements such as for loop, understood object-oriented programming, solved problems with object-oriented programming, said the difference between static methods and ordinary methods, and was familiar with the modification of access qualifiers. I learned a lot in this experiment