Object oriented analysuis and design Homework PLEASE USE JAV

Object oriented analysuis and design Homework PLEASE USE JAVA

These are the prof slides and the homework at the end but I cant gues what he wants. I need Help

\"Think

how to write a simple program which takes two inputs from users and output the sum of the two numbers

Input (m,n);
Answer = sum(m,n);
output (Answer);

Ignore the syntax, any comments on this design?

In a multi designer/implementer environment, it may need
recompile,
relink,
reload. If we want to add functionality such as the difference, multiplication, and the division of those two numbers.

The feature in OOP called inheritance can come to help.
Declare
Class f (m,n, op);

Class f-sum extends f;
Class f-difference extends f;
Class f-multiply extends f;

Home work #1 finish what I intend to do, such as
declare
Class f-sum (m,n, op) extends f;

Better yet, use another feature in OOP, instead of inheritance, to achieve the REUSE goal\"

Solution

1. Simple program to add two numbers

import java.util.*;
class Sum
{
   public static void main (String[] args)
   {
       Scanner scan = new Scanner(System.in);    //Scanner class object taken to input two numbers
       System.out.println(\"Enter two numbers\");
       int num1 = scan.nextInt();
       int num2 = scan.nextInt();
       int sum = num1+num2;
       System.out.println(\"Sum : \"+sum);   //print sum
      
      
   }
}

output:

Inheritance

import java.util.*;
class f                           //base class
{
   protected int m,n;
   protected String op;
   public f(int m,int n,String op)
   {
       this.m = m;
       this.n = n;
       this.op = op;
   }

}
class fsum extends f                  //sum class to do addition
{
   public fsum(int m,int n)
   {
       super(m,n,\"+\");
   }
   public int sum()
   {
       return m+n;
   }
}
class fdifference extends f          //difference class to do subtraction
{
   public fdifference(int m,int n)
   {
       super(m,n,\"-\");
   }
   public int diff()
   {
       return m-n;
   }
}
class fmultiply extends f               //multiply class to do multiplication
{
   public fmultiply(int m,int n)
   {
       super(m,n,\"*\");
   }
   public int mul()
   {
       return m*n;
   }
}
class Test                                  //test class to test all operations by testing different derived class
{
   public static void main (String[] args)
   {
      
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter two numbers\");
       int num1 = scan.nextInt();
       int num2 = scan.nextInt();
      
       fsum objsum = new fsum(num1,num2);
       fdifference objdiff = new fdifference(num1,num2);
       fmultiply objmul = new fmultiply(num1,num2);
      
      
       int sum = objsum.sum();
       int diff = objdiff.diff();
       int mul = objmul.mul();
       System.out.println(\"Sum : \"+sum + \" Difference : \"+ diff +\" Product : \"+mul);
      
      
   }
}

output:

Encapsulation (All operation classes are merged into one class and encapsulation is used for code reusability)

import java.util.*;
class f
{
   private int m,n;
  
   public f(int m,int n)
   {
       this.m = m;
       this.n = n;
   }

   public int sum()
   {
       return m+n;
   }


   public int diff()
   {
       return m-n;
   }

   public int mul()
   {
       return m*n;
   }
}
class Test
{
   public static void main (String[] args)
   {
      
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter two numbers\");
       int num1 = scan.nextInt();
       int num2 = scan.nextInt();
      
       f obj = new f(num1,num2);
      
      
       int sum = obj.sum();
       int diff = obj.diff();
       int mul = obj.mul();
       System.out.println(\"Sum : \"+sum + \" Difference : \"+ diff +\" Product : \"+mul);
      
      
   }
}

output:

Object oriented analysuis and design Homework PLEASE USE JAVA These are the prof slides and the homework at the end but I cant gues what he wants. I need Help \
Object oriented analysuis and design Homework PLEASE USE JAVA These are the prof slides and the homework at the end but I cant gues what he wants. I need Help \
Object oriented analysuis and design Homework PLEASE USE JAVA These are the prof slides and the homework at the end but I cant gues what he wants. I need Help \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site