The use of methods for objects and properties in Java, simple to write a case
Import Java.util.Scanner;
Class Calculste {int A;//define two integer int b; String option; Defines the receive operator string public void count () {//To determine the operator switch (option) {case "+": System.out.println ("Compute and:" +a+ "+" +b+ "="
+ (a+b));
Break
Case "-": System.out.println ("Calculated difference:" +a+ "-" +b+ "=" + (a-b));
Break
Case "*": System.out.println ("Calculated product:" +a+ "*" +b+ "=" + (a*b));
Break
Case "/": {//For division operations, the denominator can not be 0 if (b!= 0) {System.out.println ("Calculated quotient:" +a+ "/" +b+ "=" + (A/a));
}else{System.out.println ("The second number you enter cannot be 0, please re-enter");
}} break;
Case "%": System.out.println ("calculated remainder:" +a+ "%" +b+ "=" + (a%b));
Break
DEFAULT:SYSTEM.OUT.PRINTLN ("You entered a wrong operator, please re-enter");
Class Demo3 {//Requirement: Use Java class to describe a calculator class, Calculator has operand 1, operand 2, operator Three common attribute, also have computational function behavior.
public static void Main (string[] args) {System.out.println ("operation of two digits");
Scanner SCA = new Scanner (system.in);
Creates an object for the class Calculste cal = new Calculste ();
Assign a value to a property System.out.println ("First number:"); CAL.A = (int) sca.nextint ();
System.out.println ("second number:");
cal.b = (int) sca.nextint ();
SYSTEM.OUT.PRINTLN ("Input operation symbol:"); Cal.option = Sca.next ();
The method that accepts the character//calls the method operation Cal.count ();
}
}
Here are some effects on the console:
"The Java language is an object-oriented programming language, a class, an object in a class, an object's properties and methods are very important," says Mo.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.