Ok, I know most of you will read this and think I am a liar, but I am really just trying to practice with multiple classes and methods. So I picked out a programming challenge from my book to see if I could get through it. I wrote the class and now I am starting to write the driver for it and I get a compile error when I am writing main that says

CarDemo.java:7: cannot find symbol
symbol : constructor Car()
location: class Car
Car c = new Car();

when I try to make an instance of that class.

The challenge is to make a class named car that has 3 fields, yearModel as in int, make as a String, and speed as an int. Then it says to make a Constructor that accepts the cars year model and make as arguments. It says these values should be assigned to the objects year model and make fields. and assign speed to 0. It says to make appropriate accessors. and a method of accelerate that adds 5 to the speed field everytime its called. and a method called brake that declines 5 everytime its called.

It says I need to demonstrate a class that creates a car object and calls the accelerate method five times, after each call display the speed of it, then call brake 5 times and display the speed after each call. I see a for loop there, but I need to get past this stupid error.

My class's code is:

public class Car
{
	private int yearModel;
	private String make;
	private int speed;
	
	public Car(int model, String m)
	{
		yearModel = model;
		make = m;
		speed = 0;
	}
	
	public int getYearModel()
	{
		return yearModel;
	}
	
	public String getMake()
	{
		return make;
	}
	
	public int getSpeed()
	{
		return speed;
	}
	
	public int accelerate(int s)
	{
		s = s + 5;
		
		return s;
	}
	
	public int brake(int b)
	{
		b = b - 5;
		
		return b;
	}
}

and my driver so far is:

import javax.swing.JOptionPane;

public class CarDemo
{
	public static void main(String[] args)
	{
		Car c = new Car();
		String make;
		String input;
		int model;
		int speed;
		
		input = JOptionPane.showInputDialog("What is the year of your car?");
		model = Integer.parseInt(input);
		
		c.Car(model);
	}
}

Recommended Answers

All 2 Replies

Your constructor takes two parameters and you are calling it with 0. And don't try bringing up th "default constructor" (if you know about that) as that is only created by compiler if the doesn't already have any constructors.

when you don't define any constructor in ur class then jvm will create a default constructor(with no parameter), but when u are creating any other constructor then default constructor will not be available.
so either u create one default constructor
as

public Car(){}

or pass parameters in ur existing constructor as

Car car = new Car(2005, "xyz model");

Ok, I know most of you will read this and think I am a liar, but I am really just trying to practice with multiple classes and methods. So I picked out a programming challenge from my book to see if I could get through it. I wrote the class and now I am starting to write the driver for it and I get a compile error when I am writing main that says

CarDemo.java:7: cannot find symbol
symbol : constructor Car()
location: class Car
Car c = new Car();

when I try to make an instance of that class.

The challenge is to make a class named car that has 3 fields, yearModel as in int, make as a String, and speed as an int. Then it says to make a Constructor that accepts the cars year model and make as arguments. It says these values should be assigned to the objects year model and make fields. and assign speed to 0. It says to make appropriate accessors. and a method of accelerate that adds 5 to the speed field everytime its called. and a method called brake that declines 5 everytime its called.

It says I need to demonstrate a class that creates a car object and calls the accelerate method five times, after each call display the speed of it, then call brake 5 times and display the speed after each call. I see a for loop there, but I need to get past this stupid error.

My class's code is:

public class Car
{
	private int yearModel;
	private String make;
	private int speed;
	
	public Car(int model, String m)
	{
		yearModel = model;
		make = m;
		speed = 0;
	}
	
	public int getYearModel()
	{
		return yearModel;
	}
	
	public String getMake()
	{
		return make;
	}
	
	public int getSpeed()
	{
		return speed;
	}
	
	public int accelerate(int s)
	{
		s = s + 5;
		
		return s;
	}
	
	public int brake(int b)
	{
		b = b - 5;
		
		return b;
	}
}

and my driver so far is:

import javax.swing.JOptionPane;

public class CarDemo
{
	public static void main(String[] args)
	{
		Car c = new Car();
		String make;
		String input;
		int model;
		int speed;
		
		input = JOptionPane.showInputDialog("What is the year of your car?");
		model = Integer.parseInt(input);
		
		c.Car(model);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.