Getting started in JAVA Chapter II (Facing objects)

Source: Internet
Author: User
Tags stub

This slag pigeon has one months to finally have time to update. Because there is a C + + foundation, this chapter is still relatively simple, this chapter I think is the process of the ape change course, understand the object-oriented thinking, master the basic principles of object-oriented and Java object-oriented programming of fundamental implementation principles, skilled use of encapsulation, inheritance, multi-state object-oriented three characteristics, feelings and C + + differences between, Here is the learning route.

1. Classes and objects

1-1 What are classes and objects

1-2 How to define a class in Java

1-3 How to use objects in Java

1-4 member variables and local variables in Java

1-5 Construction Methods in Java

1-6 static variables used in Java

1-7 static method for use of static in Java

1-8 static initialization for static use in Java

2. Encapsulation

2-1 what is encapsulation in Java

2-2 using packages to manage classes in Java

2-3 access Modifiers in Java

2-4 the This keyword in Java

2-5 what is an internal class in Java

3. Inheritance

3-1 Inheritance in Java

3-2 method overrides in Java

3-3 inheritance Initialization order in Java

3-4 use of final in Java

3-5 the use of super in Java

3-6 the Object class in Java

4. polymorphic

4-1 Polymorphism in Java

4-2 conversion of reference types in polymorphic

4-3 Abstract classes in Java

4-4 Interfaces in Java

4-5 UML

With a comparative comprehensive exercise, write a console version of the car rental system.

1. Display of all available rental vehicles

2. Select vehicle, number of car rental, rental car days

3. Show car rental list, including total amount, total cargo capacity, and total price

Finally, I enclose my own source code

 PackageIts car rental system;ImportJava.util.Scanner;ImportJavax.print.DocFlavor.INPUT_STREAM; Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner input=NewScanner (system.in); Smallcar C1=NewSmallcar ();intsmallcarmoney[]=New int[10];intsmallcarpeople[]=New int[10]; Bus C2=NewBus ();intbusmoney[]=New int[10];intbuspeople[]=New int[10]; Pika C3=NewPika ();intpikamoney[]=New int[10];intpikapeople[]=New int[10];intpikagoods[]=New int[10]; Lorry C4=Newlorry ();intlorrymoney[]=New int[10];intlorrygoods[]=New int[10]; System.out.println ("Welcome to the car rental system"); System.out.println ("1. Car rental 2. Exit");intnum1=input.nextint ();Switch(NUM1) { Case1: System.out.println ("Car type:"); System.out.println ("1. Trolley passenger: 4 people pull 0kg Rent: 100 Yuan/day"); System.out.println ("2. Bus passenger: 30 people pull 0kg Rent: 500 Yuan/day"); System.out.println ("3. Pickup passengers: 4 people pull 200kg Rent: 200 Yuan/day"); System.out.println ("4. Truck passengers: 0 people pull 2000kg Rent: 500 Yuan/day"); System.out.println ("Please enter a total of a few cars to rent"); intNum2=Input.nextint ();  for(inti=1;i<=num2;i++) {System.out.println ("Please select" +i+ "Car Model"); intA=Input.nextint (); Switch(a) { Case1: Smallcarpeople[i]=c1.fun2 (); Smallcarmoney[i]=C1.money ();  Break; Case2: Buspeople[i]=c2.fun2 (); Busmoney[i]=C2.money ();  Break; Case3: Pikapeople[i]=c3.fun2 (); Pikamoney[i]=C3.money (); Pikagoods[i]=c3.func1 ();  Break; Case4: Lorrymoney[i]=C4.money (); Lorrygoods[i]=c4.func1 ();  Break; default: System.out.println ("Choice of car does not exist");  Break; }    }    intMoney1=0;  for(intA:smallcarmoney) {Money1+=A; }   intMoney2=0;  for(intB:busmoney) {Money2+=b; }   intMoney3=0;  for(intC:lorrymoney) {Money3+=C; }   intMoney4=0;  for(intD:pikamoney) {Money4+=D; }   intmoney=money1+money2+money3+Money4; intPeople1=0;  for(inte:buspeople) {People1+=e; }     intPeople2=0;  for(intf:smallcarpeople) {People2+=F; }         intPeople3=0;  for(intg:pikapeople) {People3+=G; }            intpeople=people1+people2+People3; intGoods1=0;  for(inth:lorrygoods) {GOODS1+=h; }            intGoods2=0;  for(intj:pikagoods) {GOODS2+=J; }intgood=goods1+Goods2; System.out.println ("+people+" and "personal"); System.out.println ("+good+" "kg cargo"); System.out.println ("Please enter the number of rental days"); intdays=Input.nextint (); System.out.println ("Total cost" +days*money+ "Round");  Break; Case2: System.out.println ("Welcome to the next visit");  Break;default: System.out.println ("Input Error");  Break;} }}
 Package Its car rental system;  Public Abstract class Car {publicabstractint money ();}
 Package Its car rental system;  Public Interface carrygoods {int  func1 ();}
 Package Its car rental system;  Public Interface carrypeople {int  fun2 ();}
 PackageIts car rental system; Public classSmallcarextendsCarImplementscarrypeople{ Public intMoney () {//TODO auto-generated Method Stub    return100;} @Override Public intfun2 () {//TODO auto-generated Method Stub        return4; }}
 PackageIts car rental system; Public classLorryextendsCarImplementsCarrygoods { Public intMoney () {//TODO auto-generated Method Stub    return500;} @Override Public intfunc1 () {//TODO auto-generated Method Stub        return2000; }}
 PackageIts car rental system; Public classPikaextendsCarImplementsCarrygoods,carrypeople { Public intMoney () {//TODO auto-generated Method Stub    return200;} @Override Public intfun2 () {//TODO auto-generated Method Stub        return4 ; } @Override Public intfunc1 () {//TODO auto-generated Method Stub        return200; }}
 PackageIts car rental system; Public classBusextendsCarImplementscarrypeople{ Public intMoney () {//TODO auto-generated Method Stub        return500; } @Override Public intfun2 () {//TODO auto-generated Method Stub    return30;}}

Attached run

Getting started in JAVA Chapter II (Facing objects)

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.