2017-06-26 170 views
0

我想通過使用數據抽象和封裝來實現一個小程序來進行基本計算。 但每種情況下的輸出產生零。 任何人都可以找到我在執行代碼時出錯的錯誤。數據抽象和封裝

import java.util.Scanner; 
interface Sum 
{ 
    int sum(); 
} 
interface Sub extends Sum 
{ 
    int sub(); 
} 
    interface Mul extends Sub 
    { 
     int mul(); 
    } 
     interface Div extends Mul 
     { 
      int div(); 
     } 
    class Algebra implements Sum,Mul,Sub,Div 
    { 
    private int number1,number2; 
Algebra() 
{ 
    number1=0; 
    number2=0; 
} 
    Algebra(int number1,int number2) 
    { 
     this.number1=number1; 
     this.number2=number2; 
    } 

     public int sum() 
     { 
      return number1+number2; 
     } 
     public int mul() 
     { 
      return number1*number2; 
     } 
     public int div() 
     { 
      return number1/number2; 
     } 
     public int sub() 
     { 
      return number1-number2; 
     } 
} 
class Test 
{ 
public static void main(String [] args) 
{ 
    int number1,number2; 
    char choice; 
    Scanner input=new Scanner(System.in); 
    Algebra A=new Algebra(); 
    Div d; 
    d=(Algebra)A; 
    System.out.println("Enter the number 1 : "); 
    number1=input.nextInt(); 
    System.out.println("Enter the numbre 2 : "); 
    number2=input.nextInt(); 
    System.out.println("Enter choice : 1.)Sum 2.)Mul 3.)Div 4.)Sub "); 
    choice=input.next().charAt(0); 
    d=new Algebra(number1,number2); 
     switch(choice) 
     { 
      case '1': 
      System.out.println("Sum is : "+d.sum()); 
      break; 
      case '2': 
      System.out.println("Mul is : "+d.mul()); 
      break; 
      case '3': 
      System.out.println("Div is : "+d.div()); 
      break; 
      case '4': 
      System.out.println("Sub us : "+d.sub()); 
      break; 
     } 
} 
} 

對於每種情況,輸出爲零。

+0

格式化您的代碼屬性 –

+0

爲什麼您的操作符彼此繼承?你不能只有一個'運營商'接口和Mul/Sub/Div等的實現嗎? – byxor

+1

您正在創建2個代數對象'代數A =新代數(); d =(代數)A;''和'新代數(number1,number2);'你將參數傳遞給1個對象,然後用另一個來調用計算方法。這就像在一臺計算器上輸入數據,然後到另一臺計算器並按等號。它不會明顯工作。 –

回答

0

我格式化代碼從同級接收寶貴的建議非常有用。最終代碼如下。

import java.util.Scanner; 
/*Data Abstraction Using interface */ 
interface Operation 
{ 
int sum(); 
int mul(); 
int sub(); 
int div(); 
} 

/* Or Using Abstract Class 

abstract class Operation 
{ 
    abstract int sum(); 
    abstract int mul(); 
    abstract int sub(); 
    abstract int div(); 
} 
class Algebra extends Operation (Inheritance) 
{ 
    code.. 
} 
*/ 

class Algebra implements Operation 
{ 
private int number1,number2; 
Algebra() 
{ 
    number1=0; 
    number2=0; 
} 
    Algebra(int number1,int number2) 
    { 
     this.number1=number1; 
     this.number2=number2; 
    } 

    /* Encapsulation of the Data Memebers and Functions*/ 
     public int sum() 
     { 
      return number1+number2; 
     } 
     public int mul() 
     { 
      return number1*number2; 
     } 
     public int div() 
     { 
      return number1/number2; 
     } 
     public int sub() 
     { 
      return number1-number2; 
     } 
} 
class Test 
{ 
public static void main(String [] args) 
{ 
    int number1,number2; 
    char choice; 
    Scanner input=new Scanner(System.in); 
    Algebra A=new Algebra(); //Dynamic Object of Sub Class 
    Operation Op; //Static Object 
    Op=(Algebra)A; //Type Casting of the Static Object of interface or abstract class 
    System.out.println("Enter the number 1 : "); 
    number1=input.nextInt(); 
    System.out.println("Enter the numbre 2 : "); 
    number2=input.nextInt(); 
    System.out.println("Enter choice : 1.)Sum 2.)Mul 3.)Div 4.)Sub "); 
    choice=input.next().charAt(0); 
    Op=new Algebra(number1,number2); 


     switch(choice) 
     { 
      case '1': 
      System.out.println("Sum is : "+Op.sum()); 
      break; 
      case '2': 
      System.out.println("Mul is : "+Op.mul()); 
      break; 
      case '3': 
      System.out.println("Div is : "+Op.div()); 
      break; 
      case '4': 
      System.out.println("Sub us : "+Op.sub()); 
      break; 
     } 
} 
} 
3

要解決你的問題,你可能需要更換

new Algebra(number1,number2); 

d = new Algebra(number1,number2); 

在你的代碼的對象d是默認的一個用數字1和數字2設置爲零。

您可能還需要格式化你的代碼,想想你的接口層次結構這似乎並不

+0

我剛開始接口和繼承,我不是親這個。我很快就會很好。感謝您的修復 –