Just made my first Java program without a tutorial
I am pretty happy with it :D: 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
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? :(happy):) one interesting aspect is that it uses recursion as part of its logic here:
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!
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