battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

descriptionauto programming dev Emptyauto programming dev

more_horiz
http://aidreams.co.uk/forum/index.php?topic=12776.0

auto programming dev 22dl0b

descriptionauto programming dev Emptydeck build Auto program puzzle

more_horiz
card class :

Code:


package gambit;

public class Card {
 String type, color;
 int level;
 public void printCard() {
 
 switch (level) {
 case 1:System.out.println("Ace of " + type);break;
 case 11:System.out.println("prince of " + type);break;
 case 12:System.out.println("queen of " + type);break;
 case 13:System.out.println("king of " + type);break;
 default:System.out.println(color + " " + type + " level " + level);break;
 }
 }
}

deck class :

Code:


package gambit;

public class Deck {
 Card[] cards = new Card[52];
 public void initDeck() {
 for (int i = 0; i < 13; i++) {
 cards[i] = new Card();
 cards[i].type = "clubs";
 cards[i].color = "black";
 cards[i].level = i+1;
 }
 for (int i = 13; i < 26; i++) {
 cards[i] = new Card();
 cards[i].type = "hearts";
 cards[i].color = "red";
 cards[i].level = i-12;
 }
 for (int i = 26; i < 39; i++) {
 cards[i] = new Card();
 cards[i].type = "spades";
 cards[i].color = "black";
 cards[i].level = i-25;
 }
 for (int i = 39; i < 52; i++) {
 cards[i] = new Card();
 cards[i].type = "diamonds";
 cards[i].color = "red";
 cards[i].level = i-38;
 }
 }
 public void printDeck() {
 for (int i = 0; i < 52; i++) {
 cards[i].printCard();
 }
 
 }
}

Main class : (set as public static void main)

Code:


package gambit;

public class Main {

 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Deck d1 = new Deck();
 d1.initDeck();
 d1.printDeck();
 }

}


auto programming dev 22zeog

descriptionauto programming dev EmptyRe: auto programming dev

more_horiz
class with constructor getter and setter

Code:

package morseCodePackage;

public class morseCode {
   private String user; // click on var and type alt + shift + s then select generate getters setters
   public String getUser() {
      return user;
   }
   public void setUser(String user) {
      this.user = user;
   }
   private static String lastMsg;
   public morseCode(String user) {this.user = user;} // I'm a constructor, use this. if the param names are
   // the same as the classes
   public String toMorse(String msg) {lastMsg = code(msg);return this.user + " " + this.lastMsg;}
   public String toHuman(String msg) {return this.user + " " + decoder(msg);}
   public static String code(String msg) {
      char[] arr = msg.toCharArray();
      String result ="";
      for (int i = 0; i < arr.length; i++) {
         
         result += charToMorse(arr[i]);
      }
      return result;
   }
   private static String charToMorse(char x) {
      String result="";
      switch (x) {
      case 'a': result = "*-";break;
      case 'b': result = "-***";break;
      case 'c': result = "-*-*";break;
      case 'd': result = "-**";break;
      case 'e': result = "*";break;
      case 'f': result = "**-*";break;
      case 'g': result = "--*";break;
      case 'h': result = "****";break;
      case 'i': result = "**";break;
      case 'j': result = "*---";break;
      case 'k': result = "-*-";break;
      case 'l': result = "*-**";break;
      case 'm': result = "--";break;
      case 'n': result = "-*";break;
      case 'o': result = "---";break;
      case 'p': result = "*--*";break;
      case 'q': result = "--*-";break;
      case 'r': result = "*-*";break;
      case 's': result = "***";break;
      case 't': result = "-";break;
      case 'u': result = "**-";break;
      case 'v': result = "***-";break;
      case 'w': result = "*--";break;
      case 'x': result = "-**-";break;
      case 'y': result = "-*--";break;
      case 'z': result = "--**";break;
      case '0': result = "-----";break;
      case '1': result = "*----";break;
      case '2': result = "**---";break;
      case '3': result = "***--";break;
      case '4': result = "****-";break;
      case '5': result = "*****";break;
      case '6': result = "-****";break;
      case '7': result = "--***";break;
      case '8': result = "---**";break;
      case '9': result = "----*";break;
      case '.': result = "*-*-*-";break;
      case ',': result = "--**--";break;
      case '?': result = "**--**";break;
      case ' ': result = "/";break;
      
      default:
         break;
      }
      return result + "/";
   }
   public static String decoder(String msg) {
      
      String morseChr ="";
      String result = "";
      char[] arr = msg.toCharArray();
      for (int i = 0; i < arr.length; i++) {
         if(arr[i]!='/') {morseChr += arr[i];}
         else {result += morseCharToChar(morseChr);morseChr="";}
      }
      result = result.replaceAll("  ", " ");
      return result;
   }
   private static char morseCharToChar(String x) {
      char result='@';
      switch (x) {
      case "*-": result = 'a';break;
      case "-***": result = 'b';break;
      case "-*-*": result = 'c';break;
      case "-**": result = 'd';break;
      case "*": result = 'e';break;
      case "**-*": result ='f' ;break;
      case "--*": result = 'g';break;
      case "****": result = 'h';break;
      case "**": result = 'i';break;
      case "*---": result = 'j';break;
      case "-*-": result = 'k';break;
      case "*-**": result = 'l';break;
      case "--": result = 'm';break;
      case "-*": result = 'n';break;
      case "---": result = 'o';break;
      case "*--*": result = 'p';break;
      case "--*-": result = 'q';break;
      case "*-*": result = 'r';break;
      case "***": result = 's';break;
      case "-": result = 't';break;
      case "**-": result = 'u';break;
      case "***-": result = 'v';break;
      case "*--": result = 'w';break;
      case "-**-": result = 'x';break;
      case "-*--": result = 'y';break;
      case "--**": result = 'z';break;
      case "-----": result = '0';break;
      case "*----": result = '1';break;
      case "**---": result = '2';break;
      case "***--": result = '3';break;
      case "****-": result = '4';break;
      case "*****": result = '5';break;
      case "-****": result = '6';break;
      case "--***": result = '7';break;
      case "---**": result = '8';break;
      case "----*": result = '9';break;
      case "*-*-*-": result = '.';break;
      case "--**--": result =',' ;break;
      case "**--**": result = '?';break;
      case "/": result = ' ';break;
      
      default:
         break;
      }
      if(result == '@') {result = ' ';}
      return result;
   }
   public static void describeME() {System.out.println("I'm a class that can code and decode morse code");}
}

main :

Code:

package morseCodePackage;

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      System.out.println(morseCode.code("hadouken"));
      System.out.println(morseCode.code("over 9000"));
      String n1 = morseCode.code("hadouken");
      System.out.println(morseCode.decoder(n1));
      morseCode mc1 = new morseCode("moti");
      System.out.println(mc1.toMorse("souryuken"));
      System.out.println(mc1.toHuman("****/*-/-**/---/**-/-*-/*/-*/"));
      morseCode.describeME();
   }

}




auto programming dev 23a5bd

descriptionauto programming dev Emptypolymorphism

more_horiz
https://www.yotamarker.com/t144-polymorphism-in-java-eclipse#276

:!:

descriptionauto programming dev EmptyRe: auto programming dev

more_horiz
singleton is a object that can be created only x amount of times, usually once.
used for objects like server connection and rare game objects.

singleton class :

Code:

package singleton;

public class Singleton {
   private static Singleton singleton; // array this to get x number of singletons enabled
   private Singleton() {
      System.out.println("new singleton instace has been created");
   }
   public static Singleton newInstance() {
      if(singleton == null) {
         singleton = new Singleton();
      }
      return singleton;
   }
}


summoning the singleton object :

Code:

package singleton;

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Singleton sing1 = Singleton.newInstance(); // can only create 1, all others point to the same object
      
      
   }

}


auto programming dev 23pdom

descriptionauto programming dev Emptymorphing the deck class into a singleton

more_horiz

Code:

package singleton;

public class Deck {
   private static Deck singleton;
   private Card[] cards = new Card[52];
   private Deck() {
      initDeck();
      System.out.println("new singleton instace has been created");
   }
   public static Deck newInstance() {
      if(singleton == null) {
         singleton = new Deck();
      }
      return singleton;
   }
    public void initDeck() {
    for (int i = 0; i < 13; i++) {
    cards[i] = new Card();
    cards[i].type = "clubs";
    cards[i].color = "black";
    cards[i].level = i+1;
    }
    for (int i = 13; i < 26; i++) {
    cards[i] = new Card();
    cards[i].type = "hearts";
    cards[i].color = "red";
    cards[i].level = i-12;
    }
    for (int i = 26; i < 39; i++) {
    cards[i] = new Card();
    cards[i].type = "spades";
    cards[i].color = "black";
    cards[i].level = i-25;
    }
    for (int i = 39; i < 52; i++) {
    cards[i] = new Card();
    cards[i].type = "diamonds";
    cards[i].color = "red";
    cards[i].level = i-38;
    }
    }
    public void printDeck() {
    for (int i = 0; i < 52; i++) {
    cards[i].printCard();
    }
   
    }
}


summoning a singleton Deck object (itch turn ichido(one time)) :

Code:

package singleton;

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Deck sing1 = Deck.newInstance(); // can only create 1, all others point to the same object
      
      
   }

}


auto programming dev 23pe9r

descriptionauto programming dev EmptyRe: auto programming dev

more_horiz

descriptionauto programming dev EmptyRe: auto programming dev

more_horiz
an abs class doesn't have to have a constructor.

Code:

public abstract class AbsShape {
   public abstract int Area();
}


2nd class :

Code:


import java.util.regex.Matcher;

public class Square extends AbsShape{
   private int side;
   
   public int getSide() {
      return side;
   }

   public void setSide(int side) {
      this.side = side;
   }

   public Square(int side) {
      super();
      this.side = side;
   }

   @Override
   public int Area() {
      // TODO Auto-generated method stub
      return (int)Math.pow(side, 2); // powe ^
   }

}


Code:

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      AbsShape ribua = new Square(5);
      System.out.println("area: " + ribua.Area());
   }

}

descriptionauto programming dev Emptyabstract class

more_horiz
an abstract class doesn't have to have a constructor.

Code:

public abstract class BaseClass {
   protected int x = 100; // visible to this base class and sub classes(that extend it)
   protected int y = 150;
   
   public int getX() {
      return x;
   }

   public void setX(int x) {
      this.x = x;
   }

   public int getY() {
      return y;
   }

   public void setY(int y) {
      this.y = y;
   }

   public abstract void AbstractMethode();
   
}


derived class :

Code:

public class DerivedClass extends BaseClass{
   
   @Override
   public void AbstractMethode() {this.x++;this.y++;}
}


main :

Code:

public class Main {

   public static void main(String[] args) {
      BaseClass patamon = new DerivedClass();
      patamon.AbstractMethode();
      System.out.println(patamon.getX() + " " + patamon.getY());
   }

}


output : 101 151

nani mo gissei nashi ni nani mo eru koto ga dekinai

descriptionauto programming dev Emptyjava interface jutsu

more_horiz
an interface is like an item an object class can be equiped with.
interface contains methode signatures that has to be implemented in your object class.

right click the project folder in the package explorer window, interface.

Code:

public interface MyCalc {
   public int Add(int x, int y);
   public int subtract(int x, int y);
   public int Multiple(int x, int y);
   public int div(int x, int y);
}



2nd interface :

Code:

public interface MyCalc2 {
   public int Multiple(int x, int y);
   public int sum(int ... sigma);
}


create a class and add implements MyCalc,MyCalc2.
next click the error line under the class name and click add unimplemented methods.
finnally fill in the methodes body.
to get :

Code:

public class MainClass implements MyCalc,MyCalc2 {

   @Override
   public int sum(int... sigma) {
      // TODO Auto-generated method stub
      int result = 0;
      for (int i = 0; i < sigma.length; i++) {
         result += sigma[i];
      }
      return result;
   }

   @Override
   public int Add(int x, int y) {
      // TODO Auto-generated method stub
      return x+y;
   }

   @Override
   public int subtract(int x, int y) {
      // TODO Auto-generated method stub
      return x-y;
   }

   @Override
   public int Multiple(int x, int y) {
      // TODO Auto-generated method stub
      return x*y;
   }

   @Override
   public int div(int x, int y) {
      // TODO Auto-generated method stub
      return x/y;
   }

}


and creat some main class to test MainClass

:pirat:

descriptionauto programming dev Emptylink extra

more_horiz
https://www.yotamarker.com/t145-java-class-with-learnability-exampled-with-simple-car-class

descriptionauto programming dev EmptyRe: auto programming dev

more_horiz


(concept) summoning requests from cmds :
token class (the above pattern) :

Code:

import java.util.HashSet;
import java.util.Scanner;

public class TokenClass {
   private static HashSet<String> CMDs = new HashSet<>();
   // AGI, goals
   public static void enable(String cmd, String skill) {CMDs.add(cmd+" " + skill);}
   public static void disable(String cmd, String skill) {CMDs.remove(cmd+" " + skill);}
   public static boolean isAble(String cmd, String skill) {return CMDs.contains(cmd+" " + skill);}
//   public static void enableWithGoalBundle(String bundle, String cmd, String skill) {
//      CMDs.add(bundle + " " + cmd + " " + skill);
//      }
   public void Activate(String cmd, String g) {
      if(isAble(cmd, g)) {
         switch (g) {
         case "create":
            Scanner s1 = new Scanner(System.in);System.out.println("enter name");
            String x = s1.next();
            System.out.println("enter field_value_field_value...");
            String y = s1.next();
            y= y.replaceAll("_", " ");
            JsonCRUDA.create(x, y);
            break;

         default:
            break;
         }
      }
      else {System.out.println("unAble to do that");}
   }
}


JSON Create Remove Update Delete Add

Code:

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonCRUDA{
   private static JSONObject cars = new JSONObject();
   public static JSONObject getCars() {
      return cars;
   }

   public static boolean create(String name, String attributes) {
      // TODO Auto-generated method stub
      String[] strArr = attributes.split(" ");
      cars.put(name, new JSONArray());
      cars.getJSONArray(name).put(new JSONObject());
      for (int i = 0; i < strArr.length / 2; i++) {
         cars.getJSONArray(name).getJSONObject(0).put(strArr[i*2], strArr[i*2+1]);
      }
      System.out.println(cars);
      
      return false;
   }

   public static String Read(String name, String Id, String field) {
      // TODO Auto-generated method stub
      String result="";
      JSONArray jArr = cars.getJSONArray(name);
        for (Object object : jArr) {
          if(((JSONObject)object).get("id").equals(Id))
          {result = ((JSONObject)object).get(field).toString();break;}
        }
      return result;
   }

   public static boolean Update(String name, String Id, String field, String newVal) {
      // TODO Auto-generated method stub
      JSONArray jArr = cars.getJSONArray(name);
        for (Object object : jArr) {
          if(((JSONObject)object).get("id").equals(Id))
          {((JSONObject)object).put(field, newVal);break;}
         
        }
        System.out.println(cars);
      return false;
   }

   public static boolean delete(String name) {
      // TODO Auto-generated method stub
      cars.remove(name);
      System.out.println(cars);
      return false;
   }

   public static boolean Add(String name, String attributes) {
      // TODO Auto-generated method stub
      String[] strArr = attributes.split(" ");
      cars.put(name, new JSONArray());
      cars.getJSONArray(name).put(new JSONObject());
      for (int i = 0; i < strArr.length / 2; i++) {
         cars.getJSONArray(name).put(new JSONObject().put(strArr[i*2], strArr[i*2+1]));
      }
      System.out.println(cars);
      return false;
   }

}


Json Print class :

Code:

import org.json.JSONArray;
import org.json.JSONObject;

public class printJson {
   public static void print(JSONObject x) {System.out.println(x);}
   public static void printCarJsonSum(JSONObject x) {
      double sum=0;
       for(String s : x.keySet()) {
           JSONArray jArr = x.getJSONArray(s);
           for (Object object : jArr) {
               sum += ((JSONObject)object).getDouble("price")*((JSONObject)object).getDouble("quantity");
           }
         }
       System.out.println(sum);
   }
}


Main :

Code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONObject;

public class main {

  public static void main(String[] args) {
     TokenClass tokenClass = new TokenClass();
     tokenClass.enable("secratary", "create");//init
     tokenClass.Activate("secratary", "create");
    
     //String path = "C:\\Users\\Lenovo\\eclipse-workspace\\Json\\writeDepottest2.json"; //"C:\\Users\\Lenovo\\Desktop\\HW\\writeDepottest.json";
      //JsonCRUDA carSys = new JsonCRUDA();
     JsonCRUDA.create("delorean", "price 30000 id flaxCapacitor quantity 1");
     JsonCRUDA.Add("nightRider", "price 1000 id wheel quantity 4");
     JsonCRUDA.delete("nightRider");
     JsonCRUDA.create("nightRider", "price 1000 id wheel quantity 4");
      System.out.println(JsonCRUDA.Read("delorean", "flaxCapacitor", "price"));
      JsonCRUDA.Update("delorean", "flaxCapacitor", "price", "60000");
      printJson.print(JsonCRUDA.getCars());
      printJson.printCarJsonSum(JsonCRUDA.getCars());
//      FileWriter fWriter;
//      try {
//        fWriter = new FileWriter(path);
//        fWriter.write(cars.toString());
//        fWriter.close();
//       
//      }
//      catch (IOException e) {
//        // TODO Auto-generated catch block
//        e.printStackTrace();
//      }
  }
  }


the project isn't clean or complete (cost time) but the concept is there (HADOUKEN )
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply