A simple case was written for the use of objects, properties and methods in Java
import java.util.Scanner;class Calculste{ int a; //Define two integers int b; String option; //Define the string that receives the operator public void count(){ //Defend the operator switch(option){ case "+": System.out.println("calculate sum: "+a+"+"+b+"="+(a+b)); break; case "-": System.out.println("calculate difference: "+a+"-"+b+"="+(ab)); break; case "*": System.out.println("calculate product: "+a+"*"+b+"="+(a*b)); break; case "/": { //When performing division operation, the denominator cannot be 0 if (b != 0) { System.out.println("calculate quotient: "+a+"/"+b+"="+(a/b)); }else{ System.out.println("The second number you entered cannot be 0, please re-enter"); } } break; case "%": System.out.println("calculate rest: "+a+"%"+b+"="+(a%b)); break; default: System.out.println("You entered the operator incorrectly, please re-enter"); } } } class demo3 { //Requirements: Use the java class to describe a calculator class. The calculator has three common properties: operand 1, operand 2, and operator, and also has the functional behavior of calculation. public static void main(String[] args) { System.out.println("Operation of two numbers"); Scanner sca = new Scanner(System.in); //Create object of class Calculste cal = new Calculste(); //Assign the value to the attribute System.out.println("First number:"); cal.a = (int)sca.nextInt(); System.out.println("Second number:"); cal.b = (int)sca.nextInt(); System.out.println("Input operator symbol:"); cal.option = sca.next(); //Methods that accept characters//Call method operation cal.count(); }}Here are some effects on the console:
Momo said: Java language is an object-oriented programming language. Classes, objects in classes, objects, and objects' properties and methods are very important.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.