Answer:
#include <iostream>
using namespace std;
class Digits
{
public:
int num;
int read() //method to read num from user
{
cout<<"Enter number(>0)\n";
cin>>num;
return num;
}
int digit_count(int num) //method to count number of digits of num
{
int count=0;
while(num>0) //loop till num>0
{
num/=10;
count++; //counter which counts number of digits
}
return count;
}
int countDigits(int num) //method to return remainder
{
int c=digit_count(num); //calls method inside method
return num%c;
}
};
int main()
{
Digits d; //object of class Digits is created
int number=d.read(); //num is read from user
cout<<"\nRemainder is: "<<d.countDigits(number); //used to find remainder
return 0;
}
Output:
Enter number(>0)
343
Remainder is: 1
Explanation:
The program has a logical error that needs rectification. A correctly structured program calculates the remainder when a number is divided by the count of its digits. A class named Digits is created, consisting of the public variable 'num' and methods for reading input, counting digits, and calculating the remainder.
- read() - This function asks the user to enter the value for 'num' and returns it.
- digit_count() - This function accepts an integer and counts how many digits it has, incrementing a counter until 'num' is less than or equal to 0. It ultimately returns the digit count.
- countDigits() - This function takes an integer and delivers the remainder from dividing that number by its digit count. The digit count is computed using the 'digit_count()' method.
Finally, in the main function, a Digits object is instantiated, and its methods are utilized to produce an output.
To serialize an object, the following code can be used: FileOutputStream out = new FileOutputStream("ObjectData.dat"); ObjectOutputStream ostream = new ObjectOutputStream(out); ostream.writeObject(r); Explanation: For serializing an object, the writeObject method from the java.io.ObjectOutputStream class is utilized. The complete code snippet is as follows: import java.io.*; class Demo{ public static void main(String args[]){ try{ r = <reference to="" object="" be="" serialized="">; FileOutputStream out = new FileOutputStream("ObjectData.dat"); ObjectOutputStream ostream = new ObjectOutputStream(out); ostream.writeObject(r); ostream.close(); } catch(Exception e){ e.printStackTrace(); }} }.
</reference>
Answer:
def average_strings(lst):
total_length = 0
avg = 0
for s in lst:
total_length += len(s)
avg = total_length/len(lst)
return avg
Explanation:
Define a function named average_strings that takes a single argument, lst
Initialize both total length and avg variables
Utilize a for loop to iterate through lst, accumulating the length of each string into total length
Once the loop ends, compute the average by dividing the total length by the number of elements in lst
Return the computed average.
Answer:
public class PostAccount
{
public void withdraw(float savings)
{
if (savings >=0 )
{
IllegalArgumentException exception
= new IllegalArgumentException("Savings cannot be negative");
throw exception;
}
balance = savings - withdraw;
}
}
Explanation:
An IllegalArgumentException is categorized as a NumberFormatException of runtime exceptions. The code snippet demonstrates an instance of creating an IllegalArgumentException object, which is subsequently raised to indicate when the condition specified is not fulfilled.