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 :: convert date to offsetdatetime in java 
Java :: convert fahrenheit to celsius 
Java :: how to read a excel file in java 
Java :: java bukkit double jump 
Java :: spring boot docker 
Java :: convert base64 to image png without saving 
Java :: range in java 
Java :: java random array 
Java :: simple login password and username java 
Java :: hello world in different programming languages 
Java :: android list index 
Java :: instantiate optinal java 8 
Java :: java list distinct by object attribute 
Java :: find min in array java 
Java :: java method return list 
Java :: java localdate to calendar 
Java :: convert char to string in java 
Java :: break java 
Java :: operador ternario java 
Java :: java secretkey 
Java :: java break multiple loops 
Java :: how to instance a calendar in java 
Java :: Getting Pending intent for 12 version 
Java :: charat(0).touppercase() java 
Java :: how to remove null values collections 
Java :: template competitive programming java 
Java :: get thumbnail from video in android 
Java :: java clone matrix 
Java :: android xml change button background 
Java :: how generate a random number in java between 3 and 5 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =