Author Topic: My java progress.  (Read 1134 times)

As you might've seen my game idea topic, I announced that I was going to be making the game, and I need to learn Java. Im just gonna post my progress here.

Im learning from my dad.

First small project:
Code: [Select]
package Again;

public class First {

static int offset = 20;

/**
* @param args
*/
public static void main(String[] args) {
printRhombus(20);
}

private static void printTriangle(int Lines) {
for (int LineNumber = 0; LineNumber < Lines; LineNumber++) {
print(offset, " ");
printLine(Lines, LineNumber);
}
print(offset, " ");
print(Lines * 2 + 2, "*");
System.out.println();
}

private static void printTriangleDown(int Lines) {
print(offset, " ");
print(Lines * 2 + 2, "*");
System.out.println();
for (int LineNumber = Lines; LineNumber > 0; LineNumber--) {
print(offset, " ");
printLine(Lines, LineNumber);
}
}

private static void printLine(int Lines, int LineNumber) {
print(Lines - LineNumber, " ");
System.out.print("*");
print(LineNumber * 2, "O");
System.out.println("*");
}

private static void printRhombus(int Lines) {
printTriangle(Lines);
printTriangleDown(Lines);
}

private static void print(int count, String what) {
for (int i = count; i > 0; i--) {
System.out.print(what);
}
}
}
Prints a triangle, upside-down triangle or a rhombus.


New thing:
Code: [Select]
package Again;

public class PrimeNumber {

/**
* @param args
*/
public static void main(String[] args) {
int number = 1;
int max = 100;
for (; number < max + 1; number++) {
boolean dividedToAny = false;
for (int primeChecker = 2; primeChecker <= number/2; primeChecker++) {
int primeNumberDivisorBefore = number / primeChecker*primeChecker;
if(primeNumberDivisorBefore == number){
dividedToAny = true;
break;
}

}
if(!dividedToAny){
System.out.println(number);
}
}
}
}

Finds all the prime numbers in the "Max" number
My dad helped me a lot on this one.
« Last Edit: February 08, 2012, 11:52:41 AM by Communist »

At first I thought it was a graphical program so I was searching at the top for the imported libraries but it's just direct communication to the OS.

Nice.

At first I thought it was a graphical program so I was searching at the top for the imported libraries but it's just direct communication to the OS.

Nice.
Thanks.

I don't like how the variable in the for loops (and some of your functions) begins with a capital letter. Capital letters are for classes, start variable names with lowercase letters.

You've also defined variable "offset" to be 20 - and you're then calling printRhombus(20); .. why not use offset? In addition, if offset never changes (I don't see you changing it anywhere), consider making it a constant.

You also have "magic numbers" - numbers that seem to appear from nowhere. For example, in this line of code:
Code: [Select]
print(Lines * 2 + 2, "*");the numbers 2 and 2 are simply placed there. Why are they there (not asking you, you should be asking yourself this)? Name them accordingly.
« Last Edit: February 08, 2012, 12:26:40 PM by SpreadsPlague »

Code: [Select]
print(Lines * 2 + 2, "*");The multiplication by 2 is required to make it go all the way on the bottom of the triangle. Without the first 2 it would go halfway. Try it yourself. The 2nd 2 was to make it line up with the triangle.
And since the Rhombus just uses both triangle and triangleDown, which do have the offset, it doesn't require the offset itself. And we may have different ideas of offset - its supposed to be how much spaces between the most left point of actual figure and the left side of the console. It just adds the "Offset" amounts of spaces, never changing while running.I might've explained this a bit weirdly, and it might be a bit messy, but I am still learning.

i've done graphical things in java a while ago, here's a little something i did a few months ago, feel free to use/steal/whatever it ;P

Code: [Select]
import javax.swing.JApplet;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Canvas;

import java.util.Random;

public class Test extends JApplet implements Runnable {
private static final long serialVersionUID = 1L;

private Canvas canvas;

private static Test instance;
private boolean running = false;
private final long ticksPerSecond = 60;

private Random rng;

private int cx = 0;
private int cy = 0;

public static Test getInstance() {
return instance;
}

public void init() {
rng = new Random();
canvas = new Canvas();
canvas.setFocusable(true);
add(canvas);
canvas.createBufferStrategy(2);
cx = getWidth() / 2;
cy = getHeight() / 2;
}

public void exit() {

}

public void start() {
running = true;
new Thread(this).start();
}

public void stop() {
running = false;
}

public void paint(Graphics g) {}
public void update(Graphics g) {}

public void run() {
long lastTime, currentTime, deltaTime;
lastTime = System.nanoTime();
while(running) {
currentTime = System.nanoTime();
deltaTime = currentTime - lastTime;
if(deltaTime >= 1000000000L / ticksPerSecond) {
tick(deltaTime / (1000000000.0 / ticksPerSecond));
lastTime = currentTime;
try {
Thread.sleep( 1000 / ticksPerSecond );
}
catch(InterruptedException e) {}
}
}
}

public void tick(final double relativeTimeLag) {
logic(relativeTimeLag);
Graphics g = canvas.getBufferStrategy().getDrawGraphics();
render(g);
g.dispose();
canvas.getBufferStrategy().show();
}

public void render(Graphics g) {
g.setColor(Color.red);
g.fillRect(0,  0, getWidth(), getHeight());
g.setColor(Color.blue);
g.fillOval(cx - 10, cy - 10, 20, 20);
}

public void logic(final double relativeTimeLag) {
cx = (int)((double)cx + (double)(rng.nextInt(20) - 10) * relativeTimeLag);
cy = (int)((double)cy + (double)(rng.nextInt(20) - 10) * relativeTimeLag);
if(cx < 0) cx = 0;
if(cy < 0) cy = 0;
if(cx > getWidth()) cx = getWidth();
if(cy > getHeight()) cy = getHeight();
}
}

Code: [Select]
print(Lines * 2 + 2, "*");The multiplication by 2 is required to make it go all the way on the bottom of the triangle. Without the first 2 it would go halfway. Try it yourself. The 2nd 2 was to make it line up with the triangle.
And since the Rhombus just uses both triangle and triangleDown, which do have the offset, it doesn't require the offset itself. And we may have different ideas of offset - its supposed to be how much spaces between the most left point of actual figure and the left side of the console. It just adds the "Offset" amounts of spaces, never changing while running.I might've explained this a bit weirdly, and it might be a bit messy, but I am still learning.

You missed my point. You can use offset instead of 20 because it's the same number (but yes I see why you wouldn't), and you should give the 2's a name.

Package names are supposed to be all-lowercase. Your for loops should be in the style of "for (int i = 0;i < 0;i++) {/*do something*/}". Variable and method names should start with lower-case.

Did your dad write any of the code or just show it to you?

You also have "magic numbers" - numbers that seem to appear from nowhere. For example, in this line of code:
Code: [Select]
print(Lines * 2 + 2, "*");the numbers 2 and 2 are simply placed there. Why are they there (not asking you, you should be asking yourself this)? Name them accordingly.
There can be legit uses for magic numbers, for example, having a constant named DOUBLE_FACTOR would be fairly ridiculous.

There can be legit uses for magic numbers, for example, having a constant named DOUBLE_FACTOR would be fairly ridiculous.
Agreed. In these cases though it helps to explain, in a comment, what the number represents.'

Also, naming conventions aren't fixed. Yes, it's generally a good idea to start your local variables with a lowercase letter, but it's all up to the person writing the code. The key factor is consistency (and creating a standard that everyone agrees to when working with others).

Additionally, it's often times good practice to distinguish member variables from local variables, such as using an 'm' or an underscore as a prefix.

Additionally, it's often times good practice to distinguish member variables from local variables, such as using an 'm' or an underscore as a prefix.
Not really. But if you absolutely want to distinguish them then you can just use explicit "this.".

Not really. But if you absolutely want to distinguish them then you can just use explicit "this.".
You could. I prefer the use of a prefix to change the name in some way. It's a "religious issue", really. I probably shouldn't have called it good practice.

You could. I prefer the use of a prefix to change the name in some way. It's a "religious issue", really. I probably shouldn't have called it good practice.
I've never seen anyone else have such a practice. Also, do you use the prefix for in-method variables or member variables?

I've never seen anyone else have such a practice. Also, do you use the prefix for in-method variables or member variables?
I prefix my member variables with an '_', but my locals (or in-method) with no prefix. The company I worked at used a semi-Hungarian notation. Prefixing their constants with 'k', their interfaces with 'i', and their members with 'm'. Their code base written in Java. The company my friend worked at, on the other hand, used '_' for members, all caps for constants, and suffixed their interfaces with a capital I. Their code base was written in C#. I've adopted his approach mainly because our current project is written in C#.

I prefix my member variables with an '_', but my locals (or in-method) with no prefix. The company I worked at used a semi-Hungarian notation. Prefixing their constants with 'k', their interfaces with 'i', and their members with 'm'. Their code base written in Java. The company my friend worked at, on the other hand, used '_' for members, all caps for constants, and suffixed their interfaces with a capital I. Their code base was written in C#. I've adopted his approach mainly because our current project is written in C#.
That's odd. AFAIK the official recommendations are as follows:

C#Java
InterfacesPrefix with I, then same as classesSame as classes
ClassesBegin with upper-case, then begin every word after that as upper-caseSame as C#
Member variablesBegin with lower-case, then begin every word after that as upper-caseSame as C#
Local variablesSame as member variablesSame as C#
MethodsSame as classesSame as member variables
PropertiesSame as methodsN/A
ConstantsNot sureAll upper-case, separate words with underscore
Enum constantsSame as propertiesSame as regular constants

There are many ways of going about it. Those are simply Oracle/Sun and Microsofts conventions that they follow and recommend. It doesn't mean, in any way, that they must be followed. As long as the code base you are working with is consistent, that's mainly what matters.