Homework (7th,Mar.)

第一题:

定义一个水果类(fruit),水果类中有
属性:颜色(color)、价格(price)、重量(weight),
再定义一个<测试类>,
创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g。
然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g。
最后输出:苹果的颜色、价格、重量、
香蕉的颜色、价格、重量、

 1 package Homework;
 2 public class Fruit 
 3 {
 4     private String name;
 5     
 6     private String colour;
 7     private double price;
 8     private String weight;
 9     
10     public String getName() {
11         return name;
12     }
13     public void setName(String name) {
14         this.name = name;
15     }
16     public String getColour() {
17         return colour;
18     }
19     public void setColour(String colour) {
20         this.colour = colour;
21     }
22     public double getPrice() {
23         return price;
24     }
25     public void setPrice(double price) {
26         this.price = price;
27     }
28     public String getWeight() {
29         return weight;
30     }
31     public void setWeight(String weight) {
32         this.weight = weight;
33     }
34     
35     public Fruit(String name,String colour, double price, String weight) {
36         super();
37         this.name=name;
38         this.colour = colour;
39         this.price = price;
40         this.weight = weight;
41     }
42     public void fruit()
43     {
44         System.out.println(this.name+":"+this.colour+"、"+this.price+"元、"+this.weight);
45     }
46     
47 
48 }
View Code
 1 package Homework;
 2 
 3 public class TestFruit {
 4 
 5     public static void main(String[] args) {
 6         
 7         Fruit apple=new Fruit ("苹果","红色",5.5,"10g");
 8         apple.fruit();
 9         Fruit banana=new Fruit ("香蕉","黄色",4.2,"5g");
10         banana.fruit();
11 
12     }
13 
14 }
View Code

第二题:

定义一个人类,属性:姓名(name),性别(sex),年龄(age),

创建一个"张三"的对象,姓名:张三、性别:男、年龄:20
创建一个"李四"的对象,姓名:李四、性别:女、年龄:21
最后输出张三与李四的信息

 1 package Homework;
 2 
 3 public class Human 
 4 {
 5     private String name;
 6     private String sex;
 7     private int age;
 8     
 9     
10     public String getName() {
11         return name;
12     }
13     public void setName(String name) {
14         this.name = name;
15     }
16     public String getSex() {
17         return sex;
18     }
19     public void setSex(String sex) {
20         this.sex = sex;
21     }
22     public int getAge() {
23         return age;
24     }
25     public void setAge(int age) {
26         this.age = age;
27     }
28     
29     
30     public Human(String name, String sex, int age) {
31         super();
32         this.name = name;
33         this.sex = sex;
34         this.age = age;
35     }
36     
37     
38     public void human()
39     {
40         System.out.println(this.name+":"+this.sex+"、"+this.age+"岁");
41     }
42     
43 
44     public static void main (String [] args)
45     {
46         
47         Human ZS = new Human("张三","男",20);
48         ZS.human();
49         Human LS = new Human("李四","女",21);
50         System.out.println();
51         LS.human();
52         
53     }
54     
55 }
View Code

第三题:

定义一个桌子类,

属性:材料、编号、价格,

再定义一个测试类,在测试类中分别创建两张桌子,分别给他们赋值,最后输出

 1 package Homework;
 2 
 3 public class Table 
 4 {
 5     private String material;
 6     private String number;
 7     private int price;
 8     
 9     public String getMaterial() {
10         return material;
11     }
12     public void setMaterial(String material) {
13         this.material = material;
14     }
15     public String getNumber() {
16         return number;
17     }
18     public void setNumber(String number) {
19         this.number = number;
20     }
21     public int getPrice() {
22         return price;
23     }
24     public void setPrice(int price) {
25         this.price = price;
26     }
27     
28     public Table(String material, String number, int price) {
29         super();
30         this.material = material;
31         this.number = number;
32         this.price = price;
33     }
34     
35     public void table(){
36         System.out.println("型号:"+this.number+"
材质:"+this.material+"
价格:"+this.price+"元");
37         System.out.println();
38         }
39 }
View Code
 1 package Homework;
 2 
 3 public class TestTable {
 4     
 5     public static void main(String[] args) {
 6         
 7         Table t1=new Table("实木","S001",1000);
 8         t1.table();
 9         Table t2=new Table("钢化玻璃","G002",300);
10         t2.table();
11 
12     }
13 
14 }
View Code

第四题:

写一个传奇游戏中的猪类,类中有属性:颜色(color)、重量(weight)、攻击力(attack)、准确度(accuracy)。

再写一个测试类,生成一个猪的对象,将此猪的颜色值为“白色(white)”,重量为5,攻击力为50点,准确度为0.8。

要求输出此猪的信息格式为:一只白色的猪,重量5,攻击为50点血,准确度为0.8,我好怕怕呀

 1 package Homework;
 2 
 3 public class Pig 
 4 {
 5     private String colour;
 6     private int weight;
 7     private int attact;
 8     private double accuracy;
 9     
10     public String getColour() {
11         return colour;
12     }
13     public void setColour(String colour) {
14         this.colour = colour;
15     }
16     public int getWeight() {
17         return weight;
18     }
19     public void setWeight(int weight) {
20         this.weight = weight;
21     }
22     public int getAttact() {
23         return attact;
24     }
25     public void setAttact(int attact) {
26         this.attact = attact;
27     }
28     public double getAccuracy() {
29         return accuracy;
30     }
31     public void setAccuracy(double accuracy) {
32         this.accuracy = accuracy;
33     }
34     
35     public Pig(String colour, int weight, int attact, double accuracy) {
36         super();
37         this.colour = colour;
38         this.weight = weight;
39         this.attact = attact;
40         this.accuracy = accuracy;
41     }
42     
43     public void pig()
44     {
45         System.out.println
46         ("一只"+this.colour+"的猪,"+
47          "重量"+this.weight+
48          ",攻击为"+this.attact+"点血,准确度为"+
49                 this.accuracy+",我好怕怕呀~");
50     }
51     
52     
53 }
View Code
package Homework;

public class TestPig {

    public static void main(String[] args) {
        
        Pig myPig=new Pig("白色",5,50,0.8);
        myPig.pig();

    }

}
View Code

原文地址:https://www.cnblogs.com/xiao55/p/5252231.html