Chapter 2 Classes without parameters, methods with parameters

1. Determine the season according to the numbers from 1 to 12
package com.bdqn.demo;

import java.util.Scanner;

public class Season {
	String season;//season
	int month;//month
	public void show() {
		switch(month) {//switch to select the corresponding data
		case 1:
		case 2:
		case 3:
			season="spring";
			System.out.println("The season is "+season);
			break;
		case 4:
		case 5:
		case 6:
			season="summer";
			System.out.println("The season is "+season);
			break;
		case 7:
		case 8:
		case 9:
			season="Autumn";
			System.out.println("The season is "+season);
			break;
		case 10:
		case 11:
		case 12:
			season="winter";
			System.out.println("The season is "+season);
			break;
		}
	}
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		 Season sea=new Season();//Create object
		System.out.print("Please enter the month: ");
		sea.month=input.nextInt();
		sea.show();//Call the method
		
	}

}
2. Define the four methods of adding, subtracting and dividing by analog computer
package com.bdqn.demo;

import java.util.Scanner;

public class Calculator {
	double num1;
	double num2;
	double sum;//Calculate sum
	int choose;//Choose the calculation method
	public double addition() {//Add and return the calculated value
		sum=num1+num2;
		return sum;
		
	}
    public double subtion() {//Subtract and return the calculated value
    	sum=num1-num2;
    	return sum;
    }
    public double multion() {//Multiply and return the calculated value
    	sum=num1*num2;
    	return sum;
    }
    public double division() {//Division and return the calculated value
    	sum=num1/num2;
    	return sum;
    }
    public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		Calculator calc=new Calculator();
		System.out.print("Please enter the first number: ");
		calc.num1=input.nextInt();
		System.out.print("Please enter the second number: ");
		calc.num2=input.nextInt();
		System.out.print("Please enter the calculation method (1. Addition 2. Subtraction 3. Multiplication 4. Division)");
		calc.choose=input.nextInt();
		switch(calc.choose) {//switch selects the calculation method
		case 1:
			System.out.println(calc.addition());//Call the calculation method
			
			break;
		case 2:
			System.out.println(calc.subtion());
			break;
		case 3:
			System.out.println(calc.multion());
			break;
		case 4:
			System.out.println(calc.division());
			break;
		}
	}
}
3. Use the parameterized method to simulate the computer
package com.bdqn.demo;

import java.util.Scanner;

public class Calculator1 {
	int choose;//Value and
    public int ope(int op,int num1,int num2) {//Receive parameters
    	if(op==1) {//Calculation method
    		choose=num1+num2;
    	} else if (op == 2) {
    		choose=num1-num2;
    	} else if (op == 3) {
    		choose=num1*num2;
    	}else {
    		choose=num1/num2;
    	}
    	return choose;//return and value
    }
    public static void main(String[] args) {
    	Scanner input=new Scanner(System.in);
    	 Calculator1 calc=new Calculator1();//Create object
		System.out.print("Please select operation 1. Addition 2. Subtraction 3. Multiplication 4. Division: ");
		int op=input.nextInt();
		System.out.print("Please enter the first number: ");
		int num1=input.nextInt();
		System.out.print("Please enter the second number: ");
		int num2=input.nextInt();
		System.out.println("The result of the operation is "+calc.ope(op, num1, num2));//Pass parameters
	}
}
4. Analog TV commodity price quiz
package com.bdqn.demo;

import java.util.Scanner;

public class QuessMachine {
    int money; // commodity price
    
    String type;//Product name
   
    public void initial() {
     int choose=(int)(Math.random()*3);//The generated random number output corresponds to the product
    	if(choose==0) {
    		type="electric bicycle";
    		money=1500;
    	}else if(choose==1) {
    		type="bicycle";
    		money=500;
    	}else {
    		type="toy car";
    		money=99;
    	}
    }
    public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		QuessMachine ques=new QuessMachine();//Create object
		int num=0;
		ques.initial();//Call the method
		System.out.print("Please guess the price of ""+(ques.type)+"": ");
		int money=input.nextInt();
		while(money!=ques.money) {
			num++;//Add one if output once
			if(num>=4) {//Exit loop when input 4 times
				System.out.println("I didn't guess correctly within 4 times, try my best next time!");
				break;
			}
			if(money>ques.money) {//Return the corresponding data corresponding to the commodity price
				System.out.println("Smaller!\n");
				System.out.print("Guess again: ");
				money=input.nextInt();//Re-input
			}else if(money<ques.money) {
				System.out.println("Bigger!\n");
				System.out.print("Guess again: ");
				money=input.nextInt();
			}
		}
	    if(money==ques.money) {
	    	System.out.println("\nCongratulations on your guess and got the "+ques.type+" reward!");
	    }
	}
}
5. Insert elements into the specified array and output
package com.bdqn.demo;

import java.util.Arrays;
import java.util.Scanner;

public class TestAdd {
	public void insertArray(int[]arr,int index,int value) {//Element backward method
		for(int i=arr.length-1;i>index;i--) {
		    arr[i]=arr[i-1];	
		}
		arr[index]=value;//Find the insert number position
	}
    public void showarr(int[]arr) {//Print the original array method
    	for(int i=0;i<arr.length;i++) {
    		System.out.println(arr[i]);
    	}
    	
    }
    public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		TestAdd test=new TestAdd();//Create object
		int[]arr1=new int[5];//Create the original array
		for(int i=0;i<arr1.length;i++) {//loop to output numbers
			System.out.print("Please enter "+(i+1)+" number: ");
			arr1[i]=input.nextInt();
		}
		System.out.println("The original array is: ");
		test.showarr(arr1);//Call the print array method
		int[]arr=Arrays.copyOf(arr1,arr1.length+1);//Copy the original array value and add a new length
		System.out.print("Please output the number to be inserted: ");
		int value=input.nextInt();
		System.out.print("Please enter the subscript of the number to be inserted: ");
		int index=input.nextInt();
		test.insertArray(arr, index, value);//Pass parameters
		System.out.println("The inserted array is: ");
		for(int i=0;i<arr.length;i++) {//Print new array
			System.out.println(arr[i]);
		}
	}
}
6. The object parameter outputs java, c#, sql scores and calculates the average score
package com.bdqn.demo;

class Student {//Student class
	int java;//java score
	int c;//c# grades
	int sql;//SQL score
	double avg;//average grade

	public void addScore() {//Calculate the average score method and print the score
		avg = (java + c + sql + avg) /3.0;
		System.out.println("JAVA score:" + java + "C# score:" + c + "SQL score:" + sql +"\nAverage score: "+avg);
	}

	public static class StudentBiz {//Student management class
		Student student[] = new Student[30];//Create an array of grade objects

		public void addName(Student stu) {//Add score array method
			for (int i = 0; i < student.length; i++) {
				if (student[i] == null) {
					student[i] = stu;
					break;
				}
			}
		}

		public void showName() {//Print result array method
			System.out.println("List of grades:");
			for (int i = 0; i < student.length; i++) {
				if (student[i] != null) {
					student[i].addScore();
				
				}
			}
			System.out.println();
		}

		public static void main(String[] args) {
			Student student1 = new Student();//Create a student object
			student1.java = 90;//Attribute assignment
			student1.c = 85;
			student1.sql=88;
		    Student student2=new Student();
		    student2.java=75;
		    student2.c=80;
		    student2.sql=90;
		     StudentBiz student=new StudentBiz();//Create student management object
		     student.addName(student1);//Incoming the method and parameters of adding student grades
		     student.addName(student2);
		     student.showName();//Import array score printing method
		   
		}

	}
}






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325957184&siteId=291194637