Java applet text fields shortening/not displaying properly - java

I wrote a price calculator for a friend (runescape dungeoneering, but that's not particularly important). He's part of a fairly large group that does this, and one of the other admins is having troubles with it.
I wrote this in Eclipse on Windows 7, Java 6u32 (I believe it was u32). He's running Mac OS X version 10.7.4, with Java 6u33. Whenever he loads the page, the text fields don't display properly. Half of them are shortened with ellipses at the end of the line.
Picture of the problem:
http://puu.sh/KckA
Also, as a side question, I was wondering why the image only shows up after it paints once?
Thanks for all your help, and sorry about the badly named variables! I wasn't able to figure out how to embed code properly into the post, so I realize this is badly space - I just copy/pasted it over and used space to indent the imports :/.
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class IHideKeysCalc extends JApplet implements ActionListener {
JRadioButton t = new JRadioButton("Get a chaotic at your current level."), z = new JRadioButton("Get to a certain level.");
JRadioButton t2 = new JRadioButton("Yes."), z2 = new JRadioButton("No.");
JTextField startLevel = new JTextField(), endLevel = new JTextField();
JLabel e = new JLabel("What is your ending level?"), p = new JLabel("What is your starting level?");
JLabel cost = new JLabel("It will cost: 0m");
JLabel chaotics = new JLabel("You will gain: 0 chaotics");
JLabel time = new JLabel("It will take: 0 hours");
JButton calc = new JButton("Calculate!");
JLabel leech = new JLabel("Will you leech c1s from us?");
Color dots = new Color(215, 215, 215);
double[] normalCost = new double[] {0.147408147408147, 0.161616161616162, 0.156052782558807, 0.171352074966533, 0.166624674561182, 0.18543713781809, 0.180714893457874, 0.201060278814058, 0.206251654890812, 0.227434257285004, 0.228290358693966, 0.251523448295564, 0.253108393640709, 0.280820991484583, 0.28508229125052, 0.314016016332662, 0.321855573638658, 0.354987765042637, 0.365389331821599, 0.402884011746955, 0.413233529990681, 0.454829644395684, 0.469244088131267, 0.518269589876325, 0.536842873549166, 0.591842366162221, 0.617068005673544, 0.680805377374742, 0.711865189407446, 0.785616988310019, 0.824813238661612, 0.910191318818822, 0.958469472772604, 1.05785553342935, 1.11731759927444, 1.2328062178794, 1.30540086348993, 1.44105580841818, 1.52112093542576, 1.67893297878229, 1.79759198326453, 1.98403337299274, 2.0576867439263, 2.27198067413181, 2.34861652795415, 2.59287845397998, 2.67171019944355, 2.94948405053316, 2.30627996576819, 2.54621754023429, 2.63839002652197, 2.91292672667821, 3.02782375824338, 3.34294815274889, 2.99278604307127, 3.30403304019601, 3.44280379223394, 3.8011722050782, 3.35899412255012, 3.70845630500912, 3.7979745635451, 4.19312491872546, 4.11809281699623, 4.5465605645372, 4.59525960492191, 5.07347042913889, 5.11281698937835, 5.64488890579082, 5.81225689595036, 6.41724086226737, 6.46511043188491, 7.13790513866952, 7.31926635024289, 8.08104832531242, 8.18451767951496, 9.03633826548025, 9.03072096418207, 9.97065806503748, 11.6582196154413, 12.8716058180737, 13.3368037528554, 14.7248844101516, 15.2703841396691, 16.8597698035761, 17.5341941654067, 19.3591671629348, 20.1405224921476, 22.2368354307871, 23.1540859239001, 25.5640472079718, 26.7502409626548, 29.534621910156, 31.0514204050245, 34.2834446556508, 36.3576095095093, 40.1419477374292, 42.8132373341253, 47.2695988929749, 50.5171187677128, 55.775336875572, 59.0583839135564, 65.2056239687282, 69.5517856204352, 76.791366428954, 80.8126533895158, 89.2243342865382, 94.9773484573605, 104.863428916985, 112.165424237996, 123.840557129701, 130.059514288612, 143.597320738684, 154.344556268421, 170.410147105144, 178.81609217768, 197.428905849894, 209.623017271163, 231.442490011038, 248.40964248305, 2418.96525482347};
double[] normalCost2 = new double[] {0.147408147408147, 0.161616161616162, 0.156052782558807, 0.171352074966533, 0.166624674561182, 0.18543713781809, 0.180714893457874, 0.201060278814058, 0.206251654890812, 0.227434257285004, 0.228290358693966, 0.251523448295564, 0.253108393640709, 0.280820991484583, 0.28508229125052, 0.314016016332662, 0.321855573638658, 0.354987765042637, 0.365389331821599, 0.402884011746955, 0.413233529990681, 0.454829644395684, 0.469244088131267, 0.518269589876325, 0.536842873549166, 0.591842366162221, 0.617068005673544, 0.680805377374742, 0.711865189407446, 0.785616988310019, 0.824813238661612, 0.910191318818822, 0.958469472772604, 1.05785553342935, 1.11731759927444, 1.2328062178794, 1.30540086348993, 1.44105580841818, 1.52112093542576, 1.67893297878229, 1.79759198326453, 1.98403337299274, 2.0576867439263, 2.27198067413181, 2.34861652795415, 2.59287845397998, 2.67171019944355, 2.94948405053316, 2.30627996576819, 2.54621754023429, 2.63839002652197, 2.91292672667821, 3.02782375824338, 3.34294815274889, 2.99278604307127, 3.30403304019601, 3.44280379223394, 3.8011722050782, 3.35899412255012, 3.70845630500912, 3.7979745635451, 4.19312491872546, 4.11809281699623, 4.5465605645372, 4.59525960492191, 5.07347042913889, 5.11281698937835, 5.64488890579082, 5.81225689595036, 6.41724086226737, 6.46511043188491, 7.13790513866952, 7.31926635024289, 8.08104832531242, 8.18451767951496, 9.03633826548025, 9.03072096418207, 9.97065806503748, 9.16677990396157, 10.1208573381611, 10.6767809960961, 11.7880092526936, 12.4151865893764, 13.7073950498449, 14.4486866969796, 15.952517601496, 16.79338694904, 18.5413155024615, 19.5087629499962, 21.5393057908644, 22.749431329251, 25.1173757245215, 26.628575651853, 29.4002428137358, 31.4882868073428, 34.7657940225949, 37.4024999022994, 41.2956664355536, 44.4734040636548, 49.1025449226364, 52.3507084560114, 57.7997632829849, 62.0331408031167, 68.4901128512714, 72.4784892775103, 80.0226781901313, 85.613081327045, 94.52444623818, 101.572293638011, 112.144803254013, 118.272870806207, 130.583813546741, 140.900446941876, 155.566652113006, 163.822838496629, 180.875017252225, 192.680242742557, 212.736157206356, 229.030482784318, 2230.25473010176};
double[] normalTokens = new double[] {8.3, 9.1, 10.2, 11.2, 12.4, 13.8, 15.1, 16.8, 18.5, 20.4, 22.6, 24.9, 27.4, 30.4, 33.5, 36.9, 40.8, 45, 49.7, 54.8, 60.6, 66.7, 73.7, 81.4, 89.8, 99, 109.4, 120.7, 133.2, 147, 162.3, 179.1, 197.7, 218.2, 240.9, 265.8, 293.5, 324, 357.6, 394.7, 435.8, 481, 531, 586.3, 647.1, 714.4, 788.7, 870.7, 961.2, 1061.2, 1171.5, 1293.4, 1427.8, 1576.4, 1740.4, 1921.4, 2121.2, 2342, 2585.6, 2854.6, 3151.6, 3479.5, 3841.6, 4241.3, 4682.6, 5169.9, 5707.9, 6301.9, 6957.6, 7681.8, 8481.2, 9363.8, 10338.3, 11414.3, 12602.2, 13913.8, 15361.9, 16960.8, 18726, 20675, 22826.9, 25202.7, 27825.9, 30722.1, 33919.8, 37450.2, 41348.2, 45651.9, 50403.7, 55649.9, 61442.2, 67837.6, 74898.5, 82694.4, 91301.9, 100805.2, 111297.7, 122882.5, 135672.9, 149794.8, 165386.8, 182601.5, 201608.1, 222593.3, 245762.6, 271343.7, 299587.4, 330771.1, 365200.7, 403213.9, 445184, 491522.9, 542684.9, 599172.5, 661539.7, 730398.8, 806425.4, 890365.5, 983043, 9572683.4};
double[] normalTime = new double[] {1.00958241758242, 1.11495970695971, 1.06963166953528, 1.18127672595142, 1.15018594104308, 1.27023901906442, 1.2472594878124, 1.37744482155713, 1.41685187507839, 1.56473877113034, 1.56755170743479, 1.73116821561822, 1.74434730120433, 1.92641722153205, 1.95725417087426, 2.16154669376012, 2.21124718747179, 2.44205076596211, 2.50994901343847, 2.77193022359534, 2.86812333250013, 3.1674897409429, 3.29805772944222, 3.64229944530993, 3.80841107155634, 4.20592199148309, 4.42242809498221, 4.8840283338524, 5.15065412806684, 5.68826449160243, 6.02144053611861, 6.64994106345759, 7.05825151342297, 7.79497136835937, 8.29491528158907, 9.16071451973473, 9.77092350100014, 10.7907842031247, 11.311355104669, 12.4920015970756, 13.4631737052244, 14.8684207923017, 15.322810010546, 16.9221605503673, 17.602791084445, 19.4401194467945, 19.9138619998677, 21.9924132523791, 16.9295074753534, 18.6965604441613, 19.4662745138955, 21.4981078806657, 22.4508340041317, 24.794187048334, 21.9299806848883, 24.2189685678228, 25.3352524716474, 27.9796727633009, 24.6417064586782, 27.2137364257457, 27.7930719891776, 30.6940323651524, 30.0742869561431, 33.2133539448314, 33.3534941382039, 36.8348352772083, 37.0696258402337, 40.93884604578, 42.4260461563957, 46.8543540043697, 47.1239377238921, 52.0425978903589, 53.693321666918, 59.2976751068993, 60.1692757457264, 66.4495705205329, 66.7720718040988, 73.7415473122707, 76.8324023748064, 84.8519460570252, 88.1453522545292, 97.3457088871568, 101.175803906389, 111.736241351164, 116.429000968713, 128.581522955346, 133.995020100622, 147.981032299676, 154.311116147988, 170.417663624714, 178.555672255407, 197.192796295533, 207.556928886271, 229.221120116824, 239.701753082987, 264.721127983905, 278.790936846473, 307.890327561088, 325.297010290488, 359.250570287139, 376.451809570906, 415.744759391439, 439.247661643271, 485.09506067001, 506.047490207454, 558.867261912609, 590.119126007236, 651.714051617419, 691.908460702742, 764.127862324914, 796.957117808243, 880.14119407779, 939.928811085602, 1038.03585870733, 1082.69792300564, 1195.70679712407, 1262.42094024116, 1394.18878248856, 1488.70556306347, 14496.7280485445};
double[] secondCost = new double[] {205.809139511126, 205.666898683103, 205.510947895753, 205.361245312921, 205.196866006281, 205.037961182368, 204.861115491238, 204.68980138365, 204.499200257327, 204.304466121548, 204.089732263715, 203.875511955396, 203.639490465256, 203.403440451273, 203.141545545247, 202.877319302118, 202.586276067149, 202.289821292416, 201.962849114402, 201.628401442031, 201.259634189599, 200.884128316807, 200.470823998073, 200.047463215808, 199.579870710024, 199.098934496811, 198.568726422112, 198.019767421347, 197.414106074069, 196.785167022266, 196.091067618249, 195.36729726347, 194.568607981024, 193.733220320596, 192.811209255825, 191.843868432443, 190.776540948612, 189.653863969407, 188.414520387594, 187.116029983754, 185.682824831642, 184.156548244533, 182.471970368444, 180.744867551454, 178.837898847873, 176.892146461388, 174.744031015699, 172.563340671575, 170.155927101612, 168.448060074772, 166.562512333528, 164.653461422647, 162.545764924027, 160.406844613766, 158.045313603054, 156.136045952524, 153.988235498788, 151.784760223372, 149.351920748064, 147.503584655442, 145.462951839076, 143.506325452791, 141.346126978658, 139.472520504396, 137.403974289463, 135.544562182586, 133.491648277631, 131.713718729864, 129.75076664292, 127.774684202822, 125.573085489923, 123.762314431261, 121.763104645016, 120.117322794866, 118.300249603707, 116.783640524603, 114.939775999494, 113.850096472748, 112.647000728262, 110.627734605662, 107.826828419619, 105.60890512289, 103.160142677246, 101.141235060418, 98.3943606030692, 96.4191212414268, 94.1197186833565, 91.9810632039793, 89.6198071860878, 88.0967551101201, 85.6040436484927, 84.3265101874514, 82.221926362036, 80.9960672634902, 79.5357439871363, 78.2995243514211, 76.9346308757958, 75.9839103477244, 74.468989411611, 73.7032366599374, 71.4184976232158, 71.2078468065862, 68.9970151203599, 69.3621460275368, 65.7648099340712, 65.7648099340712, 63.4054359144346, 63.4054359144346, 61.4267301448194, 61.4267301448194, 58.4295546509364, 58.4295546509364, 56.8818318948696, 56.8818318948696, 54.0605778240308, 54.0605778240308, 51.9881981076397, 51.9881981076397, 50.5389169106641, 50.5389169106641};
double[] secondCost2 = new double[] {203.13233860099, 202.988993482163, 202.83183196634, 202.680772303242, 202.514902869252, 202.354348261486, 202.175666520585, 202.002343402015, 201.809507084666, 201.612311577817, 201.394863559453, 201.177636387015, 200.938302024462, 200.698606520352, 200.432666982947, 200.163983662474, 199.868030990072, 199.566147894276, 199.233188597443, 198.892128484954, 198.51607025227, 198.132501726134, 197.710323166839, 197.277156814418, 196.798734275924, 196.30585043449, 195.762470698387, 195.198956346536, 194.577236216113, 193.930575292604, 193.216917967109, 192.471554070454, 191.649036055106, 190.787344985998, 189.836303047305, 188.836911181175, 187.734219782183, 186.572493483566, 185.290042646251, 183.943974604858, 182.458255768343, 180.873997261643, 179.125423737726, 177.327672918664, 175.342698709217, 173.310851554106, 171.067687270979, 168.782062691008, 166.25880502424, 164.423053139412, 162.396315831693, 160.331400260874, 158.051620662669, 155.722735724616, 153.151469289297, 151.01064649224, 148.624436411962, 146.173777319575, 143.468024163638, 141.318387638638, 138.945107771579, 136.62122487126, 134.055559637202, 131.734290824981, 129.171505159301, 126.76642883342, 124.111065552939, 121.667993393022, 118.970679767243, 116.273634388718, 113.283811160463, 110.60278967096, 107.642766440354, 104.936178332934, 101.947891222429, 99.2288303248363, 96.1192999109482, 93.6322283836829, 90.8862970366724, 89.6688583891789, 87.9450600565222, 86.5393226499183, 84.9872775567425, 83.6651681411018, 81.8487685186973, 80.5094726227178, 78.954690408184, 77.4707819037332, 75.832421765823, 74.7640388864952, 73.0098766187729, 72.104918422045, 70.6542197712317, 69.8567106080253, 68.9065542546254, 68.1010156606756, 67.2116313316438, 66.574684853225, 65.5597456288688, 64.9942472119106, 63.3069972404224, 63.1531374790764, 61.538341766146, 61.8270638283787, 58.9825215696044, 58.9825215696044, 57.1539933435418, 57.1539933435418, 55.6254649227186, 55.6254649227186, 53.1343762606953, 53.1343762606953, 51.9271669220486, 51.9271669220486, 49.5277421737892, 49.5277421737892, 47.7862534445361, 47.7862534445361, 46.596228808774, 46.596228808774};
double[] reachLevel = new double[] {80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 83, 83, 83, 84, 84, 84, 85, 85, 86, 86, 87, 87, 88, 88, 89, 90, 90, 91, 91, 92, 93, 93, 94, 95, 96, 97, 97, 98, 99, 100, 101, 102, 103, 104, 105, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120};
double[] secondTime = new double[] {24.9946429257347, 24.9783842111745, 24.9604239225849, 24.9432943332101, 24.9243723855536, 24.9060506888359, 24.8858238572065, 24.8660689282208, 24.8442605111923, 24.8219115774579, 24.797227808174, 24.7726476204672, 24.7454977940721, 24.718299297897, 24.6882714810255, 24.6579417322399, 24.624439643112, 24.5903759437907, 24.5527527673272, 24.5143193989011, 24.4718684795311, 24.4282110190991, 24.3799813137437, 24.3300542238567, 24.2749163955096, 24.2175845383904, 24.1542567117313, 24.0880317336631, 24.0148862553222, 23.9381519219767, 23.8534012345939, 23.7641440208121, 23.6655607949053, 23.5614444982819, 23.4464515869104, 23.3246787903348, 23.1901789836552, 23.0474035039304, 22.8897163219235, 22.7256509515522, 22.544445507274, 22.3498647994608, 22.1349546550305, 21.9158909898917, 21.6739535964416, 21.4248306343984, 21.1496883589048, 20.8717319487254, 20.5647412156741, 20.3483217811742, 20.1092907297184, 19.8649748206746, 19.595132087265, 19.3186025584435, 19.0131802946162, 18.7667111995194, 18.490451007457, 18.2047319219977, 17.8891513821645, 17.6448833971303, 17.3750629489418, 17.1147038849072, 16.8271013961493, 16.5731352612704, 16.2923706355883, 16.0378836320328, 15.7567402789638, 15.5063131557712, 15.2296329829085, 14.9463360163191, 14.6310161166959, 14.3596539672288, 14.0595718337876, 13.7912768736783, 13.4947912766823, 13.2295985529344, 12.9181982399467, 12.684004188419, 12.425408200958, 12.212164466137, 11.9147945189033, 11.678465792127, 11.4174452967627, 11.202029099851, 10.9072154065515, 10.6977333721524, 10.4482346235909, 10.2179425784472, 9.96261811883693, 9.79890600858667, 9.5292281368228, 9.39100021700554, 9.13546626658899, 8.95336286950375, 8.73661022043875, 8.5538243186338, 8.35021455477117, 8.21361702837751, 7.99254171400577, 7.89238136322017, 7.58748131883126, 7.5609402291254, 7.26239303170813, 7.30945047798586, 6.86457666518511, 6.86457666518511, 6.56680908628267, 6.56680908628267, 6.31619233951591, 6.31619233951591, 5.96806504391798, 5.96806504391798, 5.77411467139106, 5.77411467139106, 5.456191060476, 5.456191060476, 5.21889040213051, 5.21889040213051, 5.04794996442499, 5.04794996442499};
private static final long serialVersionUID = 1L;
JPanel x = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image oi = getImage(getCodeBase(), "Logo.png");
g.drawImage(oi, 0, 200, 537, 93, null);
}
};
public void init() {
t.setBounds(25, 25, 220, 25);
t.setSelected(true);
t.addActionListener(this);
t.setBackground(Color.WHITE);
z.setBounds(250, 25, 150, 25);
z.addActionListener(this);
z.setBackground(Color.WHITE);
ButtonGroup group = new ButtonGroup();
group.add(t);
group.add(z);
leech.setBounds(30, 100, 160, 25);
leech.setOpaque(true);
leech.setBackground(Color.WHITE);
t2.setBounds(30, 125, 50, 25);
t2.setSelected(true);
t2.addActionListener(this);
t2.setBackground(Color.WHITE);
z2.setBounds(85, 125, 50, 25);
z2.addActionListener(this);
z2.setBackground(Color.WHITE);
ButtonGroup group2 = new ButtonGroup();
group2.add(t2);
group2.add(z2);
p.setBounds(30, 50, 160, 25);
p.setBackground(Color.WHITE);
p.setOpaque(true);
e.setBounds(195, 50, 150, 25);
e.setBackground(Color.WHITE);
e.setOpaque(true);
startLevel.setBounds(30, 75, 150, 25);
startLevel.setBackground(Color.WHITE);
startLevel.setText("1");
endLevel.setBounds(195, 75, 150, 25);
endLevel.setBackground(Color.WHITE);
endLevel.setText("1");
cost.setBounds(225, 100, 150, 25);
cost.setBackground(Color.WHITE);
cost.setOpaque(true);
chaotics.setBounds(225, 120, 160, 25);
chaotics.setBackground(Color.WHITE);
chaotics.setOpaque(true);
time.setBounds(225, 140, 150, 25);
time.setBackground(Color.WHITE);
time.setOpaque(true);
calc.setBounds(155, 175, 90, 25);
calc.setBackground(Color.WHITE);
calc.addActionListener(this);
x.setBounds(0, 0, 400, 293);
x.setVisible(true);
x.setLayout(null);
x.setBackground(Color.WHITE);
x.add(t);
x.add(z);
x.add(startLevel);
x.add(endLevel);
x.add(p);
x.add(e);
x.add(cost);
x.add(chaotics);
x.add(time);
x.add(calc);
x.add(t2);
x.add(z2);
x.add(leech);
this.add(x);
endLevel.setVisible(false);
e.setVisible(false);
calc.doClick();
this.setSize(400, 293);
this.setVisible(true);
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}
#Override
public void actionPerformed(ActionEvent arg0) {
if(t.isSelected()) {
endLevel.setVisible(false);
e.setVisible(false);
if(startLevel.getText().length() < 1) {
} else {
double cost1 = 0;
double chaotic = 0;
double time = 0;
for(int i = 0; i <= normalCost.length; i++) {
if(Integer.parseInt(startLevel.getText()) == i) {
if(z2.isSelected()){
cost1 = secondCost2[i - 1];
} else {
cost1 = secondCost[i - 1];
}
time = secondTime[i - 1];
chaotic = reachLevel[i - 1];
}
}
cost.setText("It will cost: " + round(cost1, 2) + "m");
chaotics.setText("You will reach level: " + round(chaotic, 2));
this.time.setText("It will take: " + round(time, 2) + " hours");
}
x.repaint();
} else if(z.isSelected()) {
endLevel.setVisible(true);
e.setVisible(true);
if((startLevel.getText().length() < 1) || (endLevel.getText().length() < 1)) {
} else {
double cost1 = 0;
double chaotic = 0;
double time = 0;
for(int i = 0; i <= normalCost.length; i++) {
if(!((Integer.parseInt(startLevel.getText())) >= (i))) {
if(!(Integer.parseInt(endLevel.getText()) < (i))) {
if(z2.isSelected()){
cost1 = cost1 + normalCost2[i - 2];
} else {
cost1 = cost1 + normalCost[i - 2];
}
chaotic = chaotic + normalTokens[i - 2];
time = time + normalTime[i - 2];
}
}
}
cost.setText("It will cost: " + round(cost1, 2) + "m");
chaotics.setText("You will gain: " + round(chaotic / 200000, 2) + " chaotics");
this.time.setText("It will take: " + round(time / 60, 2) + " hours");
}
x.repaint();
}
}
}
To clarify some, this works as intended for other people. As far as I know, he's the only one having problems.

Your issue here is that Mac vs Windows vs Linux display Swing components differently. If you want it to look the same on all OSes you will have to use a non-default L&F.
I can see already that the guy having trouble is on Mac OSX because of how the GUI looks.

Related

implementing sudoku XV board

I have found out the solution for standard sudoku however I do not know what to do with sudoku xv.
A Sudoku XV puzzle consists of a standard Sudoku grid with the addition of X and V markers between neighbouring pairs of squares. X markers show all adjacent pairs of squares where the two values in those squares add up to 10, while V markers show all pairs where the two values sum to 5.
I have developed simple sudoku, however, I need help implementing x and v markers between neighbouring pairs of squares
package n.sudoku_cnf;
import java.util.ArrayList;
/**
*
*
*/
public class Sudoku_CNF_Solution {
static final int subN = 3;
static final int N = subN*subN;
static final int N2 = N*N;
static final int VARS = N*N*N;
static int clauses = 0;
static ArrayList<String> DIMACS_CNF = new ArrayList();
static int[] solution = {-1, -2, -3, -4, -5, -6, -7, -8, 9, -10, -11, -12, 13, -14, -15, -16, -17, -18, -19, -20, 21, -22, -23, -24, -25, -26, -27, 28, -29, -30, -31, -32, -33, -34, -35, -36, -37, -38, -39, -40, -41, -42, 43, -44, -45, -46, -47, -48, -49, -50, 51, -52, -53, -54, -55, 56, -57, -58, -59, -60, -61, -62, -63, -64, -65, -66, -67, 68, -69, -70, -71, -72, -73, -74, -75, -76, -77, -78, -79, 80, -81, -82, -83, -84, -85, -86, -87, -88, 89, -90, 91, -92, -93, -94, -95, -96, -97, -98, -99, -100, -101, -102, -103, 104, -105, -106, -107, -108, -109, 110, -111, -112, -113, -114, -115, -116, -117, -118, -119, -120, -121, -122, 123, -124, -125, -126, -127, -128, -129, -130, -131, -132, -133, -134, 135, -136, -137, -138, 139, -140, -141, -142, -143, -144, -145, -146, 147, -148, -149, -150, -151, -152, -153, -154, -155, -156, -157, -158, -159, 160, -161, -162, -163, -164, -165, -166, -167, -168, 169, -170, -171, -172, -173, -174, -175, -176, 177, -178, -179, -180, 181, -182, -183, -184, -185, -186, -187, -188, -189, -190, -191, -192, -193, 194, -195, -196, -197, -198, -199, 200, -201, -202, -203, -204, -205, -206, -207, -208, -209, -210, -211, -212, -213, -214, 215, -216, -217, -218, -219, -220, -221, -222, -223, -224, 225, -226, -227, -228, 229, -230, -231, -232, -233, -234, -235, -236, 237, -238, -239, -240, -241, -242, -243, -244, -245, -246, -247, -248, 249, -250, -251, -252, -253, 254, -255, -256, -257, -258, -259, -260, -261, -262, -263, -264, -265, -266, -267, -268, 269, -270, -271, -272, -273, -274, -275, -276, -277, -278, 279, -280, -281, 282, -283, -284, -285, -286, -287, -288, -289, -290, -291, 292, -293, -294, -295, -296, -297, -298, -299, -300, -301, -302, -303, 304, -305, -306, 307, -308, -309, -310, -311, -312, -313, -314, -315, -316, -317, -318, -319, 320, -321, -322, -323, -324, -325, -326, -327, -328, 329, -330, -331, -332, -333, -334, -335, -336, -337, -338, -339, -340, -341, 342, -343, 344, -345, -346, -347, -348, -349, -350, -351, -352, -353, -354, 355, -356, -357, -358, -359, -360, -361, -362, -363, -364, -365, -366, -367, 368, -369, -370, -371, 372, -373, -374, -375, -376, -377, -378, 379, -380, -381, -382, -383, -384, -385, -386, -387, -388, -389, -390, -391, -392, -393, 394, -395, -396, -397, -398, -399, -400, -401, 402, -403, -404, -405, -406, -407, -408, 409, -410, -411, -412, -413, -414, -415, -416, 417, -418, -419, -420, -421, -422, -423, -424, -425, -426, -427, -428, -429, -430, -431, 432, -433, -434, -435, -436, -437, -438, 439, -440, -441, 442, -443, -444, -445, -446, -447, -448, -449, -450, -451, -452, -453, -454, 455, -456, -457, -458, -459, -460, -461, -462, -463, -464, 465, -466, -467, -468, -469, -470, -471, -472, -473, -474, -475, 476, -477, -478, 479, -480, -481, -482, -483, -484, -485, -486, -487, -488, 489, -490, -491, -492, -493, -494, -495, -496, -497, -498, -499, 500, -501, -502, -503, -504, -505, -506, -507, -508, -509, -510, 511, -512, -513, -514, -515, -516, -517, -518, 519, -520, -521, -522, -523, -524, -525, -526, -527, -528, -529, -530, 531, 532, -533, -534, -535, -536, -537, -538, -539, -540, -541, -542, -543, -544, -545, -546, -547, 548, -549, -550, 551, -552, -553, -554, -555, -556, -557, -558, -559, -560, -561, 562, -563, -564, -565, -566, -567, -568, 569, -570, -571, -572, -573, -574, -575, -576, -577, -578, -579, -580, -581, -582, -583, 584, -585, -586, -587, -588, -589, -590, 591, -592, -593, -594, -595, -596, 597, -598, -599, -600, -601, -602, -603, -604, -605, -606, 607, -608, -609, -610, -611, -612, -613, -614, -615, -616, -617, -618, 619, -620, -621, -622, -623, -624, -625, 626, -627, -628, -629, -630, -631, -632, -633, -634, -635, -636, -637, -638, 639, 640, -641, -642, -643, -644, -645, -646, -647, -648, 649, -650, -651, -652, -653, -654, -655, -656, -657, -658, -659, -660, -661, -662, -663, 664, -665, -666, -667, -668, -669, 670, -671, -672, -673, -674, -675, -676, -677, -678, -679, -680, -681, -682, 683, -684, -685, -686, -687, -688, 689, -690, -691, -692, -693, -694, 695, -696, -697, -698, -699, -700, -701, -702, -703, -704, 705, -706, -707, -708, -709, -710, -711, -712, -713, -714, -715, -716, 717, -718, -719, -720, -721, -722, -723, -724, -725, -726, -727, -728, 729, 0};
/**
* Generate DIMACS CNF format and print any given SAT solution
* #param args the command line arguments
*/
public static void main(String[] args) {
// Generate DIMACS CNF format
addFacts();
atLeastOneDigitInCell();
eachDigitAtMostOnesInRow();
eachDigitAtMostOnesInColumn();
eachDigitAtMostOnesInSubGrid();
print_DIMACS_CNF_format();
// Print a SAT solution
printSATSolutionBoard(solution);
}
/**
*
* #param digit [1...N]
* #param row [1...N]
* #param column [1...N]
* #return
*/
private static int toVariable(int digit, int row, int column){
return (N2*(digit-1) + N*(row-1) + (column-1) + 1);
}
private static void addFacts(){
// Facts
DIMACS_CNF.add("c Pre-assigned entries");
// Update the number of facts according to the number of added DIMACS CNF clauses
int facts=17;
DIMACS_CNF.add(toVariable(3, 2, 6) + " 0");
DIMACS_CNF.add(toVariable(8, 2, 8) + " 0");
DIMACS_CNF.add(toVariable(5, 2, 9) + " 0");
DIMACS_CNF.add(toVariable(1, 3, 3) + " 0");
DIMACS_CNF.add(toVariable(2, 3, 5) + " 0");
DIMACS_CNF.add(toVariable(5, 4, 4) + " 0");
DIMACS_CNF.add(toVariable(7, 4, 6) + " 0");
DIMACS_CNF.add(toVariable(4, 5, 3) + " 0");
DIMACS_CNF.add(toVariable(1, 5, 7) + " 0");
DIMACS_CNF.add(toVariable(9, 6, 2) + " 0");
DIMACS_CNF.add(toVariable(5, 7, 1) + " 0");
DIMACS_CNF.add(toVariable(7, 7, 8) + " 0");
DIMACS_CNF.add(toVariable(3, 7, 9) + " 0");
DIMACS_CNF.add(toVariable(2, 8, 3) + " 0");
DIMACS_CNF.add(toVariable(1, 8, 5) + " 0");
DIMACS_CNF.add(toVariable(4, 9, 5) + " 0");
DIMACS_CNF.add(toVariable(9, 9, 9) + " 0");
clauses += facts;
}
private static void atLeastOneDigitInCell(){
// Every cell contains at least one digit
DIMACS_CNF.add("c Every cell contains at least one digit:");
String str;
for (int row = 1; row <= N; row++) {
for (int column = 1; column <= N; column++) {
str = "";
for (int digit = 1; digit <= N; digit++) {
str += toVariable(digit,row,column) + " ";
}
DIMACS_CNF.add(str + "0");
clauses++;
}
}
}
private static void eachDigitAtMostOnesInRow(){
// Each digit appears at most once in each row
DIMACS_CNF.add("c Each digit appears at most once in each row:");
for (int digit = 1; digit <= N; digit++) {
for (int row = 1; row < N; row++) {
for (int columnLow = 1; columnLow <= N-1; columnLow++) {
for (int columnHigh = columnLow+1; columnHigh <= N; columnHigh++) {
DIMACS_CNF.add("-" + toVariable(digit,row,columnLow) + " -" + toVariable(digit,row,columnHigh) + " 0");
clauses++;
}
}
}
}
}
private static void eachDigitAtMostOnesInColumn(){
// Each number appears at most once in each column
DIMACS_CNF.add("c Each number appears at most once in each column:");
for (int digit = 1; digit <= N; digit++) {
for (int column = 1; column <= N; column++) {
for (int rowLow = 1; rowLow <= N-1; rowLow++) {
for (int rowHigh = rowLow+1; rowHigh <= N; rowHigh++) {
DIMACS_CNF.add("-" + toVariable(digit,rowLow,column) + " -" + toVariable(digit,rowHigh,column) + " 0");
clauses++;
}
}
}
}
}
private static void eachDigitAtMostOnesInSubGrid(){
// Each number appears at most once in each sub-grid
DIMACS_CNF.add("c Each number appears at most once in each sub-grid:");
for(int digit = 1; digit<=N; digit++){
for(int subGridRow = 0; subGridRow <= subN-1; subGridRow++){
for(int subRowLow = 1; subRowLow <= subN-1; subRowLow++){
for(int columnSubGrid = 0; columnSubGrid <= subN-1; columnSubGrid++){
for(int subColumnLow = 1; subColumnLow <=subN; subColumnLow++){
for(int subRowHigh = subRowLow+1; subRowHigh <=subN; subRowHigh++){
for(int subColumHigh = 1; subColumHigh <= subN; subColumHigh++){
if(subColumnLow != subColumHigh){
DIMACS_CNF.add("-" + toVariable(digit,(subN*subGridRow + subRowLow),(subN*columnSubGrid + subColumnLow)) +
" -" + toVariable(digit,(subN*subGridRow + subRowHigh),(subN*columnSubGrid + subColumHigh)) + " 0");
clauses++;
}
}
}
}
}
}
}
}
}
/**
* Print DIMACS CNF format
* #param variables
*/
private static void print_DIMACS_CNF_format(){
// Print DIMACS CNF format
System.out.println("==========================================");
System.out.println("===== Beginning of DIMACS CNF format =====");
System.out.println("==========================================");
System.out.println("c digit range [1..." + N + "]");
System.out.println("c row range: [1..." + N + "]");
System.out.println("c column range: [1..." + N + "]");
System.out.println("c board[digit][row][column]: variable");
for (int digit = 1; digit <= N; digit++) {
for (int row = 1; row <= N; row++) {
for (int column = 1; column <= N; column++) {
System.out.println("c board[" + digit + "][" + row + "][" + column + "]: " + toVariable(digit,row,column));
}
}
}
System.out.println("c #vars: " + VARS);
System.out.println("c #clauses: " + clauses);
System.out.println("p cnf " + VARS + " " + clauses);
for (int i = 0; i < DIMACS_CNF.size(); i++) {
System.out.println(DIMACS_CNF.get(i));
}
System.out.println("====================================");
System.out.println("===== End of DIMACS CNF format =====");
System.out.println("====================================");
System.out.println("");
}
/**
* Print resulting board based on a set of variables from a SAT solution
* #param variables
*/
private static void printSATSolutionBoard(int[] variables){
int digit;
int tmp;
int row;
int column;
int[][] tmpBoard = new int [N][N];
for (row = 0; row < N; row++) {
for (column = 0; column < N; column++) {
tmpBoard[row][column] = -1;
}
}
for (int i = 0; i < variables.length; i++) {
if(variables[i] > 0){
digit = (variables[i]-1)/N2;
tmp = (variables[i]-1)%N2;
row = tmp/N;
column = tmp%N;
tmpBoard[row][column] = digit;
}
}
System.out.println("=======================");
System.out.println("===== Given board =====");
System.out.println("=======================");
for (row = 0; row < N; row++) {
System.out.print(" ");
for (column = 0; column < N; column++) {
System.out.print(((tmpBoard[row][column])>=0? (tmpBoard[row][column]+1):"-") + " ");
}
System.out.println();
}
System.out.println("=======================");
}
}

Java - Trouble Passing Variables Between Main Method and Drawing Method

I'm creating a dots and boxes program and I am trying to feed the coordinate values from the main method where the user inputs them to the paintComponent method in the JFrame class. However I have to throw in the (Graphics g) parameter, and I don't see a way around it to feed in the values. It's probably big cringe because I'm still starting out but any help would be great.
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
JFrame f = new JFrame("Dots & Boxes");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Drawing a = new Drawing();
f.add(a);
f.setSize(1440,990);
f.setVisible(true);
Scanner input = new Scanner(System.in);
System.out.println("You will choose two coordinates on the dot grid to place a line between.");
System.out.println("Make sure that they are right next to each other, either vertically or horizontally (not diagonal)");
int xOne;
int yOne;
int xTwo;
int yTwo;
boolean playerOneTurn = true;
for (int i = 0; i <= 760; i++){
System.out.println("Pick Your First X-Coordinate: ");
xOne = input.nextInt();
System.out.println("Pick Your First Y-Coordinate: ");
yOne = input.nextInt();
System.out.println("Pick Your Second X-Coordinate: ");
xTwo = input.nextInt();
System.out.println("Pick Your Second Y-Coordinate: ");
yTwo = input.nextInt();
playerOneTurn = !playerOneTurn;
}
}
}
class Drawing extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
setFont(new Font("TimesRoman", Font.PLAIN, 20));
g.drawString("0", 75, 45);
g.drawString("1", 110, 45);
g.drawString("2", 145, 45);
g.drawString("3", 180, 45);
g.drawString("4", 215, 45);
g.drawString("5", 250, 45);
g.drawString("6", 285, 45);
g.drawString("7", 320, 45);
g.drawString("8", 355, 45);
g.drawString("9", 390, 45);
g.drawString("10", 417, 45);
g.drawString("11", 452, 45);
g.drawString("12", 487, 45);
g.drawString("13", 522, 45);
g.drawString("14", 557, 45);
g.drawString("15", 592, 45);
g.drawString("16", 627, 45);
g.drawString("17", 662, 45);
g.drawString("18", 697, 45);
g.drawString("19", 732, 45);
g.drawString("0", 40, 75);
g.drawString("1", 40, 110);
g.drawString("2", 40, 145);
g.drawString("3", 40, 180);
g.drawString("4", 40, 215);
g.drawString("5", 40, 250);
g.drawString("6", 40, 285);
g.drawString("7", 40, 320);
g.drawString("8", 40, 355);
g.drawString("9", 40, 390);
g.drawString("10", 35, 425);
g.drawString("11", 35, 460);
g.drawString("12", 35, 495);
g.drawString("13", 35, 530);
g.drawString("14", 35, 565);
g.drawString("15", 35, 600);
g.drawString("16", 35, 635);
g.drawString("17", 35, 670);
g.drawString("18", 35, 705);
g.drawString("19", 35, 740);
int dotx1 = 80;
int doty1 = 70;
((Graphics2D) g).setStroke(new BasicStroke(5));
for (int h = 0; h <= 19; h++) {
for (int i = 0; i <= 19; i++) {
g.drawLine(dotx1, doty1, dotx1, doty1);
dotx1 = dotx1 + 35;
}
dotx1 = 80;
doty1 = doty1 + 35;
}
}
}
You shouldn't call paintComponent by yourself. The method is called when Java is drawing the window.
The reason is that the user shouldn't care about re-drawing, most of the time. Java will decide when to redraw, for example on window minimize/maximize or resizing or when an element is clicked.
To draw animated shapes you must ask Java to redraw your window. You can add f.revalidate() to force components to redraw. Take a look at the documentation here.
Be really careful when using this. Never call this inside a loop without waiting between frames, because your CPU will go crazy if not !
If you want animated shapes, call revalidate() at fixed rate, for example 60fps.
In your case, you could simple put your Scanner inside the loop, ask the user for input, process it and then redraw the window.
Also, revalidate() is not blocking, it will just tell Java that the components tree has changed and must be redraw. But it will do it when he will have the time to do it.
Edit : As we are discussing in commentary, to feed your Drawing with the data to draw you must give him the informations.
First, create your graphical elements, let start with points :
class Point {
public float x, y;
Point(float x, float y) {
this.x = x;
this.y = y;
}
}
In your Drawing (who is in fact your Scene, let rename it), add setters and List
class Scene {
private List<Point> mPoints = new LinkedList<Point>();
public addPoint(Point p) {
mPoints.add(p);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
....
for (Point p : mPoints) {
// draw your points here
}
....
}
}
There several way to improve this simple example. First, you could add a paint(Graphics g) method on Point class.
When you will add another kind of component, you should create a base class Item and extend it in your graphical items (Point, Box...) and put the paint method in this new base class.
It's the way to don't have one list for each kind of element, and then you can just iterate over items and call paint without worrying about the kind behind it (Point, Box...)

Move element of array to specific position [duplicate]

This question already has answers here:
Java: moving items in array
(6 answers)
Closed 6 years ago.
How to move element of array to specific position on Android JAVA.
We have
int oldPosition, int newPosition
and some like
JSONObject[] tmp = new JSONObject[999];
array of JSONObjects
If you want to just move
tmp[newPosition]=tmp[oldPosition];
Swap
JSONObject jo= tmp[oldPosition];
tmp[oldPosition]=tmp[newPosition];
tmp[newPosition]=jo;
EDIT: There are other ways but you can go through this as well to get your result :)
Exercise : You can understand this logic and use JSONObject type and do necessary changes, Watch out for NullPointerExceptions , handle everything i am lazy to do them
let's say you have int array --> private int array[];
array = new int[]{10,20,30,40,50,60,70,80,90,100};
call this method if you want to swap elements,
swapNumbers(array,9,1);
.
public int[] swapNumbers(int [] arr, int possition1, int possition2){
int temp = arr[possition2];
arr[possition2] = arr[possition1];
arr[possition1] = temp;
System.out.println("array -->" + Arrays.toString(array));
return arr;
}
out put : array[10, 100, 30, 40, 50, 60, 70, 80, 90, 20]
But that doesn't satisfy you?
you need out put to be like this : array[10, 100, 20, 30, 40, 50, 60, 70, 80, 90]
you can use below method
resetUpMyArray(array, array[9],1);
System.out.println("array Finally changed-->" + Arrays.toString(array));
enjoy,
public int[] resetUpMyArray(int[] inputArray, int deleteMeValue,int addMePosition) {
List resultLinkedList = new LinkedList();
for (int itemValue : inputArray)
if (deleteMeValue == itemValue) {
System.out.println("Do not add this value"+itemValue);
} else {
System.out.println("Do add this value "+itemValue +"position-"+deleteMeValue);
resultLinkedList.add(itemValue);
}
System.out.println("array -as new L.L->" + resultLinkedList);
resultLinkedList.add(addMePosition,deleteMeValue);
System.out.println("array -as new L.L all set->" + resultLinkedList);
array = new int[resultLinkedList.size()];
for (int i = 0; i < resultLinkedList.size(); i++) {
array[i] = (int) resultLinkedList.get(i); // Watch out for NullPointerExceptions!
}
return array;
}
Here is two simple algorithm.
Switch values :
switch(array, from, to)
tmp = array[to]
array[to] = array[from]
array[from] = tmp
That will give something like
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|--<---->--|
[10, 20, 60, 40, 50, 30, 70, 80, 90, 100]
This will simply store the values that will be replace at the index to to be place at index from
Move and shift values:
This one will move one values a shift the values next.
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|------------^
[10, 20, , 40, 50, 60, 30, 70, 80, 90, 100]
<-----------
[10, 20, 40, 50, 60, 30, 70, 80, 90, 100]
For this, the solution is quite the same, store the values in tmp but shift every value to fill the gap. This code will only work if from < to
tmp = array[to]
i = from
while(i < to)
array[i] = array[i+1]; --Shift the value
i = i + 1
array[to] = tmp;

Display card in Java

I am writing a program simulating a single round of the card game WAR.
I don't know how to graphically display the card (number/color/suit).
import java.util.*; // for Scanner class
import java.awt.*;
import javax.swing.*;
// Class Definition
class Project4 {
// Main method definition
public static void main(String[] args) {
// Object and Variable Declarations
JFrame win;
Container contentPane;
Graphics g;
String player1Name, player2Name, scoreWinLoss;
//This is the graphics portion, all of the graphics (so far) are working.
win = new JFrame("War Game");
win.setSize(900, 600);
win.setLocation(100, 100);
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color white = new Color(255, 255, 255);
Color white2 = new Color(255, 255, 255);
Color black = new Color(0, 0, 0);
Color black2 = new Color(0, 0, 0);
contentPane = win.getContentPane();
//Also if anyone knows how to change this .setBackground(Color.Green)from lime green to forest(card table) green that would be awesome!
contentPane.setBackground(Color.GREEN);
g = contentPane.getGraphics();
Font font = new Font("Serif", Font.BOLD, 25);
g.setFont(font);
try {
Thread.sleep(200);
} catch (Exception e) {}
player1Name = JOptionPane.showInputDialog(null, "Player 1, what is your name?");
player2Name = JOptionPane.showInputDialog(null, "Player 2, what is your name?");
g.setColor(black);
g.drawString(player1Name, 220, 35);
g.setColor(black2);
g.drawString(player2Name, 625, 35);
g.setColor(white);
g.fillRoundRect(125, 50, 250, 450, 20, 20);
g.setColor(white2);
g.fillRoundRect(525, 50, 250, 450, 20, 20);
g.setColor(black);
g.drawRoundRect(125, 50, 250, 450, 20, 20);
g.setColor(black2);
g.drawRoundRect(525, 50, 250, 450, 20, 20);
//This is where it gets confusing and this stuff is all not working
int card1Max = 14, card1Min = 2, card2Max = 14, card2Min = 2, randomNum1 = 0, randomNum2 = 0;
g.setColor(black);
g.drawInt(randomNum1, 150, 35);
g.setColor(black2);
g.drawInt(randomNum2, 500, 35);
Random randomPicker = new Random(System.currentTimeMillis());
randomNum1 = randomPicker.nextInt(card1Max - card1Min + 1) + card1Min;
randomNum2 = randomPicker.nextInt(card2Max - card2Min + 1) + card2Min;
if (randomNum1 < randomNum2) {
scoreWinLoss = player2Name + " won!";
} else if (randomNum1 > randomNum2) {
scoreWinLoss = player1Name + " won!";
} else if (randomNum1 == randomNum2) {
scoreWinLoss = "It's a tie! Play again to determine a winner.";
}
Do not play with the graphics object like you are doing :
g.setColor(black);
g.drawString(player1Name, 220, 35);
This is bad!
Instead you should look at a layout. Look at GridLayout, or GridBagLayout. These allow you to organise JPanels as a grid. With these panels you can then set the background color and add text (via a JLabel).
if i understand your problem: as simple way, you can use JPanel with CardLayout and then find card picture from web then, with JLabel.setImageIcon(ImageIcon imageIcon) method of JLabel set this pictures on it and by CardLayout bring up cards on toghether.

Parallel sieve of Eratosthenes - Java Multithreading

I wanted to write sieve of Eratosthenes which will work using specific number of threads. I figured out, that it will work in following way:
For 2 threads up to 17.
Thread-1 takes 2, and starts to remove multiple of 2 from List. Parallel Thread-2 takes 3 and does the same. After that Thread-1 takes 5( because there is no 4 in List) and Thread-2 takes 7 and so on until they reach end.
I wrote following piece of code:
private List<Integer> array = new ArrayList<Integer>();
private List<Integer> results = new ArrayList<Integer>();
public synchronized void run(){
while(array.size() > 0){
Integer tmp = array.get(0);
for(int i = 1; i < array.size(); i++){
if( (array.get(i).intValue() % tmp.intValue()) == 0)
array.remove(i);
}
results.add(array.get(0));
array.remove(0);
}
}
public void setArray(int x){
for(int i = 2; i < x; i++)
array.add(Integer.valueOf(i));
}
public void printArray(){
for(Integer i: results){
System.out.println(i);
}
}
This code works, but I added time measurement "tool" to my main class:
ThreadTask task = new ThreadTask();
task.setArray(5000);
Long beg = new Date().getTime();
for(int i = 0; i < 3;i++){
new Thread(task).start();
}
Long sleep = 1000L;
Thread.sleep(sleep);// I am sleeping main thread to wait until other Threads are done
task.printArray();
System.out.println("Time is "+(new Date().getTime()-beg-sleep));
The problem is that running this with 2 threads is slower than running with 1 thread, and 3 threads are slower than 2 threads. Could anyone explain me, why?
EDIT:
There is one important thing about that. I don't need it to be done as fast as it can be. I need it working on Threads for one reason. My Teacher wants to compare runtimes of running same program with 1, 2 .. n threads. Results should look like in this graph.
EDIT2:
I have rewritten code to following
private HashMap<Integer,Boolean> array = new HashMap<Integer,Boolean>();
private int counter = 1;
private int x;
public void run(){
while(counter < x-1){
do{
counter++;
}
while( array.get(counter));
int tmp = counter;
for(int i = tmp; i < array.size(); i+=tmp){
if( i!= tmp)
array.put(i,true);
}
try{
Thread.sleep(0L, 1);
}
catch (Exception e){}
}
}
public void setArray(int x){
this.x = x;
for(int i = 2; i < x; i++)
array.put(i, false);
}
public void printArray(){
for(int i = 2; i < array.size();i++){
if( !array.get(i))
System.out.println(i);
}
}
Now it uses HashMap and this is how it works:
Fill HashMap with keys from 2 to n and false values.
New thread goes into while loop which is based on counter variable. Counter represents current key.
Increment counter on the begging so new threads doesn't operate on counter of earlier started thread.
Put counter value into temporary variable tmp so we can work even when another thread increment counter
Iterate through the HashMap by incrementing i with tmp ( it is actually jumping on the multiplies of i) and set their values to true.
All keys which has true value are ignored in print method. Also counter skips them when incremented.
The problem is that it still works slower with more threads. What's wrong now?
The mistake is simpler than I first thought. All your threads are doing the same thing so each thread does more work. To make a multi-threaded program work faster you have to divide up the work, which has to be performed concurrently.
When you have one thread accessing a data structure, it can be in the fastest cache of one core, use multiple threads and they need to co-ordinate their actions and since most of the work is updating the data structure, a lot of time is lost as overhead. This is the case even though your data structure is not thread safe and is likely to have a corrupted result.
BTW updating an ArrayList is very expensive and using a collection objects is also an overhead.
You will get a much faster result using a BitSet and just one thread.
public class BitSetSieveMain {
private final BitSet set;
private final int size;
public BitSetSieveMain(int x) {
size = x + 1;
set = new BitSet(size);
set.flip(2, size);
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
long start = System.nanoTime();
BitSetSieveMain bitSetSieveMain = new BitSetSieveMain(5000);
bitSetSieveMain.sieve();
long time = System.nanoTime() - start;
System.out.println(time / 1000 + " micro-seconds to perform " + bitSetSieveMain);
}
}
public void sieve() {
int i = 2;
do {
for (int j = i*2; j < size; j += i)
set.clear(j);
i = set.nextSetBit(i+1);
} while (i > 0);
}
public String toString() {
return set.toString();
}
}
finally prints
87 micro-seconds to perform {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999}

Categories