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,...

  • 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);...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

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
ADVERTISEMENT