Java Object Simple practical case Calculator implementation code _java

Source: Internet
Author: User
Tags sca

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.