Question

Creating a calculator for my CS1 class and my code was able to run but it...

Creating a calculator for my CS1 class and my code was able to run but it crashes near the end. As far as I know, the error means that it's retrieving "null" from the variable but I haven't seen that kind of error from this kind of situation. I would appreciate any help. I'm just starting up in Java so I apologize if this is a silly question. Thank you!


```
----jGRASP exec: java CS1Calculator

------------

Welcome to the CS1 Calculator!
This calculator can help you with addition, subtraction, multiplication, or division.

------------

Select the problem type you would like to calculate by entering the first letter of the operation.
(A)ddition
(S)ubtraction
(M)ultiplication
(D)ivision
Exception in thread "main" java.lang.NullPointerException
   at CS1Calculator.calculate(CS1Calculator.java:42)
   at CS1Calculator.main(CS1Calculator.java:60)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
```


```
/*
Program: CS1Calculator
Date: 09/10/2019

Description: Simple interactive calculator that's able to calculate integers.
*/


import java.util.Scanner;

public class CS1Calculator {
private int addition, subtraction, multiplication, division;
private static Scanner keyboard;
private String userOperation;

public CS1Calculator () {
// Constructor
Scanner keyboard = new Scanner(System.in);   
int addition = 0;
int subtraction = 0;
int multiplication = 0;
int division = 0;
}

public void printIntro() {
// Ouputs introduction for program
System.out.println("\n------------\n");
System.out.println("Welcome to the CS1 Calculator!");
System.out.println("This calculator can help you with addition, subtraction, multiplication, or division.");
System.out.println("\n------------\n");
}

public void calculate () {
// Asks for preferred operation, calculates answer, and tallies up problems done
System.out.println("Select the problem type you would like to calculate by entering the first letter of the operation.");
System.out.println("(A)ddition");
System.out.println("(S)ubtraction");
System.out.println("(M)ultiplication");
System.out.println("(D)ivision");
String userOperation = keyboard.nextLine();

if(userOperation == "A") {
System.out.println("You have selected addition");
}
else if(userOperation == "S") {
System.out.println("You have selected subtraction.");
}
  
}

public void printRecord () {
// Outputs total of each problem done and overall total problems
}

public static void main(String[] args) {
CS1Calculator cs1c = new CS1Calculator();
cs1c.printIntro();
cs1c.calculate();
cs1c.printRecord();

}

}
```

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class CS1Calculator {
    private int addition, subtraction, multiplication, division;
    private static Scanner keyboard;
    private String userOperation;

    public CS1Calculator () {
// Constructor
        keyboard = new Scanner(System.in);
        int addition = 0;
        int subtraction = 0;
        int multiplication = 0;
        int division = 0;
    }

    public void printIntro() {
// Ouputs introduction for program
        System.out.println("\n------------\n");
        System.out.println("Welcome to the CS1 Calculator!");
        System.out.println("This calculator can help you with addition, subtraction, multiplication, or division.");
        System.out.println("\n------------\n");
    }

    public void calculate () {
// Asks for preferred operation, calculates answer, and tallies up problems done
        System.out.println("Select the problem type you would like to calculate by entering the first letter of the operation.");
        System.out.println("(A)ddition");
        System.out.println("(S)ubtraction");
        System.out.println("(M)ultiplication");
        System.out.println("(D)ivision");
        String userOperation = keyboard.nextLine();

        if(userOperation.equals("A")) {
            System.out.println("You have selected addition");
        }
        else if(userOperation.equals("S")) {
            System.out.println("You have selected subtraction.");
        }

    }

    public void printRecord () {
// Outputs total of each problem done and overall total problems
    }

    public static void main(String[] args) {
        CS1Calculator cs1c = new CS1Calculator();
        cs1c.printIntro();
        cs1c.calculate();
        cs1c.printRecord();

    }

}

Add a comment
Know the answer?
Add Answer to:
Creating a calculator for my CS1 class and my code was able to run but it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

  • LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR...

    LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR CODE: import java.util.Scanner; public class CalculatorProject { public static void main (String[] args) { Scanner yurr = new Scanner(System.in);    String mode; double i; String choice; String answer = "Y"; while (answer.equals("Y")) { System.out.println("Enter the calculator mode: Standard/Scientific?"); mode = yurr.next(); if(mode.equals("Scientific")) { System.out.print("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x,...

  • Hey guys! Huge part of my grade !! My code works already but I need it...

    Hey guys! Huge part of my grade !! My code works already but I need it so it references to my node list class instead of my array, what would I need to change? My assignment  was to create a list from an assignment when we used an array, but I cant make it to work without the array, help! The professor said it was okay to either keep the array or to delete it altogether. package zipcode; import java.io.File; import...

  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

  • Help getting my code to run. public class BusinessClass { // Length public static double inTOcm(double...

    Help getting my code to run. public class BusinessClass { // Length public static double inTOcm(double inches) { return 2.54 * inches; }public static double inTOmeter(double inches) { return 0.0254 * inches; }public static double inTOft(double inches) { return 0.083333 * inches; }public static double inTOyd(double inches) { return 0.0277778 * inches; }public static double inTOkm(double inches) { return 0.000254 * inches; }public static double ftTOmeter(double feet) { return 0.3048 * feet; }public static double ftTOyd(double feet) { return 0.0003048...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT