Question: convert the following Java code to Scheme: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Game01 { private Node

convert the following Java code to Scheme:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Game01 {

private Node head;

private Node tail;

public Game01() {

clear();

}

public void add(int point, int cost) {

if (head == null) {

head = new Node(point, cost);

tail = head;

} else {

tail.next = new Node(point, cost);

tail = tail.next;

}

}

public void clear() {

head = null;

tail = null;

}

public String toString() {

StringBuilder sb = new StringBuilder();

Node temp = head;

while (temp != null) {

sb.append("[ points: ");

sb.append(temp.points);

sb.append(", cost: ");

sb.append(temp.cost);

sb.append(" ] ");

temp = temp.next;

}

return sb.toString();

}

private class Node {

private int points;

private int cost;

private Node next;

public Node(int p, int c) {

points = p;

cost = c;

}

public String toString() {

return "[" + points + ", " + cost + "]";

}

}

public static void main(String[] args) throws FileNotFoundException {

Scanner fileIn = new Scanner(new File("in_t.txt"));

Game01 cards = new Game01();

int allowance = fileIn.nextInt();

while (fileIn.hasNextLine()) {

fileIn.nextLine();

int point = fileIn.nextInt();

int cost = fileIn.nextInt();

cards.add(point, cost);

}

fileIn.close();

System.out.println(playGame(cards, allowance));

}

public static int playGame(Game01 cards, int allowance) {

return maximumScore(cards.head, allowance, 0, 0);

}

private static int maximumScore(Node card, int allowance, int score, int cost) {

if (cost > allowance) {

return 0;

}

if (card == null) {

return score;

}

int scoreWithCard = maximumScore(card.next, allowance, score + card.points, cost + card.cost);

int scoreWithoutCard = maximumScore(card.next, allowance, score, cost);

return scoreWithCard > scoreWithoutCard ? scoreWithCard : scoreWithoutCard;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!