码迷,mamicode.com
首页 > 其他好文 > 详细

作业题

时间:2016-03-08 00:20:31      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
}
Fruit类
技术分享
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");    
    }

}
Testfruit类

技术分享

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;
    }

}
Ren类
技术分享
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());
    }
}
Testren类

技术分享

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;
    }
    
    
    

}
Zhuozi类
技术分享
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());
          
      

    }

}
Testzhuozi

技术分享

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;
    }
    
    

}
Zhu类
技术分享
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()+",我好怕怕呀");

    }

}
Testzhu类

技术分享

 

作业题

标签:

原文地址:http://www.cnblogs.com/wangchuanqi/p/5252181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!