Define a human being, give this class a way to get a birthday from an ID card, enter your ID, get your birth date.
//Main Method PackageCom.hanqi.maya.model;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {//main Method Main method main functionPerson per=NewPerson ();//Instantiate a personPer.name= "Bom"; System.out.println ("Please enter your ID to get your birthday"); Scanner Scanner=NewScanner (system.in); String Str=Scanner.nextline (); Per.showbir (str); }}
//class Method PackageCom.hanqi.maya.model;Importjava.util.Date;ImportJava.util.Scanner;ImportJava.util.Scanner;//define a class Public classPerson {//define member variables that are properties PublicString name; PublicString sex; Public intAge ; PublicDate Bir; PublicString SFZ; //Defining class Methods Publicstring Showbir (String sfz) {System.out.println ("The user's birthday is:" +sfz.substring (6,14)); returnSFZ; }}
Attention:
Local variables must be assigned initial values when used
class member variable Java automatically assigns an int type initial value 0 Bollean type initial value false string initializer null date type initial value null
Write a simple calculator
//Main Method PackageCom.hanqi.maya.model;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {//main Method Main method main functionSystem.out.println ("Please enter a 1th number"); Scanner Scanner=NewScanner (system.in); String Str=Scanner.nextline (); DoubleA=double.parsedouble (str); System.out.println ("Please enter operator"); Scanner Scanner2=NewScanner (system.in); String str2=Scanner.nextline (); System.out.println ("Please enter a 2nd number"); Scanner Scanner1=NewScanner (system.in); String str1=Scanner1.nextline (); Doubleb=double.parsedouble (STR1); Calc sum=NewCalc (); Sum.jisuan (A, B, str2); }}
PackageCom.hanqi.maya.model;ImportJava.util.Scanner;//Computer class Public classCalc {//+-*/Simple Operation method Public DoubleJisuan (DoubleADoubleb,string F) { if(F.equals ("+") {System.out.println ("A" +f+ "B" + "=" + (A +b)); returnA +b; }Else if(F.equals ("-") {System.out.println ("A" +f+ "B" + "=" + (A-b)); returnA-b; }Else if(F.equals ("/") {System.out.println ("A" +f+ "B" + "=" + (A/b)); returnA/C; }Else if(F.equals ("*") {System.out.println ("A" +f+ "B" + "=" + (A *b)); returnA *C; } return0; }}
Define a car class, define a method for displaying vehicle information, a method for calculating the remaining amount of oil based on fuel consumption and mileage
// Main Method Package Com.hanqi.maya.model; Import Java.util.Scanner; Public class Main { publicstaticvoid main (string[] args) {// Main method Main method main function Car car=new car ("Ao t", "AA", 15,10,30); car.show (); Car.run (+); Car.show (); }}
PackageCom.hanqi.maya.model; Public classCar { PublicString PINP; PublicString Chex; Public DoublePrice ; Public intYH; Public DoubleLC; Public intYXRJ; Public intSyyl; PublicCar () {//Defining Constructors } PublicCar (String pinp1,string chex1,DoublePrice1,intYH1,intyxrj1) {PINP=PINP1; Chex=chex1; Price=Price1; YH=yh1; LC=0; YXRJ=yxrj1; Syyl=yxrj1; } //ways to display information Public voidShow () {System.out.println ("Brand:" +PINP); System.out.println ("Model:" +Chex); System.out.println ("Price:" +Price ); System.out.println ("Fuel consumption:" +YH); System.out.println ("Mileage" +LC); System.out.println ("Fuel tank Volume:" +YXRJ); System.out.println ("Remaining amount of oil:" +syyl+ "L"); System.out.println (); } //method for calculating residual oil content Public voidRunDoubleGongls) { Doubled= (gongls/100) *YH; Syyl=(int) (syyl-d); System.out.println ("Driving" +d+ "km"); System.out.println (); }}
Java Object-oriented example (i)