Thursday, 3 October 2013

Random data generator in java

Random data generator in java

i have to fill every property of a given object with a random value.
All properties are native java type (int, double, String, etc)
I canuse reflection
I can use Spring DirectFieldAccessor
I don't want to re-invent the square wheel so I prefer to ask.
For now I came up with this : Get all properties name with
Field field : myObject.getClass().getDeclaredFields()
Iterate over those field and get their class.
Then use a giant switch statement for each known native java type and
generate a random value.
What do you think?

Wednesday, 2 October 2013

Count and Joins

Count and Joins

Customer
CustomerID Name
4001 John Bob
4002 Joey Markle
4003 Johny Brown
4004 Jessie Black
Orders
OrderID Customer
50001 4001
50002 4002
50003 4001
50004 4003
50005 4001
50006 4003
50007 4001
I tried this join
Select c.Customer, COUNT(o.OrderID) as TotalOrders
from Customer c
inner join Orders o
on c.Customer = o.Customer
Group by c.Customer
But here is the result.
Customer TotalOrders
4001 4
4002 1
4003 2
The customer with no order is not included. How I will include all the
customer even they don't have orders?
Customer TotalOrders
4001 4
4002 1
4003 2
4004 0

ReadObject is returning null values

ReadObject is returning null values

I have the following class design. My engine attribute is coming as null
each time,even though I have read its value from readObject
public class Car implements Serializable {
private int regId;
transient Engine e;
private void writeObject(ObjectOutputStream oos) {
try {
oos.defaultWriteObject();
oos.writeInt(e.horsePower);
} catch (Exception e) {
e.printStackTrace();
}
}
private void readObject(ObjectInputStream oxos) {
try {
oxos.defaultReadObject();
Engine e = new Engine(oxos.readInt());
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
public class Engine {
int horsePower;
}

HTML/CSS Help w/ Validating! Unclosed Element

HTML/CSS Help w/ Validating! Unclosed Element

I am new to HTML and CSS and am trying to make it to CSS but when i use
the validaotr, it just says unclosed element!Ignore the pi
enter code here
<style>
h1{color:white;}
body{background-color:#C9C9DB;}
p{text-align:right;font-size:2em;}
p{color:#FFFFFF}
h2{color:white;font-size:1em;text-align:left;}
IMG.displayed {
display: block;
margin-left: auto;
margin-right: auto }
p1.italic {font-style:italic;}x
p1.a {list-style-type:circle;}
</style>
<p>MyTSC</p>
<IMG class="displayed" src="123.jpg" alt="Logo">
<hr width="50%" color="white"/>
<p style="font=size:2em;text-align:left;">My Fall Courses</p>
<h2>
<p1 class="a">
<li>
<p1 class="italic">COSC 4330 Computer Graphics</p1>
</br><li>
IMED 1416 Wed Design I
ITNW 2413 Networking Hardware

RGB to binary in android

RGB to binary in android

I would like to find an android code that will assist me convert an RGB
image to a binary image. Please assist. Also kindly assist me to find out
how I can modify a code as this below to integrate it for an Adroid mobile
application.
package ij.process;
import java.awt.*;
/** This class processes binary images. */
public class BinaryProcessor extends ByteProcessor {
private ByteProcessor parent;
/** Creates a BinaryProcessor from a ByteProcessor. The ByteProcessor
must contain a binary image (pixels values are either 0 or 255).
Backgound is assumed to be white. */
public BinaryProcessor(ByteProcessor ip) {
super(ip.getWidth(), ip.getHeight(), (byte[])ip.getPixels(),
ip.getColorModel());
setRoi(ip.getRoi());
parent = ip;
}
static final int OUTLINE=0;
void process(int type, int count) {
int p1, p2, p3, p4, p5, p6, p7, p8, p9;
int inc = roiHeight/25;
if (inc<1) inc = 1;
int bgColor = 255;
if (parent.isInvertedLut())
bgColor = 0;
byte[] pixels2 = (byte[])parent.getPixelsCopy();
int offset, v=0, sum;
int rowOffset = width;
for (int y=yMin; y<=yMax; y++) {
offset = xMin + y * width;
p2 = pixels2[offset-rowOffset-1]&0xff;
p3 = pixels2[offset-rowOffset]&0xff;
p5 = pixels2[offset-1]&0xff;
p6 = pixels2[offset]&0xff;
p8 = pixels2[offset+rowOffset-1]&0xff;
p9 = pixels2[offset+rowOffset]&0xff;
for (int x=xMin; x<=xMax; x++) {
p1 = p2; p2 = p3;
p3 = pixels2[offset-rowOffset+1]&0xff;
p4 = p5; p5 = p6;
p6 = pixels2[offset+1]&0xff;
p7 = p8; p8 = p9;
p9 = pixels2[offset+rowOffset+1]&0xff;
switch (type) {
case OUTLINE:
v = p5;
if (v!=bgColor) {
if (!(p1==bgColor || p2==bgColor ||
p3==bgColor || p4==bgColor
|| p6==bgColor || p7==bgColor ||
p8==bgColor || p9==bgColor))
v = bgColor;
}
break;
}
pixels[offset++] = (byte)v;
}
if (y%inc==0)
parent.showProgress((double)(y-roiY)/roiHeight);
}
parent.hideProgress();
}
// 2012/09/16: 3,0 1->0
// 2012/09/16: 24,0 2->0
private static int[] table =
//0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1
{0,0,0,0,0,0,1,3,0,0,3,1,1,0,1,3,0,0,0,0,0,0,0,0,0,0,2,0,3,0,3,3,
0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,3,0,2,2,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,0,0,0,0,0,0,0,2,0,0,0,2,0,0,0,3,0,0,0,0,0,0,0,3,0,0,0,3,0,2,0,
0,0,3,1,0,0,1,3,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,3,1,3,0,0,1,3,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,3,0,1,0,0,0,1,0,0,0,0,0,0,0,0,3,3,0,1,0,0,0,0,2,2,0,0,2,0,0,0};
private static int[] table2 =
//0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1
{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,2,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
/** Uses a lookup table to repeatably removes pixels from the
edges of objects in a binary image, reducing them to single
pixel wide skeletons. There is an entry in the table for each
of the 256 possible 3x3 neighborhood configurations. An entry
of '1' means delete pixel on first pass, '2' means delete pixel on
second pass, and '3' means delete on either pass. Pixels are
removed from the right and bottom edges of objects on the first
pass and from the left and top edges on the second pass. A
graphical representation of the 256 neighborhoods indexed by
the table is available at
"http://imagej.nih.gov/ij/images/skeletonize-table.gif".
*/
public void skeletonize() {
int pass = 0;
int pixelsRemoved;
resetRoi();
setColor(Color.white);
moveTo(0,0); lineTo(0,height-1);
moveTo(0,0); lineTo(width-1,0);
moveTo(width-1,0); lineTo(width-1,height-1);
moveTo(0,height-1); lineTo(width/*-1*/,height-1);
ij.ImageStack movie=null;
boolean debug = ij.IJ.debugMode;
if (debug) movie = new ij.ImageStack(width, height);
if (debug) movie.addSlice("-", duplicate());
do {
snapshot();
pixelsRemoved = thin(pass++, table);
if (debug) movie.addSlice(""+(pass-1), duplicate());
snapshot();
pixelsRemoved += thin(pass++, table);
if (debug) movie.addSlice(""+(pass-1), duplicate());
} while (pixelsRemoved>0);
do { // use a second table to remove "stuck" pixels
snapshot();
pixelsRemoved = thin(pass++, table2);
if (debug) movie.addSlice("2-"+(pass-1), duplicate());
snapshot();
pixelsRemoved += thin(pass++, table2);
if (debug) movie.addSlice("2-"+(pass-1), duplicate());
} while (pixelsRemoved>0);
if (debug) new ij.ImagePlus("Skel Movie", movie).show();
}
int thin(int pass, int[] table) {
int p1, p2, p3, p4, p5, p6, p7, p8, p9;
int bgColor = -1; //255
if (parent.isInvertedLut())
bgColor = 0;
byte[] pixels2 = (byte[])getPixelsCopy();
int v, index, code;
int offset, rowOffset = width;
int pixelsRemoved = 0;
int count = 100;
for (int y=yMin; y<=yMax; y++) {
offset = xMin + y * width;
for (int x=xMin; x<=xMax; x++) {
p5 = pixels2[offset];
v = p5;
if (v!=bgColor) {
p1 = pixels2[offset-rowOffset-1];
p2 = pixels2[offset-rowOffset];
p3 = pixels2[offset-rowOffset+1];
p4 = pixels2[offset-1];
p6 = pixels2[offset+1];
p7 = pixels2[offset+rowOffset-1];
p8 = pixels2[offset+rowOffset];
p9 = pixels2[offset+rowOffset+1];
index = 0;
if (p1!=bgColor) index |= 1;
if (p2!=bgColor) index |= 2;
if (p3!=bgColor) index |= 4;
if (p6!=bgColor) index |= 8;
if (p9!=bgColor) index |= 16;
if (p8!=bgColor) index |= 32;
if (p7!=bgColor) index |= 64;
if (p4!=bgColor) index |= 128;
code = table[index];
if ((pass&1)==1) { //odd pass
if (code==2||code==3) {
v = bgColor;
pixelsRemoved++;
}
} else { //even pass
if (code==1||code==3) {
v = bgColor;
pixelsRemoved++;
}
}
}
pixels[offset++] = (byte)v;
}
}
return pixelsRemoved;
}
public void outline() {
process(OUTLINE, 0);
}
}

Tuesday, 1 October 2013

Pick 4 random elements from an NSArray of count 6

Pick 4 random elements from an NSArray of count 6

I am new to Objective C. I am coding the game Mastermind where the
computer chooses 4 random colors out of 6 and the user tries to guess the
4 colors in 6 tries.
I have an NSArray to represent all the six possible colors here:
NSArray * allColors = [[NSArray alloc] initWithObjects:@"r", @"g",
@"b", @"y", @"p", @"o", nil];
//Computer choose 4 random colors:
NSArray * computersSelection = [[NSArray alloc] init];
I need to write code to choose 4 UNIQUE random colors from the array. Is
there a smart way to do this?
I can create four int variables and use a while loop to generate four
random numbers and then pull the objects from the NSArray based on the
four random integer values and put them in the computerSelection array but
I am wondering if there's any simpler way of doing things?
Thanks

Menus in Batch File

Menus in Batch File

I don't usually create batch files as I just type what I need into the run
box or the command prompt but I'm trying to make one just to let me access
basic utilities in windows and check up on things (I really don't need it
but I think my dad would find it helpful). I am familiar (but new) with
python so if using python for these things is a better option I can do
that, however I thought batch was the best way of doing something as
simple as this. The problem is with my menu. I think because of my menu ,
it is cycling through all of the commands before doing the command
selected. Any help with this will be fully appreciated, the batch script
is in a code box below.
echo off
:menu
echo This is a simple cleanup and repair utility. Please select an option:
echo 1 - Check the hard disk c:\ for errors and inconsistancies.
echo 2 - Renew the IP address
echo 3 - View IP Address information
echo 4 - Check internet connection by pinging http://www.google.co.uk/
echo 5 - Start disk cleanup utility
echo 6 - ping 192.168.0.1
echo 7 - ping 192.168.1.1
echo 8 - Open notepad
choice /n /c:12345678 /M "Choose an option (1-8) "
IF ERRORLEVEL == 1 GOTO CHKDSK
IF ERRORLEVEL == 2 GOTO RENEW
IF ERRORLEVEL == 3 GOTO DISPLAYIP
IF ERRORLEVEL == 4 GOTO PINGGOOGLE
IF ERRORLEVEL == 5 GOTO CLEANMGR
IF ERRORLEVEL == 6 GOTO PING0
IF ERRORLEVEL == 7 GOTO PING1
IF ERRORLEVEL == 8 GOTO STARTNOTE
:CHKDSK
CHKDSK C:
PAUSE
goto menu
:RENEW
IPCONFIG /RENEW
PAUSE
goto menu
:DISPLAYIP
IPCONFIG /ALL
PAUSE
goto menu
:PINGGOOGLE
PING HTTP://WWW.GOOGLE.CO.UK/
PAUSE
goto menu
:CLEANMGR
CLEANMGR
PAUSE
goto menu
:PING0
PING 192.168.0.1
PAUSE
goto menu
:PING1
PING 192.168.1.1
PAUSE
goto menu
:STARTNOTE
START NOTEPAD
PAUSE
goto menu