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.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
Post what you have and we'll take a look at it
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.
My class is:public class Car { private int yearModel; private String make; private int speed; public Car(int model, String m) { yearModel = model; make = m; speed = 0; } public int getYearModel() { return yearModel; } public String getMake() { return make; } public int getSpeed() { return speed; } public void accelerate() { speed = speed + 5; } public void brake() { speed -= 5; } }
and the driver is:
import javax.swing.JOptionPane; public class CarDemo { public static void main(String[] args) { Car c = new Car(1992, "Mustang"); int s = 0; for(int i = 0; i < 5; i++) { c.accelerate(); s = c.getSpeed(); System.out.println("The " + c.getYearModel() + " " + c.getMake() + "\n is going " + s); } for(int i = 0; i < 5; i++) { s = c.getSpeed(); System.out.println("The speed is now " + s); c.brake(); } } }
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.
/* * 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
something like this:
for (int i = 0; i < 5; i++) { c.brake(); s = c.getSpeed(); System.out.println("The speed is now " + s); }
All in all, nice work Just remember to comment. Also, as a last note, more commenting does not always mean better commenting.
ex.: bad
// times vector.x by 5 vector.x = vector.x * 5; // times vetor.y by 5 vector.y = vector.y * 5;
ex.: better
// scale vector by 5 vector.x = vector.x * 5; vector.y = vector.y * 5;
This is especially apparent when you have loops or a recursive algorithm.
alright thanks, I use JGrasp, where there is no help but the errors the compiler gives, haha.
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.