File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Car class advice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Car class advice" Watch "Car class advice" New topic
Author

Car class advice

Emmett Daly
Greenhorn

Joined: May 11, 2012
Posts: 4

Hi all,

I'm new to java programming and have been stuck on my 3rd assignment for a week now. is there anyone who can give me advice, not a solution, to this assignment. I'm unsure where to go to next. Thanks in advance.

this is my coding so far..

package Car;
/**Create a class called car.
Declare and initialise two global String constants describing attributes e.g. type, colour etc.
Add a global method to describe a behaviour e.g. accelerating
then create a new program class named "FirstObject" containing the standard main method.
Between the curly brackets of the main method, insert statements to output the value of each car attribute and call its behaviour method.
Save the program as FirstObject.java then compile and run the program to see the output.
*/

public class Car {
/* All member variables are private */
private String make;
private String model;
private String colour;
private String registration;
private boolean running;
private int speed;

/** Constructor to create a registered and coloured car */
public Car(String make, String model, String colour, String registration ) {
this.make = make;
this.model = model;
this.colour = colour;
this.registration = registration;
this.speed = 0;
this.running = false;
}

/** Constructor to create an unregistered car */
public Car(String make, String model, String colour){
this.make = make;
this.model = model;
this.colour = colour;
this.speed = 0;
this.running = false;
}

public boolean registerCar(String registration){
/*TODO: validate the registration before assigning */
this.registration = registration;
return true;
}

public boolean startIgnition(){
if (this.running = true)
return false; // return false to indicate the engine couldn't be started when running
else
this.running = true;
return true;
}

public boolean stopIgnition(){
if (this.running){
this.running = false; // return a false to indicate the engine couldn't be stopped when not running
return true;
} else {
return false;
}
}

public void accelerate(int speed){
if (this.running){
this.speed += speed;
} else {
System.out.println("Start the engine lad!");
}
}

public void decelerate(int speed){
if (speed > 0 ){
this.speed -= speed;
if (this.speed < 0)
this.speed = 0;
} else {
this.speed = 0;
System.out.println("We're not moving!!");
}

}

public static void main(String args[]){ //TODO: create car ignition method , to prevent error from car running and not being started.
Car myCar = new Car("Mazda" , "6" , "Green", "xxxxx");
;
myCar.accelerate(6 );
}
}

Im unsure how to pass the ignition behaviour to a specific car here.

thanks
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Use code tag please.


Tell the difficulties that i am difficult.
Emmett Daly
Greenhorn

Joined: May 11, 2012
Posts: 4

Sorry about that ...

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Please, always mention the source of the questions you post.
Emmett Daly
Greenhorn

Joined: May 11, 2012
Posts: 4

Java Programming SE6 [N36] a Oracle run course through FAS in Ireland. The source of this is an assignment I have been given for module 3 . I hope this is what you need.

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
You can create some set and get methods for the attributes:


In your FirstObject.java , you may want to do:
public class FirstObject{
public static void main (String[] args){
Car myCar= new Car(); //create a default constructor in Car
myCar.setMake("Mazda");
....
}
}
Emmett Daly
Greenhorn

Joined: May 11, 2012
Posts: 4

ah yes , i was unsure whether to use getter and setters here,

thanks v.much Helen Ma ill try this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Car class advice