Define a class Calculation as follows: [5]
Data Member/ Instance Variables: N (integer type) and W, res1, res2 and res3 (all
double
Member Functions/Methods:
void Accept() : Input values of N and W from the user
void Process(): To find the N raised to the power 3 and square of W, then store
the results in variables res1 and res2. Also find the sum of results in res1 and res2
and store in variable res3.
void Show(): to print the values of N, W, res1, res2 and res3 as the output
Write a main () function to create suitable object of the class and call the methods
to print he result.
Answers
Answered by
6
Program :
import java.util.*;
public class MyClass {
int N ;
double W , res1 , res2 , res3;
void Accept()
{
Scanner Sc = new Scanner(System.in);
System.out.print("Enter a number : ");
N = Sc.nextInt();
System.out.print("Enter a number : ");
W = Sc.nextDouble();
}
void Process()
{
res1 = N * N * N;
res2 = W * W;
res3 = res1 + res2;
}
void Show()
{
System.out.println(N);
System.out.println(W);
System.out.println(res1);
System.out.println(res2);
System.out.println(res3);
}
public static void main(String args[])
{
MyClass m = new MyClass();
m.Accept();
m.Process();
m.Show();
}
}
Similar questions