Question

In: Computer Science

Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...

Please take this c++ code and make it into java code.

/* RecursionPuzzleSolver

* ------------

* This program takes a puzzle board and returns true if the

* board is solvable and false otherwise

*

* Example: board 3 6 4 1 3 4 2 5 3 0

* The goal is to reach the 0. You can move the number of spaces of your

* current position in either the positive / negative direction

* Solution for this game is +3, -1, +4, +2, -3, +4

*/

#include <iostream>

#include <vector> // for vector

using namespace std;



/* Solvable

* --------

* Solvable returns true if there is a solution to the puzzle

* false otherwise.

*

*/

bool Solvable(int start, vector<int> & squares){

if (squares[start] == 0) return true;

else{

int nextForward = start + squares[start];

int nextBackward = start - squares[start];

if( nextForward<=squares.size()-1 && nextBackward>=0 ){

return ( Solvable(nextForward, squares) || Solvable(nextBackward,squares) );

}

if ( nextForward>squares.size()-1 && nextBackward>=0) {

return ( Solvable(nextBackward, squares));

}

if (nextForward<=squares.size()-1 && nextBackward<0 && squares[start]!=squares[nextForward]){

return ( Solvable(nextForward, squares));

}

if ( squares[start] == squares[nextForward] &&

nextBackward<0 &&

nextForward+squares[nextForward]>squares.size()-1){

return false;

}

else return false;

}

}

/* boolToString

* --------

* boolToString returns string "true" if the argument is true or 1

* returns string "false" otherwise

*

*/

string boolToString(bool boolean){

if(boolean)

{

return "true";

}

else

{

return "false";

}

}

int main(){

vector<int> puzzle1 {3, 6, 4, 1, 3 ,4, 2, 5, 3, 0};

vector<int> puzzle2 {3, 1, 2, 3, 0};

vector<int> puzzle3 {1,0};

vector<int> puzzle4 {1, 3, 2, 2, 1, 0};

cout << boolToString( Solvable(0, puzzle1) ) << endl; // true

cout << boolToString( Solvable(0, puzzle2) ) << endl; // false

cout << boolToString( Solvable(0, puzzle3) ) << endl; // true

cout << boolToString( Solvable(0, puzzle4) ) << endl; // true

return 0;

}

Solutions

Expert Solution

Please hit upvote(class name is Main so save as Main.java)

import java.util.*;
public class Main
{


static boolean Solvable (int start, Vector < Integer > squares)
{

if (squares.get (start) == 0)
return true;

else
{

   int nextForward = start + squares.get (start);

   int nextBackward = start - squares.get (start);

   if (nextForward <= squares.size () - 1 && nextBackward >= 0)
   {

   return (Solvable (nextForward, squares)
       || Solvable (nextBackward, squares));

   }

   if (nextForward > squares.size () - 1 && nextBackward >= 0)
   {

   return (Solvable (nextBackward, squares));

   }

   if (nextForward <= squares.size () - 1 && nextBackward < 0
   && squares.get (start) != squares.get (nextForward))
   {

   return (Solvable (nextForward, squares));

   }

   if (squares.get (start) == squares.get (nextForward) &&
   nextBackward < 0 &&
   nextForward + squares.get (nextForward) > squares.size () - 1)
   {

   return false;

   }

   else
   return false;

}

}


static String boolToString (boolean bool)
{

if (bool)

{

   return "true";

}

else

{

   return "false";

}

}
public static void main (String[]args)
{
Vector < Integer > puzzle1 =
new Vector <> (Arrays.asList (3, 6, 4, 1, 3, 4, 2, 5, 3, 0));

Vector < Integer > puzzle2 =
new Vector <> (Arrays.asList (3, 1, 2, 3, 0));

Vector < Integer > puzzle3 = new Vector <> (Arrays.asList (1, 0));

Vector < Integer > puzzle4 =
new Vector <> (Arrays.asList (1, 3, 2, 2, 1, 0));


System.out.println (boolToString (Solvable (0, puzzle1)));
System.out.println (boolToString (Solvable (0, puzzle2)));
System.out.println (boolToString (Solvable (0, puzzle3)));
System.out.println (boolToString (Solvable (0, puzzle4)));
}
}

 

 

Related Solutions

please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
C++ How to make this code take a class object instead of int for the vector...
C++ How to make this code take a class object instead of int for the vector queue and be able to enqueue and dequeue the object? #include <iostream> #include <float.h> #include <bits/stdc++.h> using namespace std; void print_queue(queue<int> Q) //This function is used to print queue { while (!Q.empty()) { cout<< Q.front() << " "; Q.pop(); } } int main() { int n = 10; vector< queue<int> > array_queues(n); //Create vector of queues of size 10, each entry has a queue...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
Please Use C language to Make a calculator. Make sure calculator is able to take up...
Please Use C language to Make a calculator. Make sure calculator is able to take up to 5 values. Try to make calculator using simple coding As you can. Please create a simple calculator with only +, -,* and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions: +, -, divide and multiply. Please have the answers, always rounded to two decimal figures. The calculator will also have to...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Write code in C please. #1 Write a function multiples() which will take an integer input...
Write code in C please. #1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50 #2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better understanding. We are mainly using basic in and out declarations and are focusing on API for method invocations. Any help would be very appreciated :) Problem 7: Distance (10 points) Use API (Data Science) Data Science is an emergent field from Computer Science with applications in almost every domain including finances, medical research, entertainment, retail, advertising, and insurance. The role of a data analyst...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT