Results 1 to 3 of 3
- 10-22-2011, 12:11 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
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); } }
-
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:
I'm just curious why you chose to repeat code this way and not via a while loop?Java Code:else { System.out.println("That is not an operation"); main(args); // main method gets repeated in a recursive way }
Oh, and welcome to the java-forums.org! Look forward to seeing you around here!
- 10-22-2011, 03:29 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Use a dll made in .net (C#) COM Visible within JAVA
By ealopez26 in forum Advanced JavaReplies: 6Last Post: 09-16-2011, 04:46 AM -
How do I run a java program that I made off of my desktop?
By Chris Rice in forum New To JavaReplies: 2Last Post: 06-30-2010, 07:19 AM -
Sun java tutorial in pdf?
By makpandian in forum New To JavaReplies: 2Last Post: 06-29-2009, 09:25 PM -
how to run my java program made in netbeans to another PC?
By kwink in forum AWT / SwingReplies: 2Last Post: 03-22-2009, 06:43 PM -
Java 5 Tutorial
By Ganeshag777 in forum New To JavaReplies: 1Last Post: 08-18-2008, 06:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks