Question 2: Three errors have occurred while running the following program. Expl
ID: 3876494 • Letter: Q
Question
Question 2: Three errors have occurred while running the following program. Explain why in detail and Correct the errors. class Car4 String color; String gearType; int door; int year; CarType (String c, String g, int door, string year) color -c: gearType g; door - door; year-year;) public class Constructor8 public static void main(Stringl] args) t Car4 cl new Car4 ("white", "auto","4", 2018); System.out..println ("my car "+ c1.color +""c1.gearType + ", " c1.door"" +c1-year) /* explain why in detailExplanation / Answer
class Car4{
String color; String gearType;
int door; int year;
CarType(String c, String g, int door, String year){
color = c; gearType=g;
door = door; year = year;}
}
public class Constructor8{
public static void main(String[] args) {
Car4 c1 = new Car4 ("white","auto","4",2018);
System.out.println(" my car : " + c1.color + " , " + c1.gearType + " , " + c1.door + " " + c1. year);
}
}
/* whlie creating a new car object the values passed to the constructor are not matching the type defined in the Car24 class
1. int door value is sent as "4" which would be considered as a string rather than as a number so while sending the values from the main function "4" should be changed with integer 4
2.year is defined as an integer in the variable defination part and defined as string in the Cartype function of the class and the value sent in integer while creating an object for Car24 class in main function
so the rectified code would be as follows */
class Car4{
String color; String gearType;
int door; int year;
CarType(String c, String g, int door, int year){
color = c; gearType=g;
door = door; year = year;}
}
public class Constructor8{
public static void main(String[] args) {
Car4 c1 = new Car4 ("white","auto",4,2018);
System.out.println(" my car : " + c1.color + " , " + c1.gearType + " , " + c1.door + " " + c1. year);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.