作业题
1,定义一个水果类(fruit),水果类中的有【属性】:颜色(color)、价格(price)、重量(weigth),再定义一个<测试类>,创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g。然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g。最后输出:苹果的颜色、价格、重量、香蕉的颜色、价格、重量
package org.hanqi.pn0120; public class Fruit { private String color; private double price; private double weigth; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getWeigth() { return weigth; } public void setWeigth(double weigth) { this.weigth = weigth; } }
package org.hanqi.pn0120; public class Testfruit { public static void main(String[] args) { Fruit apple=new Fruit(); apple.setColor("红色"); apple.setPrice(5.5); apple.setWeigth(10); System.out.print("苹果的颜色:"+apple.getColor()); System.out.print(" 苹果的价格:"+apple.getPrice()); System.out.println(" 苹果的重量:"+apple.getWeigth()+"g"); System.out.println(); Fruit banana=new Fruit(); banana.setColor("黄色"); banana.setPrice(4.2); banana.setWeigth(5); System.out.print("香蕉的颜色:"+banana.getColor()); System.out.print(" 香蕉的价格:"+banana.getPrice()); System.out.println(" 香蕉的重量:"+banana.getWeigth()+"g"); } }
2、定义一个人类,人类中有以下属性:姓名(name),性别(sex),年龄(age),再定义一个测试类
创建一个"张三"的对象
姓名:张三
性别:男
年龄:20
创建一个"李四"的对象
姓名:李四
性别:女
年龄:21
最后输出张三与李四的信息
package org.hanqi.pn0120; public class Ren { private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Ren(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } }
package org.hanqi.pn0120; public class Testren { public static void main(String[] args) { Ren r1=new Ren("张三","男",20); System.out.println("姓名:"+r1.getName()+" 性别:"+r1.getSex()+" 年龄:"+r1.getAge()); Ren r2=new Ren("李四","女",21); System.out.println("姓名:"+r2.getName()+" 性别:"+r2.getSex()+" 年龄:"+r2.getAge()); } }
3、定义一个桌子类,属性有:材料、编号、价格,再定义一个测试类,在测试类中分别创建两张桌子,分别给他们赋值,最后输出
package org.hanqi.pn0120; public class Zhuozi { private String cailiao; private int bianhao; private double jiage; public String getCailiao() { return cailiao; } public void setCailiao(String cailiao) { this.cailiao=cailiao; } public int getBianhao() { return bianhao; } public void setBianhao(int bianhao) { this.bianhao=bianhao; } public double getJiage() { return jiage; } public void setJiage(double jiage) { this.jiage=jiage; } public Zhuozi() { } public Zhuozi(String cailiao, int bianhao, double jiage) { this.cailiao = cailiao; this.bianhao = bianhao; this.jiage = jiage; } }
package org.hanqi.pn0120; public class Testzhuozi { public static void main(String[] args) { Zhuozi z1=new Zhuozi(); z1.setCailiao("木头"); z1.setBianhao(1); z1.setJiage(100); System.out.println("第一张桌子的材料是:" +z1.getCailiao()+" 编号:"+z1.getBianhao()+" 价格:"+z1.getJiage()); Zhuozi z2=new Zhuozi("铁",2,200); System.out.println("第二张桌子的材料是: " +z2.getCailiao()+" 编号:"+z2.getBianhao()+" 价格:"+z2.getJiage()); } }
4,写一个传奇游戏中的猪类,类中有属性:颜色(color)、重量(weight)、攻击力(attack)、准确度(accuracy)。再写一个测试类,生成一个猪的对象,将此猪的颜色值为“白色(white)”,重量为5,攻击力为50点,准确度为0.8。要求输出此猪的信息格式为:一只白色的猪,重量5,攻击为50点血,准确度为0.8,我好怕怕呀
package org.hanqi.pn0120; public class Zhu { private String color; private int weight; private int attack; private double accuracy; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public int getAttack() { return attack; } public void setAttack(int attack) { this.attack = attack; } public double getAccuracy() { return accuracy; } public void setAccuracy(double accuracy) { this.accuracy = accuracy; } public Zhu(String color, int weight, int attack, double accuracy) { super(); this.color = color; this.weight = weight; this.attack = attack; this.accuracy = accuracy; } }
package org.hanqi.pn0120; public class Testzhu { public static void main(String[] args) { Zhu z=new Zhu("白色",5,50,0.8); System.out.println("一只"+z.getColor()+"的猪,重量"+z.getWeight()+",攻击为"+z.getAttack()+"点血,准确度为"+z.getAccuracy()+",我好怕怕呀"); } }