1. Create a triangle class, a member variable three sides, a method to find the perimeter, and create a class master Class A to test it.
Public class sanjiao{ publicdouble z (double A,double B,double c) { return a+b+C; }}
Package publicclass A { publicstaticvoid Main (string[] args) { Sanjiao sj=new Sanjiao (); System.out.println ("perimeter of the triangle =" +sj.z (5, 8)); } }
2. Write Java applications as required.
(1) Create a class called people:
Properties: Name, age, gender, height
Behavior: Speaking, calculating addition, renaming
Write a construction method that can assign values to all attributes;
(2) Create the main class:
Create an object: named "Zhang San", gender "male", age 18 years old, height 1.80;
Let the object invoke the member method:
Say "Hello!" ”
Calculate the value of a 23+45
Change the name to "John Doe"
Packageliu0919; Public classpeople{Private DoubleHeight//Height PrivateString name;//name Private intAge//Age PrivateString sex;//Sex//member Methods Public Doublegetheight () {returnheight; } Public voidSetHeight (Doubleheight) { This. Height =height; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; } Publicstring Xingwei (String xw) {returnXW; } Public intJia (intAintb) {returnA +b; } Publicstring gaiming (string gm) {returnGM; }}
Packageliu0919; Public classCishi_people { Public Static voidMain (string[] args) {people PE=Newpeople (); Pe.setname ("Zhang San"); System.out.println ("Name is:" +pe.getname ()); Pe.setsex (Male); System.out.println ("Gender is:" +pe.getsex ()); Pe.setage (18); System.out.println ("Age is:" +pe.getage ()); Pe.setheight (1.80); System.out.println ("Height is:" +pe.getheight ()); System.out.println ("Say:" +pe.xingwei ("Hello!") ")); System.out.println ("23+45=" +pe. Jia (23, 45)); System.out.println ("Name changed to:" +pe. Gaiming ("John Doe")); } }
3. 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{PrivateString number;//License plate Private intSpeed//Speed Private DoubleLoad//Load PrivateString Xinxi; //no parameter has a return value PublicString GetNumber () {returnNumber ; } Public voidSetnumber (String number) { This. Number =Number ; } Public intgetspeed () {returnSpeed ; } Public voidSetspeed (intSpeed ) { This. Speed =Speed ; } Public Doublegetload () {returnload; } Public voidSetload (Doubleload) { This. Load =load; } Public DoubleAddspeed (intSD) { speed+=SD; returnSpeed ; } Public intDownspeed (intSD) { speed-=SD; returnSpeed ; } //Construction MethodJidongche () { number= "XX1234"; speed=100; Load=100; } jidongche (String number,intSpeedDoubleload) { This. number=Number ; This. speed=Speed ; This. load=load; } //no return value with parameter voidXinxi (String N,intSDoublel) {Xinxi=n+s+M; System.out.println ("Vehicle 2 license plate number is:" +n+ "Speed:" +s+ "Load:" +M); } }
Packageliu0919; Public classCeshi_jidongche { Public Static voidMain (string[] args) {Jidongche JD=NewJidongche (); Jd.setnumber ("XX1234"); System.out.println ("The license plate number is:" +Jd.getnumber ()); Jd.setload (100); System.out.println ("Load is:" +jd.getload ()); Jd.setspeed (100); System.out.println ("Speed is:" +jd.getspeed ()); System.out.println ("Vehicle 1 license plate is:" +jd.getnumber () + "Load:" +jd.getload () + "Speed:" +jd.getspeed ()); //calling a constructor without parametersJd.setnumber ("Liao A9752")); System.out.println ("The modified license plate number is:" +Jd.getnumber ()); System.out.println ("After acceleration:" +jd.addspeed (20)); //with parameters.Jidongche JD2=NewJidongche ("Liao B5086", 150, 200); JD2. Xinxi ("Liao B5086", 150, 200); System.out.println ("After deceleration:" +jd2.downspeed (20)); } }
4. Create a point class with member variable x, Y, Method Getx (), SetX (), and a constructor
Initialization of x and Y. Create the Class master Class A to test it.
Packageliu0919; Public classpoint{//Properties Private Doublex; Private Doubley; Private DoubleD; //Construction MethodPoint (DoubleXDoubley) { This. x=x; This. y=y; This. d=x+y; } //member Methods Public DoubleGetX () {returnx; } Public voidSetX (Doublex) { This. x =x; } Public DoubleGetY () {returny; } Public voidSety (Doubley) { This. y =y; }
}
Package publicclass ceshi_point { publicstatic void Main (string[] args) {point po=new point (5,6); System.out.println ("Original size x=" +po.getx () + "y=" +po.gety ()); Po.sety (8); Po.setx (2); System.out.println ("point size x=" +po.getx () + "y=" +po.gety ()); } }
Java-class and Object basics Exercises