├── README.md ├── Snake Game in Java ├── bin │ ├── Apple.class │ ├── Main.class │ ├── BodyPart.class │ ├── GamePanel.class │ ├── GamePanel$1.class │ ├── GamePanel$2.class │ └── java.policy.applet └── src │ ├── Main.java │ ├── Apple.java │ ├── BodyPart.java │ └── GamePanel.java ├── Super Mario Game ├── bin │ └── eu │ │ └── supermario │ │ └── implementation │ │ ├── Item.class │ │ ├── Main.class │ │ ├── State.class │ │ ├── SecondMain.class │ │ ├── SuperMario.class │ │ └── SuperMarioImplementation.class └── src │ └── eu │ └── supermario │ └── implementation │ ├── Item.class │ ├── Main.class │ ├── State.class │ ├── SuperMario.class │ ├── SuperMarioImplementation.class │ ├── Data.java │ ├── Main.java │ ├── SecondMain.java │ └── SuperMarioImplementation.java ├── Java GUI Calculator that evaluates strings └── src │ ├── javacodingcommunity.png │ ├── Evaluation.java │ └── Calculator.java ├── .gitignore ├── LICENSE └── Unit Convertor └── UnitConverter.java /README.md: -------------------------------------------------------------------------------- 1 | # JavaCodingCommunityRepository 2 | Snake Game & Unit Converter 3 | -------------------------------------------------------------------------------- /Snake Game in Java/bin/Apple.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Snake Game in Java/bin/Apple.class -------------------------------------------------------------------------------- /Snake Game in Java/bin/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Snake Game in Java/bin/Main.class -------------------------------------------------------------------------------- /Snake Game in Java/bin/BodyPart.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Snake Game in Java/bin/BodyPart.class -------------------------------------------------------------------------------- /Snake Game in Java/bin/GamePanel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Snake Game in Java/bin/GamePanel.class -------------------------------------------------------------------------------- /Snake Game in Java/bin/GamePanel$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Snake Game in Java/bin/GamePanel$1.class -------------------------------------------------------------------------------- /Snake Game in Java/bin/GamePanel$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Snake Game in Java/bin/GamePanel$2.class -------------------------------------------------------------------------------- /Snake Game in Java/bin/java.policy.applet: -------------------------------------------------------------------------------- 1 | /* AUTOMATICALLY GENERATED ON Tue Apr 16 17:20:59 EDT 2002*/ 2 | /* DO NOT EDIT */ 3 | 4 | grant { 5 | permission java.security.AllPermission; 6 | }; 7 | 8 | -------------------------------------------------------------------------------- /Super Mario Game/bin/eu/supermario/implementation/Item.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/bin/eu/supermario/implementation/Item.class -------------------------------------------------------------------------------- /Super Mario Game/bin/eu/supermario/implementation/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/bin/eu/supermario/implementation/Main.class -------------------------------------------------------------------------------- /Super Mario Game/bin/eu/supermario/implementation/State.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/bin/eu/supermario/implementation/State.class -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/Item.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/src/eu/supermario/implementation/Item.class -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/src/eu/supermario/implementation/Main.class -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/State.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/src/eu/supermario/implementation/State.class -------------------------------------------------------------------------------- /Super Mario Game/bin/eu/supermario/implementation/SecondMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/bin/eu/supermario/implementation/SecondMain.class -------------------------------------------------------------------------------- /Super Mario Game/bin/eu/supermario/implementation/SuperMario.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/bin/eu/supermario/implementation/SuperMario.class -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/SuperMario.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/src/eu/supermario/implementation/SuperMario.class -------------------------------------------------------------------------------- /Java GUI Calculator that evaluates strings/src/javacodingcommunity.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Java GUI Calculator that evaluates strings/src/javacodingcommunity.png -------------------------------------------------------------------------------- /Super Mario Game/bin/eu/supermario/implementation/SuperMarioImplementation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/bin/eu/supermario/implementation/SuperMarioImplementation.class -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/SuperMarioImplementation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javacodingcommunity/JavaCodingCommunityRepository/HEAD/Super Mario Game/src/eu/supermario/implementation/SuperMarioImplementation.class -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /Snake Game in Java/src/Main.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame; 2 | 3 | public class Main { 4 | public static JFrame frame = null; 5 | 6 | public Main() { 7 | 8 | frame = new JFrame(); 9 | GamePanel gamePanel = new GamePanel(); 10 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 11 | frame.add(gamePanel); 12 | 13 | frame.setTitle("The Snake Game by Java Coding Community"); 14 | frame.pack(); 15 | frame.setVisible(true); 16 | frame.setLocationRelativeTo(null); 17 | 18 | } 19 | 20 | public static JFrame returnFrame() { 21 | return frame; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | public static void main(String[] args) { 32 | new Main(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Snake Game in Java/src/Apple.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | public class Apple { 5 | 6 | private int xCoord, yCoord, width, height; // every Apple consists of these elements 7 | 8 | public Apple(int xCoord, int yCoord, int tileSize) { // constructor for the Apple. 9 | this.xCoord = xCoord; 10 | this.yCoord = yCoord; 11 | width = tileSize; 12 | height = tileSize; 13 | 14 | } 15 | 16 | public void draw(Graphics g) { // drawing the apple 17 | g.setColor(Color.YELLOW); // choose color 18 | g.fillRect(xCoord * width,yCoord*height, width,height); 19 | } 20 | 21 | public int getxCoord() { // getter for x 22 | return xCoord; 23 | } 24 | 25 | public void setxCoord(int xCoord) { // setter for x 26 | this.xCoord = xCoord; 27 | } 28 | 29 | public int getyCoord() { // getter for y 30 | return yCoord; 31 | } 32 | 33 | public void setyCoord(int yCoord) { // setter for y 34 | this.yCoord = yCoord; 35 | } 36 | } -------------------------------------------------------------------------------- /Snake Game in Java/src/BodyPart.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | public class BodyPart { 5 | 6 | private int xCoord, yCoord, width, height; // every Body Part is composed of these elements. 7 | 8 | public BodyPart(int xCoord, int yCoord, int tileSize) { // constructor for the Body Part. 9 | this.xCoord = xCoord; 10 | this.yCoord = yCoord; 11 | width = tileSize; 12 | height = tileSize; 13 | 14 | } 15 | 16 | 17 | 18 | public void draw(Graphics g) { // draws the Body Part 19 | 20 | g.setColor(Color.RED); // change the color 21 | g.fillRect(xCoord*width, yCoord * height, width, height); 22 | 23 | } 24 | 25 | public int getxCoord() { // getter for x 26 | return xCoord; 27 | } 28 | 29 | public void setxCoord(int xCoord) { // setter for x 30 | this.xCoord = xCoord; 31 | } 32 | 33 | public int getyCoord() { // getter for y 34 | return yCoord; 35 | } 36 | 37 | public void setyCoord(int yCoord) { // setter for y 38 | this.yCoord = yCoord; 39 | } 40 | 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 javacodingcommunity 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/Data.java: -------------------------------------------------------------------------------- 1 | package eu.supermario.implementation; 2 | 3 | interface SuperMario { 4 | 5 | // We've stored methods in side an interface 6 | // And we implement them in SuperMarioImplementation.java 7 | 8 | // NOTE: Mario MUST be alive to preform these actions 9 | 10 | void pick(Item item); // picks up and item and 11 | // changes the state depending on what he picked up 12 | 13 | public void receiveHit(); // if he is alive, he recieves a hit and changes the state 14 | 15 | 16 | public boolean fire(); // shoots fire if in correct state 17 | // returns true if possible, false otherwise 18 | 19 | 20 | public int getCointCount(); // returns and integer containing Coin Count 21 | 22 | 23 | public State getCurrentState(); // returns the Current State of Mario 24 | 25 | } 26 | 27 | enum Item { // storing items that Mario can pick up 28 | COIN, MUSHROOM, FLOWER // inside an enumeration 29 | } 30 | 31 | enum State { // storing Mario's states 32 | DEAD, SMALL_MARIO, BIG_MARIO, FIRE_MARIO // inside an enumeration 33 | 34 | } 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/Main.java: -------------------------------------------------------------------------------- 1 | package eu.supermario.implementation; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | 8 | 9 | SuperMarioImplementation sm = new SuperMarioImplementation(); // creating Super Mario 10 | Scanner sc = new Scanner(System.in); 11 | String line = ""; 12 | System.out.println("---------------------------Super Mario Version 1.0---------------------------"); 13 | System.out.println(".......................powered by @javacodingcommunity......................."); 14 | 15 | System.out.println(); 16 | System.out.println("Pringting possible commands..."); 17 | System.out.println("GET_COINS | GET_STATE | FIRE | SHROOM | COIN | FLOWER | HIT | "); 18 | System.out.println(); 19 | sm.getCointCount(); // returns 0 20 | sm.getCurrentState(); 21 | 22 | while(true) { 23 | if(sm.checkCurrentState() == State.DEAD) { 24 | System.out.println("Whoops. Mario is dead. Game over!"); 25 | break; 26 | } 27 | System.out.println("Enter a command or enter 'END' to exit: "); 28 | line = sc.nextLine(); 29 | 30 | if(line.contentEquals("END")) { 31 | System.out.println("Exiting..."); 32 | System.out.println("Sucessfully exited"); 33 | break; 34 | } 35 | else if(line.contentEquals("GET_COINS")) { 36 | sm.getCointCount(); 37 | } 38 | else if(line.contentEquals("GET_STATE")) { 39 | sm.getCurrentState(); 40 | } 41 | else if(line.contentEquals("FIRE")) { 42 | sm.fire(); 43 | } 44 | else if(line.contentEquals("SHROOM")) { 45 | sm.pick(Item.MUSHROOM); 46 | } 47 | else if(line.contentEquals("COIN")) { 48 | sm.pick(Item.COIN); 49 | } 50 | else if(line.contentEquals("FLOWER")) { 51 | sm.pick(Item.FLOWER); 52 | } 53 | else if(line.contentEquals("HIT")) { 54 | sm.receiveHit(); 55 | }}}} 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/SecondMain.java: -------------------------------------------------------------------------------- 1 | package eu.supermario.implementation; 2 | 3 | public class SecondMain { 4 | 5 | public static void main(String[] args) { 6 | // this main lets you run the game 7 | // and the computer will play it for you 8 | 9 | SuperMarioImplementation sm = new SuperMarioImplementation(); // creating Super Mario 10 | // adding coins 11 | 12 | 13 | sm.getCointCount(); // returns 0 14 | sm.getCurrentState(); 15 | 16 | 17 | 18 | for(int i=0; i<10;i++) { 19 | sm.pick(Item.COIN); 20 | } 21 | System.out.println(); 22 | sm.getCointCount(); // returns 13 23 | sm.getCurrentState(); // returns State.SMALL_MARIO 24 | System.out.println(); 25 | 26 | // Mario can't shoot as SMALL_MARIO 27 | sm.fire(); // returns false 28 | 29 | //picking up the shroom and changes states 30 | sm.pick(Item.MUSHROOM); 31 | System.out.println(); 32 | System.out.println(sm.getCurrentState()); // returns State.BIG_MARIO 33 | System.out.println(sm.fire()); // returns false, he can't shoot as BIG MARIO 34 | sm.pick(Item.MUSHROOM); // picks up the shroom 35 | System.out.println(sm.getCurrentState()); // returns State.BIG_MARIO 36 | System.out.println(sm.fire()); // returns false, can't shoot as BIG MARIO 37 | 38 | // picks up a flower 39 | sm.pick(Item.FLOWER); 40 | System.out.println("Picks up a flower!"); 41 | System.out.println(sm.getCurrentState()); // returns State.FIRE_MARIO 42 | System.out.println(sm.fire()); // returns true, he CAN shoot as FIRE_MARIO 43 | sm.pick(Item.FLOWER); 44 | System.out.println(sm.getCurrentState()); // returns State.FIRE_MARIO 45 | System.out.println(sm.fire()); // returns true, he CAN shoot as BIG MARIO 46 | 47 | //takes a hit 48 | sm.receiveHit(); 49 | System.out.println("Mario recieved a hit"); 50 | System.out.println(sm.getCurrentState()); // returns State.SMALL_MARIO 51 | sm.pick(Item.FLOWER); // picks up the flower 52 | System.out.println(sm.getCurrentState()); // returns State.BIG_MARIO 53 | 54 | sm.receiveHit(); 55 | System.out.println(sm.getCurrentState()); // returns State.SMALL_MARIO 56 | 57 | sm.receiveHit(); 58 | System.out.println(sm.getCurrentState()); // returns State.DEAD 59 | 60 | sm.receiveHit(); 61 | sm.getCurrentState(); // returns State.DEAD 62 | 63 | // checking if our conditions work while dead 64 | sm.pick(Item.FLOWER); 65 | System.out.println(sm.getCurrentState()); // returns State.DEAD 66 | sm.pick(Item.MUSHROOM); 67 | System.out.println(sm.getCurrentState()); // returns State.DEAD 68 | 69 | sm.pick(Item.COIN); 70 | System.out.println(sm.getCointCount()); // returns 13, he didn't loose his coins 71 | 72 | 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /Super Mario Game/src/eu/supermario/implementation/SuperMarioImplementation.java: -------------------------------------------------------------------------------- 1 | package eu.supermario.implementation; 2 | 3 | 4 | public class SuperMarioImplementation implements SuperMario { 5 | 6 | 7 | private int coin_count; 8 | private State state; 9 | 10 | public SuperMarioImplementation() { // Constructor that sets him to a default state 11 | this.coin_count = 0; 12 | this.state = State.SMALL_MARIO; 13 | // 0 coints and SMALL_MARIO 14 | } 15 | 16 | 17 | @Override //overriding methods from SuperMario interface in Data.java 18 | public void pick(Item item) { 19 | if(state != State.DEAD) { // only do this while Mario is alive 20 | if(item == Item.COIN) { 21 | System.out.println("You picked up a coin!"); 22 | coin_count++; // adding the coint count 23 | } 24 | else if(item == Item.MUSHROOM) { // if Mario picks up a shroom 25 | System.out.println("You picked up a shroom!"); 26 | if(state == State.SMALL_MARIO) { // he changes his state to BIG_MARIO 27 | this.state = State.BIG_MARIO; 28 | } 29 | 30 | } 31 | else if(item == Item.FLOWER) { // if he picks up a flower 32 | System.out.println("You picked up a flower!"); 33 | if(state == State.SMALL_MARIO) { // 2 things can happen 34 | this.state = State.BIG_MARIO; // from SMALL go to BIG 35 | } 36 | else if(state == State.BIG_MARIO) { // from BIG go to FIRE 37 | this.state = State.FIRE_MARIO; 38 | System.out.println("Woah, now you can FIRE!"); 39 | }}}} 40 | 41 | 42 | 43 | @Override 44 | public void receiveHit() { 45 | 46 | if(state != State.DEAD) { // only do this while Mario is alive 47 | if(state == State.BIG_MARIO || state == State.FIRE_MARIO) { // Mario gets hit, he changes his state 48 | System.out.println("Mario recieved a hit!"); 49 | this.state = State.SMALL_MARIO; 50 | System.out.println("His new state is "+state); 51 | } 52 | 53 | else if(state == State.SMALL_MARIO) { // if he is SMALL_MARIO and gets hit 54 | System.out.println("Mario recieved a hit!"); 55 | this.state = State.DEAD; // he is DEAD 56 | System.out.println("Game over."); 57 | }}} 58 | 59 | @Override 60 | public boolean fire() { 61 | 62 | if(this.state == State.FIRE_MARIO) { // no need for DEAD condition as he 63 | System.out.println("Mario Fires at the enemies!"); 64 | return true; // needs to be in FIRE_MARIO anyways 65 | } 66 | System.out.println("You can't fire yet, Mario!"); 67 | return false; 68 | } 69 | 70 | @Override 71 | public int getCointCount() { // returns coin_count; 72 | System.out.println("Mario has: "+coin_count+ " coins"); 73 | if(coin_count > 10) { 74 | System.out.println("Wow, Mario. Could you lend me some?"); 75 | } 76 | return coin_count;} 77 | 78 | @Override 79 | public State getCurrentState() { // returns current state; 80 | // TODO Auto-generated method stub 81 | System.out.println("Mario's current state is: "+ state); 82 | return state; 83 | } 84 | 85 | public State checkCurrentState() { 86 | return state; } 87 | } 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /Java GUI Calculator that evaluates strings/src/Evaluation.java: -------------------------------------------------------------------------------- 1 | 2 | public class Evaluation { 3 | 4 | // code in this Class is written by StackOverflow user: Boann 5 | // public code for evaluation of math expressions from text 6 | 7 | public static double eval(final String str) { 8 | return new Object() { 9 | int pos = -1, ch; 10 | 11 | void nextChar() { 12 | ch = (++pos < str.length()) ? str.charAt(pos) : -1; 13 | } 14 | 15 | boolean eat(int charToEat) { 16 | while (ch == ' ') nextChar(); 17 | if (ch == charToEat) { 18 | nextChar(); 19 | return true; 20 | } 21 | return false; 22 | } 23 | 24 | double parse() { 25 | nextChar(); 26 | double x = parseExpression(); 27 | if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch); 28 | return x; 29 | } 30 | 31 | // Grammar: 32 | // expression = term | expression `+` term | expression `-` term 33 | // term = factor | term `*` factor | term `/` factor 34 | // factor = `+` factor | `-` factor | `(` expression `)` 35 | // | number | functionName factor | factor `^` factor 36 | 37 | double parseExpression() { 38 | double x = parseTerm(); 39 | for (;;) { 40 | if (eat('+')) x += parseTerm(); // addition 41 | else if (eat('-')) x -= parseTerm(); // subtraction 42 | else return x; 43 | } 44 | } 45 | 46 | double parseTerm() { 47 | double x = parseFactor(); 48 | for (;;) { 49 | if (eat('*')) x *= parseFactor(); // multiplication 50 | else if (eat('/')) x /= parseFactor(); // division 51 | else return x; 52 | } 53 | } 54 | 55 | double parseFactor() { 56 | if (eat('+')) return parseFactor(); // unary plus 57 | if (eat('-')) return -parseFactor(); // unary minus 58 | 59 | double x; 60 | int startPos = this.pos; 61 | if (eat('(')) { // parentheses 62 | x = parseExpression(); 63 | eat(')'); 64 | } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers 65 | while ((ch >= '0' && ch <= '9') || ch == '.') nextChar(); 66 | x = Double.parseDouble(str.substring(startPos, this.pos)); 67 | } else if (ch >= 'a' && ch <= 'z') { // functions 68 | while (ch >= 'a' && ch <= 'z') nextChar(); 69 | String func = str.substring(startPos, this.pos); 70 | x = parseFactor(); 71 | if (func.equals("sqrt")) x = Math.sqrt(x); 72 | else if (func.equals("sin")) x = Math.sin(Math.toRadians(x)); 73 | else if (func.equals("cos")) x = Math.cos(Math.toRadians(x)); 74 | else if (func.equals("tan")) x = Math.tan(Math.toRadians(x)); 75 | else throw new RuntimeException("Unknown function: " + func); 76 | } else { 77 | throw new RuntimeException("Unexpected: " + (char)ch); 78 | } 79 | 80 | if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation 81 | 82 | return x; 83 | } 84 | }.parse(); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Unit Convertor/UnitConverter.java: -------------------------------------------------------------------------------- 1 | package javacodingcommunity.main; 2 | 3 | import java.awt.Dimension; 4 | import java.awt.FlowLayout; 5 | import java.awt.Frame; 6 | import java.awt.GridLayout; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | import java.awt.event.KeyAdapter; 10 | import java.awt.event.KeyEvent; 11 | 12 | import javax.swing.*; 13 | 14 | public class UnitConverter { 15 | private boolean state = true; // default state 16 | public UnitConverter() { 17 | 18 | JFrame frame = new JFrame("Unit Converter v.1.0 by Java Coding Community"); // title 19 | 20 | frame.setLocationRelativeTo(null); 21 | // first row 22 | // adding labels 23 | JButton convert1 = new JButton("Convert"); 24 | JLabel label1 = new JLabel("Enter km/h :"); 25 | label1.setHorizontalAlignment(SwingConstants.CENTER); 26 | label1.setVerticalAlignment(SwingConstants.CENTER); 27 | JLabel end_label1 = new JLabel("m/s"); 28 | end_label1.setHorizontalAlignment(SwingConstants.CENTER); 29 | end_label1.setVerticalAlignment(SwingConstants.CENTER); 30 | JTextField textfield1= new JTextField(); 31 | textfield1.setHorizontalAlignment(SwingConstants.CENTER); 32 | JTextField output1 = new JTextField(); 33 | 34 | // when you hit the button convert1 35 | // this happens 36 | 37 | convert1.addActionListener(new ActionListener() { 38 | 39 | @Override 40 | public void actionPerformed(ActionEvent e) { 41 | if(state) { // checks the state 42 | // this allows us to switch between states 43 | double kilometers_per_hour = Double.parseDouble(textfield1.getText()); 44 | double meters_per_second = 5*kilometers_per_hour/18; 45 | output1.setText(String.format("%.10g",meters_per_second)); 46 | } 47 | else { 48 | 49 | double meters_per_second = Double.parseDouble(textfield1.getText()); 50 | double kilometers_per_hour = 3.6* meters_per_second; 51 | output1.setText(String.format("%.10g",kilometers_per_hour)); 52 | 53 | } 54 | // converts 55 | } 56 | }); 57 | 58 | // second row 59 | JButton convert2 = new JButton("Convert"); 60 | convert2.setVerticalAlignment(SwingConstants.CENTER); 61 | convert2.setHorizontalAlignment(SwingConstants.CENTER); 62 | JLabel end_label2 = new JLabel("Kilometers"); 63 | end_label2.setVerticalAlignment(SwingConstants.CENTER); 64 | end_label2.setHorizontalAlignment(SwingConstants.CENTER); 65 | JLabel label2 = new JLabel(); 66 | label2.setText("Enter Miles :"); 67 | label2.setVerticalAlignment(SwingConstants.CENTER); 68 | label2.setHorizontalAlignment(SwingConstants.CENTER); 69 | JTextField textfield2= new JTextField(); 70 | textfield2.setHorizontalAlignment(SwingConstants.CENTER); 71 | JTextField text_output2 = new JTextField(); 72 | 73 | convert2.addActionListener(new ActionListener() { 74 | 75 | @Override 76 | public void actionPerformed(ActionEvent e) { 77 | if(state) { 78 | double miles = Double.parseDouble(textfield2.getText()); 79 | double kilometers = miles * 1.609344; 80 | text_output2.setText(String.format("%.10g",kilometers)); 81 | } 82 | else { 83 | double kilometers = Double.parseDouble(textfield2.getText()); 84 | double miles = kilometers / 1.609344; 85 | text_output2.setText(String.format("%.10g",miles)); 86 | } 87 | 88 | } 89 | }); 90 | 91 | 92 | // third row 93 | JButton convert3 = new JButton("Convert"); 94 | convert3.setVerticalAlignment(SwingConstants.CENTER); 95 | convert3.setHorizontalAlignment(SwingConstants.CENTER); 96 | JLabel end_label3 = new JLabel("Dollars"); 97 | end_label3.setVerticalAlignment(SwingConstants.CENTER); 98 | end_label3.setHorizontalAlignment(SwingConstants.CENTER); 99 | JLabel label3 = new JLabel(); 100 | label3.setText("Enter Euros :"); 101 | label3.setVerticalAlignment(SwingConstants.CENTER); 102 | label3.setHorizontalAlignment(SwingConstants.CENTER); 103 | JTextField textfield3= new JTextField(); 104 | textfield3.setHorizontalAlignment(SwingConstants.CENTER); 105 | JTextField text_output3 = new JTextField(); 106 | 107 | convert3.addActionListener(new ActionListener() { 108 | 109 | @Override 110 | public void actionPerformed(ActionEvent e) { 111 | if(state) { 112 | 113 | 114 | double euro = Double.parseDouble(textfield3.getText()); 115 | double dollars = euro * 1.09; 116 | text_output3.setText(String.format("%.5g",dollars)); 117 | } 118 | else { 119 | double dollars = Double.parseDouble(textfield3.getText()); 120 | double euro = dollars * 0.92; 121 | text_output3.setText(String.format("%.5g",euro)); 122 | } 123 | 124 | } 125 | }); 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | frame.setSize(new Dimension(600,300)); // dimensions 136 | frame.setResizable(false); // resizable = false 137 | frame.setVisible(true); // visible 138 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 139 | 140 | GridLayout gl = new GridLayout(0, 5,10,5); // we use 141 | // the grid layout 142 | 143 | 144 | //ADDING COMPONETNS 145 | 146 | // first row 147 | frame.setLayout(gl); 148 | frame.add(label1); 149 | frame.add(textfield1); 150 | frame.add(convert1); 151 | frame.add(output1); 152 | frame.add(end_label1); 153 | 154 | // second row 155 | frame.add(label2); 156 | frame.add(textfield2); 157 | frame.add(convert2); 158 | frame.add(text_output2); 159 | frame.add(end_label2); 160 | 161 | 162 | // third row 163 | 164 | frame.add(label3); 165 | frame.add(textfield3); 166 | frame.add(convert3); 167 | frame.add(text_output3); 168 | frame.add(end_label3); 169 | 170 | // switch units button 171 | 172 | JButton switch_button = new JButton("Switch Units"); 173 | frame.add(switch_button); 174 | 175 | switch_button.addActionListener(new ActionListener() { 176 | 177 | @Override 178 | public void actionPerformed(ActionEvent e) { 179 | // this button switches the state, and updates the text 180 | if(state) { 181 | state = false; 182 | label1.setText("Enter m/s:"); 183 | end_label1.setText("km/h"); 184 | label2.setText("Enter Kilometers: "); 185 | end_label2.setText("Miles"); 186 | label3.setText("Enter Dollars: "); 187 | end_label3.setText("Euros"); 188 | } 189 | else if(!state) { 190 | state = true; 191 | label1.setText("Enter km/h: "); 192 | end_label1.setText("m/s"); 193 | label2.setText("Enter Miles: "); 194 | end_label2.setText("Kilometers"); 195 | label3.setText("Enter Euros: "); 196 | end_label3.setText("Dollars"); 197 | } 198 | 199 | 200 | } 201 | }); 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | } 212 | 213 | 214 | public static void main(String[] args) { 215 | new UnitConverter(); 216 | 217 | } 218 | } 219 | 220 | 221 | 222 | -------------------------------------------------------------------------------- /Snake Game in Java/src/GamePanel.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Dimension; 3 | import java.awt.FlowLayout; 4 | import java.awt.Graphics; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | import java.awt.event.KeyEvent; 8 | import java.awt.event.KeyListener; 9 | import java.awt.event.WindowEvent; 10 | import java.util.ArrayList; 11 | import java.util.Random; 12 | 13 | import javax.swing.JButton; 14 | import javax.swing.JFrame; 15 | import javax.swing.JLabel; 16 | import javax.swing.JOptionPane; 17 | import javax.swing.JPanel; 18 | 19 | public class GamePanel extends JPanel implements Runnable, KeyListener { 20 | 21 | private static final long serialVersionUID = 1L; 22 | 23 | public static final int WIDTH = 500, HEIGHT = 500; // width and height of our window 24 | 25 | private Thread thread; // we need a Thread so it can execute along with main 26 | // this thread starts the run function 27 | 28 | private boolean running; // boolean that keeps track of game state 29 | 30 | private boolean right = true, left = false, up = false, down = false; // keeping track of where 31 | // we are moving 32 | 33 | private BodyPart b; // a single body part 34 | 35 | private ArrayList snake; // array of body parts makes up a snake 36 | 37 | private Apple apple; // a single apple 38 | private ArrayList apples; 39 | 40 | private Random r; // for creating random coordinates for an apple 41 | 42 | public int score = 0; // keeps track of score 43 | 44 | 45 | 46 | private int xCoord = 10, yCoord = 10, size = 5; // where snake starts, and its size 47 | 48 | private int ticks = 0; // game speed 49 | 50 | 51 | 52 | public GamePanel() { // constructor for game panel 53 | // called when game is starting 54 | setFocusable(true); 55 | 56 | setPreferredSize(new Dimension(WIDTH,HEIGHT)); 57 | addKeyListener(this); // keeps track of key pressess 58 | snake = new ArrayList(); // initialize snake 59 | apples = new ArrayList(); // initialize apples 60 | 61 | r = new Random(); // initialize random 62 | 63 | start(); // starts the game 64 | 65 | 66 | } 67 | 68 | public void start() { 69 | running = true; // running is set to true when game starts 70 | thread = new Thread(this); // thread that keeps running without caring for Main 71 | thread.start(); 72 | } 73 | public void stop() { 74 | 75 | running = false; // if we stop, running goes to false 76 | // now we create the Game Over Screen 77 | JFrame f=new JFrame("Game Over Screen"); 78 | JButton b=new JButton("Press this to play again!"); 79 | JLabel l = new JLabel("Your score was: "+ score); 80 | f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 81 | f.setPreferredSize(new Dimension(500,500)); 82 | JButton close = new JButton("Quit!"); 83 | b.setBounds(130,200,250, 40); 84 | f.add(l); 85 | f.add(b); 86 | f.add(close); 87 | f.setLayout(new FlowLayout()); 88 | f.pack(); 89 | f.setVisible(true); 90 | f.setLocationRelativeTo(null); 91 | Main.returnFrame().dispatchEvent(new WindowEvent(Main.returnFrame(), WindowEvent.WINDOW_CLOSING)); 92 | Main.returnFrame().dispose(); 93 | b.addActionListener(new ActionListener() { 94 | 95 | @Override 96 | public void actionPerformed(ActionEvent e) { 97 | // if we hit play again --> we start the main again 98 | f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING)); 99 | Main.main(null); 100 | } 101 | }); 102 | close.addActionListener(new ActionListener() { 103 | // if we hit quit --> we quit the program 104 | @Override 105 | public void actionPerformed(ActionEvent e) { 106 | System.exit(0); 107 | } 108 | }); 109 | try { 110 | thread.join(); 111 | } catch (InterruptedException e) { 112 | e.printStackTrace(); 113 | } 114 | } 115 | 116 | public void tick() { // every tick, likes frame per second 117 | if(snake.size() == 0) { // if the snake doesn't exist, we create it 118 | b = new BodyPart(xCoord, yCoord, 10); 119 | snake.add(b); // add the body part to snake list 120 | } 121 | ticks++; // now we wait for ticks to get to 750 000 122 | // chanche this depending on what speed you want 123 | if(ticks > 750000) { 124 | 125 | // these are self explanatory, with every move we change the coordinates 126 | if(right) xCoord++; 127 | if(left) xCoord--; 128 | if(up) yCoord--; 129 | if(down) yCoord++; 130 | 131 | ticks = 0; // set ticks to 0 132 | 133 | b = new BodyPart(xCoord, yCoord, 10); // we create a body part 134 | snake.add(b); // add it to the snake 135 | 136 | if(snake.size() > size ) { 137 | snake.remove(0); // remove the body part if the snake didn't increase 138 | } 139 | } 140 | if(apples.size() == 0) { // if there are no apples 141 | // we create them 142 | int xCoord = r.nextInt(49); 143 | int yCoord = r.nextInt(49); 144 | // with random coordinates 145 | 146 | apple = new Apple(xCoord, yCoord, 10); 147 | apples.add(apple); 148 | } 149 | 150 | 151 | // now we check if we ate an apple 152 | for(int i = 0;i< apples.size();i++) { 153 | if(xCoord == apples.get(i).getxCoord() && yCoord == apples.get(i).getyCoord()) { 154 | // if the current coordinates are the same as apples 155 | // we increase the size and the score 156 | // and remove the apple 157 | score++; 158 | size++; 159 | apples.remove(i); 160 | } 161 | 162 | 163 | } // we check if we hit the border 164 | if(xCoord < 0 || xCoord > 49 || yCoord < 0 || yCoord > 49) { 165 | JOptionPane.showMessageDialog(null, "Game Over!"); 166 | 167 | stop(); 168 | } 169 | // we check if we hit our tail 170 | for(int i =0;i < snake.size(); i++) { 171 | if(xCoord == snake.get(i).getxCoord() && yCoord == snake.get(i).getyCoord()) { 172 | if( i!= snake.size()-1) { 173 | JOptionPane.showMessageDialog(null, "Game Over!"); 174 | 175 | stop(); 176 | } 177 | } 178 | } 179 | } 180 | 181 | public void paint(Graphics g) { 182 | g.clearRect(0, 0, WIDTH, HEIGHT); // clears the screen 183 | g.setColor(Color.BLACK); 184 | g.fillRect(0, 0, WIDTH, HEIGHT); // fills it with black 185 | 186 | for(int i = 0;i 197 | for(int i = 0;i< snake.size();i++ ) { 198 | snake.get(i).draw(g); 199 | } 200 | // draws apples in the same way 201 | for(int i = 0;i