-3

I have a project for my java programming course.

The instructions are that we have to create a simple class and a tester class, and the class must include a Default constructor; Parameterized constructor with three parameters (make, model and price); Accessor method called getMake( ) to return the make; Accessor method called getModel( ) to return the model; Accessor method called getPrice( ) to return the price; Mutator method setMake( String newMake) to set the make; Mutator method setModel( String newModel) to set the model; and a Mutator method setPrice( double newPrice ) to set the price..

I have created my class and tester program, and my class compiles perfectly. When I try to run it, though get the error that there is no main method. Now, I followed my professor's example for the tester program and I get several errors on that. If anyone could give me the a pointer in the right direction, I would appreciate it.

My question is this: How do I implement my tester program? Do I need to create a zip file? I've tried doing so and didn't seem to help much...

The following is my code for the class:

public class Automobile
{ 
    private String make;    
    private String model;
    private double price;   

    public Automobile()
    {
        make = "Lexus2017";
        model = "RX";
    }

    public Automobile(String initMake, String initModel, double initPrice)
    {
        make = initMake;
        model = initModel;
        price = initPrice; 
    }

    public String getMake()
    {
        return make;
    }

    public String getModel()
    {
        return model;
    }

    public double getPrice()
    {
        return price;
    }   

    public void setMake(String newMake)
    {
        make = newMake;
    }

    public void setModel(String newModel)
    {
        model = newModel;
    }

Also, the following is my tester class(the one that has a lot of errors):

public class AutomobileTester
{
    public static void main(String[] args)
    {
        Automobile make = new Automobile("Lexus 2017");
        System.out.println("The car is " + make.getMake());

        Automobile model = new Automobile("RX");
        System.out.println("The car is " + Automobile.getModel());

        Automobile price = new Automobile("43020"); 
        System.out.println("The car is " + Automobile.getPrice());  

        // Use the mutator to change the make variable
        Automobile.setMake("Lexus 2017");
        System.out.println("The car is " + backDoor.getState());

        // Use the mutator to change the model variable
        Automobile.setModel("RX");
        System.out.println("The car is called " + backDoor.getName());

        Automobile.setPrice("43020");
        System.out.println("The car is " + price.getPrice());
    }
}

This is my first time working with constructors, and I'm very new to Java, so I'm sorry for any obvious errors. Thank you ahead of time for your time and help.

vandench
  • 1,973
  • 3
  • 19
  • 28
J. Nav
  • 55
  • 2
  • 9
  • 1
    `Parameterized constructor with three parameters (make, model and price);` does not match `new Automobile("Lexus 2017");` – Scary Wombat Oct 24 '16 at 00:34
  • Don't use the class name to access the methods... You have a lot of errors here. Are you expecting us to fix them all and do your assignment for you? – OneCricketeer Oct 24 '16 at 00:47
  • @cricket_007 No. That would be ridiculous and I wouldn't learn anything that way. I just want to understand how to implement a class because I've read through my chapter three times, listened to the lecture notes, and something isn't clicking for me. I just wanted to put in my code for context only. – J. Nav Oct 24 '16 at 00:53
  • Alright. I've answered with the immediate problems I see. The Automobile class looks okay – OneCricketeer Oct 24 '16 at 00:54
  • @ScaryWombat So would I fix it by getting rid of the car year? – J. Nav Oct 24 '16 at 00:57

3 Answers3

1

One of the first problems is that you do not use the proper number of parameters for your calls to the constructor, in Java (and most programming languages) you have to supply all of the required parameters to a method/function/constructor in one call. The fix for your code would be to use:

Automobile car = new Automobile("Lexus 2017", "RX", 43020.0D);

Also when you print out the cars information you first use an instance call then you use a static call, I won't go to much into the difference between the two but basically an instance call requires you to instantiate an object while a static does not. The fix for this problem would be to do:

System.out.println("The car is a " + car.getMake() + ", the brand is " + car.getModel() + ", the price is $" + car.getPrice());

As for changing the variables you should be using:

car.setMake("My New Car Make");

instead of:

Automobile.setMake("My New Car Make");

For the difference between static and instance you can look here, here, and here.

Community
  • 1
  • 1
vandench
  • 1,973
  • 3
  • 19
  • 28
  • When you have 'Automobile car = new Automobile("Lexus 2017", "RX", 43020.0D);', does the D after the 43020 recognize the price as a double variable? Also, my assignment says to use instance variables, not static. That doesn't come till the next chapter. – J. Nav Oct 24 '16 at 00:55
  • What about the class names? Are those okay? Automobile and AutomobileTester? Through those names, does the program know to implement the class? – J. Nav Oct 24 '16 at 01:03
  • The `d` or `D` character signifies that that number is of type `double` you can find more about these types of identifiers [here](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). As for your use of static vs instance variables I was pointing out that you were using static calls where you should be using instance calls. As for names, you can really use whatever you want, as long as they are typed exactly the same it should be able to properly instantiate the class, an [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) can help with that. – vandench Oct 24 '16 at 01:21
  • Thank you for taking the time to help. I really appreciate it. – J. Nav Oct 24 '16 at 01:28
0

You did this correctly. You accessed the method by using the make instance variable of an Automobile class.

(side note: make is a bad name for an automobile instance, rather call it car1, or something)

Automobile make = new Automobile("Lexus 2017");
System.out.println("The car is " + make.getMake());

Now, everywhere else that you use Automobile.someMethod(), that's not right, because you need to set or get the data on one instance of the class, not the entire class.

Then, finally, you need to test the constructor with three parameters that you have in that class.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you. "make" is the instance variable my professor wants us to use, but I do have to create two automobile objects so would car1, car2, work? Would I use something like `setMake(String newMake)` ? And do that for every instance variable? – J. Nav Oct 24 '16 at 01:01
  • I'm not talking about `String make`, I'm talking about `Automatible make = new Automobile(...)`, because that's an Automobile which has a make, yes, but not a make itself – OneCricketeer Oct 24 '16 at 04:52
0

You have an error in the constructor call. Your constructor takes three parameters (make, model and price) but when you call the method only send one. That is an error. By default, the Java class constructor takes no parameters (in your case, this would be "new Automobile ()"). To implement the tester you have two options. First, create the car using the constructor without parameters and then set the parameters:

Automobile auto = new Automobile();
auto.setMake("Lexus 2017");
auto.setModel("RX");
auto.setPrice(43020);

Automobile Automobile make = new Automobile ();

Another option is to use your own builder and pass parameters:

Automobile auto2 = new Automobile("Lexus 2017", "RX", 43020);

Automobile.java:

public class Automobile {
private String make;
private String model;
private double price;

public Automobile() {
}

public Automobile(String make, String model, double price) {
    this.make = make;
    this.model = model;
    this.price = price;
}

public String getMake() {
    return make;
}

public void setMake(String make) {
    this.make = make;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}
}

AutomobileTester.java:

public class AutomobileTester {

public static void main(String[] args) {

    Automobile auto = new Automobile();
    auto.setMake("Lexus 2017");
    auto.setModel("RX");
    auto.setPrice(43020);
    System.out.println("The car1 is " + auto.getMake() + " " + auto.getModel() + " " + auto.getPrice());

    Automobile auto2 = new Automobile("Lexus 2017", "RX", 43020);
    System.out.println("The car2 is " + auto2.getMake() + " " + auto2.getModel() + " " + auto2.getPrice());

}

}

Juan
  • 1
  • 1
  • Wouldn't automobile automobile make = new(); be redundant? Also, I know that you use this parameters when you want to have parameters in a method equal the instance variables. When wouldn't you want to use them? – J. Nav Oct 24 '16 at 01:12
  • Yeah, that's a mistake. It would be Automobile auto = new Automobile(); If you only want to use a number of parameters you can implement multiple constructors. For example, if you only want to use the price: public Automobile(double price) { this.price = price; } And the call would be Automobile auto = new Automobile(1000); Or if you want to use the price and model: public Automobile(String model, double price) { this.model = model; this.price = price; } And the call would be: Automobile auto = new Automobile("Rx", 1000); – Juan Oct 24 '16 at 20:09