Results 1 to 9 of 9
Like Tree5Likes
  • 3 Post By Laon8orsSon
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: My First Useful Java Program!

  1. #1
    Laon8orsSon is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Talking 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!");
    	}
    
    }
    Zigster, pbrockway2 and JosAH like this.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Laon8orsSon is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: My First Useful Java Program!

    Quote Originally Posted by pbrockway2 View Post
    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?
    Because thats what I learned to do.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: My First Useful Java Program!

    Quote Originally Posted by Laon8orsSon View Post
    Because thats what I learned to do.
    Just think about it: should the sum or difference of two int numbers be a floating point number? For division there could be a point depending on how you define 'division', but for a sum and difference, nah ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Laon8orsSon is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

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

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default 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.

    Java Code:
    public void again() {
       ...
       again();
    }
    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.

    kind regards,

    Jos
    quad64bit likes this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    quad64bit likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •