Question: PROGRAMING LANGUAGE JAVA Write a Java program that implements the following Car.java class: a- A Car has a name, a max_speed value and an instance
PROGRAMING LANGUAGE JAVA
Write a Java program that implements the following Car.java class:
a- A Car has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Write public methods to set and get the values of these variables.
b- Write a class called CarDemo.java that tests the Car.java class as follows:
1- Creates an instance of the Car class.
2- Calls all the public methods of the car instance and prints the result of its getter methods.
COULD ANYBODY TELL WHAT IS WRONG IN THIS PROGRAM .I AM GETTING HARD TIME TO COMPILE IT .
SOLUTION:-
CarDemo.java
import java.util.Scanner;
public class CarDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the car name: ");
String name = scan.next();
System.out.println("Enter the car max speed: ");
int maxSpeed = scan.nextInt();
System.out.println("Enter the car number of cylinders: ");
int numberOfCylinders = scan.nextInt();
Car c = new Car();
c.setMax_speed(maxSpeed);
c.setName(name);
c.setNumber_of_cylinders(numberOfCylinders);
System.out.println("Car Name: "+c.getName());
System.out.println("Car Max Speed: "+c.getMax_speed());
System.out.println("Car Number of cyliders: "+c.getNumber_of_cylinders());
}
}
Car.java
public class Car {
private String name;
private int max_speed, number_of_cylinders ;
public Car() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMax_speed() {
return max_speed;
}
public void setMax_speed(int maxSpeed) {
max_speed = maxSpeed;
}
public int getNumber_of_cylinders() {
return number_of_cylinders;
}
public void setNumber_of_cylinders(int numberOfCylinders) {
number_of_cylinders = numberOfCylinders;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
