Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Task #1 Practice: Refactor code that is all ‘stuffed’ into main()
- // Define and call at least 2 subroutines that would help the program to be more understandable and re-usable.
- // They should be MEANINGFUL not just like public static void printTitle() { System.out.println("**Simple Calculator**"); }
- // They should produce (almost) the same output and behavior* as the original, 'stuffed main()' version
- // *There is ONE boo-boo in the calculations...find it and fix it.
- public class SimpleCalculator {
- public static void main(String[] args) {
- System.out.println("**Simple Calculator**");
- System.out.println("Here are your options:");
- System.out.println("1. Addition");
- System.out.println("2. Subtraction");
- System.out.println("3. Division");
- System.out.println("4. Multiplication");
- int choice;
- do {
- System.out.print("What would you like to do?: ");
- choice = TextIO.getlnInt();
- System.out.println();
- } while(choice != 1 && choice != 2 && choice != 3 && choice != 4);
- if (choice == 1){
- int numOne, numTwo;
- System.out.print("First Number: ");
- numOne = TextIO.getlnInt();
- System.out.print("Second Number: ");
- numTwo = TextIO.getlnInt();
- System.out.println("Result: " + numOne + " + " + numTwo + " = " + (numOne + numTwo));
- }
- else if (choice == 2){
- int numOne, numTwo;
- System.out.print("First Number: ");
- numOne = TextIO.getlnInt();
- System.out.print("Second Number: ");
- numTwo = TextIO.getlnInt();
- System.out.println("Result: " + numOne + " - " + numTwo + " = " + (numOne - numTwo));
- }
- else if (choice == 3){
- int numOne, numTwo;
- System.out.print("First Number: ");
- numOne = TextIO.getlnInt();
- System.out.print("Second Number: ");
- numTwo = TextIO.getlnInt();
- System.out.println("Result: " + numOne + " / " + numTwo + " = " + (numOne / numTwo));
- }
- else if (choice == 4){
- int numOne, numTwo;
- System.out.print("First Number: ");
- numOne = TextIO.getlnInt();
- System.out.print("Second Number: ");
- numTwo = TextIO.getlnInt();
- System.out.println("Result: " + numOne + " x " + numTwo + " = " + (numOne * numTwo));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement