Blog: Http://blog.csdn.net/muyang_ren
Please use encapsulation and construction to complete the following exercises, no special declaration please write attributes to private
Practice One:
1) Create a Student class
2) Create four private fields: Name,sex,age,dire
3) Provide properties for private fields, respectively
4) Test: Create student objects, set properties, and print the student's basic information in the console separately
Package Lhy.java_1day;class Student {private String name= "Lianghui Chung";p rivate char sex= ' man ';p rivate int age=18;private String dir E= "embedded";p ublic String getName () {return name;} Public Char Getsex () {return sex;} public int getage () {return age;} Public String Getdire () {return dire;}} public class Test_1 {public static void main (string[] args) {//TODO auto-generated method stub student lhystudent = new Student (); System.out.println ("Name:" +lhystudent.getname ()); System.out.println ("Sex:" +lhystudent.getsex ()); System.out.println ("Age:" +lhystudent.getage ()); System.out.println ("Dire:" +lhystudent.getdire ());}}
Exercise two:
Define a tank class (tank Class), define X (horizontal), Y (Ordinate), dir (direction: value is up and down), type (Type) 4 fields, the access modifier is public;
Define a construction method for the tank class to implement initialization of 4 fields
Define a ShowData method for the tank class, and the console prints 4 field values
Creates a tank object with a horizontal axis of 10, an ordinate of 50, a right direction, a heavy tank type, and prints tank information by calling the object's ShowData () method
Package Lhy.java_1day;class tank{public int x;public int y;public char dir;public String type;public tank () {//TODO auto-generated Constructor stub this.x = 100;this.y = 200;this.dir = ' down '; this.type = "Heavy Tank";} public void ShowData () {System.out.println ("x =" +this.x); System.out.println ("y =" +this.y); System.out.println ("dir =" +this.dir); System.out.println ("type =" +this.type);}} public class Test_2 {public static void main (string[] args) {Tank tank_01 = new tank (); Tank_01.showdata ();}}
Exercise Three:
1) Create a Computer class: computer
2) Create three fields: Name: Computer name, type: Computer brand, Size: Dimensions
3) Create a constructor to assign an initial value to three fields
4) Add a ShowData method to print each field value
5) Create a computer object, calling its ShowData ();
Package Lhy.java_1day;class computer{private String name;private string type;private Double size;public computer () { THIS.name = "small new"; This.type = "Lenovo"; this.size = 15.6;} public void ShowData () {System.out.println ("name:" +this.name); System.out.println ("type:" +this.type); SYSTEM.OUT.PRINTLN ("Size:" +this.size);}} public class Test_3 {public static void main (string[] args) {Computer lianxiangcomputer = new computer (); Lianxiangcomputer . ShowData ();}}
Exercise Four:
1) Create a dog class: Dog
2) Add a Name field and a sex field to the dog class
3) Provide a property for the name field and the Sex field separately (name: No checksum, sex: Only "male and female")
4) Add ShowData method to dog, print name and gender
5) Add the Yaren method to the dog, implement the canine bite function, Yaoren (string str), if STR is a "man", Output "bark", if Str is a "woman", Output "whoa", if STR is the other output "Wah"
6) Test
Package Lhy.java_1day;class dog{private String name = "Small Black";p rivate char sex = ' public ';p ublic void ShowData () { System.out.println ("Name:" +this.name); System.out.println ("Sex :" +this.sex);} public void Yaoren (String str) {if (str = = "man") {System.out.println ("str =" +str); System.out.println ("barking"); else if (str = = "Woman") {System.out.println ("str =" +str); System.out.println ("Whoa");} else {System.out.println ("str =" +str); System.out.println ("Wah");}}} public class Test_4 {public static void main (string[] args) {Dog dog = new Dog ();d og.showdata ();d og. Yaoren ("man");d og. Yaoren ("Woman");d og. Yaoren ("Child");}}
Exercise Five:
1) Create a teacher class: Teacher
2) Add a field to the teacher: Id,name,sex,age,height
3) Add the corresponding properties for the field, respectively:
Id:int, no calibration
Name:string, longer than one bit less than four bits
Sex:string, only men and women.
Age:int, only between 20-55.
Height:double, only between 1.50-1.80
Define a ShowData method, print number, name, gender, height
4) Create a teacher object, call its showdata ();
Package lhy.java_1day;import java.util.scanner;class teacher{private int id;private string name;private string sex; private int age;private Double height;public void setId (int id) {this.id = ID;} public void SetName (String name) {if (Name.length () >1 | | (Name.length () <4)) {this.name = name;} else {System.out.println ("Error in name length"); System.out.println ("Please Enter: Name"), SetName (New Scanner (system.in). Next ());}} public void Setsex (String sex) {if (Sex.equals ("male") | | sex.equals ("female") {this.sex = sex;} else {System.out.println ("Gender input error (male or female)"); Setsex (new Scanner (system.in). Next ());}} public void Setage (int.) {if (age>20&&age<55) {this.age = age;} else {System.out.println ("Age input error (20~55)"); System.out.println ("Please Enter: Age"); Setage (new Scanner (system.in). Nextint ());}} public void SetHeight (Double height) {if (height>1.5 && height<1.8) {this.height = height;} else {System.out.println ("Height input error (1.5~1.8)"); System.out.println ("Please enter: height"), SetHeight (New Scanner (system.in). nextdouble ());}} Public void ShowData () {System.out.println ("ID:" +this.id); System.out.println ("Name:" +this.name); System.out.println ("Sex:" +this.sex); System.out.println ("Age:" +this.age); System.out.println ("Height:" +this.height);}} public class Test_5 {public static void main (string[] args) {Teacher Teacher = new Teacher (); Scanner stdin = new Scanner (system.in); System.out.println ("Please enter: ID"); Teacher.setid (Stdin.nextint ()); System.out.println ("Please Enter: Name"); Teacher.setname (Stdin.next ()); System.out.println ("Please Enter: Age"); Teacher.setage (Stdin.nextint ()); System.out.println ("Please Enter: Gender"); Teacher.setsex (Stdin.next ()); System.out.println ("Please enter: height"), Teacher.setheight (Stdin.nextdouble ()); Stdin.close (); Teacher.showdata ();}}
Exercise VI:
1) Define a Fruit class: Fruit, with fields in common: Name,color,weidao
2) Define a constructor: Assign an initial value to three fields
3) Add a method public string getmsg () to the fruit class to return the basic information about the fruit
4) Define a juicer class: Zhafruit
5) Add a juicing method to the Zhafruit class: public string Getfruitzhi (Fruit f), return:
"A cup of XX (color) xx (fruit name) juice, taste xxx"
6) Create a fruit within the main method of the test class program:
Name: Apple, Color: cyan, taste: Sour
7) Call the fruit's getmsg () and receive it with a string variable
8) Print the return value of the method: "Apple, cyan, sour"
9) Create a Zhafruit Juicer object
10) Call the Juicer object of the string Getfruitzhi (Fruit f) method and pass a fruit (Apple) object
11) Call the Juicer object Getfruitzhi (Fruit f) method, print the result: a cup of cyan apple juice, taste sour
12) Define a Fruit object:
Name: orange, Color: yellow, taste: Sweet
13) Call the Juicer object of the string Getfruitzhi (Fruit f) method and pass a fruit (orange) object
) Call the Getfruitzhi (Fruit f) method of the Juicer object and print the result as: a glass of yellow orange juice, sweet taste
Package Lhy.java_1day;class fruit{public String Name;public string Color;public string Weidao;public Fruit (string name, String color,string Weidao) {This.name=name;this.color=color;this.weidao=weidao;} Public Fruit () {}public String getmsg () {return this.name+ "," +this.color+ "," +this.weidao;}} Class Zhafruit{public string Getfruitzhi (Fruit f) {string string = "One cup" +f.color+ "+f.name+" juice, taste "+f.weidao;return string;}} public class Test_6 {private static String msg;public static void Main (string[] args) {Fruit fruit1 = new Fruit ("Apple", "cyan", "Sour"); msg = Fruit1. Getmsg (); SYSTEM.OUT.PRINTLN (msg); Zhafruit zhafuit1 =new zhafruit (); System.out.println (zhafuit1. Getfruitzhi (fruit1)); Fruit fruit2 = new Fruit ("Orange", "Yellow", "sweet"); Zhafruit zhafuit2 =new zhafruit (); System.out.println (Zhafuit2. Getfruitzhi (Fruit2));}}
Java learning "after-school notes"