From: senrath
Huh? Where did you hear that // is semi-forbidden?
I did not hear it. It is general knowledge. I think ANSI C even warns (or errors) if coders use //.
Since Java is pretty much based on C when it comes to syntax, one can assume that the same applies. JavaDoc is even stricter.
Try to compile this in C, with the following flags:
int main() {
// this is a comment
return 0;
}
$ gcc test.c -ansi -Wall -pedantic
test.c: In function ‘main’:
test.c:2: error: expected expression before ‘/’ token
test.c:4: warning: control reaches end of non-void function
Furthermore, if one wants others to read his code, it must be well commented.
I think at every university they teach pupils that "//" is simply a no-go, and they must use /* for single line comments too.
For you to compare:
/* In this statement we test b, and if b is true, we give a the value of c. */
if(b)
a = c;
versus
if(b) // In this statement we test b, and if b is true, we give a the value of c.
a = c;
And
/*
* Program: Bank Manager
* Author: Nadeem Syed
* Date: Sept. 25, 2009
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class BankManager extends Applet implements ActionListener
{
//* State global variables
final static Boolean DebugMode = true;
private int[] intAmountArr = new int[2];
private int[][] intBillsArr = new int[11][2];
private Image[] imgBillsArr = new Image[11];
private TextField txfWithdraw = new TextField(10);
private Button btnWithdraw = new Button("Withdraw");
private Button btnReset = new Button("Reset");
private Label strError = new Label("", Label.CENTER);
public void init()
{
PrintLine("~BEGIN~", true, true, true);
// Set layout null
setLayout(null);
// Add the title
Label lblTitle = new Label("Enter Amount");
add(lblTitle);
lblTitle.setBackground(new Color(160, 0, 0));
lblTitle.setForeground(Color.white);
lblTitle.setFont(new Font("TimesRoman", Font.PLAIN, 19));
lblTitle.setBounds(50, 5, 120, 30);
// Add the textfield
add(txfWithdraw);
txfWithdraw.setBounds(170, 9, 100, 20);
txfWithdraw.addActionListener(this);
// Add the buttons
add(btnWithdraw);
btnWithdraw.setBounds(280, 7, 70, 25);
btnWithdraw.addActionListener(this);
add(btnReset);
btnReset.setBounds(325, 270, 70, 25);
btnReset.addActionListener(this);
// Load images
for (int i = 0; i <= 10; i++)
imgBillsArr[i] = getImage(getCodeBase(), "images/" + (i>6 ? "c" : "d") + (i+1) + ".jpg");
this.LoadBills();
}
//* ObtainValues(): Obtain and insert amount withdrawn into array
// @param String amount: The amount entered by user to withdraw
private void ObtainValues(String amount)
{
PrintLine("[ObtainValues] @amount: " + amount, true, true, true);
try
{
// Check if @amount starts with '$', if false, print error
if (!amount.startsWith("$")) strError.setText("Please enter the dollar sign. ($)");
// Create a regex compilation based on pattern
java.util.regex.Pattern p = java.util.regex.Pattern.compile("[$.\\s]");
// Split amount using the pattern above
String[] arr = p.split(amount);
// Adds '1' incase of array [0] being blank
int push = arr[0].equals("") ? 1 : 0;
// Add leading zeroes to the cents
if (arr[push + 1].length() < 2) arr[push + 1] += "00";
// Check if number of cents is greater than 2 digits, if true, trim it
if (arr[push + 1].length() > 2) arr[push + 1] = arr[push + 1].substring(0, 2);
// Store values into our array
intAmountArr[0] = Integer.parseInt(arr[push]);
intAmountArr[1] = Integer.parseInt(arr[push + 1]);
// Check if numbers are positive, if negative, throw new Exception()
if (intAmountArr[0] < 0 || intAmountArr[1] < 0) throw new Exception();
PrintLine("Dollars: " + intAmountArr[0] + ", Cents: " + intAmountArr[1], true, true, true);
}
catch(Exception e)
{
strError.setText("Please enter a proper format. ($##.##)");
}
}
//* LoadBills(): Loads the array with values of the bills
private void LoadBills()
{
// Assign dollar and cent amounts to the 2D array
// Dolars: [0] = 1, [1] = 2, [2] = 5, [3] = 10, [4] = 20, [5] = 50, [6] = 100
// Cents: [7] = 1, [8] = 5, [9] = 10, [10] = 25
for (int i = 0; i <= 10; i++)
{
intBillsArr[i][0] = i==2 || i==8 ? 5 : (i==3 || i==9 ? 10 :
(i==4 ? 20 : (i==5 ? 50 : (i==6 ? 100 : (i==10 ? 25 : (i==7 ? 1 : i+1))))));
PrintLine((i>0 ? ", " : "") + intBillsArr[i][0], i==10, true, i==0);
}
}
//* SetChange(): Sets the amount of change given
private void SetChange()
{
// Local variables to hold looping value of the amount left
int c = intAmountArr[1], d = intAmountArr[0];
// Loop through and assign change to each array of bills
for (int i = 10; i >= 0; i--)
{
intBillsArr[i][1] = (int)(i > 6 ? c : d) / intBillsArr[i][0];
if (i > 6) c -= intBillsArr[i][1] * intBillsArr[i][0];
else d -= intBillsArr[i][1] * intBillsArr[i][0];
PrintLine(String.format("i[%d] | %s%d: %d | c/d: %d", i, (i>6) ? "¢" : "$",
intBillsArr[i][0], intBillsArr[i][1], (i>6) ? c : d), true, true, true);
}
}
//* PrintLine(String output, Boolean newLine, Boolean debug): Writes into the console
// @param String output: The text to output in the Console
// @param Boolean newLine: Adds a break for new line if true
// @param Boolean debug: If true, then prints in DEBUG mode
static void PrintLine(String output, Boolean newLine, Boolean debug, Boolean sayDebug)
{
if (debug && !DebugMode) return;
System.out.print((debug && sayDebug ? "[DEBUG]: " : "") + output + (newLine ? "\n" : ""));
}
//* actionPerformed(ActionEvent e): Execute upon user action
public void actionPerformed(ActionEvent e)
{
// Execute withdrawal if the user clicked the Withdraw button
if (e.getSource() == btnWithdraw)
{
String amount = txfWithdraw.getText().trim();
PrintLine("[BankManager] @amount: " + amount, true, true, true);
this.strError.setText("");
this.remove(this.strError);
this.ObtainValues(amount);
this.SetChange();
if (!this.strError.getText().equals("")) this.add(this.strError);
repaint();
}
if (e.getSource() == btnReset)
{
this.strError.setText("");
this.remove(this.strError);
this.ObtainValues("$0.001");
this.SetChange();
if (!this.strError.getText().equals("")) this.add(this.strError);
repaint();
}
}
//* paint(Graphics g): Paint onto the canvas
public void paint(Graphics g)
{
// Create the 2 rectangles in the background
g.setColor(new Color(160, 0, 0));
g.drawRect(0, 0, 399, 299);
g.fillRect(0, 0, 399, 39);
// Check and display error
if (!this.strError.getText().equals(""))
{
this.strError.setBackground(new Color(255, 99, 99));
this.strError.setForeground(Color.white);
this.strError.setBounds(26, 71, 349, 124);
this.strError.setFont(new Font("TimesRoman", Font.PLAIN, 19));
g.setColor(new Color(160, 0, 0));
g.drawRect(25, 70, 350, 125);
return;
}
// Draw the images for the bills, and all the text beneath the images
for (int i = 0; i <= 10; i++)
{
Point p = new Point(((i>1 && i<7 ? 46 : 56) * (i-(i>6 ? 7 : 0))) + (i>1 && i<7 ? 65 : 20), 60 + (i>6 ? 130 : 0));
g.setColor((i+1) % 2 == 0 ? new Color(219, 219, 219) : new Color(243, 243, 243));
g.fillRect(p.x - (i>1 && i<7 ? 5 : 3), p.y + 50, i>2 && i<7 ? 46 : 56, i>6 ? 55 : 65);
g.setColor((i+1) % 2 == 0 ? new Color(200, 200, 200) : new Color(233, 233, 233));
g.fillRect(p.x - (i>1 && i<7 ? 5 : 3), p.y + 50, i>2 && i<7 ? 46 : 56, i>6 ? 35 : 45);
g.drawImage(imgBillsArr[i], p.x, p.y, this);
g.setColor((i+1) % 2 == 0 ? Color.black : Color.gray);
g.drawString("" + intBillsArr[i][1], p.x + (i>1 && i<7 ? 15 : 20), p.y + (i>6 ? 80 : 90));
g.drawString((i>6 ? "¢" : "$") + (intBillsArr[i][1] * intBillsArr[i][0]), p.x + 3, p.y + (i>6 ? 99 : 109));
}
}
}
vs
package Sudoku;
public class BitClass {
protected long[] b;
protected long bits;
protected long bitsSet;
protected long bitMask;
/*
* Creates a BitClass with the given amount of bits.
*/
public BitClass(long amountOfBits) {
b = new long[(int) Math.ceil((double)(amountOfBits / 64.0d))];
bits = amountOfBits;
bitMask = this.computeNumMask();
}
/*
* Creates a copy of the current BitClass
*/
public BitClass copy() {
BitClass bC = new BitClass(0);
bC.bits = this.bits;
bC.b = this.b.clone();
bC.bitsSet = this.bitsSet;
bC.bitMask = this.bitMask;
return bC;
}
/*
* Returns the length of b.
*/
public long length() {
return b.length;
}
/*
* Returns a mask used to compute the number of bits set.
* If the number of bits would be 9, it returns 0x0000 0000 0000 01FF
* This mask is used to drop off the bits that are not used in the last long
* in the array. Because of operations like NOT, the bits in the last long
* of the array might not be 0, therefor they need to be dropped.
* 0xFFFFFFFFFFFFFFFF & 0x0000 0000 0000 01FF leaves only the first 9 bits
* intact.
*/
public long computeNumMask() {
final long bitsMod64 = bits % 64;
long mask = 0;
for(int i = 0; i < bitsMod64; i += 2)
mask |= 3 << i;
return mask >> (bits & 0x1);
}
/*
* Returns the value of the bit at position pos.
*/
public long get(int pos) {
if ((pos < 0) || (pos > b.length)) {
/* Throw Exception */
}
return (b[pos / 64] >> (pos % 64)) & 1;
}
/*
* Set's the value of the bit at position pos to 1.
*/
public void set(int pos) {
if ((pos < 0) || (pos > bits)) {
/* Throw Exception */
}
/* If the bit is set to 0, increase bitsSet and set the bit to 1 */
if((b[pos / 64] >> (pos % 64) & 1) == 0) {
b[pos / 64] |= (1 << pos);
bitsSet++;
}
}
/*
* Set's the value of the bit at position pos to 0.
*/
public void unset(int pos) {
if ((pos < 0) || (pos > bits)) {
/* Throw Exception */
}
/* If the bit is set to 1, increase bitsSet and set the bit to 0 */
if((b[pos / 64] >> (pos % 64) & 1) == 1) {
b[pos / 64] &= ~(1 << pos);
bitsSet--;
}
}
/*
* Performs a not operation on the current BitClass.
*/
public BitClass not() {
BitClass resBitClass = new BitClass(bits);
for(int i = 0; i < this.b.length; i++)
resBitClass.b[i] = ~this.b[i];
resBitClass.bitsSet = resBitClass.countBitsSet();
return resBitClass;
}
/*
* Performs an and operation on the current BitClass and BitClass c.
*/
public BitClass and(BitClass c) {
BitClass resBitClass = new BitClass(bits);
for(int i = 0; i < b.length; i++)
resBitClass.b[i] = b[i] & c.b[i];
resBitClass.bitsSet = resBitClass.countBitsSet();
return resBitClass;
}
/*
* Performs an or operation on the current BitClass and BitClass c.
*/
public BitClass or(BitClass c) {
BitClass resBitClass = new BitClass(bits);
for(int i = 0; i < b.length; i++)
resBitClass.b[i] = b[i] | c.b[i];
resBitClass.bitsSet = resBitClass.countBitsSet();
return resBitClass;
}
/*
* Performs a xor operation on the current BitClass and BitClass c
*/
public BitClass xor(BitClass c) {
BitClass resBitClass = new BitClass(bits);
for(int i = 0; i < b.length; i++)
resBitClass.b[i] = b[i] ^ c.b[i];
resBitClass.bitsSet = resBitClass.countBitsSet();
return resBitClass;
}
/*
* Fills the BitClass with either 0 or 1.
*/
public BitClass fill(boolean set) {
BitClass resBitClass = this.copy();
long num = set ? 0xFFFFFFFFFFFFFFFFl : 0l;
for(int i = 0; i < resBitClass.length(); i++)
resBitClass.b[i] = num;
resBitClass.bitsSet = resBitClass.b.length * (64 & num);
return resBitClass;
}
/*
* printOptions prints every bit in the BitClass.
*/
public void printOptions() {
for(int i = 0; i < bits; i++) {
System.out.print(get(i));
}
System.out.println(" has " + countBitsSet() + ", " + bitsSet);
}
public long countBitsSet() {
/*
* We have to copy the current bits,
* because they will be changed by the counting algorithm.
*/
long j = 0, t = 0, tt;
int i = 0;
for(i = 0; i < b.length - 1; i++) {
tt = this.b[i];
for(t = 0; tt != 0; t++)
tt &= tt - 1;
j += t;
}
tt = b[i] & this.bitMask;
for(t = 0; tt != 0; t++)
tt &= tt - 1;
return j + t;
}
/*
* returns all the possiblities as an integer array.
*/
public int[] returnNumbers() {
int[] tmpInt = new int[(int)countBitsSet()];
int c = 0;
for(int i = 0; i < bits; i++)
if((b[i / 64] >> (i % 64) & 1) == 1)
tmpInt[c++] = i;
int[] resInt = new int[c];
for(int i = 0; i < c; i++) {
resInt[i] = tmpInt[i] + 1;
}
return resInt;
}
/*
* Returns true if the current BitClass equals BitClass a
*/
public boolean equals(BitClass a) {
if(a.bits != this.bits)
return false;
for(int i = 0; i < b.length; i++) {
if(a.b[i] != this.b[i])
return false;
}
return true;
}
@Override
public String toString() {
String s = new String();
for(int i = 0; i < bits; i++) {
s = s + get(i);
}
return s;
}
}
EDIT: The last bit code from a very old sudoku-java project. I didn't add any JavaDoc. (Which is also /* only afaik, and not //)
Also note now Nadeem's code needs a horizontal scroll bar - he goes past the 80 characters per line. :)
(No offence intended!)