This is just a simple program that acts as a calculator for school age children. My professor requested that I add a declarations section. If possible could someone help me which parts of my code I need to add.

Edited 2 Years Ago by JamesCherrill: Changed type to discussion

My instructor has us do the same. His meaning is that we comment out a sentence or two at the very beginning stating the program is our own work and not copied and that we agree to get a zero for the assignment for any cheating.

Declarations as in:
//Declarations
not sure what to add here (some variables)
//endDeclarations

package calculatortrial; // This Program is designed to calculate for children

import java.util.Scanner;
public class Calculatortrial {

    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

String option;

do{

System.out.println("****Welcome to calculator*******");  // Welcomes user

System.out.print("Enter ANY Number You Would LIKE XD:: "); // prompts for number

int first = in.nextInt();

System.out.print("Enter the operator('+','-','*' or '/')::"); // ask for oper

String operator = in.next();

System.out.print("Enter Your second number"); // prompts user for number

int second = in.nextInt();

double result = 0;

if(operator.equals("+")){

result = first+second;

}

else if(operator.equals("-")){

result = first-second;

}

else if(operator.equals("*")){

result = first*second;

}

else if(operator.equals("/")){

result = first/second;

}

String resultFormatted = String.format("%.2f",result);

System.out.println("Result for "+first+operator+second+" = "+resultFormatted);

System.out.println("Would You Like To Do More Awesome Math With Me??(Y/N)");

option = in.next();

System.err.println("Have a Wonderful Day!!!!!");  // if user is done. says bye


}

while(option.equalsIgnoreCase("Y"));

in.close();  


}


}

I mean I must be having a brain fart I had no trouble doing the code it's just cant remember of to do the declarations.

This is confusing. That code is perfectly valid Java; everything that needs to be declared is declared.
Do you mean declaration like ricewtnd said, eg something like

// I declare that this is all my own work and I didn't cheat...

Nope, no doubt it's mine I worked on it for a few hours lol. He teaches pseudocode asw ell where all the variables needed to be declared at the start. Maybe I'm over complicating things.

I think that he would like you to place all of your variable declarations in one place rather than throughout your program.

public static void main(String[] args) {

    //Declarations
    int first = 0;  //first integer
    int second = 0; //second integer
    double result = 0; 
    String option;


    Scanner in = new Scanner(System.in);

    do{
        // Welcomes user
        System.out.println("****Welcome to calculator*******");  

        first = in.nextInt();

        ....


    } while(option.equalsIgnoreCase("Y"));

    in.close();
}

Edited 2 Years Ago by cgeier

Maybe that's what was asked for, but it's poor coding practice. Any variable should have a scope that's just as big as is needed, but no bigger. By moving declarations out of their local block into a wider scope you are just increasing the risk of problems.

This article has been dead for over six months. Start a new discussion instead.