Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 08:56 AM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
Create a Calculator in Java
Hi, i just created a very simple calculator using what i learned so far.
Im not sure if its in the correct section so correct me if it isnt.

Code:
import java.util.*; public class Calculator { public static void main(String[] args) { /*Scanner input = new Scanner(System.in); System.out.println("What are you doing?"); if (answer.equalsIgnorecase("multiplication")) { Multiplication(); }*/ System.out.println("Hello, If you are doing Multiplication Type 1."); System.out.println("If you are doing Addition, Type 2."); System.out.println("If you are doing Subtraction, Type 3"); System.out.println("If you are doing Division, Type 4"); Scanner input = new Scanner(System.in); int decision; decision = input.nextInt(); if (decision == 1) { Multiplication(); } if (decision == 2) { Addition(); } if (decision == 3) { Subtraction(); } if (decision == 4) { Division(); } else { System.out.println("Those aren't valid numbers."); wrongInput(); } } public static void wrongInput() { System.out.println("If you are doing Multiplication Type 1."); System.out.println("If you are doing Addition, Type 2."); System.out.println("If you are doing Subtraction, Type 3"); System.out.println("If you are doing Division, Type 4"); Scanner input = new Scanner(System.in); int decision; decision = input.nextInt(); if (decision == 1) { Multiplication(); } if (decision == 2) { Addition(); } if (decision == 3) { Subtraction(); } if (decision == 4) { Division(); } else { System.out.println("Those aren't valid numbers."); wrongInput(); } } public static void Multiplication() { int mInput1; int mInput2; Scanner input = new Scanner(System.in); System.out.print("What is the first number? "); mInput1 = input.nextInt(); System.out.print("What is the second number? "); mInput2 = input.nextInt(); System.out.println(mInput1 + " times " + mInput2 + " equals " + mInput1 * mInput2); } public static void Addition() { int aInput1; int aInput2; int aAnswer; Scanner input = new Scanner(System.in); System.out.println("What is the first number?"); aInput1 = input.nextInt(); System.out.println("What is the second number?"); aInput2 = input.nextInt(); aAnswer = aInput1 + aInput2; System.out.println(aInput1 + " plus " + aInput2 + " equals " + aAnswer); } public static void Subtraction() { int sInput1; int sInput2; int sAnswer; Scanner input = new Scanner(System.in); System.out.println("What is the first number?"); sInput1 = input.nextInt(); System.out.println("What is the second number?"); sInput2 = input.nextInt(); sAnswer = sInput1 - sInput2; System.out.println(sInput1 + " minus " + sInput2 + " equals " + sAnswer); } public static void Division() { int dInput1; int dInput2; int dAnswer; Scanner input = new Scanner(System.in); System.out.println("What is the first number?"); dInput1 = input.nextInt(); System.out.println("What is the second number?"); dInput2 = input.nextInt(); dAnswer = dInput2 / dInput1; System.out.println(dInput1 + " divided by " + dInput2 + " equals " + dAnswer); } }
Thanks.
Albert
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 08:58 AM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
To trim down the code a bit, you can eliminate your wrongInput method, by using a loop in the main method of your code, along with another question like "Do you want to do more calculations?"

The basic idea is: answer = ""

Code:
While answer does not equal "no" Calculations logic(if statements, method calls etc...) answer = result of question to continue
Just an idea to continue on what you've learned thus far.

You can also look into how to pass parameters, and use only one method called doCalculation() and pass it a value that tells which type of calculation to do. That would take out all of the repetition of code.

Greetings
Marcus
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 09:01 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
Congrats on your first Java program!!!

Marcus has made some good suggestions.

If you're looking to forward your knowledge take them into consideration, they will help teach you good programming style.

Greetings

Felissa
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Calculator aapanju New To Java 3 04-17-2008 06:33 AM
Java calculator decimal cart1443 New To Java 2 04-16-2008 02:19 PM
calculator not working Renegade85 New To Java 5 03-10-2008 04:27 PM
How to create ToolTip in Java 3d roshithmca AWT / Swing 0 02-04-2008 07:57 AM
Swing Calculator nemo AWT / Swing 1 05-28-2007 12:07 PM


All times are GMT +3. The time now is 12:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org