Java Programming Standards Help Needed!

I hope this is the right section:

I have some assignment I need to be working on and once you finish each one your suppost to get it checked by a program called “gide” and this program is super super pickey with java standards.

It was made by one of my instructor and it automaticly checks if you followed the standards and if mess up 4 times with the standards you fail.

So I am getting like alot of errors and I don’t even know what 90% of them mean.

Question 1:

Write an application that reads values representing a time duration in hours,minutes and seconds and then prints the quivalent totoral number of seconds (For example, 1 hour, 28 minutes and 42 seconds is equivalent to 5322 seconds)


/**
   * ToSeconds is the main class where all the converting of time will be done
   * This program will allow a user to input any amount of hours,minutes &
   * seconds and find out how many seconds it is.
   *
   * @author My Name
   * @version 1.0
  */

   import java.util.Scanner;

   public class ToSeconds
{

   /*
    Following statements takes the users input and converts it to seconds
   */
   public static void main (String[] args)
{
   int hour;
   int min;
   int sec;

   /*
    *Below takes an hour what is 60mins and times it by 60secs
   */
   final int MAX_HOUR =60*60;
   final int MAX_MIN =60; // this shows that there is 60mins
   final int TOTAL;

   Scanner scan = new Scanner (System.in);

   System.out.println(" Enter the number of hours ");
   hour = scan.nextInt(); // scans what was inputed and holds it in hour

   System.out.println(" Enter the number of minutes ");
   min = scan.nextInt(); // scans what was inputed and holds it in min

   System.out.println(" Enter the number of seconds ");
   sec = scan.nextInt(); // scans what was inputed and holds it in sec


   /*
    *  The statement below takes the user inputs and converts it into seconds
    *  and holds it in TOTAL
   */

   TOTAL=(hour*MAX_HOUR) + (min*MAX_MIN) + (sec);

   System.out.println(" Your total time " + TOTAL + " converted into seconds");
}

}


So that is what I got. As you can probably notice I am a big noob and one of the things I suck at is naming things like all I could think of was MAX_HOUR and stuff like that but it seems to all work though.

now these are the errors I get:


Course        : COMP 1510
Assignment    : sample
Name          :
Set           :
Automated Mark: 0.00 / 10.00
Manual Mark   : ___.__ / 15.00
Total Mark    : 0.00 / 25.00

------------------------------------------------------------
Style: 0.00 / 10.00

	Lint 0.00 / 10.00

------------------------------------------------------------
Additional: ___.__ / 15.00

	__.__ / 5.00 for Understandable-code

	__.__ / 5.00 for Sensible-variable-names

	__.__ / 5.00 for Valuable-inline-comments


------------------------------------------------------------
Checkstyle Coding :
	ToSeconds.java (31:12):  Variable 'scan' should be declared final.

Checkstyle Javadoc Comments :
	ToSeconds.java (12:0):  Missing a Javadoc comment.
	ToSeconds.java (18:4):  Missing a Javadoc comment.

Checkstyle Miscellaneous :
	ToSeconds.java (12:0):  class def ident at indentation level 3 not at correct indentation, 0
	ToSeconds.java (12:0):  class def modifier at indentation level 3 not at correct indentation, 0
	ToSeconds.java (19:0):  method def lcurly at indentation level 0 not at correct indentation, 3
	ToSeconds.java (20:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (21:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (22:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (27:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (28:0):  Don't use trailing comments.
	ToSeconds.java (28:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (29:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (31:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (33:0):  method call child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (33:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (34:0):  Don't use trailing comments.
	ToSeconds.java (34:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (36:0):  method call child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (36:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (37:0):  Don't use trailing comments.
	ToSeconds.java (37:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (39:0):  method call child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (39:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (40:0):  Don't use trailing comments.
	ToSeconds.java (40:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (48:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (50:0):  method call child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (50:0):  method def child at indentation level 3 not at correct indentation, 6
	ToSeconds.java (51:0):  method def rcurly at indentation level 0 not at correct indentation, 3

Checkstyle Naming Conventions :
	ToSeconds.java (27:14):  Name 'MAX_HOUR' must match pattern '^[a-z][a-zA-Z0-9]*$'.
	ToSeconds.java (28:14):  Name 'MAX_MIN' must match pattern '^[a-z][a-zA-Z0-9]*$'.
	ToSeconds.java (29:14):  Name 'TOTAL' must match pattern '^[a-z][a-zA-Z0-9]*$'.

Checkstyle Whitespace :
	ToSeconds.java (18:27):  'main' is followed by whitespace.
	ToSeconds.java (27:24):  '=' is not followed by whitespace.
	ToSeconds.java (27:26):  '*' is not preceded with whitespace.
	ToSeconds.java (27:27):  '*' is not followed by whitespace.
	ToSeconds.java (28:23):  '=' is not followed by whitespace.
	ToSeconds.java (31:30):  'Scanner' is followed by whitespace.
	ToSeconds.java (48:9):  '=' is not preceded with whitespace.
	ToSeconds.java (48:10):  '=' is not followed by whitespace.
	ToSeconds.java (48:15):  '*' is not preceded with whitespace.
	ToSeconds.java (48:16):  '*' is not followed by whitespace.
	ToSeconds.java (48:32):  '*' is not preceded with whitespace.
	ToSeconds.java (48:33):  '*' is not followed by whitespace.

------------------------------------------------------------


like some of these things I don’t know what they are like the javadocs I read about them in my book but I don’t know how to make them really. Yet at the same time I can’t trust me book because some of the stuff they have written there does not follow what my teacher wants.

Here is some more info about my assignement:



.  For this assignment, the Javadoc comments required are a header comment block as described below.
For this assignment, each program will consist of a single class, with the name as given above.

Comments and documentation

Each class must have a Javadoc header block comment immediately before the class statement.  This must have the following format:

/**
 * Introductory summary sentence describing the class.
 *More complete description of everything the class is supposed to do(may be several
 * lines long
 *
 * @author name of author of the code
 * @version version number, such as 1.0
*/

Each paragraph should be separated from the next by a <p> tag.  If you need to use lists or tables in the description, you should use HTML tags.

Example header block:
/**
 * Graphics is the abstract base class for all graphics contexts
 * which allow an application to draw onto components realized on
 * various devices or onto off-screen images.
 * A Graphics object encapsulates the state information needed
 * for the various rendering operations that Java supports.  This
 * state information includes:
 * <ul>
 * <li>The Component to draw on
 * <li>A translation origin for rendering and clipping coordinates
 * <li>The current clip
 * <li>The current color
 * <li>The current font
 * <li>The current logical pixel operation function (XOR or Paint)
 * <li>The current XOR alternation color
 *     (see <a href="#setXORMode">setXORMode</a>)
 * </ul>
 * <p>
 * Coordinates are infinitely thin and lie between the pixels of the
 * output device.
 * Operations which draw the outline of a figure operate by traversing
 * along the infinitely thin path with a pixel-sized pen that hangs
 * down and to the right of the anchor point on the path.
 * Operations which fill a figure operate by filling the interior
 * of the infinitely thin path.
 * Operations which render horizontal text render the ascending
 * portion of the characters entirely above the baseline coordinate.
 * <p>
 * Some important points to consider are that drawing a figure that
 * covers a given rectangle will occupy one extra row of pixels on
 * the right and bottom edges compared to filling a figure that is
 * bounded by that same rectangle.
 * Also, drawing a horizontal line along the same y coordinate as
 * the baseline of a line of text will draw the line entirely below
 * the text except for any descenders.
 * Both of these properties are due to the pen hanging down and to
 * the right from the path that it traverses.
 * <p>
 * All coordinates which appear as arguments to the methods of this
 * Graphics object are considered relative to the translation origin
 * of this Graphics object prior to the invocation of the method.
 * All rendering operations modify only pixels which lie within the
 * area bounded by both the current clip of the graphics context
 * and the extents of the Component used to create the Graphics object.
 *
 * @author      Sami Shaio
 * @author      Arthur van Hoff
 * @version     %I%, %G%
*/



You might have to copy my code into a editor so you can see what going on which lines.

Looks like a lot of it is just comments (javadoc) and whitespace … here’s a bit of it redone to help you along:

import java.util.Scanner;

/**
* ToSeconds is the main class where all the converting of time will be done
* This program will allow a user to input any amount of hours,minutes &
* seconds and find out how many seconds it is.
*
* @author My Name
* @version 1.0
*/

public class ToSeconds	{

   /**
   * Following statements takes the users input and converts it to seconds
   */
   public static void main (String[] args)	{
	   int hour;
	   int min;
	   int sec;

	   /**
	   * Below takes an hour what is 60mins and times it by 60secs
	   */
	   final int MAXHOUR = 60*60;
	   final int MAXMIN = 60; // this shows that there is 60mins
	   final int TOTAL;

	   Scanner scan = new Scanner(System.in);
	
	   System.out.println(" Enter the number of hours ");
	   hour = scan.nextInt(); // scans what was inputed and holds it in hour
	
	   System.out.println(" Enter the number of minutes ");
	   min = scan.nextInt(); // scans what was inputed and holds it in min
	
	   System.out.println(" Enter the number of seconds ");
	   sec = scan.nextInt(); // scans what was inputed and holds it in sec


	   /**
		*  The statement below takes the user inputs and converts it into seconds
		*  and holds it in TOTAL
	   */
		
	   TOTAL=(hour * MAXHOUR) + (min * MAXMIN) + (sec);
	
	   System.out.println(" Your total time " + TOTAL + " converted into seconds");
	}
};

BTW, what the heck is a java.util.Scanner ??? Is that something new in 1.5 cause Ive never heard of it! And I have NO idea why he woulod want it declared as final.

Ya I just figured that out with the indents it just confused me when it was talking about child and stuff.

My new problems is with the whitespace:

like if I have this(This is line 49 in my code)


total=(hour*MAX_HOUR) + (min*MAX_MIN) + (sec);

I get this erro:


ToSeconds.java (49:18):  '*' is not preceded with whitespace.
ToSeconds.java (49:19):  '*' is not followed by whitespace.

What I can’t get is where is the whitespace??? since I got none there why is it complainning?

Another couple things that are pissing me off are in the book they don’t use final with scanner & in his notes they don’t use final with scanner.

Same is said for this:


ToSeconds.java (25:17):  Name 'MAX_HOUR' must match pattern '^[a-z][a-zA-Z0-9]*$'.

Well my MAX_HOUR I declared as a final constant and according to his notes and the book it should be ALL CAPS and if there is more then 1 word it should be with a underscore.

Yet it is complainning about my name here.

Also can you tell me what a Javadoc comment is is it just what I been doing with /* and */ it is nothing else right? because I am getting confused here when he starting to use html and stuff.

Scanner API … looks interesting, you haven’t switched yet either, eh?

I agree, I don’t see any real reason for scan to be final. I suppose it could save a little time because it’ll be hard-coded into the byte code.

Style tip: for readibilty consider every action to be like a word, youputspacesbetweenyourwordsforareason!

bad: z=x*y
good: z = x * y

You will never save any execution time by making your code hard to read (never ever even) but you will cause yourself much hair-tearing :slight_smile:

Do yourself a favor and learn this right now (repeat after me): “The computer is always right, therefore I will read its error messages closely and with great interest”.

Now, notice that it says “not” in “is not proceeded with whitespace”

questions?

with your example there I think it is going against what the stupid program wants since it wants no whitespaces and your has whitespaces(even though my code has none it still says it does)

Trust me alot of these things are stupid what the program wants

In my book it goes it is ok to combine int variables on the same line like this:

int hi, by, no;

according to the program that is not allowed


ToSeconds.java (20:7):  Each variable declaration must be in its own statement.

No I still do not understand maybe I been working on this for too long but that still did not make sence.

Hmm, I think I see the disconnect here.

The error: “is not proceeded by whitespace” is telling you what is wrong with the code.

It is not telling you what the condition of the code should be. (ie: no whitespace, that error would be: “should not be proceeded by whitespace”)

As to variable declaration, yes “int a, b, c;” is possible and allowed by Java. Apparently, it is not allowed by your instructor. I suggest doing it his way. (seeing as he’s grading your work)

int a;
int b;
int c;

It’s simply easier to read, even if it does take more vertical space.

Hmm, try this: int a; int b; int c; all on one line, just like it is here. There may be a loophole on the ‘separate statement’ rule. :wink:

I think he’s missing the simple fact that this app checker wants to see this with math equations

MIN * MAX … NOT MIN*MAX

It also wants those underscores out of those variables, which i agree with 100% … thats Perl Syntax!

No I switched on one machine and then ripped it right back out. Honestly I freaking HATE 1.5. For one you cant mix 1.5 and 1.4 code in the same app. I get this freaking STUPID compiler versioning error. I did some code on a 1.5 machine and then uploaded it to a 1.4 server (with NO specific 1.5 code) and the server freaked… REALLY pisses me off that these idiots did this.

The other thing I HATE about 1.5 is all these unchecked warnings on collection objects… I find it absolutely assinine they want everything put in a collection object tested… thats just plain stupid!

At this point I have no plans to upgrade again any time soon, especially on our production servers… Im really starting to wonder about the idiots that are in charge of the language!

Ok I fixed the Max * Min thing I understand it now. Yes I did the int the way he wants it even though he showed us how to do it why bother showing us if your going to turn around and say it is wrong?

With the CAPS this I don’t know again the book says it that way and he showed us that way but all of a sudden it is not good anymore.

I also got all the problem down to 4:


Checkstyle Javadoc Comments :
	ToSeconds.java (15:0):  Type Javadoc comment is missing an @author tag.
	ToSeconds.java (21:37):  Expected @param tag for 'args'.

I will contact him about these 2
Checkstyle Naming Conventions :
	ToSeconds.java (31:17):  Name 'MAX_HOUR' must match pattern '^[a-z][a-zA-Z0-9]*$'.
	ToSeconds.java (33:17):  Name 'MAX_MIN' must match pattern '^[a-z][a-zA-Z0-9]*$'.

It is the first 2 I don’t get.

This is for the first one:


/**
  * this starts a class called ToSeconds.
 */
public class ToSeconds

like I know I don’t need to put author in again(well I think I don’t) that would be stupid putting it twice in the same code.

if I put this in:


/*
  * this starts a class called ToSeconds.
 */
public class ToSeconds

I get this error:


ToSeconds.java (15:0):  Missing a Javadoc comment.

This is for the 2nd one


/**
   Following statements takes the users input and converts it to seconds.
 */
   public static void main(String[] args)

I get that @parm tag whatever that is.

if I do this


/*
   Following statements takes the users input and converts it to seconds.
 */
   public static void main(String[] args)
/*

I get this error


ToSeconds.java (21:4):  Missing a Javadoc comment.

It seems like I can’t win


/*
 */
 is a comment
/** <-- note 2 asterisks
 */
is a Javadoc comment

If your instructor hasn’t told you yet, Javadoc is a java tool included with the JDK that can automatically create a set of API documentation, based on your comments. The comments must adhere to the Javadoc spec for Javadoc to be able to generate the documentation.

For an example of the power and wonderfulness of Javadoc look here

Miles and miles of Java API documentation is available on the web, all because Java developers take the little bit of extra time to write Javadoc style comments. Power!

All that documentation is already written because good programmers document their code, it doesn’t have to be rewritten and made into html manually. Wonderful!

@param is a Javadoc tag that tells it a param name and description is to follow, if your method accepts parameters, document them with a @param tag, it’s that easy

So in my case then I should use the javadoc right? but then I still get those errors like should I put for that one my name again on it? would that be right?

and for the second one how do I know if my method accepts a paramater?

Also I am starting my 3rd question now and I need to make an applet of a pie chart but how do I set Jcreator up so it works. Like If I write some code and compile it. It does not work because it can’t display the applet.

So what options do I need to change?

yes, always

but then I still get those errors like should I put for that one my name again on it? would that be right?
huh?
and for the second one how do I know if my method accepts a paramater?
private void method() <– no parameter
private void method( String string ) <– parameter
Also I am starting my 3rd question now and I need to make an applet of a pie chart but how do I set Jcreator up so it works. Like If I write some code and compile it. It does not work because it can’t display the applet.

So what options do I need to change?
I’m not clear on this question, so I’ll guess.

First, IDEs (such as JCreator) are a dime a dozen, most suck. If you’re serious about Java, use a simple code oriented text editor like jEdit or UltraEdit or whatever crap dc uses (heh heh). Anyway, I hadn’t heard of jCreator until you mentioned it and have no idea how to set it up (yet another reason to not use an IDE: if you speak the common language, anyone can help you, go off on some tangent, no one can help you)

Second, applets are not something that can run on the command line…at least not with java.exe. You can use appletviewer.exe (included with the jdk), or you can imbed an applet into an html page with an
applet tag and use your browser to load the page, which will cause your applet to execute.

that would look something like this:


<!-- myhtml.html -->
<html>
  <applet code='MyApplet.class'  height='200px' width='200px' />
</html>

assuming both myhtml.html and MyApplet.class were in the same folder.

Sun’s applet tag reference

hi chobo2

your code is all fine at my side :agree: , i m on jdk 1.5 and netbeans IDE 4.1

[output]

I 've been programming the seductive Java for a couple of months…i thought this program might be useful for you… take a look



/*
 * TimeApplet.java
 *
 * Created on September 27, 2005, 11:38 AM
 *
*/

/**
 *
 * @author hussain
 */
import javax.swing.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TimeApplet extends JApplet implements Runnable {

    Thread datetimeThread;

    Date date;

    GregorianCalendar calendar;

    String strDate,strTime,strStatus,strTimesecs;

    public void init()
    {
        datetimeThread=new Thread(this);
        datetimeThread.start();
    }
    public void run()
    {
        while(datetimeThread !=null)
        {
            display();
            try{
                datetimeThread.sleep(1000);
            }
            catch(InterruptedException e)
            {
                getAppletContext().showStatus("Thread Interrupted");
            }
        }
    }
    public void display()
    {
        date=new Date();
        calendar=new GregorianCalendar();
        calendar.setTime(date);

        strTime=calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
        strTimesecs=String.valueOf(calendar.get(Calendar.HOUR)*60*60+calendar.get(Calendar.MINUTE)*60+calendar.get(Calendar.SECOND));
        strDate=(calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR);

        strStatus=strTime+"    "+strDate+" Total time in seconds   "+strTimesecs;
        getAppletContext().showStatus(strStatus);
    }

}




[output]

How is Netbeans? Is it any good? I tried it for like a couple seconds and found it very heavey and not really sure where to even begin. Might try it again though. I hear they got plugins what validate ur code.

its damn seriously cool!
it ll broaden your programming capabilities

I will have to look into it. I was trying it now for a couple seconds and I don’t even know what to do. Like I opened up some of my project files and I can’t even get them to compile(everything is grayed out).

I also don’t know how to like remove bookmarks and stuff when I put one and want to get rid of it.

I was also opened a web app project and it came with all this crap in so I probably would like to get that removed(also too a min or 2 load up the web app project file).

I will have to read through it all I guess and see but right now I don’t have time. I guess since there is so much going on in it I going to have to the read the tutorials.

Since of course there would be a couple things I would like to change. Like I would like to have tabs be spaces and when you do a open bracket it puts the closing brack at the same time in(I just find that useful).