Lab 3a - DAA Lab Programs: (Design and Analysis of Algorithm)
3a) Write a Java program to read two integers a and b. Compute a/b and print, when b is not zero. Raise an exception when b is equal to zero.
Program:
import java.util.; class MyException extends Exception { public String toString() { return “Denominator is 0! divide by zero Exception”; } } class Compute { private int a, b; public Compute(int a, int b) { this.a=a; this.b=b; } public void compute_a_by_b() { try { if(this.b!=0) { System.out.println(“result of “+a+”/“+b+”=”+(float)1.0a/b); } else throw new MyException(); } catch(MyException e) { System.out.println(“Exception !!!!! :<br />“+e); } } } class Lab3A { public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.println(“Enter the value of a:-”); int a=in.nextInt(); System.out.println(“Enter the value of b:-”); int b=in.nextInt(); Compute c=new Compute(a, b); c.compute_a_by_b(); } }
Content
2.8 -> Hello everyone in this session we'll discuss about
10.51 -> Third program of design and analysis of algorithm laboratory
14.769 -> That is our program to read two integers a and we compute a by B a
20.74 -> Divided by B and print when B is not 0 lies an exception when B is equal to 0
27.22 -> Yes, so here
30.58 -> Execution starts from main method this that is mean here. We have main class lab 3a is the main class so before
40.12 -> We what is that timon to the programming part you need to know exception handling? What is the exception handling?
49 -> So if say for example, okay, if I take this example only here we have what is the
54.46 -> Program first compute a by B. If a is equal to 20 and B is equal to 5. What is the answer it is?
62.32 -> Right, so you should know ok, being a programmer. You should know what
68.77 -> end-user
71.08 -> Might enter ok, for example
74.619 -> if the end user
77.5 -> End user can enter any value right so he can enter 0
82.84 -> minus 1 minus 2 various different different values, but
86.77 -> Here B is what if B is equal to if end user enters B
91 -> As 0 ok if end user enters B is equal to 0
94.75 -> Then what happens it's a it is an error right divided by zero error exception
99.88 -> so denominator is 0 that error has to be displayed so
104.11 -> That is why we are you we use exception handling to by the end user to correct his or her mistake
112.45 -> Okay, so exception handling or also called error handling
117.939 -> exception handling is used to
119.95 -> Tell the end user to correct the mistake
123.52 -> Okay - correct mistake and what mistake has done and how to fix that error?
129.25 -> Ok, you had to tell the end user
131.86 -> So that so that is how that is. Very exception handling comes into picture. Yes X here
139.05 -> So lab 3 a class and main execution starts from in with it
143.21 -> This naphtha is a main plus and a scanner class scanner class used to accept the input from the keyboard
149.93 -> I have used if I have you
151.65 -> Why do you why did we create this object of scanner class?
154.37 -> Because we are using an extinct method non-static next ring method or this mixing method is non-static method of scanner class
160.68 -> So this is the object in object to his use access the non-static method next thing right?
167.12 -> And so this world and this is what will happen. I enter the value of a so it waits for this
172.92 -> Instruction pointer is pointing to this particular life
176.25 -> Of the code then what happens it will wait for user input
179.04 -> So if you enter 20 that 20 gets stored in variable E, which is declared as int this Andolini
186.03 -> So you cannot enter 20.3 or decimal points and at all because it is declared an integer
192.78 -> We had entry increase your value of u so here is system. I enter the value of B. Yes
198.63 -> So in B is equal to in a clinic wait for a user input then
203.79 -> B if B is equal to 10 so 10 is what integer so compute C is equal to new compute
209.69 -> So compute is not a simple class. It is user defined class here. We are passing to two parameters, right?
215.31 -> so now here what is what are the parameters twenty and ten are the parameters we are passing so
222.51 -> As soon as you create the object construct a block get executed so which is construct a block here
228.18 -> Here we have construct a block. Okay, so constructor as soon as you create the object constructor block gets executed
236.76 -> So here we have construct a block. Yes. So we have created the object or instance of compute class file
243.92 -> so then this block gets executed and this constructor is of
248.64 -> Is declared as public means what you can access this computer?
253.23 -> Constructor block outside the class also outside the compute class. So this dot a is equal to
261.12 -> 20 so since we have 2a
263.88 -> Right here
264.68 -> We have a and here also we have eight that is why
267.97 -> So we are using a disk even means what this is an instance or object of compute class used to access private
276.34 -> members of compute class
278.59 -> This is this keyword is used to access private
282.73 -> variables of
284.38 -> compute class
286.03 -> so a is equal to 20 and B is equal to 10 this 20 is assigned to
292.21 -> this dot e that is this one this e and
296.47 -> B is equal to 10
298.78 -> B is equal to 10 it is assigned to this dot B, that is private variable
304.69 -> Okay. So this is what a is equal to 20 and B is equal to 10 after that
309.49 -> Once this console construct a block gets executed again. It will execute the next instruction
314.32 -> That is C dot compute a by B compute. An a by B is what it is a
320.56 -> non-static method of
322.81 -> Compute class. Ok, since it is a non-static method you had to access you can access this
330.04 -> Compute a by B method using object of compute class, which is C
334.48 -> So your via we had you see dot dot operator as C dot compute a by B, right?
341.23 -> So now where is that compute a by B?
343.86 -> It's here your public it is declared as public and it is off-white typewriter type is one because we are not returning any
350.47 -> Values you right?
352.06 -> Then now now we have try and catch so try and catch this so
358.21 -> Then means what if we have cation charge keywords in our program
362.67 -> Then it is a we are using exception handling concepts
365.86 -> So what is a try and catch then in this try block in this type lock inside this tie block?
373.03 -> You can perform
375.1 -> valid computation
377.56 -> Whatever it is mathematical or logical computations. You can perform computations inside try block if
385.39 -> if you want to tell the end user to
389.32 -> Correct his or her mistake then?
392.38 -> type it inside catch block
395.229 -> okay, so that
397.24 -> The end user can correct the mystic and also he can fix three era
402.81 -> So next
403.52 -> What is that?
404.34 -> Error now that in this example B is equal to zero right B is equal to zero it is a divided by zero then
410.49 -> Display that error message inside cache block. That's it. So tries to perform computation of computation
417 -> purpose computational purpose and catch C is to display thee or tell the end user to
422.46 -> Fix the error to correct the error or to LD
426.719 -> Tell what error or what is that error? Okay
430.379 -> So now what is the first we'll see try block if this dot B. Okay, that is this
435.8 -> Don't worry why we are using this keyword here because we are accessing a non-static method B off that is private
443.31 -> Variable off computer class since it is a non-static variable they say and we are non-static variable
449.069 -> You have to use this Keeper this dot B
453 -> Object this is what object or control class. This dot B is not equal to 0
457.279 -> So what is the what pay of enter 10 right so B is equal to 10, so it's not equal to 0 there
462.529 -> So it is quote system dot out dot print it will perform
465.779 -> Operation division operation at this float here. We are using or typecasting because you find because a is also integer B is also integer
473.099 -> So the result will be integer. Right? So for example, 25 10 is what - it is not a distance
479.15 -> We don't have any decimal point. What is me if I enter 20 divided by?
484.5 -> 3 it is will get decimal points right obviously will get decimal points. So that is why
491.49 -> We use what are typecasting here float off you have a keep it inside of the bracket and into 1 point 0
499.349 -> Into and multiply it with the result a by B, right?
503.61 -> So to get the decimal points to retain the decimal points, so it's all about try block s else and yes row
510.539 -> Ok for B is not equal to 0 if denominator is 0 then
516.57 -> display
517.709 -> The error message means what you had to guide the end user to correct?
522.289 -> the mistake is throw is the keyword use the keyword row is
527.79 -> new my exception you are using words that create the
532.8 -> Instance of my exception class new my exception what happens?
538.44 -> So this particular?
540.66 -> Block, where is that my exception class here. We have my exception class. This block gets executed
546.24 -> So my exception is a user-defined class. It is not in built class. It is user defined class
552.2 -> You can give any name for this here
554.36 -> We have used my exception and extends inheritance concept right offense exception. This is in built class
562.92 -> Exception is in built class and my exception is not in Bill Josh's user-defined class and in this my exception class
568.88 -> We have two string method
572.01 -> This two string method. We are wore riding. We are overriding two string method
578.73 -> Which belongs to this two string method belongs to?
583.44 -> exception class
584.91 -> Ok. So since we are using we are overriding this two string method we have to use inheritance
592.35 -> Concept that is exchange space exception
595.68 -> So this two string is declared as drink because we are returning what is the return type here denominator is zero divided by zero
604.14 -> Its divided by zero exception, correct?
606.32 -> It is a it is declared as public even if you don't declare it as public you get the output
610.86 -> Okay, so you can access this constructor. This is what now, it is a constructor overriding so we are using two string method
619.8 -> means what overriding be method of
624.42 -> Exception class. Okay. So now what happens so now make use of this my exception class
631.11 -> inside catch parentheses in this catch parentheses
634.82 -> We have knew we had to that name and this seemed to be seen okay?
638.18 -> my exception my exceptions and last name and this thing should be safe space e
643.17 -> This is what exception error this export exception error. So in this we are printing the error message
650.24 -> What is that message this particular law gets executed. What is that return?
655.05 -> Denominator or a 0/0 exception so this block gets executed
660.42 -> this
662.31 -> so
663.51 -> This block gets executed so it will display the message. So let us execute this program
669.18 -> so first
671.43 -> Okay, here we are using. Okay, sorry
675.24 -> lab 3 e3
677.459 -> and
679.95 -> Let me use
682.86 -> 2 string right, so let me run this program
687.959 -> Into value of enter the value of P 20 and 10 a result when 20 by 10, which is 2 point 0 we are getting
694.649 -> 2.0 now, yes. So what if I remove this float, as I said, I will show you the typecasting also this typecasting
703.05 -> Here this float
705.63 -> 1.0. Right. I will just remove this part
708.66 -> okay, I
710.88 -> Have removed this typecasting now. We will see point 0 will not it will not be shown
716.76 -> so
719.01 -> And 20 and so now it is showing only 2 so if it is 20 by
726.269 -> 20 by 3 so answer is 6 actual answer is what six point two six six six
732.089 -> Alright, but here we are getting 6 only because it is a little what is that?
738.36 -> That point decimal points will be truncated
741.3 -> So that is why we use a float keep it inside the pants design 1.0 and multiplied with the result
747.36 -> so now it will show the decimal points save this and I'll drag that space so 20 and
754.649 -> Answer is six point six six six six, right?
757.19 -> So that is why we use this one and one more thing here as I said exception handling
763.26 -> This one is not required. Yeah
767.16 -> So we have you however have not used here. So in this here we have used exception, right?
774 -> you can even if you don't use that, I
776.73 -> don't affect the
778.98 -> output, okay
781.98 -> Yes, now let me use this it will just display the message that's it
786.48 -> That exception message
788.25 -> So 20 and 0 so exception denominator is 0 divided by 0
795.51 -> exception, right
797.91 -> Put error handling exception handling
800.37 -> So if exception handling means water make use of try block and catch block and also through keyword. So try
807.87 -> catch and throw keyword
810.3 -> This this is what user defined exception means what user means what user exception?
816.48 -> So there are so many inbuilt exceptions. Also you can even make use of those
821.879 -> built-in exceptions if you want so it's all about
826.949 -> Program three a hope you have understood if you have any doubt, please comment below. Thank you so much for watching