Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,831 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,742 people online right now. Registration is fast and FREE... Join Now!




Write a CalculatorClass using the ScannerClass and a SwitchStatement

 

Write a CalculatorClass using the ScannerClass and a SwitchStatement, Have to write a Calculator which calculate the 5 fundamental rules of

p.tryfle

17 May, 2009 - 04:50 AM
Post #1

New D.I.C Head
*

Joined: 17 May, 2009
Posts: 4

Hallo to everybody,
I'm a noob to java and have to write this calculator. I'm learning JAVA for three weeks now but this is a number to big for me.
Sitting three days of thinking, reading, searching and of course trying to get it right I'm out of any ideas!

Most of the programm code have to be in methods and the main method should just start the programm!

My version works until the system asks you about the arithmetic operation. I got no Idea how to link the switch statement including the arithmetic operations to the scannermethod. So that one choose an arithmetic operation and the result comes out like this:
for example systemoutput: 12+12=24.
The task is to realize this using local variables as much as possible.

here is the code:

CODE
import java.util.Scanner;


public class calculator {
    
    private int calculate;
    private int x;
    private int y;
    private int result;
    
    
    

    public static void input(){
        
        
        Scanner sc = new Scanner(System.in);
        
       System.out.print("Systemoutput:        ");
          System.out.print("insert an integral number:");
          System.out.println("");
         
       int x = Integer.parseInt(sc.next());
      
       System.out.print("userinput:        ");
       System.out.println(x);
       System.out.println("");
      
       System.out.print("Systeoutput:        ");
       System.out.print("insert another integral number:");
       System.out.println("");
      
       int y = Integer.parseInt(sc.next());
      
       System.out.print("userinput:        ");
       System.out.println(y);
       System.out.println("");
      
       System.out.print("Systemoutput:        ");
       System.out.println("please choose the arithmetic operation");
       System.out.println("            (" + 1 + " = " + "addition; " + 2 + " = " + "subtraction;" + 3 + " = ");
       System.out.print("                   multiplication; " + 4 + " = " + "division; " + 5 + " = " + "percent; )" );
      
        int calculate = Integer.parseInt(sc.next());
      
      
       }
    
      
    
    public static void calculate(){
    
        public int getcalculate(){
    
        
    
    switch  (calculate) {
    
    case 1 : result = x + y;
    break;
    
    case 2: result = x - y;
    break;
    
    case 3: result = x * y;
    break;
    
    case 4: result = x / y;
    break;
    
    case 5: result = x * 100 / y;
    break;
    
    }
    }
    }
    
    

public static void main (String[] args){
    
    input();
    calculate();
    

}

}





Maybe somebody can help.
Thanks in advance.
greets

User is offlineProfile CardPM
+Quote Post


g00se

RE: Write A CalculatorClass Using The ScannerClass And A SwitchStatement

17 May, 2009 - 05:55 AM
Post #2

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,234



Thanked: 144 times
My Contributions
You're not far off. Inegrate something like the following into it:

java

System.out.println("please choose the arithmetic operation (+ - / *)");
char operator = sc.next().charAt(0);
System.out.println(calculate(x, y, operator));


....

public int calculate(int x, int y, char operator) {
// Assume integer only operations
switch(operator) {
case '+':
return x + y;
case '-':
return x - y;
case '/':
return x / y;
case '*':
return x * y;
default:
throw new IllegalArgumentException(String.format("%s is not a valid operator", operator));
}




User is offlineProfile CardPM
+Quote Post

p.tryfle

RE: Write A CalculatorClass Using The ScannerClass And A SwitchStatement

17 May, 2009 - 07:20 AM
Post #3

New D.I.C Head
*

Joined: 17 May, 2009
Posts: 4

QUOTE(g00se @ 17 May, 2009 - 05:55 AM) *

You're not far off. Inegrate something like the following into it:

java

System.out.println("please choose the arithmetic operation (+ - / *)");
char operator = sc.next().charAt(0);
System.out.println(calculate(x, y, operator));


....

public int calculate(int x, int y, char operator) {
// Assume integer only operations
switch(operator) {
case '+':
return x + y;
case '-':
return x - y;
case '/':
return x / y;
case '*':
return x * y;
default:
throw new IllegalArgumentException(String.format("%s is not a valid operator", operator));
}







thanks, this helps but I have to put all the basic arithmetiic operations into it.
In addition the results of the "/" and percent: " x * 100 / y" have to put out in double!

I have changed the code: please excuse the german language, because I'm running out of time I can't rewrite it anymore.

CODE
import java.util.Scanner;


public class TaschenRechner {

    

    




    public static void eingabe(){
        
        
        Scanner sc = new Scanner(System.in);
        
        int zahl1,zahl2,ergebnis;
        float ergebnis1;
        
       System.out.print("Systemausgabe:        ");
          System.out.print("Bitte geben Sie einen ganzzahligen Wert ein:");
          System.out.println("");
         
          zahl1 = sc.nextInt();
  
      
       System.out.print("usereingabe:        ");
       System.out.println(zahl1);
       System.out.println("");
      
       System.out.print("Systemausgabe:        ");
       System.out.print("Bitte geben Sie einen weiteren ganzzahligen Wert ein:");
       System.out.println("");
      
       zahl2 = sc.nextInt();
      
      
       System.out.print("usereingabe:        ");
       System.out.println(zahl2);
       System.out.println("");
      
       System.out.print("Systemausgabe:        ");
       System.out.println("Bitte geben Sie die gewünschte Operation an");
       System.out.println("            (" + 1 + " = " + "Addieren; " + 2 + " = " + "Subtraktion;" + 3 + " = ");
       System.out.print("             Multiplikation; " + 4 + " = " + "Division; " + 5 + " = " + "Prozent; )" );
      
       ergebnis =  sc.nextInt();
      
      
       System.out.print("usereingabe:        ");
       System.out.println(ergebnis);
       System.out.println("");
      
      System.out.print("Systemausgabe:        ");
      
    
    
        switch  (ergebnis) {
    
    
    case 1 : System.out.print(zahl1 + " + " + zahl2);
            System.out.print (" = " );
             System.out.print(ergebnis = zahl1 + zahl2);
    break;
            

    case 2: System.out.print(zahl1 + " - " + zahl2);
            System.out.print (" = " );
            System.out.print(ergebnis = zahl1 - zahl2);
            
    break;
    
    case 3:    System.out.print(zahl1 + " * " + zahl2);
            System.out.print (" = " );
            System.out.print(ergebnis = zahl1 * zahl2);
    break;
    
    case 4: // Here the result should be put out in double not in integer
            System.out.print(zahl1 + " / " + zahl2);
            System.out.print (" = " );
            System.out.print(ergebnis = zahl1 / zahl2);
        
    break;
    
    case 5:  // Here the result should be put out in double not in integer
            System.out.print(zahl1 + " * 100 / " + zahl2);
            System.out.print (" = " );
            System.out.print(ergebnis= zahl1 * 100 / zahl2);
    break;
    
    }
    }
    
    

public static void main (String[] args){
    
    eingabe();

    

}

}



User is offlineProfile CardPM
+Quote Post

g00se

RE: Write A CalculatorClass Using The ScannerClass And A SwitchStatement

17 May, 2009 - 07:34 AM
Post #4

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,234



Thanked: 144 times
My Contributions
QUOTE
but I have to put all the basic arithmetiic operations into it.


I don't understand sorry. The basic operations are in the code i gave you, with the exception of percent. As for needing to return double, just change the return type of the 'calculate' method
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:45PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month