Describe a car and garage in Java two things

Source: Internet
Author: User

Requirements: Use Java to describe a car and garage two things, the car has the public properties: Number of wheels, name, color, also
Has the function of running the behavior. Before running to check whether the wheel is less than 4, if less than 4, then to be sent to the garage repair,
After repairing the garage, the number of wheels of the car has to be back up to 4. Then the car went on running.

Garage: With public properties: Name, address, telephone.
Public behavior: Repairing a car.

Class car{

Public properties

String name;//Name

String color;//Color

int num = 4;//java is allowed to define member variables and initialize values.

public void Run () {

if (num < 4) {

System.out.println (name+ "driving");

}

}

}

Garage
Class Carfactory
{
String name;//The name of the repair shop
String address; Address
String Tel; Phone

How to repair a car
A method with parameters
public void repair (car car) {//parameter variable also belongs to local variable

Car.num = 4;
System.out.println ("Car to repair");
}
}

Class Demo8
{
public static void Main (string[] args)
{

Create a car
Car car = new car ();

Car.name = "Mercedes-Benz";

Call the Run method
Car.run ();

After one months of driving, a wheel flew.
Car.num = 3;

Car.run ();

Need a maintenance Production object
Carfactory CF = new Carfactory ()
Cf.name = "Bever repair shop";
cf.address = "Jiangxi Institute of Engineering (Tiangong campus)";
Cf.tel = "10086";

Need to repair the car
Cf.repair (car);

And you can drive.
Car.run ();
}
}

Describe a car and garage in Java two things

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.