Java Object-oriented exercises 9

Source: Internet
Author: User

Write Java applications as required.
(1) Create a class called a motor vehicle:
Property: Car Grade (String), speed (int), load capacity (double)
Function: Speed up (auto increase), deceleration (speed reduction), modify the license plate number, check the load capacity of the vehicle.
Write two construction methods: a non-formal parameter, in the method of the license plate number Set "XX1234", speed
is set to 100, the load weight is set to 100, and the other can assign values to all properties of the object;
(2) Create the main class:
Create two motor vehicle objects in the main class.
When you create the first one, call the parameterless constructor, calling the Member method to make its license plate "Liao
A9752 "and let it accelerate.
Create the second time call the constructor method of the parameter, so that its license plate is "Liao B5086", the speed of 150,
Load is 200 and let it slow down.
Output of two vehicles all information

 Public classJidongche {String Chepai;  PublicString Getchepai () {returnChepai; }     Public voidSetchepai (String chepai) { This. Chepai =Chepai; }    intChesu; DoubleZaizhong;  Public voidJiasu (inta) {Chesu+=A; System.out.println ("Accelerate" +a+ "after speed changes" +Chesu); }              Public voidJiansu (intb) {Chesu-=b; if(chesu>=0) {System.out.println ("Deceleration" +b+ "after speed changed to" +Chesu); }                Else{System.out.println ("Deceleration" +b+ "speed changed to 0"); }} jidongche () {Chepai= "XX1234"; Chesu=100; Zaizhong=100; } jidongche (String A,intBDoublec) {Chepai=A; Chesu=b; Zaizhong=C; }      Public Static voidMain (string[] args) {Jidongche J1=NewJidongche (); J1.setchepai ("Liao A9752");        System.out.println (J1.chepai); System.out.println ("The second car's license plate is" +j1.chepai+ "Speed is" +j1.chesu+ "load capacity is" +J1.zaizhong); J1.jiasu (30); Jidongche J2=NewJidongche ("Liao B5086", 150,200); System.out.println ("The second car's license plate is" +j2.chepai+ "Speed is" +j2.chesu+ "load capacity is" +J2.zaizhong); J2.jiansu (30); }}

Java Object-oriented exercises 9

Related Article

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.