id stringlengths 36 36 | text stringlengths 1 1.25M |
|---|---|
55f93081-1ab6-4e9d-853b-4759000c8ee2 | public boolean isBrancas() {
return brancas;
} |
71c18604-dac5-4fb8-a9f9-38d790ab682e | public Checkpoint(ArrayList<Peca> pecas, boolean brancas)
{
this.pecas = pecas;
this.brancas = brancas;
} |
53c18f23-460f-4c5d-9c9b-dd5976cda078 | public Posicao(int mX, int mY){
x = mX;
y = mY;
} |
17a23f0c-dc88-4583-bf04-c06c22985db2 | public Posicao(Posicao posicao)
{
x = posicao.x;
y = posicao.y;
} |
a7b2b001-5700-41d0-9cdd-fbf9a9bb66a9 | Bispo(boolean b, Posicao posicao) {
super(b, posicao);
} |
d24815d3-a836-4571-b56e-7aa559c4129a | public char getSimbolo() {
if (this.brancas == true){
return 'b';
}
else{
return 'B';
}
} |
72fe0a1d-1311-4da5-b1f7-96c73e73e5f8 | public String getNome() {
return "Bispo";
} |
53724f2e-f114-49e2-b6a2-96bb4ea83161 | public boolean validaMovimento(Posicao posicao, boolean captura) {
super.validaTabuleiroMaximo(posicao);
int xOffset = this.posicao.x - posicao.x;
int yOffset = this.posicao.y - posicao.y;
if (Math.abs(xOffset) == Math.abs(yOffset))
{
return true;
}
return false;
} |
b343736b-427b-4cf5-aaa2-9fcf3f373396 | public static void main(String[] args) {
// Specify the relative path to a .jev file the working directory
Map.getInstance().loadMap("maps/mazes/drew.jev");
runJerooCode();
new JerooGUI();
} |
4e809262-ec27-4f09-81ba-8dd49678e5da | public static void runJerooCode() {
// Jeroo code goes here
Jeroo kim = new Jeroo();
while(!kim.hasFlower()){
kim.moveTendingRight();
}
} |
3c9c9ef9-a77f-4ed5-b3e2-429c082463c0 | public static boolean coordsInBounds(int x, int y) {
return x >= 0 && x < Map.WIDTH && y >= 0 && y < Map.HEIGHT;
} |
63270964-2f25-4d39-b904-0f336f4bed91 | public static int findXRelative(RelativeDirection relDir, CompassDirection compDir, int x) {
if (compDir == NORTH) {
if (relDir == LEFT) {
x--;
}
if (relDir == RIGHT) {
x++;
}
} else if (compDir == EAST) {
if (relDir == AHEAD) {
x++;
}
} else if (compDir == SOUTH) {
if (relDir == LEFT) {
x++;
}
if (relDir == RIGHT) {
x--;
}
} else if (compDir == WEST) {
if (relDir == AHEAD) {
x--;
}
}
return x;
} |
4aa00413-067f-46f1-bd06-1701a26cbb50 | public static int findYRelative(RelativeDirection relDir, CompassDirection compDir, int y) {
if (compDir == EAST) {
if (relDir == LEFT) {
y--;
}
if (relDir == RIGHT) {
y++;
}
} else if (compDir == SOUTH) {
if (relDir == AHEAD) {
y++;
}
} else if (compDir == WEST) {
if (relDir == LEFT) {
y++;
}
if (relDir == RIGHT) {
y--;
}
} else if (compDir == NORTH) {
if (relDir == AHEAD) {
y--;
}
}
return y;
} |
ce7e5796-2a31-4de4-be3c-329e88331342 | public JerooGUI() {
counter = 0;
mapPanels = new JPanel[24][24];
init();
} |
b1ba4f84-bc88-42b8-ac5e-04c6fd3e8303 | private void init() {
frame = new JFrame();
frame.setLayout(new GridLayout(26, 26));
frame.addKeyListener(this); // JLabel cannot get keyboard focus
frame.setResizable(true);
frame.setSize(520, 520);
frame.setBackground(WATER);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setTitle("Jeroo");
frame.requestFocusInWindow();
// save data to the char maps
char[][] tempMap = Map.getInstance().getCharMatrix();
for (int i = 0; i < Map.HEIGHT; i++) {
for (int j = 0; j < Map.WIDTH; j++) {
map[i][j] = tempMap[i][j];
}
}
// save data to the panel map
for (int i = 0; i < Map.HEIGHT + 2; i++) {
for (int j = 0; j < Map.WIDTH + 2; j++) {
JPanel panel = new JPanel();
panel.setSize(20, 20);
panel.setVisible(true);
if (i < 1 || i > 24 || j < 1 || j > 24) {
panel.setBackground(WATER);
} else {
panel.setBackground(GRASS);
mapPanels[i - 1][j - 1] = panel;
}
frame.add(panel);
}
}
frame.setVisible(true);
updateMap();
} |
20abd156-d3cb-4503-aa7b-3f7cf63dfbba | private void updateMap() {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
char temp = map[i][j];
Color c = Color.BLACK;
if (temp == '.') {
c = GRASS;
} else if (temp == 'W') {
c = WATER;
} else if (temp == 'F') {
c = FLOWER;
} else if (temp == 'N') {
c = NET;
}
mapPanels[i][j].setBackground(c);
}
}
} |
28385800-19d8-4d68-b2d5-fc59623c2412 | public void stepForwards() {
if (counter < Map.getInstance().getHistoryLength() - 1) {
counter++;
MapState newState = Map.getInstance().getHistory(counter);
if (newState.getX() >= 0) {
int x = newState.getX();
int y = newState.getY();
char newItem = newState.getNewItem();
map[y][x] = newItem;
}
updateMap();
ArrayList<JerooState> jeroos = newState.getJerooState();
for (JerooState j : jeroos) {
mapPanels[j.getY()][j.getX()].setBackground(JEROO);
}
}
} |
fa869fcb-0467-4758-a0a7-281fe8c186cb | public void stepBackwards() {
if (counter > 0) {
MapState currentState = Map.getInstance().getHistory(counter);
counter--;
if (currentState.getX() >= 0) {
int x = currentState.getX();
int y = currentState.getY();
char oldItem = currentState.getOldItem();
map[y][x] = oldItem;
}
updateMap();
ArrayList<JerooState> jeroos = Map.getInstance().getHistory(counter).getJerooState();
for (JerooState j : jeroos) {
mapPanels[j.getY()][j.getX()].setBackground(JEROO);
}
}
} |
e585c771-6f01-40d7-9fc0-6a8f8854e879 | @Override
public void keyReleased(KeyEvent e) {
} |
4c987da7-b10b-49d5-93be-88913f8ea5e4 | @Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
stepBackwards();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
stepForwards();
}
} |
d85be3b5-fb18-48dc-ab50-53484bfcc3e4 | @Override
public void keyTyped(KeyEvent e) {
} |
313b129e-6eab-4b61-992d-af6413d8c9ce | public JerooState(int x, int y, CompassDirection direction, int flowers){
this.x = x;
this.y = y;
this.direction = direction;
this.flowers = flowers;
} |
890ff53c-e6b4-4591-a38e-3f691c793bac | public int getFlowers(){
return flowers;
} |
6d2baadd-e65d-4e18-9432-e5461bbc855e | public int getX(){
return x;
} |
c8473636-4cc1-4383-a9ce-e126ad6bbf25 | public int getY(){
return y;
} |
2847dcd1-8a4d-4ffb-887e-3a9939d5aeb3 | public CompassDirection getDirection(){
return direction;
} |
509397a9-32a8-4c24-9eaa-ba33299e0815 | private Map() {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
map[i][j] = '.';
}
}
} |
c333873d-a806-4e13-ac9e-0432ea376614 | public void addJeroo(Jeroo j) {
jeroos.add(j);
} |
81fd6920-cca3-43a0-bda8-32456285989b | public ArrayList<Jeroo> getJeroos() {
return jeroos;
} |
eb3c4dee-f27a-4b36-ae0b-b736cfa10faf | public static Map getInstance() {
return instance;
} |
234b07ad-8679-48d7-a083-b7bfe16f6fc9 | public char[][] getCharMatrix(){
return map;
} |
dc6f6383-210b-4d63-8a44-8ef80a9d8b6b | public MapState getHistory(int n) {
return history.get(n);
} |
d0a59617-ace4-4171-9f70-ae89a836cb50 | public int getHistoryLength() {
return history.size();
} |
4b6548c6-8288-4e46-a570-a09bbb937a11 | public Map loadMap(String filename) {
try {
Scanner sc = new Scanner(new File(filename));
String stringMap = "";
char[][] tempMap = new char[HEIGHT][WIDTH];
// Read in each line of the map from the map file
while (sc.hasNextLine()) {
stringMap += sc.nextLine();
}
// Transfer data from the String to the char map
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
tempMap[i][j] = stringMap.charAt(i * WIDTH + j);
}
}
map = tempMap;
} catch (Exception e) {
System.out.println("Failed to load map.");
}
return instance;
} |
cc2ce747-bcfd-4eb8-ba2a-782b94fb358f | public void saveMap(int x, int y, char oldItem, char newItem) {
MapState mapState = new MapState(jeroos, x, y, oldItem, newItem);
history.add(mapState);
} |
6b72389a-fbde-4f59-beaa-0a94f91c5183 | public void saveMap() {
MapState mapState = new MapState(jeroos);
history.add(mapState);
} |
8bf1d791-718f-434c-89b2-3250a28827a5 | public void printMap() {
char[][] tempMap = new char[HEIGHT][WIDTH];
for(int i=0; i<HEIGHT; i++){
for(int j=0; j<WIDTH; j++){
tempMap[i][j] = map[i][j];
}
}
for(Jeroo j:jeroos){
tempMap[j.getY()][j.getX()] = 'J';
}
for(int i=0; i<HEIGHT; i++){
for(int j=0; j<WIDTH; j++){
System.out.print(tempMap[i][j] + " ");
}
System.out.println("");
}
} |
01b9e8af-9cc5-4f20-aef9-628466228356 | public void clearSpace(int x, int y) {
saveMap(x, y, map[y][x], '.');
map[y][x] = '.';
} |
01b0cc42-b59c-4de2-90d2-81b831426971 | public void placeFlower(int x, int y) {
saveMap(x, y, map[y][x], 'F');
map[y][x] = 'F';
} |
c0f879a1-1edb-479f-b8ca-ba3dca6b0cc4 | public boolean isFlower(int x, int y) {
return map[y][x] == 'F';
} |
af9cb241-228c-44f6-a053-61a281d064a0 | public boolean isJeroo(int x, int y) {
for (Jeroo j : jeroos) {
if (j.getX() == x && j.getY() == y) {
return true;
}
}
return false;
} |
45b1662b-22f4-4524-aebe-a29d43d5895a | public boolean isAnotherJeroo(int x, int y) {
int count = 0;
for (Jeroo j : jeroos) {
if (j.getX() == x && j.getY() == y) {
count++;
}
}
if (count > 1) {
return true;
}
return true;
} |
fb36e001-b4c2-4c91-9278-233f9ceea9e2 | public Jeroo getJerooAt(int x, int y) {
for (Jeroo j : jeroos) {
if (j.getX() == x && j.getY() == y) {
return j;
}
}
return null;
} |
756ef982-9736-4eb2-ab4f-0f79f62480dc | public boolean isNet(int x, int y) {
return map[y][x] == 'N';
} |
b848c138-b150-46e1-be9a-1becedb77937 | public boolean isClear(int x, int y) {
return !isJeroo(x, y) && map[y][x] == '.';
} |
ad333b02-2c5d-46ec-adbe-9ba01c91b446 | public boolean isWater(int x, int y) {
return map[y][x] == 'W';
} |
7069aedf-9a6c-4db1-88fd-91dfb863f43d | public Jeroo() {
// New jeroos must be added to the map.
Map.getInstance().addJeroo(this);
Map.getInstance().saveMap();
} |
a919bdf0-7df0-494c-b08d-d7dd5144f0dd | public Jeroo(int flowers) {
this();
this.flowers = flowers;
} |
739e7682-5cd7-4a7a-9983-9f5fe390e41f | public Jeroo(int x, int y) {
this();
this.x = x;
this.y = y;
} |
cf405b80-c6a6-49f6-856f-92fabafa70c3 | public Jeroo(int x, int y, CompassDirection direction) {
this();
this.x = x;
this.y = y;
this.direction = direction;
} |
4a5a8545-43de-4db5-b047-c1a607107d27 | public Jeroo(int x, int y, int flowers) {
this();
this.x = x;
this.y = y;
this.flowers = flowers;
} |
13ee5bdc-6ae9-4f95-a8d9-c105bba368c3 | public Jeroo(int x, int y, CompassDirection direction, int flowers) {
this();
this.x = x;
this.y = y;
this.direction = direction;
this.flowers = flowers;
} |
f253c5b1-9c8e-4980-ae67-e16bb3f180f0 | public int getX() {
return x;
} |
05ac26ab-0287-4bc8-94f0-ab43533981ab | public int getY() {
return y;
} |
b8acf448-9616-4934-8410-4458984e2bb2 | public CompassDirection getDirection() {
return direction;
} |
c69cd38e-4b9f-49e2-b146-e79d8fcbaa8e | public int getFlowers() {
return flowers;
} |
798f89eb-8e65-43a7-b302-5f95aea49b3e | public void recieveFlower() {
flowers++;
} |
d825c992-c0c7-4405-ba9e-08ae22abc863 | public void hop() {
int tempX = JerooHelper.findXRelative(AHEAD, direction, x);
int tempY = JerooHelper.findYRelative(AHEAD, direction, y);
Map map = Map.getInstance();
if (JerooHelper.coordsInBounds(tempX, tempY)){
if (map.isClear(tempX, tempY) || map.isFlower(tempX, tempY)) {
x = tempX;
y = tempY;
} else {
if (map.isNet(tempX, tempY)) {
throw new Error("Jeroo trapped in net!");
} else if (map.isWater(tempX, tempY)) {
throw new Error("Jeroo drowned in water!");
}
}
map.saveMap();
} else {
throw new Error("Jeroo drowned in water!");
}
} |
b1c86af2-118e-43b5-b1ca-4e78db94bb11 | public void hop(int n) {
for (int i = 0; i < n; i++) {
hop();
}
} |
c9f01feb-cbe6-4eca-941f-6a265d200ce1 | public void pick() {
if (isFlower(HERE)) {
flowers++;
Map.getInstance().clearSpace(x, y);
}
} |
773d257a-5c79-4bd1-b106-b99626fd3a0f | public void plant() {
if (flowers > 0) {
flowers--;
Map.getInstance().placeFlower(x, y);
}
} |
5fa82ad5-c768-42b5-848c-124b18988a72 | public void toss() {
if (flowers > 0) {
flowers--;
int tempX = JerooHelper.findXRelative(AHEAD, direction, x);
int tempY = JerooHelper.findYRelative(AHEAD, direction, y);
if (JerooHelper.coordsInBounds(tempX, tempY) && Map.getInstance().isNet(tempX, tempY)) {
Map.getInstance().clearSpace(tempX, tempY);
} else {
Map.getInstance().saveMap();
}
}
} |
6ba1cc9e-33a0-49ab-9806-a8c6af3ac63b | public void give(RelativeDirection relDir) {
if (flowers > 0) {
int tempX = JerooHelper.findXRelative(relDir, direction, x);
int tempY = JerooHelper.findYRelative(relDir, direction, y);
if (JerooHelper.coordsInBounds(tempX, tempY) && Map.getInstance().isJeroo(tempX, tempY)) {
flowers--;
Map.getInstance().getJerooAt(tempX, tempY).recieveFlower();
Map.getInstance().saveMap();
}
}
} |
58b02694-bfd8-4043-9688-4bb0baacd47e | public void turn(RelativeDirection relDir) {
if (relDir == RIGHT) {
if (direction == NORTH) {
direction = EAST;
} else if (direction == EAST) {
direction = SOUTH;
} else if (direction == SOUTH) {
direction = WEST;
} else if (direction == WEST) {
direction = NORTH;
}
} else if (relDir == LEFT) {
if (direction == NORTH) {
direction = WEST;
} else if (direction == EAST) {
direction = NORTH;
} else if (direction == SOUTH) {
direction = EAST;
} else if (direction == WEST) {
direction = SOUTH;
}
}
Map.getInstance().saveMap();
} |
842d730e-55c8-46e8-ba6f-49d356ee2a69 | public boolean hasFlower() {
return flowers > 0;
} |
2be3c2de-4e24-4d5f-b671-c4894c620843 | public boolean isFacing(CompassDirection compDir) {
return direction.equals(compDir);
} |
b1969e73-2bf7-4036-a54f-5f076067ef87 | public boolean isFlower(RelativeDirection relDir) {
int tempX = JerooHelper.findXRelative(relDir, direction, x);
int tempY = JerooHelper.findYRelative(relDir, direction, y);
if (JerooHelper.coordsInBounds(tempX, tempY)) {
return Map.getInstance().isFlower(tempX, tempY);
}
return false;
} |
bb1b4c55-e3f2-478c-ba96-8697906e27c4 | public boolean isJeroo(RelativeDirection relDir) {
int tempX = JerooHelper.findXRelative(relDir, direction, x);
int tempY = JerooHelper.findYRelative(relDir, direction, y);
if (JerooHelper.coordsInBounds(tempX, tempY)) {
return Map.getInstance().isJeroo(tempX, tempY);
}
return false;
} |
7159adea-ae09-4c68-a812-01e4eda949ed | public boolean isNet(RelativeDirection relDir) {
int tempX = JerooHelper.findXRelative(relDir, direction, x);
int tempY = JerooHelper.findYRelative(relDir, direction, y);
if (JerooHelper.coordsInBounds(tempX, tempY)) {
return Map.getInstance().isNet(tempX, tempY);
}
return false;
} |
225ad532-c88c-4058-a488-97b76c219e6e | public boolean isWater(RelativeDirection relDir) {
int tempX = JerooHelper.findXRelative(relDir, direction, x);
int tempY = JerooHelper.findYRelative(relDir, direction, y);
if (JerooHelper.coordsInBounds(tempX, tempY)) {
return Map.getInstance().isWater(tempX, tempY);
}
return false;
} |
44229865-038d-4dcf-8879-5f0c38d9c8dd | public boolean isClear(RelativeDirection relDir) {
int tempX = JerooHelper.findXRelative(relDir, direction, x);
int tempY = JerooHelper.findYRelative(relDir, direction, y);
if (JerooHelper.coordsInBounds(tempX, tempY)) {
return Map.getInstance().isClear(tempX, tempY);
}
return false;
} |
226c9b58-51a0-4dbd-89ff-7e5a23f99e53 | public void getFlowerNearby() {
if (isFlower(HERE) || isFlower(AHEAD) || isFlower(LEFT) || isFlower(RIGHT)) {
if (isFlower(LEFT)) {
turn(LEFT);
} else if (isFlower(RIGHT)) {
turn(RIGHT);
}
hop();
pick();
}
} |
bada3b5c-86ba-4eca-82e8-10ff50672ed0 | public void moveTendingRight() {
getFlowerNearby();
if (isClear(RIGHT)) {
turn(RIGHT);
} else {
if (isClear(AHEAD)) {
} // no need to turn...
else {
if (isClear(LEFT)) {
turn(LEFT);
} else {
turn(RIGHT);
turn(RIGHT);
}
}
}
hop();
} |
62449acf-7e10-4f20-96a7-b12de5b10447 | public MapState(ArrayList<Jeroo> jeroos){
for (Jeroo j : jeroos) {
jerooState.add(new JerooState(j.getX(), j.getY(), j.getDirection(), j.getFlowers()));
}
} |
e0d8fed2-9604-4aa4-8882-2dcc488d767e | public MapState(ArrayList<Jeroo> jeroos, int x, int y, char oldItem, char newItem) {
for (Jeroo j : jeroos) {
jerooState.add(new JerooState(j.getX(), j.getY(), j.getDirection(), j.getFlowers()));
}
this.x = x;
this.y = y;
this.oldItem = oldItem;
this.newItem = newItem;
} |
3c05b065-df7f-409e-bde1-f4fde255b230 | public int getX(){
return x;
} |
d1908f35-17a7-4108-90d2-10c4389b2b38 | public int getY(){
return y;
} |
a0024760-6490-4f7a-b660-bbf3381108dc | public char getOldItem(){
return oldItem;
} |
585d70ba-fef3-4665-ab8f-e19b46cd4c6e | public char getNewItem(){
return newItem;
} |
b54e1d80-39e7-4843-9216-ba94bed6141a | public ArrayList<JerooState> getJerooState(){
return jerooState;
} |
485af081-bf70-4f82-b784-2f74a9c54817 | public SimpleCalc() {
super();
inst = this;
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
setResizable(false);
setLocationRelativeTo(null);
} |
25dcf674-72dc-42c6-9963-1d5689317081 | public static SimpleCalc inst() {
return inst;
} |
3794f2e5-5ed4-49d6-9f89-eb7c9bad9853 | public static void main(String[] args) {
if(inst != null) throw new ExceptionInInitializerError("Can't create more than one window simultaneously.");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
} |
2756ef55-026f-42fa-bb90-72767779364d | public void run() {
createGUI();
} |
97d17c73-070f-460a-ab04-5c3540d25836 | private static void createGUI() {
SimpleCalc main = new SimpleCalc();
// Create all GUI elements
CalcDisplay.inst();
new CalcButton("⇦", "delete", new DeleteListener(), 1, 2);
new CalcButton("(", "(", new CharacterListener("("), 2, 2);
new CalcButton(")", ")", new CharacterListener(")"), 3, 2);
new CalcButton("/", "/", new CharacterListener("/"), 4, 2);
/**
* Variable to store button text. Used to loop over to make button creating more efficient.
*/
String[][] buttonText = {
{"7", "8", "9", "*"},
{"4", "5", "6", "-"},
{"1", "2", "3", "+"}
};
for(int y : new int[] {3, 4, 5}) {
for(int x : new int[] {1, 2, 3, 4}) {
String text = buttonText[y-3][x-1];
new CalcButton(text, text, new CharacterListener(text), x, y);
}
}
new CalcButton(".", ".", new CharacterListener("."), 1, 6);
new CalcButton("0", "0", new CharacterListener("0"), 2, 6);
new CalcButton("=", "=", new EqualsListener(), 3, 6, 2, 1);
main.pack();
main.setVisible(true);
} |
51307daf-5ae9-47b3-b271-b79fb2b5a5aa | public static GridBagConstraints getGBConstraint() {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
return c;
} |
2ee5e3a8-3726-48a3-9dad-32b760952abd | public static Double calculate(String eq) {
try {
DoubleEvaluator engine = new DoubleEvaluator();
return engine.evaluate(eq);
}catch(Exception e) {
// Will throw exception if it can't calculate, so return null
// In this case, a fuzzy exception is ok
return null;
}
} |
0242fdf5-2fc2-45c8-96c9-0df49a786bf9 | public static boolean isTooBig(double num) {
// Check if number is too large to fit on screen
// if(num >= Math.pow(10, CalcDisplay.COLUMNS)) {
// return true;
// }
// return false;
String str = new DecimalFormat("#").format(num);
return str.split("[.]")[0].length() > CalcDisplay.COLUMNS;
} |
75c00bf1-4744-4a1d-beca-95a9f02df9c3 | public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
return true;
} |
3aa52fa5-ca52-4ef0-8016-5f617fbaea76 | public static String formatDecimalForDisplay(double num) {
/*
LOGIC:
screen size is 13.
subtract number of digits needed for integers.
if screen size left is <= 1 (ie 0 or 1), print only integers.
subtract by one for decimal place. Any left is decimal accuracy.
*/
// Get number of digits before the decimal place
int numInt = Long.toString((long) Math.floor(num)).length();
// Number of decimal places
int decChars = CalcDisplay.COLUMNS - 1 - numInt;
// Decimal section of the DecimalFormat.
String decString = "";
if(decChars > 0) {
decString = "." + StringUtils.repeat("#", decChars); // Repeat the "#" for as many decimal places as needed
}
return new DecimalFormat("#" + decString).format(num);
} |
2d85fda7-a55b-4a7d-a732-92d0bbcb0634 | @Override
public void actionPerformed(ActionEvent event) {
CalcDisplay disp = CalcDisplay.inst();
if(disp.isError() || disp.isAnswer()) {
disp.setText("");
disp.setIsError(false);
disp.setIsAnswer(false);
}
String text = disp.getText();
if(text.length() > 0) {
disp.setText(text.substring(0, text.length() - 1));
}
} |
e3a26dc0-81fd-4448-8e79-51b022787ba2 | public CharacterListener(String c) {
this.c = c;
} |
888e6eaf-70d4-4df7-b9f4-72e9ea9ace9e | @Override
public void actionPerformed(ActionEvent event) {
CalcDisplay disp = CalcDisplay.inst();
if(disp.isError()) {
disp.setText("");
disp.setIsError(false);
}
// If user is trying to write new equation after calculation (eg writing a number), reset display, otherwise don't
if(disp.isAnswer() && MathUtil.isInteger(event.getActionCommand())) {
disp.setText("");
}
disp.setIsAnswer(false);
disp.addText(c);
} |
634d603c-63bb-40ff-a763-5c9270940e37 | @Override
public void actionPerformed(ActionEvent event) {
CalcDisplay disp = CalcDisplay.inst();
String eq = disp.getText();
if(disp.isError()) {
return;
}
Double val = MathUtil.calculate(eq);
if(val == null) {
disp.showError();
return;
}
if(MathUtil.isTooBig(val)) {
disp.showError();
return;
}
/* screen size is 13.
subtract number of digits needed for integers.
if screen size left is <= 1 (ie 0 or 1), print only integers.
subtract by one for decimal place. Any left is decimal accuracy. */
// Get number of digits before the decimal place
int numInt = Integer.toString((int) Math.floor(val)).length();
int decChars = CalcDisplay.COLUMNS;
decChars -= numInt;
if(decChars <= 1) {
// Return integer (long) of double, as it's to large to have decimals
disp.setText(Long.toString(Math.round(val)));
return;
}
// Number of decimal places for DecimalFormat, as hashes
String numDec = StringUtils.repeat("#", decChars -1);
DecimalFormat format = new DecimalFormat("#." + numDec);
disp.setText(format.format(val));
disp.setIsAnswer(true);
} |
83ef1f61-9788-4b05-8702-581b242c3e2b | public static CalcDisplay inst() {
if(inst == null) {
inst = new CalcDisplay();
}
return inst;
} |
8576bb04-367d-4004-a3f7-97d230f85c94 | private CalcDisplay() {
super(COLUMNS);
GridBagConstraints c = SimpleCalc.getGBConstraint();
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 4;
setEditable(false);
setFont(new Font("Courier New", Font.PLAIN, 20));
setPreferredSize(new Dimension(134, 40));
setHorizontalAlignment(JTextField.RIGHT);
SimpleCalc.inst().add(this, c);
} |
84052f23-66e2-4510-acab-176a568e7363 | public boolean addText(String str) {
if(getText().length()+ str.length() <= getColumns()) {
setText(getText()+str);
return true;
}
return false;
} |
25513912-3ef7-4e72-ad84-ab6cba4726fa | public void showError() {
error = true;
setText("ERROR");
} |
f8739453-ffbc-4e77-9c84-2f0dc115f458 | public boolean isError() {
return error;
} |
02554455-b8fa-443b-a802-871214126038 | public void setIsError(boolean val) {
this.error = val;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.