Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Define Conventional Marketing.

Answered: 1 week ago

Question

Define Synchro Marketing.

Answered: 1 week ago

Question

Define marketing concepts.

Answered: 1 week ago

Question

1 what does yellow colour on the map represent?

Answered: 1 week ago