0

Is it better to put the switch to some method of Calc class or is it ok in the main method?

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); Calc calc = new Calc(); System.out.println("Welcome\nAdd first number:"); int a = sc.nextInt(); Systen.out.println("Niw the second number"); int b = sc.nextInt(); calc.getNumbers(a,b); System.out.printl("What would you like to do?\n1=+\n2=-\n3=÷\n4=×"); int op = sc.nextInt(); switch (op){ //Is it OK to put this switch in to main method, or should i create a new method for this in the Calc class? case (1): System.out.println(calc.sum()); break; case (2): System.out.println(calc.minus()); break; case(3): System.out.println(calc.div()); break; case(4) : System.out.println(calc.mul()); break; default: System.out.println("wrong option"); }} } class Calc { private int a; private int b; public void getNumbers(int a, int b) { this.a=a; this.b=b; } public int sum(){ return a+b; } public int minus(){ return a-b; } public int div(){ if (b>0) return a/b; else System.out.println("you cant divide by zero"); return 0; } public int mul(){ return a*b; } }

7th Aug 2017, 8:29 PM
Víťa Dvořák
Víťa Dvořák - avatar
2 Respostas
+ 4
1. the getNumbers should be setNumbers instead 2. create a new method for the switch in the Calc class to reduce cluttering your main Program class.
8th Aug 2017, 2:08 AM
Benneth Yankey
Benneth Yankey - avatar
+ 1
I think it's good as you did... "Program" is like a menu and I prefer this.
7th Aug 2017, 9:48 PM
Niar
Niar - avatar