Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone organize, and make it where my code compiles with out errors, and th

ID: 3530429 • Letter: C

Question

can someone organize, and make it where my code compiles with out errors, and the program runs.




import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;


public class Critter {

public static void main(String[] args) {

CritterFrame frame = new CritterFrame();


//frame.add(25, Bird.class);

//frame.add(25, Frog.class);

//frame.add(25, Mouse.class);

//frame.add(25, Turtle.class);

//frame.add(25, Wolf.class);

frame.add(25, Stone.class);


frame.start();

}

}


public interface Critter {

public char getChar();

public int getMove(CritterInfo info);


public static final int NORTH = -1;

public static final int SOUTH = 3;

public static final int EAST = 8;

public static final int WEST = 11;

public static final int CENTER = 42;

}

public class CritterFrame extends JFrame {

private CritterModel myModel;

private CritterPanel myPicture;

private javax.swing.Timer myTimer;

private JButton[] counts;


public CritterFrame() {

// create frame and order list

setTitle("CS141 critter simulation");

setDefaultCloseOperation(EXIT_ON_CLOSE);

myModel = new CritterModel(100, 50);


// set up critter picture panel and set size

myPicture = new CritterPanel(myModel);

add(myPicture, BorderLayout.CENTER);


addTimer();


// add timer controls to the south

JPanel p = new JPanel();

JButton b1 = new JButton("start");

b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

myTimer.start();

}

});

p.add(b1);

JButton b2 = new JButton("stop");

b2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

myTimer.stop();

}

});

p.add(b2);

JButton b3 = new JButton("step");

b3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

doOneStep();

}

});

p.add(b3);

add(p, BorderLayout.SOUTH);

}


// starts the simulation...assumes all critters have already been added

public void start() {

addClassCounts();

pack();

setVisible(true);

}


// add right-hand column showing how many of each critter are alive

private void addClassCounts() {

Set<Map.Entry<String, Integer>> entries = myModel.getCounts();

JPanel p = new JPanel(new GridLayout(entries.size(), 1));

counts = new JButton[entries.size()];

for (int i = 0; i < counts.length; i++) {

counts[i] = new JButton();

p.add(counts[i]);

}

add(p, BorderLayout.EAST);

setCounts();

}


private void setCounts() {

Set<Map.Entry<String, Integer>> entries = myModel.getCounts();

int i = 0;

for (Map.Entry<String, Integer> entry: myModel.getCounts()) {

counts[i].setText(entry.toString());

i++;

}

}


// add a certain number of critters of a particular class to the simulation

public void add(int number, Class c) {

myModel.add(number, c);

}


// post: creates a timer that calls the model's update

// method and repaints the display

private void addTimer() {

ActionListener updater = new ActionListener() {

public void actionPerformed(ActionEvent e) {

doOneStep();

}

};

myTimer = new javax.swing.Timer(100, updater);

myTimer.setCoalesce(true);

}


// one step of the simulation

private void doOneStep() {

myModel.update();

setCounts();

myPicture.repaint();

}

}

public interface CritterInfo {

public int getX();

public int getY();

public char getNeighbor(int direction);

public int getHeight();

public int getWidth();

}

public class CritterModel {

private int myHeight;

private int myWidth;

private Critter[][] myGrid;

private char[][] display;

private Map<Critter, Point>myList;

private SortedMap<String, Integer>critterCount;


public CritterModel(int width, int height) {

myWidth = width;

myHeight = height;

myGrid = new Critter[width][height];

display = new char[width][height];

updateDisplay();

myList = new HashMap<Critter, Point>();

critterCount = new TreeMap<String, Integer>();

}


public void add(int number, Class critter) {

if (myList.size() + number > myWidth * myHeight)

throw new RuntimeException("adding too many critters");

for (int i = 0; i < number; i++) {

Object instance;

try {

instance = critter.newInstance();

} catch (Exception e) {

throw new RuntimeException("no zero-argument constructor for "

+ critter);

}

if (!(instance instanceof Critter)) {

throw new RuntimeException(critter

+ " does not implement Critter");

}

Critter next = (Critter)instance;

int x, y;

do {

x = randomInt(0, myWidth - 1);

y = randomInt(0, myHeight - 1);

} while (myGrid[x][y] != null);

myGrid[x][y] = next;

display[x][y] = next.getChar();

myList.put(next, new Point(x, y));

}

String name = critter.getName();

if (!critterCount.containsKey(name))

critterCount.put(name, number);

else

critterCount.put(name, critterCount.get(name) + number);

}


private static int randomInt(int low, int high) {

return low + (int)(Math.random() * (high - low + 1));

}


public int getWidth() {

return myWidth;

}


public int getHeight() {

return myHeight;

}


public char getChar(int x, int y) {

return display[x][y];

}


// We want to ask each critter once on each round how to display it.

// This method does the asking, storing the result in display.

private void updateDisplay() {

// set it to all dots

for (int x = 0; x < myWidth; x++)

for (int y = 0; y < myHeight; y++)

if (myGrid[x][y] == null)

display[x][y] = '.';

else

display[x][y] = myGrid[x][y].getChar();

}


public void update() {

Critter[][] newGrid = new Critter[myWidth][myHeight];

Object[] list = myList.keySet().toArray();

Collections.shuffle(Arrays.asList(list));

for (int i = 0; i < list.length; i++) {

Critter next = (Critter) list[i];

Point p = myList.get(next);

int move = next.getMove(new Info(p.x, p.y));

movePoint(p, move);

if (newGrid[p.x][p.y] != null) {

String c = newGrid[p.x][p.y].getClass().getName();

critterCount.put(c, critterCount.get(c) - 1);

myList.remove(newGrid[p.x][p.y]);

}

newGrid[p.x][p.y] = next;

}

myGrid = newGrid;

updateDisplay();

}


public Set<Map.Entry<String, Integer>> getCounts() {

return Collections.unmodifiableSet(critterCount.entrySet());

}


// translates a point's coordinates 1 unit in a particular direction

private void movePoint(Point p, int direction) {

if (direction == Critter.NORTH)

p.y = (p.y + myHeight - 1) % myHeight;

else if (direction == Critter.SOUTH)

p.y = (p.y + 1) % myHeight;

else if (direction == Critter.EAST)

p.x = (p.x + 1) % myWidth;

else if (direction == Critter.WEST)

p.x = (p.x + myWidth - 1) % myWidth;

else if (direction != Critter.CENTER)

throw new RuntimeException("Illegal direction");

}


// an object used to query a critter's state (position, neighbors)

private class Info implements CritterInfo {

private int x;

private int y;


public Info(int x, int y) {

this.x = x;

this.y = y;

}


public int getX() {

return x;

}


public int getY() {

return y;

}


public char getNeighbor(int direction) {

Point other = new Point(x, y);

movePoint(other, direction);

return display[other.x][other.y];

}


public int getHeight() {

return myHeight;

}


public int getWidth() {

return myWidth;

}

}

}

public class CritterPanel extends JPanel {

private CritterModel myModel;

private Font myFont;


public static final int FONT_SIZE = 12;


public CritterPanel(CritterModel model) {

myModel = model;

// construct font and compute char width once in constructor

// for efficiency

myFont = new Font("Monospaced", Font.BOLD, FONT_SIZE);

setBackground(Color.cyan);

setPreferredSize(new Dimension(FONT_SIZE * model.getWidth() / 2 + 125,

FONT_SIZE * model.getHeight() + 20));

}


public void paintComponent(Graphics g) {

super.paintComponent(g);

g.setFont(myFont);

int height = myModel.getHeight();

int width = myModel.getWidth();

// because font is monospaced, all widths should be the same;

// so we can get char width from any char (in this case x)

int charWidth = g.getFontMetrics().charWidth('x');

int extraX = getWidth() - (width + 1) * charWidth;

int extraY = getHeight() - (height - 1) * FONT_SIZE;

for (int i = 0; i < width; i++)

for (int j = 0; j < height; j++)

g.drawString("" + myModel.getChar(i, j),

extraX/2 + i * charWidth,

extraY/2 + j * FONT_SIZE);

}

}

public class Stone implements Critter {

public char getChar() {

return 'S';

}


public int getMove(CritterInfo info) {

return CENTER;

}

}

Explanation / Answer

In this statements you the no.of braces do not match import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Critter { public static void main(String[] args) { //2 opening braces CritterFrame frame = new CritterFrame(); //frame.add(25, Bird.class); //frame.add(25, Frog.class); //frame.add(25, Mouse.class); //frame.add(25, Turtle.class); //frame.add(25, Wolf.class); frame.add(25, Stone.class); frame.start(); } } public interface Critter { public char getChar(); public int getMove(CritterInfo info); public static final int NORTH = -1; public static final int SOUTH = 3; public static final int EAST = 8; public static final int WEST = 11; public static final int CENTER = 42; } //misplaced brace, 3rd closing brace

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote