•

0 likes•3 views

Using JAVA There is very deep and strong river and the only way one person can cross the river is to jump on the stones. There are N-1 stones on river from one side to another, placed in equal distances. If we give numbers to stones, we have 1, 2, 3,...,N-1 and the other side of the river can be numbered as N. A person can only jump to stone infront of him, or to the next one or to the next next one, but not longer. In other words, one possibility to cross the river is: {1, 2, 3,..., N}, another possibility is: { 3, 4,..., N} and another one is {3,6,9,.., N}, and so on. Design and implement an algorihtm to print number of possibilities and all possible paths to cross the river, when n is given. For example; when n=4 is given, it should print these lines to the screen: Pseudocode for your algorithm. Your Java file that implements the algorithm. Output files of your running program for three different n values: n=6, n=7, and n=8. Solution java code public class RiverCrossing { private int noOfways; public void noOfWaysToCross(int n){ int f1=1,f2=2,f3=0; noOfways=0; while(f3<=n){ f3=f1+f2; noOfways+=f3; f1=f2; f2=f3; } System.out.println(\"No. of ways to cross the river: \"+noOfways); } public static void main(String[] args) { RiverCrossing r=new RiverCrossing(); r.noOfWaysToCross(6); r.noOfWaysToCross(7); r.noOfWaysToCross(8); } } .

- 1. Using JAVA There is very deep and strong river and the only way one person can cross the river is to jump on the stones. There are N-1 stones on river from one side to another, placed in equal distances. If we give numbers to stones, we have 1, 2, 3,...,N-1 and the other side of the river can be numbered as N. A person can only jump to stone infront of him, or to the next one or to the next next one, but not longer. In other words, one possibility to cross the river is: {1, 2, 3,..., N}, another possibility is: { 3, 4,..., N} and another one is {3,6,9,.., N}, and so on. Design and implement an algorihtm to print number of possibilities and all possible paths to cross the river, when n is given. For example; when n=4 is given, it should print these lines to the screen: Pseudocode for your algorithm. Your Java file that implements the algorithm. Output files of your running program for three different n values: n=6, n=7, and n=8. Solution java code public class RiverCrossing { private int noOfways; public void noOfWaysToCross(int n){ int f1=1,f2=2,f3=0; noOfways=0; while(f3<=n){ f3=f1+f2; noOfways+=f3; f1=f2; f2=f3; } System.out.println("No. of ways to cross the river: "+noOfways); } public static void main(String[] args) { RiverCrossing r=new RiverCrossing(); r.noOfWaysToCross(6); r.noOfWaysToCross(7); r.noOfWaysToCross(8); } }