Hey guys, I'm working on an assignment which requires me to have one of my constructors to have parameters though I get an error in "Car c2 = new Car();" that it cannot find symbol.

Also I am required to enter the necessary get and set methods to be able to access and change the contents of the car object fields accordingly. Where do I place the get/set methods and what are they?


class Car {
	String regnum;
	String make;
	String model;
	int price;

	Car(String rn, String ma, String mo, int p){
		regnum = rn;
		make = ma;
		model = mo;
		price = p;
class Garage{
	public static void main (String args[]){
		Car c1 = new Car("ABC123", "BMW", "Saloon",15000);
		Car c2 = new Car();

		c2.regnum = "DEF123";
		c2.make = "Honda";
		c2.model = "Hatchback";
		c2.price = 8000;

		System.out.println("Registration Number: " +c1.regnum);
		System.out.println("Make: " +c1.make);
		System.out.println("Model: " +c1.model);
		System.out.println("Price: " +c1.price);
		System.out.println("Registration Number: " +c2.regnum);
		System.out.println("Make: " +c2.make);
		System.out.println("Model: " +c2.model);
		System.out.println("Price: " +c2.price);

6 Years
Discussion Span
Last Post by kramerd

If your class doesn't have any constructors, then Java will supply a default one with no parameters. But as soon as you supply a constructor, Java no longer supplies any. So if you want to be able to construct a Car with no parameters, you have to supply a constructor that takes no arguments (in addition to the one that takes 4 arguments).

Get and set methods are methods that allow the caller to get and set values for member variables. Methods in general can go anywhere inside the class, but not inside any other method. The naming convention is to use the word get (or set) followed by the name of your member variable, starting with a capital letter. So for your variable called regnum, you would have a method called getRegnum which is type String, takes no parameters, and returns the value stored in the regnum variable, and a method called setRegnum which is type void, takes a String parameter, and uses that input parameter to set the value of the regnum variable.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.