• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Car class advice

 
Emmett Daly
Greenhorn
Posts: 4
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use code tag please.
 
Emmett Daly
Greenhorn
Posts: 4
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that ...

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, always mention the source of the questions you post.
 
Emmett Daly
Greenhorn
Posts: 4
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah yes , i was unsure whether to use getter and setters here,

thanks v.much Helen Ma ill try this
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic