Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Calculator Program in Java.

// Java program for simple calculator
  
import java.io.*;
import java.lang.*;
import java.lang.Math;
import java.util.Scanner;
public class BasicCalculator {
  
    public static void main(String[] args)
    {
        // stores two numbers
        double num1, num2;
  
        // Take input from the user
        Scanner sc = new Scanner(System.in);
  
        System.out.println("Enter the numbers");
  
        // take the inputs
        num1 = sc.nextDouble();
  
        num2 = sc.nextDouble();
  
        System.out.println("Enter the operator (+,-,*,/)");
  
        char op = sc.next().charAt(0);
  
        double o = 0;
  
        switch (op) {
  
        // case to add two numbers
        case '+':
  
            o = num1 + num2;
  
            break;
  
        // case to subtract two numbers
        case '-':
  
            o = num1 - num2;
  
            break;
  
        // case to multiply two numbers
        case '*':
  
            o = num1 * num2;
  
            break;
  
        // case to divide two numbers
        case '/':
  
            o = num1 / num2;
  
            break;
  
        default:
  
            System.out.println("You enter wrong input");
  
            break;
        }
  
        System.out.println("The final result:");
  
        System.out.println();
  
        // print the final result
        System.out.println(num1 + " " + op + " " + num2
                           + " = " + o);
    }
}
Comment

calculator with java

import java.awt.*;
import java.awt.event.*;
 
// An AWT GUI program inherits the top-level container java.awt.Frame
public class AWTPanelDemo extends Frame {
   private Button[] btnNumbers;  // Array of 10 numeric Buttons
   private Button btnHash, btnStar;
   private TextField tfDisplay;
 
   // Constructor to setup GUI components and event handlers
   public AWTPanelDemo () {
      // Set up display panel
      Panel panelDisplay = new Panel(new FlowLayout());
      tfDisplay = new TextField("0", 20);
      panelDisplay.add(tfDisplay);
 
      // Set up button panel
      Panel panelButtons = new Panel(new GridLayout(4, 3));
      btnNumbers = new Button[10];  // Construct an array of 10 numeric Buttons
      btnNumbers[1] = new Button("1");  // Construct Button "1"
      panelButtons.add(btnNumbers[1]);  // The Panel adds this Button
      btnNumbers[2] = new Button("2");
      panelButtons.add(btnNumbers[2]);
      btnNumbers[3] = new Button("3");
      panelButtons.add(btnNumbers[3]);
      btnNumbers[4] = new Button("4");
      panelButtons.add(btnNumbers[4]);
      btnNumbers[5] = new Button("5");
      panelButtons.add(btnNumbers[5]);
      btnNumbers[6] = new Button("6");
      panelButtons.add(btnNumbers[6]);
      btnNumbers[7] = new Button("7");
      panelButtons.add(btnNumbers[7]);
      btnNumbers[8] = new Button("8");
      panelButtons.add(btnNumbers[8]);
      btnNumbers[9] = new Button("9");
      panelButtons.add(btnNumbers[9]);
            // You should use a loop for the above statements!!!
      btnStar = new Button("*");
      panelButtons.add(btnStar);
      btnNumbers[0] = new Button("0");
      panelButtons.add(btnNumbers[0]);
      btnHash = new Button("#");
      panelButtons.add(btnHash);
 
      setLayout(new BorderLayout());  // "super" Frame sets to BorderLayout
      add(panelDisplay, BorderLayout.NORTH);
      add(panelButtons, BorderLayout.CENTER);
 
      setTitle("BorderLayout Demo"); // "super" Frame sets title
      setSize(200, 200);             // "super" Frame sets initial size
      setVisible(true);              // "super" Frame shows
   }
 
   // The entry main() method
   public static void main(String[] args) {
      new AWTPanelDemo();  // Let the constructor do the job
   }
}
Comment

PREVIOUS NEXT
Code Example
Java :: language of minecraft 
Java :: do while loop in java 
Java :: selenide wait 
Java :: sort array based on string length java 
Java :: java string padding 
Java :: collections.reverseorder() in java 
Java :: Error inflating class ImageView 
Java :: what language is android written in 
Java :: android studio Clearing shared preferences; 
Java :: boolean print condition java 
Java :: java selenium send keys number 
Java :: how to control clip volume java 
Java :: change button text onclick javca 
Java :: java scanner netLine 
Java :: write a name and convert it to an array 
Java :: how to call intent in adapter class in android 
Java :: slice a list java 
Java :: get text from a jtextfield 
Java :: how to check whether a character is alphabet or not in java 
Java :: java sealed class 
Java :: factorial program in java 
Java :: start new activity android 
Java :: how to generate random id in java 
Java :: java triangle 
Java :: transformer un string en double java 
Java :: conversion of string to integer in java 
Java :: java convert java.util.Date to LocalDate 
Java :: springdoc 
Java :: fhow to find location of java jdk 
Java :: How to find the prime factors of a number in Java? 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =