battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Share | 
 

 java class with learnability exampled with simple car class

Go down 
AuthorMessage
Moti Barski

avatar

Posts : 246
Join date : 2011-08-02

PostSubject: java class with learnability exampled with simple car class   Sat Feb 10, 2018 12:55 am

written by moti barski

main :
Code:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class Main {

 public static void main(String[] args) {
 boolean[] parts = {true,true,true,false};
 Car x = new Suzuki(parts);
 FixerBot fb1 = new FixerBot();

 fb1.gainExp(x);
 fb1.gainExp(new Suzuki(parts));
 fb1.gainExp(new Suzuki(parts));
 
 fb1.getRepairProphesy(new Suzuki(parts));
 
 
 }

}

car class :
Code:
import java.awt.Point;
import java.util.Arrays;
import java.util.Random;

public class Car {
 public boolean parts[] = new boolean[4];
 // steer, spoke, tire, wheel

 public Car(boolean[] parts) {
 super();
 this.parts = Arrays.copyOf(parts, parts.length);
 effects();
 }
 private void effects() {
 if(!parts[3]) {parts[2]=false;parts[1] = false;}
 }
 public void fixWithEffects(int part) {
 parts[part] = true;
 if(part == 3) {parts[2]=true;parts[1] = true;}
 }
 public int repairSuggestion() {
 int counter = 0;
 for (int i = 0; i < parts.length; i++) {
 if(!parts[i]) {counter++;}
 }
 int counter2 = 0;
 int brokenParts[] = new int[counter];
 for (int i = 0; i < parts.length; i++) {
 if(!parts[i]) {brokenParts[counter2]=i;counter2++;}
 
 }
 Random rn = new Random();
 int answer = rn.nextInt(counter);
 return brokenParts[answer];
 }
 public boolean working() {
 for (int i = 0; i < parts.length; i++) {
 if(!parts[i]) {return false;}
 }
 return true;
 }
 public String getState() {
 String result = "";
 for (int i = 0; i < parts.length; i++) {
 result += parts[i] + "";
 }
 return result;
 }
 
}

Suzuki car class, inherits from car class :
Code:
import java.awt.Point;
import java.sql.Ref;

public class Suzuki extends Car {
 private String name;
 
 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public Suzuki(boolean[] parts) {
 super(parts);
 // TODO Auto-generated constructor stub
 }
 
 
}

algorithm class :
Code:
import java.awt.Point;
import java.util.Arrays;

public class AlgMatrix {
 public String[][] states= new String[10][10];
 //public String[][] actions= new String[10][10];
 public void defaulter() {
 for (int i = 0; i < states.length; i++) {
 for (int j = 0; j < states[0].length -1; j++) {
 states[i][j] = "";
 
 }
 
 }
 for (int i = 0; i < states.length; i++) {
 states[i][9] = "xxxxxxxxxxxxxxxxxxxx";
 }
 }
 public Point StateLocate(String str) {
 Point tP = new Point(1000, 1000);
 int sl = states.length;
 String str2="";
 for (int i = 0; i < sl; i++) {
 for (int j = 0; j < sl-1; j++) {
 str2 = states[i][j];
 if(str2 != null) {if(str2.contains(str)) {tP.x = i;tP.y= j;break;}}
 
 }
 }
 return tP;
 }
 public void sortMe() {
 
 }
}
fixer bot class :
Code:
import java.awt.Point;
import java.awt.image.ReplicateScaleFilter;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.Hashtable;

import org.omg.CORBA.PUBLIC_MEMBER;

public class FixerBot {
 public int expBarrier = 100;
 public AlgMatrix aMatrix = new AlgMatrix();
 Dictionary dic1 = new Hashtable();
 private String carKey(Car c1) {
 String Key = c1.getClass().toString().substring(6);
 
 for (int i = 0; i < c1.parts.length; i++) {
 Key += c1.parts[i] + "";
 }
 if(dic1.get(Key)==null) {aMatrix.defaulter();dic1.put(Key, aMatrix);}
 return Key;
 }
 public String repairCar(Car c1) {
 String Key = carKey(c1);
 //if too costly alg, gainExp
 //repair action + print
 
 aMatrix = (AlgMatrix)dic1.get(Key);
 
 return Key;
 }
 public void getRepairProphesy(Car c1) {
 String Key = carKey(c1);
 aMatrix = (AlgMatrix)dic1.get(Key);
 for (int i = 0; i < aMatrix.states.length; i++) {
 if(aMatrix.states[0][i]!=null) {System.out.print(aMatrix.states[0][i] +" ");}
 
 }
 }
 public void gainExp(Car c1) {
 if(!(c1.working())) {gainExpInner(c1);}
 else {System.out.println("car works");}
 }
 public void gainExpInner(Car c1) {
 
 String[] sc = new String[10];
 sc[0] = carKey(c1);
 AlgMatrix ax = (AlgMatrix)dic1.get(sc[0]);
 int sCount = 1;
 int cost =0;boolean b1;
 int nextFix;
 boolean b3 =  !(c1.working());
 boolean b2;
 Point tP1 = new Point(1000,1000);// change to 1;
 do {
 nextFix = c1.repairSuggestion();
 c1.fixWithEffects(nextFix);
 sc[sCount] = c1.getState() + "@" + nextFix;
 sCount++;cost++;this.expBarrier--;
 //AlgMatrix ax = (AlgMatrix)dic1.get(sc[0]);
 tP1 = new Point(ax.StateLocate(c1.getState()));
 b3 =  !(c1.working());
 b2 =  tP1.x < 1000;
 } while ((expBarrier == 0 && b2)|| b3);
 
 if(expBarrier == 0 && b2) {
 for (int i = tP1.y; i < sc.length; i++) {
 sc[i] = ax.states[tP1.x][i];
 cost++;
 }
 
 }
 for (int i = 0; i < sc.length; i++) {
 ax.states[sc.length -1][i] = sc[i];
 }
 String costStr ="";
 for (int i = 0; i < cost; i++) {
 costStr+="x";
 }
 ax.states[ax.states.length - 1][ax.states.length - 1] = costStr;
 //ax.sortMe();
 int min = 0;
 int minIndex = 0;
 String temp = "";
 for (int i = 0; i < ax.states.length; i++) {
 min = ax.states[i][ax.states.length-1].length();
 minIndex = i;
 for (int j = i+1; j < ax.states.length; j++) {
 if(ax.states[j][ax.states.length-1].length() < min) {minIndex =j;}
 }
 for (int j = 0; j < ax.states.length; j++) {
 if(i!=minIndex) {
 temp = ax.states[i][j];
 ax.states[i][j] = ax.states[minIndex][j];
 ax.states[minIndex][j] = temp;}
 }
 }
 dic1.put(sc[0], ax);
 }
}

about the output :
Suzukitruefalsefalsefalse truetruetruetrue@3 x  =
car + car state of parts @ part number to repair, amount of repairs = amount of x



_________________
MB over and out _______________________     
Back to top Go down
View user profile
 
java class with learnability exampled with simple car class
Back to top 
Page 1 of 1
 Similar topics
-
» EclipseIDE warns SeleneseTestCase Deprecated
» Cannot locate the class Select in web driver
» Verify command when used with Java,JUNIT and Eclipse
» JAVA tunnel Based UAE Solution Available in Just $149 @ VoIP
» Create Batch file for Selenium

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: chit chat-
Jump to: