Question: please explain each line of code :-) public class Driver { public stat
ID: 3885413 • Letter: Q
Question
Question: please explain each line of code :-)
public class Driver { public static void main(String[] args) { scanner in = new Scanner(System.in): while(true) { System.out.println("N value: "): int n = in.nextInt(): Pascal pascal = new RecursivePascal(): Stopwatch sw = new Stopwatch(): pascal.printPascal(n): System.out.println(sw.elapsedTime()): pascal = new IterativePasca1(): sw = new Stopwatch(): pascal.printPascal(n): System.out.println(sw.elapsedTime()): pascal = new RecursivePascalHashMap(): sw = new Stopwatch(): pascal.printPascal(n): System.out.println(sw.elapsedTime()): pascal = new RecursivePascalNormal): sw = new Stopwatch(): pascal.printPascal(n): System.out.println(sw.elapsedTime());Explanation / Answer
class Driver //Name of the class
{
//this program is used to know how much time it is taking by each class for printing pascal triangle
//we are comparing time taken by each class by using StopWatch class
//we have a method called elapsedTime() to konw how much time it has takne since stopwatch is created
public static void main(String args[])// Main method execution starts from this method
{
Scanner in=new Scanner(System.in);//Scanner class is used to take input form the user in runtime
while(true)//this is while loop runs infinity times
{
System.out.println("N value :");//asking the user to enter a value to print pascal triangle
int n=in.nextInt();//assigning the vlaue to variable n whick is given by the user
Pascal pascal=new RecursivePascal();//here Pascal is the interface and RecursicePacal is the class implementing Pascal interface
// so imterface variable can hold implementing class object
StopWatch sw=new StopWatch();//Creating the object for class StopWatch to know how much time RecursivePascal class is taking to print pascal triangle
pascal.printPascal(n);//calling the printPascal in Recursive Pascal class
System.out.println(sw.elapsedTime());//prints the time from object creation to current time
pascal=new IterativePascal();//similarly we are checking for all the class and knowing which class is taking less time
// for printing pascal triangle
sw=new StopWatch();
pascal.printPascal(n);
System.out.println(sw.elapsedTime());
pascal=new RecursivePascalHashMap();
sw=new StopWatch();
pascal.printPascal(n);
System.out.println(sw.elapsedTime());
pascal=new RecursivePascalNormal();
sw=new StopWatch();
pascal.printPascal(n);
System.out.println(sw.elapsedTime());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.