6 Replies - 129840 Views - Last Post: 13 April 2008 - 07:53 PM Rate Topic: ***-- 2 Votes

#1 todd3428  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-October 06

Calculator Program

Posted 10 October 2006 - 04:43 PM

I need help figuring out what type of loops/if statements to be using. I am in a beginning programming class so much of the code used for the mortgage calculators we havent learned yet. Here is our assignment:

Write a calculator program that says welcome to the calculator. It then asks if you want to multiply, divide, add, or subtract. Then once you get into the multiply, divide, add, or subtract loop it will do the calculation output the answer and then ask if you want to do another calculation or exit the program.

Example
Welcome to the calculator!
To multiply use m, to divide use d, to add use a, and to subtract use s

Input first number
Input second number
You chose to multiply. The product is 20
Do you want another calculation (y/n)?

Here is my code so far:

import java.util.Scanner;

public class calculator

{
	public static void main(String[] args)
	{
	   char answer;
	   String input;
	   int firstnumber;
	   int secondnumber;
	   
	   
	   

           System.out.println ("Welcome to the calculator!");
	   System.out.println ("To multiply use m, to divide use d, to add use a, to subtract use s");
	   
	   Scanner keyboard = new Scanner(System.in);
	   	   
	   do
	   {
	   	System.out.println ("Do you want to multiply, divide, add, or subtract?  ");
           	keyboard.nextLine();
	   	input = keyboard.nextLine();
	   	answer = input.charAt(0);
		
		if (answer == 'm')
		System.out.println ("Input first number:  ");
		keyboard.nextLine();
		input = keyboard.nextLine();
		firstnumber = keyboard.nextInt();
	   
	      
	        System.out.println ("Would you like another calculation (y/n)?   ");
		keyboard.nextLine();
		input = keyboard.nextLine();
		answer = input.charAt(0);
	   }
	   while(answer == 'y')
	   	if(answer == 'n');
		System.exit(0);
	}
}


Is This A Good Question/Topic? 1
  • +

Replies To: Calculator Program

#2 ProGraM  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 28-September 05

Re: Calculator Program

Posted 10 October 2006 - 05:21 PM

this is a version of your code using BufferReader and InputReader. personally i like using this way more then the scanner. Also the Integer.parseInt converts an int to a string you can also do this for a double and etc... If you still wish to use scanner u can change the program up but i beleive this is the best way. Enjoy!
import java.io.*;
import java.util.*;
public class calculator

{
public static void main(String[] args) throws IOException
{
	BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in)); 
	String input, input1, input2;
	int num1, num2, answer;

	System.out.println ("\t\t\tWelcome to the calculator!");
do
{
	System.out.print ("\nWould you like to Multiply(m), Divide(d), Subtract(s), Add(a)?: ");
	input = keyboard.readLine();
	if (input.equals("m"))
			{
				System.out.print ("Please enter the first number!: ");  
				num1 = Integer.parseInt (input1 = keyboard.readLine());  
				System.out.print ("Please enter the second number!: ");  
				num2 = Integer.parseInt (input2 = keyboard.readLine());
				answer = num1*num2;
				System.out.print ("You chose to multiply."+"\t"+"The Product is: "+answer);
			}
	if (input.equals("d"))
			{
				System.out.print ("Please enter the first number!: ");  
				num1 = Integer.parseInt (input1 = keyboard.readLine());  
				System.out.print ("Please enter the second number!: ");  
				num2 = Integer.parseInt (input2 = keyboard.readLine());
				answer = num1/num2;
				System.out.print ("You chose to divide."+"\t"+"The Dividend is: "+answer);
			}
	if (input.equals("a"))
			{
				System.out.print ("Please enter the first number!: ");  
				num1 = Integer.parseInt (input1 = keyboard.readLine());  
				System.out.print ("Please enter the second number!: ");  
				num2 = Integer.parseInt (input2 = keyboard.readLine());
				answer = num1+num2;
				System.out.print ("You chose to add."+"\t"+"The Sum is: "+answer);
			}
	if (input.equals("s"))
			{
				System.out.print ("Please enter the first number!: ");  
				num1 = Integer.parseInt (input1 = keyboard.readLine());  
				System.out.print ("Please enter the second number!: ");  
				num2 = Integer.parseInt (input2 = keyboard.readLine());
				answer = num1-num2;
				System.out.print ("You chose to subtract."+"\t"+"The Diffrence is: "+answer);
			}

System.out.print ("\nWould you like another calculation (y/n)?: ");
input = keyboard.readLine();
}
while(input.equals("y"));
if(input.equals("n"));
System.exit(0);
}
}


Was This Post Helpful? 1

#3 chachejave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-February 08

Re: Calculator Program

Posted 24 February 2008 - 06:05 PM

I am taking my first computer programming class in many many years. This helps a lot to see how another student and a programmer approached the problem that I am working on. And thanks for the reminder that its not your job to do my homework.
chachejave
Was This Post Helpful? 0
  • +
  • -

#4 Mastergeek666  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 10-August 07

Re: Calculator Program

Posted 24 February 2008 - 07:14 PM

First of all you need to put your code in a tag => [code ]code here[ /code], next you'd be better off using a BufferedReader to recognize input. For example,
import java.io.*;
public class Input {
	
	public static void main(String[] args) throws IOException // have to throw IOException unless you wanna do try catch
	{
	String input; // your input variable
	BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); // BufferedReader that allows input
	System.out.println("whats your name"); /
	input = keyboard.readLine(); // this line allows for input
	}
}



Then when you wanna use that input for checking just call input and sense your making a calculator, you gonna need to parse the input so that you can use it as a int instead of a string. Or you can just make the input parsed into an int as they input, like so
int first, second, answer;
first = Integer.parseInteger(input = keyboard.readLine()); // parses input into int
second = Integer.parseInteger(input = keyboard.readLine());
answer = first * second; 
System.out.printnl(answer);



That was just an example of multiplication of the two inputs. You could have the first input to check what symbol they picked(a,s,m,d) then use an if statement to check which they selected then do which ever math they picked.

This post has been edited by Mastergeek666: 24 February 2008 - 07:14 PM

Was This Post Helpful? 1

#5 Nuusiku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 08

Re: Calculator Program

Posted 12 April 2008 - 03:58 AM

Thanx for you help, but you see there is another part of the calculator that i dont understand it should be like in diffrent classes that i know but we should use an interface which i can not do, can u please help me with this?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculator Program

Posted 13 April 2008 - 12:26 PM

View PostNuusiku, on 12 Apr, 2008 - 03:58 AM, said:

should be like in diffrent classes that i know but we should use an interface


Ouf... not easy to understand

Can you post textually your assigment text ?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculator Program

Posted 13 April 2008 - 07:53 PM

View Postpbl, on 13 Apr, 2008 - 12:26 PM, said:

View PostNuusiku, on 12 Apr, 2008 - 03:58 AM, said:

should be like in diffrent classes that i know but we should use an interface


Ouf... not easy to understand

Can you post textually your assigment text ?



6.676 visits for this topic....
A lot of students must have a Calculator assigment :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1