7 Replies - 876 Views - Last Post: 27 October 2013 - 04:44 AM Rate Topic: -----

#1 mgiara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Help with calling a constructor from the main method.

Posted 26 October 2013 - 01:53 PM

How do I call the acceptor method (in my main method to make the program run)?
Apologies in advance. I'm very much a beginner.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Admin
 */
import java.util.Scanner;
public class Car {
    
    // Declaration of variables.
    private int yearModel;
    private String make;
    private int speed;
    
    
    // Constructor that accepts arguements.
     public static void acceptor(int yearModelIn, String makeIn, int speedIn){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the car's year model: ");
             yearModelIn = keyboard.nextInt();  
        System.out.println("Enter the car's make: ");
             makeIn = keyboard.next();
        System.out.println("Enter the car's speed: ");
             speedIn = keyboard.nextInt();
    }
    
    // Constructor that zeroes fields.
    public void zeroer()
    {
        yearModel = 0;
        speed = 0;
        make = ("");
    }
        
    // Accessor Methods
    public int getYearModel()
    {
        return yearModel;
    }
    public String getMake()
    {
        return make;
    }
    public int getSpeed()
    {
        return speed;
    }
    
    // Mutator methods for storing values in fields.
    public void setValues(int newYearModel, String newMake, int newSpeed)
    {
       yearModel = newYearModel;
       make = newMake;
       speed = newSpeed;
    }
    
    // Accelerate method for adding 5 to speed.
    public void Accelerate()
    {
        speed += 5;        
    }
    
    // Brake method for reducing speed.
    public void Brake()
    {
        speed-=5;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
   
         // TODO code application logic here
    }
}



Here's the project description if it helps:

Project Description: Write a class named Car that has the following fields:

yearModel. The yearModel field is an int that holds the carís year model.
make. The make field references a String object that holds the make of the car.
speed. The speed field is an int that holds the carís current speed.

In addition, the class should have the following constructor and other methods:

Constructors. One constructor should accept the carís year model, make, and speed as arguments. These values should be assigned to the objectís yearModel, make, and speed fields. Another constructor will have no arguments and will assign 0 as the carís year model and speed and an empty string (ďĒ) as the make.

Accessors. Appropriate accessor methods should get the values stored in an objectís yearModel, make, and speed fields.

Mutators. Appropriate mutator methods should store values in an objectís yearModel, make, and speed fields.

accelerate. The accelerate method should add 5 to the speed field each time it is called.

brake. The brake method should subtract 5 from the speed field each time it is called.

Demonstrate the class in a program that asks the user to input data and then creates a Car object. It then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.

The output from running this program will appear similar to:

Enter the carís year model: 1965

Enter the carís make: Mustang
Enter the carís speed: 30
Current status of the car:
Year model: 1965
Make: Mustang
Speed: 30
Accelerating...
Now the speed is 35
Accelerating...
Now the speed is 40
Accelerating...
Now the speed is 45
Accelerating...
Now the speed is 50
Accelerating...
Now the speed is 55
Braking...
Now the speed is 50
Braking...
Now the speed is 45
Braking...
Now the speed is 40
Braking...
Now the speed is 35
Braking...
Now the speed is 30

Is This A Good Question/Topic? 0
  • +

Replies To: Help with calling a constructor from the main method.

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: Help with calling a constructor from the main method.

Posted 26 October 2013 - 01:59 PM

Not a bad start, but there should be no method(s) taking user input in class Car. That should be elsewhere. Also method names, like variables, should begin lower case
Was This Post Helpful? 0
  • +
  • -

#3 mgiara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Re: Help with calling a constructor from the main method.

Posted 26 October 2013 - 02:06 PM

View Postg00se, on 26 October 2013 - 01:59 PM, said:

Not a bad start, but there should be no method(s) taking user input in class Car. That should be elsewhere. Also method names, like variables, should begin lower case


Ah, wonderful.
Thank you!!

So I can remove the main method in my Car class
public static void main(String[] args) {
   
         // TODO code application logic here
    }

And put a call to the methods in my Car class in the main method I'm writing right now.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: Help with calling a constructor from the main method.

Posted 26 October 2013 - 02:18 PM

A main method in your Car class should be regarded as a very quick and dirty method of testing it. You would normally have a driver class (no pun intended) which would contain a main method and that driver class would treat the Car class
Was This Post Helpful? 0
  • +
  • -

#5 mgiara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Re: Help with calling a constructor from the main method.

Posted 26 October 2013 - 02:35 PM

public class Driver {
    public static void main(String[] args){
        
    }
}

So how do I call it?
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: Help with calling a constructor from the main method.

Posted 26 October 2013 - 02:56 PM

Give Driver a start method and you could get the user input in that
Was This Post Helpful? 0
  • +
  • -

#7 mgiara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 13

Re: Help with calling a constructor from the main method.

Posted 26 October 2013 - 03:55 PM

View Postg00se, on 26 October 2013 - 02:56 PM, said:

Give Driver a start method and you could get the user input in that


I believe my instructor wants me to keep everything in the original class as is stated in the instructions.
I don't know how to give Driver a start method; how do I do that?
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: Help with calling a constructor from the main method.

Posted 27 October 2013 - 04:44 AM

Quote

I believe my instructor wants me to keep everything in the original class as is stated in the instructions.

See below

Quote

Demonstrate the class in a program that asks the user to input data and then creates a Car object.

The coincides exactly with what i recommended

public class CarTester {
 // (I didn't intend you to actually call the class 'Driver')
 

  public void start() {
     Car c = new Car();
     // etc
  }
  
  public static void main(String[] args) {
    new CarTester().start();
  }
}


This post has been edited by g00se: 27 October 2013 - 04:45 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1