Welcome Guest.

1Answers

Java defines a vehicle class

Asked by: Patricia 240 views IT October 26, 2018

Define a vehicle class with attributes: speed (doulbe type), number of passengers (static, integer), with methods: move, set speed, acceleration, and deceleration. It has a construction method with parameters that can assign values ​​to the number of passengers. Create a test class, create two objects of the vehicle class in its main method main, and set the speed and number of passengers respectively. Call the respective acceleration or deceleration function to modify the speed value and print the speed and passenger values ​​of the two objects separately.

1 Answers

  1. +7Votes  

    public class Transport {

       public double speed;

       public static Integer number;

        public Transport(Integer number) {

           Transport.number = number;

       }

       public void accelerate(double s) {

           speed = speed + s;

       }

       public void dccelerate(double s) {

           speed = speed – s;

       }   

    public void setSpeed(double speed) {

           this.speed = speed;    }

    }

    class Test{

       public static void main(String[] Args) {

           Transport transport1 = new Transport(10);

           transport1.setSpeed(30);

           transport1.accelerate(10);

           System.out.printl n("transport1 speed:"+transport1.speed+", passenger: "+transport1.number);

           Transport transport2 = new Transport(10);

           transport2.setSpeed(4);

           transport2.accelerate(5

           System.out.println("transport1 speed:"+transport2.speed+", passenger: "+transport2.number );;

       }

    }

    Carolyn- October 27, 2018 |