zoukankan      html  css  js  c++  java
  • OOP作业

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

    编写代码如下:

    定义水果类:

     1 package org.hanqi.pn0120;
     2 
     3 public class Fruit {
     4     
     5     private String color;
     6     private double price;
     7     private int weight;
     8     public String getColor() {
     9         return color;
    10     }
    11     public void setColor(String color) {
    12         this.color = color;
    13     }
    14     public double getPrice() {
    15         return price;
    16     }
    17     public void setPrice(double price) {
    18         this.price = price;
    19     }
    20     public int getWeight() {
    21         return weight;
    22     }
    23     public void setWeight(int weight) {
    24         this.weight = weight;
    25     }
    26 }

    定义测试类:

     1 package org.hanqi.pn0120;
     2 
     3 public class TestFruit {
     4 
     5     public static void main(String[] args) {
     6         
     7         Fruit apple = new Fruit();
     8         apple.setColor("红色");
     9         apple.setPrice(5.5);
    10         apple.setWeight(10);
    11         System.out.println("苹果的颜色是:"+apple.getColor()+" ,价格是:"+apple.getPrice()+" ,重量是:"+apple.getWeight()+"g");
    12         Fruit banana = new Fruit();
    13         banana.setColor("黄色");
    14         banana.setPrice(4.2);
    15         banana.setWeight(5);
    16         System.out.println("香蕉的颜色是:"+banana.getColor()+" ,价格是:"+banana.getPrice()+" ,重量是:"+banana.getWeight()+"g");
    17     }
    18 }

    则运行结果为:

    2、定义一个人类,人类中有以下属性:姓名(name),性别(sex),年龄(age),再定义一个测试类
    创建一个"张三"的对象
    姓名:张三
    性别:男
    年龄:20
    创建一个"李四"的对象
    姓名:李四
    性别:女
    年龄:21
    最后输出张三与李四的信息

    编写代码如下:

    定义一个人类:

     1 package org.hanqi.pn0120;
     2 
     3 public class Human {
     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 Human(String name, String sex, int age) {
    27         super();
    28         this.name = name;
    29         this.sex = sex;
    30         this.age = age;
    31     }
    32     public void human()
    33     {
    34         System.out.println("姓名:"+this.name+" ,性别:"+this.sex+" ,年龄:"+this.age);
    35     }
    36 }

    再定义一个测试类:

     1 package org.hanqi.pn0120;
     2 
     3 public class TestHuman {
     4 
     5     public static void main(String[] args) {
     6         
     7         Human zhangsan = new Human("张三","男",20);
     8         zhangsan.human();
     9         Human lisi = new Human("李四","女",21);
    10         lisi.human();
    11     }
    12 }

    则运行结果为:

    3、定义一个桌子类,属性有:材料、编号、价格,再定义一个测试类,在测试类中分别创建两张桌子,分别给他们赋值,最后输出

    编写代码如下:

    定义一个桌子类:

     1 package org.hanqi.pn0120;
     2 
     3 public class Desk {
     4     
     5     private String stuff;
     6     private int number;
     7     private double price;
     8     public String getStuff() {
     9         return stuff;
    10     }
    11     public void setStuff(String stuff) {
    12         this.stuff = stuff;
    13     }
    14     public int getNumber() {
    15         return number;
    16     }
    17     public void setNumber(int number) {
    18         this.number = number;
    19     }
    20     public double getPrice() {
    21         return price;
    22     }
    23     public void setPrice(double price) {
    24         this.price = price;
    25     }
    26 }

    再定义一个测试类:

     1 package org.hanqi.pn0120;
     2 
     3 public class TestDesk {
     4 
     5     public static void main(String[] args) {
     6             
     7         Desk desk1 = new Desk();
     8         desk1.setStuff("木制");
     9         desk1.setNumber(9527);
    10         desk1.setPrice(666);
    11         System.out.println("第一张桌子的材质是:"+desk1.getStuff()+",编号是:"+desk1.getNumber()+",价格是:"+desk1.getPrice());
    12         Desk desk2 = new Desk();
    13         desk2.setStuff("铁制");
    14         desk2.setNumber(27149);
    15         desk2.setPrice(233);
    16         System.out.println("第二张桌子的材质是:"+desk2.getStuff()+",编号是:"+desk2.getNumber()+",价格是:"+desk2.getPrice());        
    17     }
    18 }

    运行结果为:

    4、写一个传奇游戏中的猪类,类中有属性:颜色(color)、重量(weight)、攻击力(attack)、准确度(accuracy)。再写一个测试类,生成一个猪的对象,将此猪的颜色值为“白色(white)”,重量为5,攻击力为50点,准确度为0.8。要求输出此猪的信息格式为:一只白色的猪,重量5,攻击为50点血,准确度为0.8,我好怕怕呀

    编写代码如下:

    定义一个猪类:

     1 package org.hanqi.pn0120;
     2 
     3 public class Pig {
     4     
     5     private String color;
     6     private int weight;
     7     private int 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 int getWeight() {
    16         return weight;
    17     }
    18     public void setWeight(int weight) {
    19         this.weight = weight;
    20     }
    21     public int getAttack() {
    22         return attack;
    23     }
    24     public void setAttack(int 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, int weight, int 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 }

    再定义测试类:

     1 package org.hanqi.pn0120;
     2 
     3 public class TestPig {
     4 
     5     public static void main(String[] args) {
     6         
     7         Pig pig = new Pig("白色",5,50,0.8);
     8         pig.pig();
     9     }
    10 }

    则运行结果为:

  • 相关阅读:
    查看eclipse的安装路径
    js中Number()、parseInt()和parseFloat()的区别进行详细介绍
    JSON 基础学习1
    Jquery Math ceil()、floor()、round()比较与用法
    easyui获取当前点击对象tabs的title和Index
    java中String,int,Integer,char、double类型转换
    DNA排序
    The Peanuts
    牛的选举——取最大k个数
    数据筛选——第k小的数
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5251642.html
Copyright © 2011-2022 走看看