我正在尝试通过使用数据抽象和封装来实现一个用于执行基本计算的小程序。 但每个案例的输出都是零。 任何人都可以找到我在执行守则时错误的错误。
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 :(得分:3)
要解决您的问题,您可能需要替换
new Algebra(number1,number2);
与
d = new Algebra(number1,number2);
在您的代码中,您的对象d是默认值,number1和number2设置为零。
您可能还想格式化代码并考虑看起来不太合适的接口层次结构
答案 1 :(得分:0)
我在接收Peers的宝贵建议时格式化了代码。 最终代码如下。
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;
}
}
}