Car help

Things to remember:

Methods have the variables they are given as parameters (inside the parentheses in the header) and they have the variables that are at the top of their class (not inside any other method, but inside their class - instance variables).

Variables inside the class but not inside a method (instance variables) have the name this.variablename. You can think of them as you would any other variable, but the full name of the variable happens to have "this." in front of it.

If your class does not have variables inside the class but not inside a method (instance variables), you cannot use "this.". So, your car class can use "this." and your work with car class cannot use "this.".

-----

Steps:

I. Start by creating the class that will be a blueprint for all cars. Create the Car class.

II. Then, you need to define all the variables that make up a car. I have told you that the car information you need to keep track of is : owner, comany, model and year. You need to create all these inside the class, but before you start any methods. Place them properly (meaning inside class, outside methods), but create them as you would create any other variables you have done in the past. These variables can have the word private before them.

III. Then, you need to make the methods printIt and sellIt.

IV. To make printIt, you will create a method like you usually do: public .... printIt(). Then, consider the following for the method's header:

1) You need to decide whether the method header needs the word static after public. If you need the Car's variables, you cannot put static. (The correct answer is to not use static, because you do need the Car's variables so you can print them.)

2) You also need to decide whether the method returns anything, and then put void, int, double, String, Car, etc depending upon what is returned. (The correct answer is that you do not need to return anything. You are just printing.)
I am going to redo hint 3 because it was an important concept. If you followed the steps 1&2, you will be ready for a good explanation of step 3:

3) Figure out what variables the method has that you can use.

a. Under the method header, write a comment line for each variable in the parameters and put a sample value next to it. For example, if you have (int anumber, String aword) as parameters, you would write:
// anumber int 333
// aword string "kris"

b. Under those comments, write a comment line for each instance variable, and include "this." before the variable name. Find these instance variables right under the class open bracket and before the first method header. For example, if you a class that starts out :
public class MyClass
{
int aclassNumber;
String aclassWord;
public MyClass()
you would write:
// this.aclassNumber int 444
// this.aclassWord string "mary"

You can now see exactly which variables you have to code your method, and you have their names listed. They can all be treated as normal variables and you can think of them with the example data inside as you code the method. Later, you will need to test with other data, not just the sample data.

4) Then, you need to decide whether you need any more information, other than the variables you already wrote down. If your method cannot do its job with just those variables, you will need to add another parameter inside the parentheses to take in another value. If you do, add them into the () in the method header. (For example, if you needed a new model, you would put String newModel inside the method's ().) (For the printIt method, you will not need anything but the variables in the car because you can do the job of printing the car information if all you have is the car's variables.)

V. To make sellIt, follow the same procedure as for creating printIt above. The answers to some of the questions I tell you to ask yourself will be different for sellIt. SellIt's job is to take in a new owner and move that new owner to the car's owner.

VI. Then, you need to create a constructor. I recommend seeing how a student is constructed and follow that pattern.

------

Now you are ready to create your workWithCars Class. Create a class WorkWithCars and then create a main method as you are used to doing.

  1. Then create a scanner and asking for all the information you need to create one car, just as you have been used to asking users for information in the past.
  2. Then, use the car's constructor method to create a new car.
  3. Repeat that whole process for another car.
  4. Print both cars.
  5. Ask for a new owner name and print the car and then update the car's owner and print it again.
  6. Repeat that all for the second car.

Please be sure to compile and test at each step.