1. According to the input numbers from January to December, determine which season is spring, summer, autumn and winter:
package com.homework.demo.test2_1; public class Season { /** * Seasonal * Meteorological division method In the meteorological department, spring is usually from March to May, summer from June to August, autumn from September to November, and winter from December to February of the following year. */ public String spring() { /** * Display spring method */ return "spring"; } public String summer() { /** * Show summer method */ return "summer"; } public String autumn() { /** * How to show autumn */ return "autumn"; } public String winter() { /** * How to show winter */ return "Winter"; } public void season(int month) { /** * How to judge the season */ switch (month) { case 3: case 4: case 5: System.out.println("The season is "+spring()); break; case 6: case 7: case 8: System.out.println("The season is"+summer()); break; case 9: case 10: case 11: System.out.println("The season is "+autumn()); break; default: System.out.println("The season is"+winter()); break; } } } package com.homework.demo.test2_1; import java.util.Scanner; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub Season season = new Season(); Scanner input = new Scanner(System.in); System.out.print("Please enter the month: "); int month = input.nextInt(); season.season(month); } }
2. Write the calculator:
package com.homework.demo.test2_3; public class Calculator1 { public int add(int num1,int num2) { //addition return num1+num2; } public int minus(int num1,int num2) { //subtraction return num1-num2; } public int multiple(int num1,int num2) { //multiplication return num1*num2; } public double divide(int num1,int num2) { //divide return num1/(num2*1.0); } } import java.util.Scanner; public class Output { public static void main(String[] args) { Calculator1 cal = new Calculator1(); Scanner input = new Scanner(System.in); System.out.print("Please enter the first integer to be calculated: "); int num1 = input.nextInt(); System.out.print("Please enter the second integer to be calculated: "); int num2 = input.nextInt(); System.out.print("Please enter the operator you want to perform: "); String count = input.next(); switch (count) { case "+": System.out.print("The sum is: "+cal.add(num1, num2)); break; case "-": System.out.print("差为:"+cal.minus(num1, num2)); break; case "*": System.out.print("The product is: "+cal.minus(num1, num2)); break; case "/": System.out.print("商为:"+cal.divide(num1, num2)); break; } } }
3. Existing TV commodity price quiz activities, the rules of the activity: a commodity name appears randomly, and the user guesses its value. If the guess is correct within the specified number of times, the commodity can be obtained. Mock quiz.
package com.homework.demo.test2_4; public class QuessMachine { int num = 1+(int)(Math.random()*3); //Randomly generate numbers from 1 to 3; public String initial() { /** * Predefined product information */ String shopping = "Princess Electric Car"; String shopping1 = "Naruto Notebook"; String shopping2 = "iphoneX"; switch (num) { case 1: return shopping; case 2: return shopping1; default: return shopping2; } } public void guess(int sum,int num) { //The first parameter is the price, and the second is the randomly generated item number. /** * Determine the user guessing method */ switch (num) { case 1: if (sum >2000) { System.out.println("smaller!"); }else if (sum == 2000) { System.out.println("Guess correctly!"); }else { System.out.println("Bigger!"); } break; case 2: if (sum >6400) { System.out.println("smaller!"); }else if (sum == 6400) { System.out.println("Guess correctly!"); }else { System.out.println("Bigger!"); } break; default: if (sum >7000) { System.out.println("smaller!"); }else if (sum == 7000) { System.out.println("Guess correctly!"); }else { System.out.println("Bigger!"); } break; } } } package com.homework.demo.test2_4; import java.util.Scanner; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int [] price = new int [3]; //Create an array to receive the price information of the item price[0] = 2000; // price of the princess electric car price[1] = 6400; // Price of Naruto Notebook price[2] = 7000; // price of iphoneX QuessMachine quess = new QuessMachine(); // create an object System.out.println("Item number:"+quess.num +"\tItem name:"+ quess.initial() + "\tItem price:"+price[quess.num - 1]); // Print random values, meaningless System.out.print("Please guess" + """ + quess.initial() + """ + "price: "); int prices = input.nextInt(); quess.guess(prices, quess.num); int a=0; while (prices != price[quess.num-1] && a<3) { //The loop judges whether the price entered by the user is correct, and only gives the user four opportunities to enter. System.out.print("Guess again:"); prices = input.nextInt(); quess.guess(prices, quess.num); a++; if (a>=3) { System.out.println("I didn't guess correctly within 4 times, try my best next time!"); } } } }
4. Rewrite the Calculator class in question 2 of the short answer question in Chapter 1 homework. It is required to change the method of addition, subtraction, multiplication and division to a method with parameters, and define an operation method op(), which receives the operation and two numbers selected by the user, and selects the calculation method according to the user.
package com.homework.demo.test2_5; public class Cal { /* * computer class */ public int add(int num1,int num2) { //addition return num1+num2; } public int minus(int num1,int num2) { //subtraction return num1-num2; } public int multiple(int num1,int num2) { //multiplication return num1*num2; } public int divide(int num1,int num2) { //divide return num1/num2; } public int ope( int op,int num1, int num2) { //The first parameter is the operator to be selected, the second is the first operand, and the third is the second operand int number; switch (op) { case 1: number = add(num1,num2); return number; case 2: number = minus(num1, num2); return number; case 3: number = multiple(num1, num2); return number; default: number = divide(num1, num2); return number; } } } package com.homework.demo.test2_5; import java.util.Scanner; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); Cal cal = new Cal(); //Create a computer class object System.out.println("Please select the operation: 1. Addition 2. Subtraction 3. Multiplication 4. Division: "); int num = input.nextInt(); System.out.print("Please enter the first integer: "); int num1 = input.nextInt(); System.out.print("Please enter the second integer: "); int num2 = input.nextInt(); System.out.println("***The result of the operation is: "+cal.ope(num, num1, num2)); //Instantiate attributes with parameters and output } }
5. Write a program to insert an element into a specified position in an integer array. And output the values of the array before and after insertion.
package com.homework.demo.test2_6; public class Interposition { //loop output array method public void print(int arr[]) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } public void insertArray(int[] arr,int index , int value) { //The first parameter is the array to be inserted, the second is the subscript of the element to be inserted into the array, and the third is the value of the inserted array. for (int i =arr.length-1; i >index; i--) { arr[i] = arr[i-1]; } arr[index] = value; } } package com.homework.demo.test2_6; import java.util.Arrays; import java.util.Scanner; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); Interposition inter = new Interposition();//Create an object System.out.print("Please enter the length of the array (the length should not exceed 10, otherwise it will be difficult to lose!): "); int lengths = input.nextInt(); //The length of the array before the keyboard input is inserted int [] arr = new int [lengths]; //create array before insertion System.out.println("Please assign a value to the array: "); for (int i = 0; i < arr.length; i++) { //Assign the array before insertion int val = input.nextInt(); arr[i] = val; } int [] arr2 = Arrays.copyOf(arr, lengths+1); //Assign the array before insertion, and make the length of the array equal to the length of the array before insertion+1 System.out.print("Please enter the value you want to insert: "); int value = input.nextInt(); System.out.print("Please enter the subscript you want to insert: "); int index = input.nextInt(); inter.insertArray(arr2, index, value); //call method System.out.println("The array before insertion is: "); inter.print(arr); //Call the print method System.out.println("\nThe inserted array is: "); inter.print(arr2); } }
6. At the end of this semester, students took a total of three course exams, namely java, C#, SQL. The writing method calculates the average score of each student's three courses.
package com.homework.demo.test2_7; public class Student { int javaScore; // java score int cScore; // c# score int sqlScore; // SQL score } package com.homework.demo.test2_7; public class StudentBiz { Student[] student = new Student[30]; /** * Add student grade information * * @param stu */ public void add(Student stu) { for (int i = 0; i < student.length; i++) { if (student[i] == null) { student[i] = stu; break; } } } /** * Calculate the average score and output it. */ public void getavg(int arr[], int num) { // The parameters are: java score, c# score, SQL score, number of students double[] arr2 = new double[num + 1]; for (int i = 0; i < num + 1; i++) { arr2[i] = arr[i] / 3.0; } for (int i = 0; i < arr2.length; i++) { System.out.println("No." + (i + 1) + "The average score of the classmates:" + arr2[i]); } } } package com.homework.demo.test2_7; import java.util.Scanner; public class Output { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); Student stu[] = new Student[30]; // Create an array of student class objects StudentBiz stus = new StudentBiz(); // Create StudentBiz class object Student student = new Student(); // create student class object int arr[] = new int[stu.length]; // create an array to receive the total grade of each student int index = 0; // get the number of students for (int i = 0; i < stu.length; i++) { int tatleScore = 0; // Initialize the total score and clear it each time; System.out.println("The first " + (i + 1) + "The grades of the students are: "); System.out.print("java result is: "); student.javaScore = input.nextInt(); System.out.print("The result of c# is: "); student.cScore = input.nextInt(); System.out.print("SQL result is: "); student.sqlScore = input.nextInt(); tatleScore = student.javaScore + student.cScore + student.sqlScore; for (int j = 0; j < arr.length; j++) { // loop with array to receive total score if (arr[i] == 0) { arr[i] = tatleScore; break; } } System.out.print("Whether to continue input (y/n): "); String flag = input.next(); System.out.println(""); stus.add(student); // call method to add student information if (flag.equals("n")) { index = i; System.out.println("The input has been completed!"); break; } } System.out.println("The average score of students: "); stus.getavg(arr, index); // call the method } }