Simple Problem (Java Calculator - Switch)

Asked

Viewed 650 times

-3

Guys I’m in a lot of trouble for a job to be delivered tomorrow, someone can help ?

EDIT: I made the base, but I’m having trouble adding the switch to the code and completing the calculator, can you tell me how to at least add the part and enter the number? would be with scanner ?

Build a Java Calculator that has the following features:

    Menu:
      (1) - Inserir valor inicial
      (2) - Somar
      (3) - Subtrair
      (4) - Multiplicar
      (5) - Dividir
      (6) - Potencia
      (7) - Inverter Sinal
      (8) - Limpar Resultado

It is necessary to implement 2 classes:
1) Main, contains the user interaction part (interface and data input),
2) Calculator, contains the implementation of the methods responsible for the calculation itself (summing, dividing, multiplying, etc).

Main Class:

import java.util.Scanner;
public class Principal {
    public static void main(String[] args) {
        // menu
        System.out.println("CALCULADORA \n");
        System.out.println("Resultado: \n");
        System.out.println("Menu:");
        System.out.println(" (1) - Inserir valor inicial;");
        System.out.println(" (2) - Somar;");
        System.out.println(" (3) - Subtrair;");
        System.out.println(" (4) - Multiplicar;");
        System.out.println(" (5) - Dividir;");
        System.out.println(" (6) - Potencia;");
        System.out.println(" (7) - Inverter Sinal;");
        System.out.println(" (8) - Limpar Resultado;");

        //Principal p = new Principal();
        //p.limparTela();

    }

    public void limparTela() {
        for (int i=0; i< 100; i++) {
            System.out.println("\n");
        }
    }

}

Class Calculator

public class Calculadora {
    // Atributos
    private Double resultado = 0.0;

    // Metodos
    public void inserirValorInicial(Double valor) { }
    public Double obterResultado() {    return 0.0; }
    public void somar(Double valor) {   }
    public void subtrair(Double valor) {    }
    public void multiplicar(Double valor) { }
    public void dividir(Double valor) { }
    public void inverterSinal() {   }
    public void potencia(int potencia) {    }
    public void limpar() {  }
}
  • 1

    What is your doubt? Edit the question and explain where you are having doubts.

  • 1

    I don’t understand the doubt, you’re having some mistake?

  • I made the base, but I’m having trouble adding the switch to the code and completing the calculator, can you tell me how to at least add the part and enter the number? would be with scanner ?

1 answer

1

To read the numbers you can use the Scanner

Scanner leitor = new Scanner(System.in);
int escolha = leitor.nextInt();

double a = leitor.nextDouble();

double b = leitor.nextDouble();

switch(escolha){
case 1:
soma(a,b);
break;
}

function soma

public static void soma(double a,double b){
     System.out.println("Resultado = " + (a+b));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.