Java learning [After-School Notes], java after-school notes

Source: Internet
Author: User

Java learning [After-School Notes], java after-school notes

Blog: http://blog.csdn.net/muyang_ren


Complete the following exercises with encapsulation and constructor. If there is no special declaration, write the attribute as private.

Exercise 1:
1) create a student class
2) create four private fields: name, sex, age, and dire.
3) provide attributes for private fields respectively
4) test: Create a student object, set attributes, and print the basic information of the student on the console.

Package lhy. java_1day; class student {private String name = ""; private char sex = 'male'; private int age = 18; private String dire = "embedded "; public 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) {// method stub automatically generated by TODO 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 2:

Define a Tank class (Tank Class), define four fields for it: x (x coordinate), y (y coordinate), dir (Direction: Value: upper and lower left), and Type (Type, all access modifiers are public;
Define a constructor for the Tank class to initialize four fields
Define a ShowData method for the Tank class, and print four field values on the console
Create a Tank object with 10 x axis, 50 x axis, right direction, heavy Tank type, and print the Tank information by calling the ShowData () method of the object.

Package lhy. java_1day; class tank {public int x; public int y; public char dir; public String type; public tank () {// automatically generated TODO constructor Stub this. x = 100; this. y = 200; this. dir = 'downlink '; 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 3:

1) create a Computer class: Computer
2) create three fields: name: computer name, type: Computer brand, size: size
3) create a constructor and assign an initial value to the three fields.
4) Add a ShowData method to print the field values.
5) create a computer object and call its ShowData ();
Package lhy. java_1day; class Computer {private String name; private String type; private Double size; public Computer () {this. name = "Xiao Xin"; 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 4:
1) create a Dog class: Dog
2) Add a name field and sex field to the dog class.
3) provide attributes for the name field and sex field respectively (name: no verification, sex: can only be "public and mother ")
4) Add the ShowData Method to the Dog and print the name and gender.
5) Add the YaRen Method to the Dog to enable the Dog biting function. YaoRen (string str). If str is a "man", output "Wang". If str is a "woman ", output "wow". If str is another output, "wow"
6) test

Package lhy. java_1day; class Dog {private String name = ""; private char sex = 'mal'; public 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 ("Wang");} else if (str = "") {System. out. println ("str =" + str); System. out. println ("zookeeper");} else {System. out. println ("str =" + str); System. out. println ("wow") ;}} public class Test_4 {public static void main (String [] args) {Dog = new dog (); Dog. showData (); dog. yaoRen ("man"); dog. yaoRen (""); dog. yaoRen ("kid ");}}

Exercise 5:

1) create a Teacher Class: Teacher
2) add fields for the Instructor: id, name, sex, age, height
3) Add corresponding attributes for fields:
Id: int, no verification required
Name: string, with a length greater than one or fewer than four characters
Sex: string, only male and female
Age: int, only between 20-55
Height: double, only between 1.50-1.80
Define a ShowData method to print numbers, names, gender, and height

4) create a teacher object and call its ShowData ();

Package lhy. java_1day; import java. util. secret; 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 ("Name Length error"); System. out. println ("Enter: name"); setName (new partition (System. in ). next () ;}} publ Ic void setSex (String sex) {if (sex. equals ("male") | sex. equals ("female") {this. sex = sex;} else {System. out. println ("male or female)"); setSex (new sex (System. in ). next () ;}} public void setAge (int age) {if (age> 20 & age <55) {this. age = age;} else {System. out. println ("Incorrect age input (20 ~ 55) "); System. out. println ("Enter: Age"); setAge (new partition (System. in ). nextInt () ;}} public void setHeight (Double height) {if (height> 1.5 & height <1.8) {this. height = height;} else {System. out. println ("Incorrect height input (1.5 ~ 1.8) "); System. out. println ("Enter: height"); setHeight (new Height (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 (); role stdin = new role (System. in); System. out. println ("Enter: ID"); teacher. setId (stdin. nextInt (); System. out. println ("Enter: name"); teacher. setName (stdin. next (); System. out. println ("Enter: Age"); teacher. setAge (stdin. nextInt (); System. out. println ("Enter: Gender"); teacher. setSex (stdin. next (); System. out. println ("Enter height"); teacher. setHeight (stdin. nextDouble (); stdin. close (); teacher. showData ();}}

Exercise 6:

1) define a Fruit class: Fruit with public fields: name, color, and weiDao.
2) define a constructor: assign an initial value to the three fields
3) Add a public string GetMsg () method to the Fruit class and return the basic information of the Fruit.
4) define a juicer class: ZhaFruit
5) Add a juice squeeze method for the ZhaFruit class: public string GetFruitZhi (Fruit f), and return:
"A cup of XX (color) XX (fruit name) juice, taste XXX"
6) create a fruit in the Main method of the test class Program:
Name: Apple, color: blue, taste: sour
7) Call the GetMsg () of the fruit and use a string variable to receive
8) print the return value of this method: "Apple, Blue, sour"
9) create a ZhaFruit juicer object
10) Call the string GetFruitZhi (Fruit f) method of the juicer object and upload a Fruit (Apple) object
11) Call the GetFruitZhi (Fruit f) method of the juicer object. The result is: a cup of blue apple juice that tastes sour.
12) define a fruit object:
Name: Orange, color: yellow, taste: Sweet
13) Call the string GetFruitZhi (Fruit f) method of the juicer object and upload a Fruit (orange) object

14) Call the GetFruitZhi (Fruit f) method of the juicer object and print the result: a cup of yellow orange juice, sweet

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 (fruit1); Fruit fruit2 = new Fruit ("orange", "yellow", "sweet"); ZhaFruit zhafuit2 = new ZhaFruit (); System. out. println (zhafuit2.GetFruitZhi (fruit2 ));}}




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.