Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming 1) a) Write a java program int sum (int n), which returns the s

ID: 3590580 • Letter: J

Question

Java programming 1) a) Write a java program int sum (int n), which returns the sum of all the digits to the integer n (ie transverse n) For example, if the sum is called by some (234), it returns 9 (= 2 + 3 + 4) . Note that if n is an integer, then n% 10 returns its last digit (234% 10 is 4) and n / 10 is cutting its last digit (234/10 is 23). You can assume that n is not negative. Also write a short main case that tests the case. b)Write a java program int (threeheads), which throws up a coin until heads comes up three times in a row. The function returns the number of throws needed to get this result. You use Math.random () to simulate the coin flips. Also, give a main-function that calls for your function 100,000 times and returns the average number of throws. Show a screenshot of one run. Please write the full code

Explanation / Answer

ANS A)

import java.util.*;

import java.lang.*;

import java.io.*;

class Ideone

{

               public static int sum(int n)

               {

                              int sum=0;

                              int r=0;

                              while(n!=0)

                              {

                                             r=n%10;

                                             sum=sum+r;

                                             n=n/10;

                              }

                              return sum;

               }

               public static void main (String[] args) throws java.lang.Exception

               {

                                             int ans=sum(234);

                                             System.out.println("Result = "+ans);

               }

}

ANS B)

import java.util.*;

import java.lang.*;

import java.io.*;

class Ideone

{

               public static int randomWithRange(int min, int max)

               {

                              int range = (max - min) + 1;    

                              return (int)(Math.random() * range) + min;

               }

               public static int threeheads()

               {

                              //Here 1 - Head 0 -Tail

                              int cons=0;

                              int ans=0;

                              while(cons!=3)

                              {

                                             ans++;

                                             int coin=randomWithRange(0,1);

                                             if(coin==1)//HEAD

                                             cons++;

                                             else if(coin==0)//TAIL

                                             cons=0;

                              }

                              return ans;

               }

               public static void main (String[] args) throws java.lang.Exception

               {

                              int res;

                              int sum=0;

                              for(int i=0;i<100000;i++)

                              {

                                             res=threeheads();

                                             sum=sum+res;

                              }

                              double avg=0.0;

                              avg=(double)sum/100000.0;

                              System.out.println("AVERAGE "+avg);

               }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote