Code Cloud Address: Https://gitee.com/lixin-123/16012014_li_xin_hun_two
Defines a student class for students, containing class member variables: string name, string sex, int age, and all variables must be private.
1. Write the parameter constructor:
Can assign values to name,sex,age.
2. Overwrite the ToString function:
By format: class name [Name=, sex=, age=] output. Automatically generated using idea, and then modified to the output format
In the 3.main method
Enter 1 lines name age sex, call the above parameter constructor to create a new object.
Input Sample:
Tom Male
Sample output:
Student [name= ' Tom ', sex= ' male ', age=15]
ImportJava.util.Scanner;classstudent{PrivateString name; PrivateString sex; Private intAge ; PublicStudent () { This. Name = "SSS"; This. Sex = "male"; This. Age = 20; } Public voidToString (String N,intA, String s) { This. Name =N; This. Sex =s; This. Age =A; System.out.println ("Student [name= '" + This. name+ "', sex= '" + This. sex+ "', age=" + This. age+ "]"); }} Public classtest{ Public Static voidMain (string[] args) {Scanner reader=NewScanner (system.in); String N=Reader.next (); intA =Reader.nextint (); String s=Reader.next (); Student TT=NewStudent (); Tt.tostring (n,a,s); }}
Program Design ideas: Define the Student class, define the constructor method and the parameter constructor, and finally call the subclass.
Knowledge points used: Define a class, subclass and inheritance, call function
Operation Result:
7-2 Define class Please add the following code to complete the output requirements.
1ImportJava.util.Scanner;2 Public classMain {3 Public Static voidMain (string[] args) {4 Scanner in =NewScanner (system.in);5inta,b,c,d,e;6 A =in.nextint ();7 B =in.nextint ();8 C =in.nextint ();9 d =in.nextint ();Ten E =in.nextint ();RR rr =NewRR ();12DoubleDD =Rr.fun (a,b,c,d,e);System.out.printf ("%.2f", DD);14 }15 }16classrr{17 18 19}
Post-Supplemental Code:
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); inta,b,c,d,e; A=In.nextint (); b=In.nextint (); C=In.nextint (); D=In.nextint (); E=In.nextint (); RR RR=NewRR (); DoubleDD =Rr.fun (a,b,c,d,e); System.out.printf ("%.2f", DD); } } classrr{ Public DoubleFunintAintBintCintDintEDoublei) {return(a+b+c+d+e)/5; } }
Program Design ideas: By analyzing the code given, the problem occurs in defining the class, defining the Class RR, and then returning the average of five numbers
Knowledge points used: Defining classes, returning return values
Operation Result:
7-3 Horizontal Flat Vertical
Procedural blanks. Refine the code below as required by the topic. Please submit the full code. If the height of a block is larger than the width, we say it is upright, otherwise we say it is flat. Read into the height and width of a block of wood. If it is flat, output a, otherwise output b
ImportJava.util.Scanner; Public classhorizontalandvertical { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intheight, width; Charstatus; Height=In.nextint (); Width=In.nextint (); Board Board=NewBoard (height, width); Status=Board.getstatus (); System.out.print (status); }}classBoard {intheight, width; PublicBoard (intHeightintwidth) { This. Height =height; This. width =width; } Public CharGetStatus () {if(Height <=width) { returnStatus (1); } Else { returnStatus (1.0); } } Public CharStatusDoubleRate ) {returnB; } Public CharStatusintRate ) { returnA; }}
Program Design ideas: Because this problem is the program fill in the blanks, the title know, height than the width of a large vertical put, otherwise flat, if the flat output a otherwise output B, so return has returned a value of a, a
Knowledge points Applied: Method overloading, Parameter passing value
Operation Result:
7-4 procedure to change the wrong question
1 Public classMain {2 Public Static voidMain (string[] args) {3 Animal Animal =NewDog ();4animal.shout ();5Animal.run ();6 } 7 } 8 9classAnimal {10voidshout () {System.out.println ("Animal shout! ");12 }13 }14 15classDogextendsAnimal {16voidshout () {17Super. Shout ();System.out.println ("Wangwang ...");19 }20 21voidrun () {System.out.println ("Dog is running");23 }24}
Program Design ideas: Define dog as a subclass of animal (Eclipse is used)
Knowledge points used: Define classes, subclass inheritance,
Operation Result:
Learning content |
Code (line) |
Blog (word) |
Classes and object subclasses and inheritance |
500 |
500 |
|
|
|
The second process of assessment