Reputation: 1
import java.util.*;
public class projectOneAdamYoung {
public static void main(String[] args){
System.out.println("Welcome to the AP Computer Science calculator!!");
Scanner kb = new Scanner(System.in);
System.out.print("Enter an expression, or quit to exit: ");
String line = kb.nextLine();
Scanner input = new Scanner(line);
int redLight = 0;
while(redLight != 1) {
if(line.equalsIgnoreCase("quit")){
System.out.println("Thanks for using this calculator!");
redLight = 1;
}else {
if (input.hasNextDouble()) {
String numFirst = input.next();
String opperator = input.next();
String numSecond = input.next();
double num1 = Double.parseDouble(numFirst);
double num2 = Double.parseDouble(numSecond);
if (opperator.equals("+")) {
calcAdd();
}
if (opperator.equals("*")) {
calcMult();
}
if (opperator.equals("-")) {
calcSub();
}
if (opperator.equals("/")) {
calcDiv();
}
if (opperator.equals("^")) {
calcExp();
}
}else {
String opperator = input.next();
String numFirst = input.next();
double num1 = Double.parseDouble(numFirst);
if (opperator.equals("|")) {
calcAbs();
}
if (opperator.equals("v")) {
calcSqrt();
}
if (opperator.equals("~")) {
calcRound();
}
if (opperator.equals("s")) {
calcSin();
}
if (opperator.equals("c")) {
calcCos();
}
if (opperator.equals("t")) {
calcTan();
}
}
}
}
}
public static void calcAdd() {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
public static void calcSub() {
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
}
public static void calcMult() {
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
}
public static void calcDiv() {
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
}
public static void calcExp() {
System.out.println(num1 + " ^ " + num2 + " = " + (pow(num1, num2)));
}
public static void calcAbs() {
System.out.println(num1 + " | " + " = " + (abs(num1)));
}
public static void calcSqrt() {
System.out.println(num1 + " v " + " = " + (sqrt(num1)));
}
public static void calcRound() {
System.out.println(num1 + " ~ " + " = " + (round(num1)));
}
public static void calcSin() {
System.out.println(num1 + " s " + " = " + (sin(num1)));
}
public static void calcCos() {
System.out.println(num1 + " c " + " = " + (cos(num1)));
}
public static void calcTan() {
System.out.println(num1 + " t " + " = " + (tan(num1)));
}
}
I'm working on a calculator project for school, and need help.This is all my code and it doesn't compile because the variables I'm using to complete the operations are in a different Method. I can't figure out how to make this work looking at others' work.
Upvotes: 0
Views: 55
Reputation:
import java.util.*;
public class ProjectOneAdamYoung {
private double num1;
private double num2;
public static void main(String[] args) {
new ProjectOneAdamYoung().calculate();
}
public void calculate() {
System.out.println("Welcome to the AP Computer Science calculator!!");
Scanner kb = new Scanner(System.in);
System.out.print("Enter an expression, or quit to exit: ");
String line = kb.nextLine();
Scanner input = new Scanner(line);
int redLight = 0;
while (redLight != 1) {
if (line.equalsIgnoreCase("quit")) {
System.out.println("Thanks for using this calculator!");
redLight = 1;
} else {
if (input.hasNextDouble()) {
String numFirst = input.next();
String opperator = input.next();
String numSecond = input.next();
num1 = Double.parseDouble(numFirst);
num2 = Double.parseDouble(numSecond);
if (opperator.equals("+")) {
calcAdd();
}
if (opperator.equals("*")) {
calcMult();
}
if (opperator.equals("-")) {
calcSub();
}
if (opperator.equals("/")) {
calcDiv();
}
if (opperator.equals("^")) {
calcExp();
}
} else {
String opperator = input.next();
String numFirst = input.next();
num1 = Double.parseDouble(numFirst);
if (opperator.equals("|")) {
calcAbs();
}
if (opperator.equals("v")) {
calcSqrt();
}
if (opperator.equals("~")) {
calcRound();
}
if (opperator.equals("s")) {
calcSin();
}
if (opperator.equals("c")) {
calcCos();
}
if (opperator.equals("t")) {
calcTan();
}
}
}
redLight = 1;
}
}
public void calcAdd() {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
public void calcSub() {
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
}
public void calcMult() {
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
}
public void calcDiv() {
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
}
public void calcExp() {
System.out.println(num1 + " ^ " + num2 + " = " + (Math.pow(num1, num2)));
}
public void calcAbs() {
System.out.println(num1 + " | " + " = " + (Math.abs(num1)));
}
public void calcSqrt() {
System.out.println(num1 + " v " + " = " + (Math.sqrt(num1)));
}
public void calcRound() {
System.out.println(num1 + " ~ " + " = " + (Math.round(num1)));
}
public void calcSin() {
System.out.println(num1 + " s " + " = " + (Math.sin(num1)));
}
public void calcCos() {
System.out.println(num1 + " c " + " = " + (Math.cos(num1)));
}
public void calcTan() {
System.out.println(num1 + " t " + " = " + (Math.tan(num1)));
}
}
I changed your class just a little, to do what you wanted. But, that is not the better way to do so.
Upvotes: 0
Reputation: 465
You will need to read up on passing parameters and accepting arguments. Understanding variable scope is also important to learn. Until then, you can loosely use: What ever variable you declare inside of a set of {
and }
will be local to that area.
This example might help you understand a little more.
public class Pass {
public static void main(String[] args) {
// variables to pass.
// scope : everything inside of the main method.
double one = 1.0;
double two = 2.0;
for (int a = 0; a < one; a++)
{
// scope of a = this for loop
double three = 3.0; // variable three declared in this loop, scope = this loop.
Pass.methodOne(one); // passing a parameter (double one) to methodOne.
Pass.methodTwo(two); // passing a parameter (double two) to methodTwo.
}
// a = 2; This will not work because this isn't inside the scope of a.
}
static double c; // double c declared in class, scope whole class.
// Note static keyword for access in static methods such as main.
public static void methodOne(double d) { // accepting a parameter and calling it d
//double d declared as argument variable, scope = this method.
c = d; // local variable d is assigned to class variable c.
System.out.println("The number methodOne received is " + c + ".");
}
public static void methodTwo(double d) { // accepting a parameter and calling it d
c = d; // same c used in methodOne, different d.
System.out.println("The number methodTwo received is " + c + ".");
}
}
Upvotes: 0
Reputation: 221
I agree with Luiggi's commentary wholeheartedly (but don't yet have the points to add to that thread directly).
Try invoking, for example, calcAdd like:
calcAdd(num1, num2);
and defining it as:
public static void calcAdd(double num1, double num2) {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
Verify that this fixes the compilation errors for calcAdd, then make similar fixes for calcSub, etc. It will take a few minutes of typing, but it will be a big step closer.
Upvotes: 2