Problem Statement:
Write a program that uses data encapsulation / data hiding
- Define a class Car with two private variables engineno and modelno with values "EK1" and "VX6".
- Define a public method setcarinfo(engineno, modelno).
- Inside the method set each private variables with the values passed as arguments.
- Define a public method getcarinfo(self) which prints all the information.
- Create an instance of Car and name it hondacity.
- Take enginenumber, modelnumber, color and year as an input from console.
- Call the method setcarinfo() with inputs as parameters.
- Now set the color and year by setting hondacity.color and hondacity.year.
EK67G9
SX6
Blue
2018
Sample Output:
EK67G9 SX6 Blue 2018
Solution in | JAVA | PYTHON |
---|
import java.util.Scanner;
class CarInfo{
private String engineno = "EK1";
private String modelno = "VX6";
public String color;
public int year;
public void setcarinfo(String engineno,String modelno){
this.engineno = engineno;
this.modelno = modelno;
}
public void getcarinfo(){
System.out.println(engineno+" "+modelno+" "+color+" "+year);
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
CarInfo hondacity = new CarInfo();
String engineno = sc.nextLine();
String modelno = sc.nextLine();
String color = sc.nextLine();
int year = sc.nextInt();
hondacity.color = color;
hondacity.year = year;
hondacity.setcarinfo(engineno,modelno);
hondacity.getcarinfo();
}
}
class CarInfo{
private String engineno = "EK1";
private String modelno = "VX6";
public String color;
public int year;
public void setcarinfo(String engineno,String modelno){
this.engineno = engineno;
this.modelno = modelno;
}
public void getcarinfo(){
System.out.println(engineno+" "+modelno+" "+color+" "+year);
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
CarInfo hondacity = new CarInfo();
String engineno = sc.nextLine();
String modelno = sc.nextLine();
String color = sc.nextLine();
int year = sc.nextInt();
hondacity.color = color;
hondacity.year = year;
hondacity.setcarinfo(engineno,modelno);
hondacity.getcarinfo();
}
}