Programming in JAVA.
Java Week 3: Q1
This program is related to the generation of Fibonacci numbers.
For example: 0, 1, 1, 2, 3, 5, 8, 13,… is a Fibonacci sequence where 13 is the 8thFibonacci number. A partial code is given and you have to complete the code as per the instruction given as comments.
PROGRAM:
if(n==1)
return 0;
else if(n==2)
return 1;
else
return fib(n-1)+fib(n-2);
Java Week 3: Q2
Define a class Point with two fields x and y each of type double. Also , define a method distance(Point p1, Point p2) to calculate the distance between points p1 and p2 and return the value in double. Complete the code segment given below. Use Math.sqrt( ) to calculate the square root.
PROGRAM:
class Point{
double x,y;
void distance(Point p1,Point p2){
double diff,xx,yy;
xx=p2.x-p1.x;
yy=p2.y-p1.y;
diff=Math.sqrt(xx*xx+yy*yy); // sqrt((x2-x2)2 +(y2-y1)2) simply use distance formula
System.out.print(diff);
}
}
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100% correct solutions.
No comments:
Post a Comment
If you have any problems related to solutions or any concept please let me know.