School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,132 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,934 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 Rate Topic: -----

#1 p.tryfle  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 17-May 09


Dream Kudos: 0

Post icon  Posted 17 May 2009 - 04:50 AM

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:

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
Was This Post Helpful? 0
  • +
  • -


#2 g00se  Icon User is offline

  • D.I.C Lover
  • Icon
  • Group: Expert w/DIC++
  • Posts: 1,751
  • Joined: 19-September 08


Dream Kudos: 0

Posted 17 May 2009 - 05:55 AM

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

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));
}





Was This Post Helpful? 0
  • +
  • -

#3 p.tryfle  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 17-May 09


Dream Kudos: 0

Posted 17 May 2009 - 07:20 AM

View Postg00se, on 17 May, 2009 - 05:55 AM, said:

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

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.

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();

	

}

}


Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • Icon
  • Group: Expert w/DIC++
  • Posts: 1,751
  • Joined: 19-September 08


Dream Kudos: 0

Posted 17 May 2009 - 07:34 AM

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
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month