A sentimental robot

Object oriented style exercise 본문

Java

Object oriented style exercise

GOD03219 2017. 12. 28. 14:07

import java.util.Scanner;

public class Hello2 {

 int num1, num2;
 char a;

 

 public void setNum1(int x) {
  num1 = x;
 }

 public void setOp(char r) {
  a = r;
 }

 public void setNum2(int y) {
  num2 = y;
 }

 public int getNum1() {
  return num1;
 }

 public char getOp() {

  return a;

 }

 public int getNum2() {
  return num2;
 }

 public int getRes() {
  int result=0;
  switch (a) {

  case '+':
   result = (num1 + num2);
   break;
  case '-':
   result = (num1 - num2);
   break;
  case '*':
   result = (num1 * num2);
   break;
  case '/':
   result = (num1 / num2);
   break;
  default:
   System.out.print("You put wrong item");
   break;
  }
  return result;

 }

 public static void main(String[] ar) {
  char ans;
  do
  {
  Scanner sc = new Scanner(System.in);

  Hello2 ss = new Hello2();

 

  System.out.print("input number : ");
  ss.setNum1(sc.nextInt());


  System.out.print("+,*,-,/ : ");
  ss.setOp(sc.next().charAt(0));


  System.out.print("input number : ");
  ss.setNum2(sc.nextInt());


  // print
  System.out.print(ss.getNum1());
  System.out.print(ss.getOp());
  System.out.print(ss.getNum2()+"=");
  System.out.println(ss.getRes());
  System.out.print("Continue? If you want, put Y or y:");
  ans=sc.next().charAt(0);


  }while(ans=='y'||ans=='Y');


 }
  
}

'Java' 카테고리의 다른 글

Has~a  (0) 2017.12.28
This  (0) 2017.12.28
Object-oriented style score management  (0) 2017.12.28
The elements of "Class"  (0) 2017.12.28
Score Management[Three dimensional]  (0) 2017.12.28