OOP作业

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 int weight;
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 int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}

定义测试类:

 package org.hanqi.pn0120;

 public class TestFruit {

     public static void main(String[] args) {

         Fruit apple = new Fruit();
apple.setColor("红色");
apple.setPrice(5.5);
apple.setWeight(10);
System.out.println("苹果的颜色是:"+apple.getColor()+" ,价格是:"+apple.getPrice()+" ,重量是:"+apple.getWeight()+"g");
Fruit banana = new Fruit();
banana.setColor("黄色");
banana.setPrice(4.2);
banana.setWeight(5);
System.out.println("香蕉的颜色是:"+banana.getColor()+" ,价格是:"+banana.getPrice()+" ,重量是:"+banana.getWeight()+"g");
}
}

则运行结果为:

OOP作业

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

编写代码如下:

定义一个人类:

 package org.hanqi.pn0120;

 public class Human {

     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 Human(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public void human()
{
System.out.println("姓名:"+this.name+" ,性别:"+this.sex+" ,年龄:"+this.age);
}
}

再定义一个测试类:

 package org.hanqi.pn0120;

 public class TestHuman {

     public static void main(String[] args) {

         Human zhangsan = new Human("张三","男",20);
zhangsan.human();
Human lisi = new Human("李四","女",21);
lisi.human();
}
}

则运行结果为:

OOP作业

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

编写代码如下:

定义一个桌子类:

 package org.hanqi.pn0120;

 public class Desk {

     private String stuff;
private int number;
private double price;
public String getStuff() {
return stuff;
}
public void setStuff(String stuff) {
this.stuff = stuff;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

再定义一个测试类:

 package org.hanqi.pn0120;

 public class TestDesk {

     public static void main(String[] args) {

         Desk desk1 = new Desk();
desk1.setStuff("木制");
desk1.setNumber(9527);
desk1.setPrice(666);
System.out.println("第一张桌子的材质是:"+desk1.getStuff()+",编号是:"+desk1.getNumber()+",价格是:"+desk1.getPrice());
Desk desk2 = new Desk();
desk2.setStuff("铁制");
desk2.setNumber(27149);
desk2.setPrice(233);
System.out.println("第二张桌子的材质是:"+desk2.getStuff()+",编号是:"+desk2.getNumber()+",价格是:"+desk2.getPrice());
}
}

运行结果为:

OOP作业

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

编写代码如下:

定义一个猪类:

 package org.hanqi.pn0120;

 public class Pig {

     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 Pig(String color, int weight, int attack, double accuracy) {
super();
this.color = color;
this.weight = weight;
this.attack = attack;
this.accuracy = accuracy;
}
public void pig()
{
System.out.println("一只"+this.color+"的猪,重量"+this.weight+",攻击力为"+this.attack+"点血,准确度为"+this.accuracy+",我好怕怕呀");
}
}

再定义测试类:

 package org.hanqi.pn0120;

 public class TestPig {

     public static void main(String[] args) {

         Pig pig = new Pig("白色",5,50,0.8);
pig.pig();
}
}

则运行结果为:

OOP作业

上一篇:gnuplot配置HOME目录


下一篇:angularjs学习第五天笔记(第二篇:表单验证升级篇)