Would it be possible for someone to evaluate an assignment that I completed? I had to write two classes and I want to see what I need to work on and stuff like that. If so, I will post the code after someone says they will.

April 17th, 2010, 12:54 AM

helloworld922

Re: Grading

Post what you have and we'll take a look at it :)

April 17th, 2010, 01:02 AM

BuhRock

Re: Grading

Ok, so the specs are: The challenge is to make a class named car that has 3 fields, yearModel as in int, make as a String, and speed as an int. Then it says to make a Constructor that accepts the cars year model and make as arguments. It says these values should be assigned to the objects year model and make fields. and assign speed to 0. It says to make appropriate accessors. and a method of accelerate that adds 5 to the speed field everytime its called. and a method called brake that declines 5 everytime its called.

It says I need to demonstrate a class that creates a car object and calls the accelerate method five times, after each call display the speed of it, then call brake 5 times and display the speed after each call.

1. Comment, comment, comment. While comments don't add any functionality to your program, commenting allows others (and you) to quickly and easily understand what the code should do. There are three types of comments in java: Block, Line, and Javadoc.

Code :

/*
* This is a block comment. It can span several lines
*
*/
// This is a line comment. It's only this line
/**
* This is a Javadoc comment. It's similar to a block comment, but it's special in that it describes what methods/fields are for
*/

2. Code looks pretty well formatted, there is one line out of place (I'm assuming it's a copy/paste problem). If you have an IDE like Netbeans/Eclipse (sure there are others with this feature, too), you can quickly auto-format your code (on Eclipse the hotkey is CTRL+SHIFT+F, or you can set it to auto-format everytime you save the file)

3. Choose better names for your variables. variables names c and s aren't very descriptive. Something like myCar and speed could help to describe the function of that variable (see the Car class for some good variable naming). There are some exceptions to this rule (ex. i and j as loop indices are fairly common, x, y, z for 2d or 3d math)

4. Your CarDemo class has an extra import (javax.swing.JOptionPane). Generally it's a good idea to not import anything you're not using (of course, "blanket imports" are an exception), but this is a minor issue. Again, some IDE's will can automatically manage which packages/classes to import, and even remove unecessary imports.

5. You may want to split up the first printing command unless you do want the car make and year displayed 5 times. Not really necessary, but it would match the output of how you have the deceleration portion.

6. In your second for-loop where the car is decelerating, you should probably be calling brake() before querying the speed. This way you don't get this kind of output:

(accelerating)
speed is now 5
speed is now 10
speed is now 15
speed is now 20
speed is now 25
(decelerating) (off by 5)
speed is now 25
speed is now 20
speed is now 15
speed is now 10
speed is now 5

This is especially apparent when you have loops or a recursive algorithm.

April 18th, 2010, 07:52 AM

BuhRock

Re: Grading

alright thanks, I use JGrasp, where there is no help but the errors the compiler gives, haha.

April 18th, 2010, 06:05 PM

lbarowski

Re: Grading

jGRASP does come with plugins for Checkstyle and FindBugs, which may help you automatically find style, formatting, and some other bugs. Just install Checkstyle and/or FindBugs and jGRASP will find them the next time you start it.