We assume car as an object and it has various characteristics defined in variables. There are different methods that show how objects are defined and different methods can be called.
Code :
package car;
public class Car {
int maxSpeed = 100;
int minSpeed = 0;
double weight = 4079;
// float is little smaller than double i.e it takes less space in bits
boolean isTheCarOn = false;
char condition = 'A';
String nameOfCar = "haha";
public void wreckCar(){
condition = 'C';
}
public void printVariables(){
System.out.println("This is the max speed\t"+maxSpeed);
System.out.println("This is the min speed\t"+minSpeed);
System.out.println("This is the\t"+weight);
System.out.println(isTheCarOn);
System.out.println(condition);
System.out.println(nameOfCar);
}
public void upgradeMinSpeed(){
minSpeed = maxSpeed;
maxSpeed = maxSpeed + 1;
}
public static void main(String[] args) {
// TODO code application logic here
Car familyCar = new Car();
System.out.println("family's car");
familyCar.printVariables();
Car aliceCar = familyCar;
familyCar.wreckCar();
System.out.println("Alice's car");
aliceCar.printVariables();
familyCar.upgradeMinSpeed();
familyCar.printVariables();
}
}
No comments:
Post a Comment