课后作业
第一题:
Fruit类代码:
1 package zuoye; 2 3 public class Fruit { 4 private String mingcheng; 5 public String getMingcheng() { 6 return mingcheng; 7 } 8 public void setMingcheng(String mingcheng) { 9 this.mingcheng = mingcheng; 10 } 11 private String color; 12 private double price; 13 private double weigth; 14 public String getColor() { 15 return color; 16 } 17 public void setColor(String color) { 18 this.color = color; 19 } 20 public double getPrice() { 21 return price; 22 } 23 public void setPrice(double price) { 24 this.price = price; 25 } 26 public double getWeigth() { 27 return weigth; 28 } 29 public void setWeigth(double weigth) { 30 this.weigth = weigth; 31 } 32 33 public Fruit(String mingcheng,String color, double price, double weigth) { 34 super(); 35 this.mingcheng=mingcheng; 36 this.color = color; 37 this.price = price; 38 this.weigth = weigth; 39 } 40 public void fruit() 41 { 42 System.out.println(this.mingcheng+"的颜色是:"+this.color+" 价格是:"+this.price+" 重量是:"+this.weigth+"g"); 43 } 44 45 }
Test类代码:
1 package zuoye; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Fruit apple=new Fruit("苹果","红色",5.5,10); 8 apple.fruit(); 9 Fruit banana=new Fruit("香蕉","黄色",4.2,5); 10 banana.fruit(); 11 12 } 13 14 }
第二题:
People类代码:
1 package zuoye; 2 3 public class People { 4 5 private String name; 6 private String sex; 7 private int age; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getSex() { 15 return sex; 16 } 17 public void setSex(String sex) { 18 this.sex = sex; 19 } 20 public int getAge() { 21 return age; 22 } 23 public void setAge(int age) { 24 this.age = age; 25 } 26 public People(String name, String sex, int age) { 27 super(); 28 this.name = name; 29 this.sex = sex; 30 this.age = age; 31 } 32 public void people() 33 { 34 System.out.println("名字是"+this.name+"性别是:"+this.sex+"年龄是:"+this.age); 35 } 36 }
Test类代码:
1 package zuoye; 2 3 public class TestPeople { 4 5 public static void main(String[] args) { 6 7 People zhangsan=new People("张三","男",20); 8 zhangsan.people(); 9 People lisi=new People("李四","女",21); 10 lisi.people(); 11 } 12 }
第三题:
Zhuozi类代码:
1 package zuoye; 2 3 public class Zhuzi { 4 5 private String cailiao; 6 private int bianhao; 7 private double price; 8 public String getCailiao() { 9 return cailiao; 10 } 11 public void setCailiao(String cailiao) { 12 this.cailiao = cailiao; 13 } 14 public int getBianhao() { 15 return bianhao; 16 } 17 public void setBianhao(int bianhao) { 18 this.bianhao = bianhao; 19 } 20 public double getPrice() { 21 return price; 22 } 23 public void setPrice(double price) { 24 this.price = price; 25 } 26 public Zhuzi(String cailiao, int bianhao, double price) { 27 super(); 28 this.cailiao = cailiao; 29 this.bianhao = bianhao; 30 this.price = price; 31 } 32 public void zhuzi() 33 { 34 System.out.println("桌子的材料是:"+this.cailiao+" 桌子的编号为:"+this.bianhao+" 价格为:"+this.price+"元"); 35 } 36 }
Test类代码:
1 package zuoye; 2 3 public class TestZhuozi { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Zhuzi zhuozi1=new Zhuzi("枣木",01,50); 8 zhuozi1.zhuzi(); 9 Zhuzi zhuozi2=new Zhuzi("红木",02,100); 10 zhuozi2.zhuzi(); 11 } 12 13 }
第四题:
Pig类的代码:
1 package zuoye; 2 3 public class Pig { 4 5 private String color; 6 private double weight; 7 private double attack; 8 private double accuracy; 9 public String getColor() { 10 return color; 11 } 12 public void setColor(String color) { 13 this.color = color; 14 } 15 public double getWeight() { 16 return weight; 17 } 18 public void setWeight(double weight) { 19 this.weight = weight; 20 } 21 public double getAttack() { 22 return attack; 23 } 24 public void setAttack(double attack) { 25 this.attack = attack; 26 } 27 public double getAccuracy() { 28 return accuracy; 29 } 30 public void setAccuracy(double accuracy) { 31 this.accuracy = accuracy; 32 } 33 public Pig(String color, double weight, double attack, double accuracy) { 34 super(); 35 this.color = color; 36 this.weight = weight; 37 this.attack = attack; 38 this.accuracy = accuracy; 39 } 40 public void pig() 41 { 42 System.out.println("一只"+this.color+"的猪,重量:"+this.weight+",攻击力为"+this.attack+"点血,精确度为"+this.accuracy+",我好怕怕呀~"); 43 } 44 }
Test类代码:
1 package zuoye; 2 3 public class TestPig { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Pig pig=new Pig("白色",5,50,0.8); 8 pig.pig(); 9 10 } 11 12 }