Saturday, 19 November 2022

Methods - Assignment 3

 

Methods - Assignment 3

Implement a class Calculator with the instance variable and method mentioned below. 

Method Description

sumOfDigits()

  • Calculate and return the sum of the digits of the num member variable

Test the functionalities using the provided Tester class. 

 

Sample Input and Output

class Calculator{
public int num;
public int sumOfDigits()
{
int a=num,rem=0,sum=0;
while(a!=0)
{
rem=a%10;
a=a/10;
sum=sum+rem;
}
return sum;
}
}
class Tester{
public static void main(String args[]){
     Calculator calculator=new Calculator();
calculator.num=123;
int x=calculator.sumOfDigits();
System.out.println(x);
//AssignavaluetothemembervariablenumofCalculatorclass//InvokethemethodsumOfDigitsofCalculatorclassanddisplaytheoutput}
}
}





No comments:

Post a Comment

Free Resume Template

Creating a blog post that discusses the pros and cons of using a resume rule is a great way to provide valuable information to job seekers ...