Results 1 to 9 of 9
Thread: My First Useful Java Program!
- 05-27-2012, 05:34 AM #1
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
My First Useful Java Program!
I've been trying to make this for so long! Here's my code for a Java Addition or Subtraction Calculator!
Java Code:import java.util.Scanner; public class calculator { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Hello, what is your first name?"); String name = in.next(); System.out.println("Hello there, " + name + "!"); System.out.println("Would you like to add or subtract two numbers?"); System.out.println("Type add or subtract please."); String addOrSub = in.next(); if ("add".equalsIgnoreCase(addOrSub)) { System.out.println("You have chosen, Addition."); System.out.println("What two numbers would you like to add?"); System.out.println("First number?"); int numb1 = in.nextInt(); System.out.println("Second number?"); int numb2 = in.nextInt(); float sum = numb1 + numb2; System.out.println("Those two numbers added together equals " + sum + "."); } else { System.out.println("You have chosen, Subtraction."); System.out.println("What two numbers would you like to subtract?"); System.out.println("First number?"); int numb1 = in.nextInt(); System.out.println("Second number?"); int numb2 = in.nextInt(); float dif = numb1 - numb2; System.out.println("Those two numbers subtracted from eachother equals " + dif + "."); } System.out.println("Thank you for using Laon8orsSon's Java Calculator!"); } }
- 05-27-2012, 08:39 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: My First Useful Java Program!
Java coding conventions would say that the class should begin with a capital letter: Calculator. Also, is there a reason why you make sum and dif of type float?
- 05-27-2012, 09:04 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: My First Useful Java Program!
Cool; good job; now try to enhance your program a bit; e.g. make it ask that question again and again until the user is sick and tired of it or even (more difficult) make the user to the addition or subtraction and make the program check the answer. (and pay attention to what Pbrockway2 wrote)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-27-2012, 03:35 PM #4
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
- 05-27-2012, 03:56 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: My First Useful Java Program!
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-29-2012, 03:33 AM #6
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: My First Useful Java Program!
I've updated it to include some useful features like a lie detector for the name, division and multiplication support, and like JosAH said, made it repeat over and over!
Java Code:import java.util.Scanner; public class fourToolCalc { private static Scanner in; public static void main (String[] args) { beginning(); } public static void beginning() { Scanner in = new Scanner(System.in); System.out.println("Welcome! What is your name?"); String name = in.nextLine(); if ("".equalsIgnoreCase(name)) { System.out.println("Really? A blank answer..."); beginning(); } else { System.out.println("Hello, " + name + "! Welcome to my Java Calculator!"); System.out.println("With this Calculator you can Add, Subtract, Multiply, or Divide!"); again(); } } public static void again() { Scanner in = new Scanner(System.in); System.out.println("Enter the Mathmatical operator you would like to use. (+, -, *, or /)"); String tool = in.next(); if ("+".equalsIgnoreCase(tool)) { System.out.println("You have chosen Addition."); System.out.println("What two numbers would you like to add?"); System.out.println("Number One?"); int numb1 = in.nextInt(); System.out.println("Number Two?"); int numb2 = in.nextInt(); float sum = numb1 + numb2; System.out.println("Those two numbers added together equal " + sum + "."); System.out.println("Thank you for usinhg Laon8orsSon's Four Tool Calculator!"); again(); } if ("-".equalsIgnoreCase(tool)) { System.out.println("You have chosen Subtraction."); System.out.println("What two numbers would you like to subtract?"); System.out.println("Number One?"); int numb1 = in.nextInt(); System.out.println("Number Two?"); int numb2 = in.nextInt(); float dif = numb1 - numb2; System.out.println("Those two numbers subtracted from eachother equal " + dif + "."); System.out.println("Thank you for using Laon8orsSon's Four Tool Calculator!"); again(); } if ("*".equalsIgnoreCase(tool)) { System.out.println("You have chosen Multiplication."); System.out.println("What two numbers would you like to multiply?"); System.out.println("Number One?"); int numb1 = in.nextInt(); System.out.println("Number Two?"); int numb2 = in.nextInt(); float pro = numb1 * numb2; System.out.println("Those two numbers multiplyed together equal " + pro + "."); System.out.println("Thank you for using Laon8orsSon's Four Tool Calculator!"); again(); } if ("/".equalsIgnoreCase(tool)) { System.out.println("You have chosen Division."); System.out.println("What two numbers would you like to divide?"); System.out.println("Number One?"); int numb1 = in.nextInt(); System.out.println("Number Two?"); int numb2 = in.nextInt(); float div = numb1 / numb2; System.out.println("Those two numbers divided by eachother equal " + div +". This doesn't show remainders if any."); System.out.println("Thank you for using Laon8orsSon's Four Tool Calculator!"); again(); } else { System.out.println("That is not a valid Mathmatical operator!"); System.out.println("Try again!"); again(); } } }
- 05-29-2012, 08:29 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: My First Useful Java Program!
There's a sneaky bug in your new code: both the beginning() method and the again() method call themself; e.g.
That is called unconditional recursion and if a method calls itself often enough you get a stack overflow error. In Java a method can call itself a couple of million times so most likely you won't see the error but the possibility is still there. Better turn that construct into a loop structure.Java Code:public void again() { ... again(); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-29-2012, 04:31 PM #8
Re: My First Useful Java Program!
@Laon8orsSon Good work! I remember the first time I wrote a program from scratch for myself, and the feeling I got when it worked and did something useful for the first time... It was was that feeling that made me continue on to get my Master's in computer science and eventually my job as a software engineer!
Keep at it!
- 05-29-2012, 04:55 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: My First Useful Java Program!
When I ran my first program, I immediately lost faith in the university; a TA had told us that we should number all punch cards in position 73-80 (or whatever) and the computer would sort them for us; I duly typed all those numbers on those cards: 1,2,3,4 ... I shuffled them, put some JCL (Job Control Language) cards in front of them and fed the entire pile to the machine; only to see all possible error messages on a line printer ... from that day on I hated computers.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Call one Java Program from another Java Program
By rajpalparyani in forum New To JavaReplies: 3Last Post: 02-14-2011, 04:13 AM -
Is There A Way To Call Another Java Program Within A Java Program
By SwissR in forum New To JavaReplies: 4Last Post: 07-30-2010, 12:25 PM -
execute java program within java program
By popey in forum New To JavaReplies: 2Last Post: 10-22-2009, 05:32 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


5Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks