Write a program to create a class Force with the data members / instance variables M (to store mass of an object) and A (to store the acceleration). Define a method void Input( int ma, int ac) to assign parameters 'ma' to M and 'ac' to A. Another method double calculate( ) to calculate the force value after doing the calculation( force = Mass x acceleration) and return force value. Also define main method to accept the mass value of an object and acceleration value. Create suitable object of the class and call the functions/ methods to print the output
Answers
#include<iostream.h>
#include<conio.h>
class Force
{
float M;
float A;
public:
void Input(int ma,int ac)
{
M=ma;
A=ac;
}
double calculate ( )
{
return MxA;
}
};
void main( )
{
clrscr();
Force obj1;
obj1.Input(2,3);
cout<<"Force="<<obj1.calculate();
getch();
}
_______________________________
verified answer✔️
happy coding :)
Answer:
please see the sample java code below.
Explanation:
import java.util.*;
class Force{
private int M;
private int A;
public static void main(){
Scanner sc = new Scanner (System.in);
System.out.print("Enter mass ");
int m = sc.nextInt();
System.out.print("Enter acceleration");
int a = sc.nextInt();
this.input(m,a);
int f = this.calculate();
System.out.println(" Force ="+f);
}
/*
* set input values
*/
function void input(int ma, int ac){
this.M = ma;
this.A = ac;
}
/*
*calculate force
*/
function double calculate(){
return this.M*this.A;
}
}