Skip to main content

Calculator - Netbeans Java Code

Run Shot of Calculator
Java Code:

public class Calculator {
     public static void main(String[] args) {
        boolean go = true;    //sets up loop

        while(go)    //creates loop to top
        {
            System.out.println("Hello this is my calculator!");
            System.out.println("To add, type a, to subtract, type s.");
            System.out.println("To multiply, type m, to divide, type d.");

            Scanner scan = new Scanner(System.in);    //sets up scanners
            Scanner scan1 = new Scanner(System.in);


            String action = scan.nextLine();    //tells comp. to take user input

            if("a".equals(action))    //addition
            {
                System.out.println("Now type in the first number you would like to add.");
                int add1 = scan.nextInt();
                System.out.println("Now type the second number.");
                int add2 = scan.nextInt();
                int add3 = add1 + add2;
                System.out.println(add1 + " added to " + add2 + " equals " + add3 + "!");          
            }
            if("s".equals(action))    //subtraction
            {
                System.out.println("Now type in the first number you would like to subtract.");
                int sub1 = scan.nextInt();
                System.out.println("Now type the second number.");
                int sub2 = scan.nextInt();
                int sub3 = sub1 - sub2;
                System.out.println(sub1 + " subtracted bye " + sub2 + " equals " + sub3 + "!"); 
            }
            if("m".equals(action))    //multiplacation
            {
                System.out.println("Now type in the first number you would like to multiply.");
                int mul1 = scan.nextInt();
                System.out.println("Now type the second number.");
                int mul2 = scan.nextInt();
                int mul3 = mul1 * mul2;
                System.out.println(mul1 + " multiplied bye " + mul2 + " equals " + mul3 + "!"); 
            }
            if("d".equals(action))    //division
            {
                System.out.println("Now type in the first number you would like to divide.");
                int div1 = scan.nextInt();
                System.out.println("Now type the second number.");
                int div2 = scan.nextInt();
                int div3 = div1 / div2;
                System.out.println(div1 + " divided bye " + div2 + " equals " + div3 + "!"); 
            }


            System.out.println("Would you like to start over? (yes,no)");           
            String startOver = scan1.nextLine();

            if("no".equals(startOver))
            {
                go = false;
                System.out.println("Bye");
            }
        }
    }                                  

}

You can also download text file version of this code: Calculator.txt


Comments

Popular posts from this blog

Binary Converter - Netbeans Java Codes

You can also download text file of this code here:   BinaryConverter.txt Java Code: public class BinaryConv { public static void main(String[] args) { Scanner in = new Scanner(System.in); int decimalNumber; String binaryNumber; System.out.print("Enter a positive integer: "); decimalNumber = in.nextInt(); if (decimalNumber <= 0) System.out.println("ERROR: entered integer is nonpositive."); else { binaryNumber = ""; // algorithm step by step // initial: binaryNumber = "", decimalNumber = 123 // step 1 : binaryNumber = "1 ", decimalNumber = 61 // step 2 : binaryNumber = "11 ", decimalNumber = 30 // step 3 : binaryNumber = "011 ", decimalNumber = 15 // step 4 : binaryNumber = "1011 ", decimalNumber = 7 // step 5 : binaryNumber = "1 1011 ", decimalNumber = 3 // step 6 : binaryNumber = "11 1011 ", decimalNumber = 1 // step 6 : binaryNumber = "111 1011 ",...

Javax Code for Playing Music:

Javax Code for Playing Music: OUTPUT: ________________________________________________________________ Just Copy and Paste and Change the variable name respectively: ________________________________________________________________ ________________________________________________________________ Buttons: 1. jButton1 : Open Music File 2. jButton2 : Play 3, jButton3 : Stop 4. jButton4 : Pause ________________________________________________________________ import java.io.File;     import java.io.IOException;     import java.net. MalformedURLException;     import javax.sound.sampled. AudioInputStream;     import javax.sound.sampled. AudioSystem;     import javax.sound.sampled.Clip;     import javax.sound.sampled. LineUnavailableException;     import javax.sound.sampled. UnsupportedAudioFileException; public class Music extends javax.swing.JFrame {     /** ...

Get Current System Time & Date - Netbeans Java Code

Run Shot of Current System Time & Date Java Code: import java.text.SimpleDateFormat; import java.util.Date; public class Datetimemonth { public static void main(String[] args) { Date now = new Date(); System.out.println("toString(): " + now); // dow mon dd hh:mm:ss zzz yyyy // SimpleDateFormat can be used to control the date/time display format: // E (day of week): 3E or fewer (in text xxx), >3E (in full text) // M (month): M (in number), MM (in number with leading zero) // 3M: (in text xxx), >3M: (in full text full) // h (hour): h, hh (with leading zero) // m (minute) // s (second) // a (AM/PM) // H (hour in 0 to 23) // z (time zone) SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z"); System.out.println("Format 1: " + dateFormatter.format(now)); dateFormatter = new Sim...