# Java Asterisk Rectangle [closed]

For my CS class, my instructor has given us the assignment of creating a rectangle made of asterisks with diagonal lines drawn through it in Java.

He also told us to write it in as few bytes as possible. I've gotten it down to 190 bytes, but I need to find a few to simplify this code even more to decrease bytes. Can anyone help me with this?

This code is functional:

interface d{static void main(String[]a){for(int z=0,w=new Byte(a[0]),h=new Byte(a[1]);z<h*w;){int y=z/w,x=z++%w;System.out.print((x>w-2)?"*\n":(y%(h-1)*x*((y-x)%3)==0)?"*":" ");}}}


Input is 10 10.

Output:

**********
**  *  * *
* *  *  **
*  *  *  *
**  *  * *
* *  *  **
*  *  *  *
**  *  * *
* *  *  **

• Don't redirect him to SO; he'll get eaten alive. – Leaky Nun Aug 22 '16 at 15:55
• Can you clarify the output and specs of the program? i.e. example input/output etc – TheLethalCoder Aug 22 '16 at 15:56
• I'm not entirely sure why this being close voted. This is most definitely not a general programming question. It's pretty much a standard tips question, which is very much on topic. I'm not sure whether we have a policy on homework, although as far as I can see the OP is even showing their own effort, so I don't think there's really anything wrong with this? – Martin Ender Aug 22 '16 at 15:58
• @NathanMerrill Advice for specific golfing problems is most definitely not off topic. – Martin Ender Aug 22 '16 at 15:59
• @LeakyNun we don't eat people alive, we kill them first ;) – user51896 Aug 23 '16 at 0:28

logically, there should be Asterik ("*") every time i == j & i+j==w-1(for diagonals), i == 0 & j == 0 (for top line and left side) and j == w-1& i==h-1 (for right side and bottom line).

class d {
public static void main(String[] a) {
for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++) {
for(int j=0;j<w;j++) {
System.out.print(i==0 || j==0 || i==h-1 || i+j==w-1 || j==w-1 || i==j ? "*":" ");
}
System.out.println();
}
}
}

• I don't know if this is an example of the logic that can be used or a fully golfed code. If it's the second case, there's a lot to golf here. Such as, the useless System.out.println() and all the useless whitespaces between the OR pipes (||) and the ternary operator. – Yytsi Sep 14 '16 at 6:30
• this is just an example to logic @TuukkaX. – Abbas Kararawala Sep 19 '16 at 7:55

The code you provided could be reduced by doing this:

• replace "*" by 42 and " " by ' ' (taking advantage of the charcode for *)
• move ((y - x) % 3) to the begining of the things to be multiplied and remove the surrounding parenthesis
• remove surrounding parenthesis from (x > w - 2) and from ((y - x) % 3 * y % (h - 1) * x == 0)

The resulting code would be:

interface r{static void main(String[]a){for(int z=0,w=new Byte(a[0]),h=new Byte(a[1]);z<h*w;){int y=z/w,x=z++%w;System.out.print(x>w-2?"*\n":(y-x)%3*y%(h-1)*x==0?42:' ');}}}


Note: the last line is missing in the example output of the question! The output of the sample code is different.

I don't actually have Java on my computer so I can't test this but I think it should work for 174 bytes and almost definitely could be golfed more

class d{static void main(String[]a){for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++)for(int j=0;j<w;j++)System.out.print(j==w-1?"*\n":i%h-1==0||!j||i+j%2==0?"*":" ");}}


white space put in for clarity:

class d {
static void string main(String[] a) {
for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++)
for(int j=0;j<w;j++)
System.out.print(j==w-1 ? "*\n" : i % h-1 == 0 || !j || i+j % 2 == 0 ? "*":" ");
}
}


print "*\n" for the last char in each line, "*" for all of the first and last lines and the first column, and "*" for any time that the sum of the row and column is even, otherwise prints " "