Spaces:
Running
Running
public className(dtype1 param1, ...) {...}
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364In the attached program, the class 'Car' is defined.However, the class lacks a 'constructor'.
Complete the class by writing a constructor to the class, which takes as parameters the values of the attributes in the order in which they are defined in the class.
import java.util.Random;
public class Test{ public static void main(String[] args){ final Random r = new Random(); System.out.println("Testing to create an object..."); Car c1 = new Car("Datsun", "100A", 1976, 0.8); Car c2 = new Car("Nissan", "Skyline R34 GT-R", 1995, 2.5); System.out.println(c1); System.out.println(c2); }}
class Car { String brand; String model; int yearmodel; double engineCapacity;
//ADD CONSTRUCTOR public Car(String brand, String model, int yearmodel, double engineCapacity) { this.brand = brand; this.model = model; this.yearmodel = yearmodel; this.engineCapacity = engineCapacity; }
@Override public String toString() { return "Car [brand=" + brand + ", model=" + model + ", yearmodel=" + yearmodel + ", engineCapacity=" + engineCapacity + "]"; } }
Testing to create an object...Car [brand=Datsun, model=100A, yearmodel=1976, engineCapacity=0.8]Car [brand=Nissan, model=Skyline R34 GT-R, yearmodel=1995, engineCapacity=2.5]
Week 4: Writing classes/06b. Write the constructorAdded+64