Results 1 to 3 of 3
  1. #1
    Vatec is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Thumbs up Just made my first Java program without a tutorial

    I am pretty happy with it I am currently reading Java All-In-One Desk Reference For Dummies, 2nd Edition as an eBook and I am using what I have learnt from that to create a Java program. I would really appreciate feedback on the book itself and how to improve my code. Thanks for reading

    Java Code:
    import java.util.Scanner;
    
    public class Main {
    
    	static Scanner sc = new Scanner(System.in);
    	
    	public static void main(String[] args) {
    		System.out.println("What operation do you want to do?");
    		String choice = sc.next();
    		if(choice.equalsIgnoreCase("division") || choice.equalsIgnoreCase("divide")) {
    			divide();
    		}
    		else if(choice.equalsIgnoreCase("addition") || choice.equalsIgnoreCase("add") || choice.equalsIgnoreCase("plus")) {
    			add();
    		}
    		else if(choice.equalsIgnoreCase("subtract") || choice.equalsIgnoreCase("minus") || choice.equalsIgnoreCase("subtraction")) {
    			minus();
    		}
    		else if(choice.equalsIgnoreCase("times") || choice.equalsIgnoreCase("multiply") || choice.equalsIgnoreCase("multiplication")) {
    			multiply();
    		}
    		else {
    			System.out.println("That is not an operation");
    			main(args);
    		}
    	}
    
    	public static void divide() {
    		System.out.print("Enter the first number: ");
    		int number1 = sc.nextInt();
    		System.out.print("Enter the second number: ");
    		int number2 = sc.nextInt();
    		System.out.println("I am now dividing " + number1 + " and " + number2);
    		int answer = number1 / number2;
    		int remainder = number1 % number2;
    		System.out.println(number1 + " divided by " + number2 + " = " + answer + " with a remainder of " + remainder);	
    	}
    	
    	public static void add() {
    		System.out.print("Enter the first number: ");
    		int addNumber1 = sc.nextInt();
    		System.out.print("Enter the second number: ");
    		int addNumber2 = sc.nextInt();
    		System.out.println("I am now adding " + addNumber1 + " and " + addNumber2);
    		int addAnswer = addNumber1 + addNumber2;
    		System.out.println(addNumber1 + " + " + addNumber2 + " = " + addAnswer);	
    		}
    	
    	public static void minus() {
    		System.out.print("Enter the first number: ");
    		int minusNumber1 = sc.nextInt();
    		System.out.print("Enter the second number: ");
    		int minusNumber2 = sc.nextInt();
    		System.out.println("I am now subtracting " + minusNumber1 + " and " + minusNumber2);
    		int minusAnswer = minusNumber1 + minusNumber2;
    		System.out.println(minusNumber1 + " - " + minusNumber2 + " = " + minusAnswer);	
    		}
    	
    	public static void multiply() {
    		System.out.print("Enter the first number: ");
    		int multiplyNumber1 = sc.nextInt();
    		System.out.print("Enter the second number: ");
    		int multiplyNumber2 = sc.nextInt();
    		System.out.println("I am now multiplying " + multiplyNumber1 + " and " + multiplyNumber2);
    		int multiplyAnswer = multiplyNumber1 * multiplyNumber2;
    		System.out.println(multiplyNumber1 + " * " + multiplyNumber2 + " = " + multiplyAnswer);	
    		}
    
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Just made my first Java program without a tutorial

    Your code looks good, but (and there always has to be a but doesn't there? ) one interesting aspect is that it uses recursion as part of its logic here:

    Java Code:
            else {
                System.out.println("That is not an operation");
                main(args); // main method gets repeated in a recursive way
            }
    I'm just curious why you chose to repeat code this way and not via a while loop?

    Oh, and welcome to the java-forums.org! Look forward to seeing you around here!

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

    Default Re: Just made my first Java program without a tutorial

    Just nitpicking: I see quite a bit of code duplication when you want the user to supply two operands for the operation. Challenge: design an interface Operator that asks for two numbers, applies the operator and displays the result. You need an implementation for each operator you want to use. Maybe an abstract superclass can be used to implement the part that asks the user for the two operands (it's the same for all the operators anyway).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Use a dll made in .net (C#) COM Visible within JAVA
    By ealopez26 in forum Advanced Java
    Replies: 6
    Last Post: 09-16-2011, 04:46 AM
  2. Replies: 2
    Last Post: 06-30-2010, 07:19 AM
  3. Sun java tutorial in pdf?
    By makpandian in forum New To Java
    Replies: 2
    Last Post: 06-29-2009, 09:25 PM
  4. Replies: 2
    Last Post: 03-22-2009, 06:43 PM
  5. Java 5 Tutorial
    By Ganeshag777 in forum New To Java
    Replies: 1
    Last Post: 08-18-2008, 06:38 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
  •