作业1:
小王去超市购物,他拿了一个篮子。走到化妆品区买了一个28.5元的洗面奶;走到水果区买了3.9元的新疆提子;走到饮料区买了一打啤酒,价格是46;走到光盘区买了一张Celine的CD,价值62元。最后小王走到收银台付账的时候,突然想起来医生告诫他不能再喝酒了。于是他从篮子中把一打啤酒拿出来。最后小王结账回家了。
请编写控制台应用程序,使用面向对象的编程思想,模拟整个过程。(最后打出小票 包括每个物品名称和价钱  最后得出总价钱 )作业2:
街上有一种电子称,你站上去一量身高体重,它就会告诉你的身材是否为标准体重。不过,它是如何计算你的体重是否标准体重呢?嗯,就是我们这节课的“标准体重计算程序”实现的功能了。
从不同的角度来考虑,计算标准(理想)体重的方法可能有很多种。我们这里采用最流行的一种计算方法,这种方法不分男女,但是分南北地域。计算公式是:
男性
北方人理想体重=〔身高cm—150〕x0.6十50(kg)
南方人理想体重=〔身高cm—150〕x0.6十48(kg)
考虑到女性一般要比男性轻,所以如果是女性,我们还需要将标准体重减去2公斤。
女性
北方人理想体重=〔身高cm—150〕x0.6十48(kg)
南方人理想体重=〔身高cm—150〕x0.6十46(kg)
可见,要计算一个人的标准体重,必须知道是男人女人,是北方人还是南方人,及身高。
此外用户还必须输入他的现实体重,这样,在程序计算出标准体重之后,我们计算实际体重在标准体重百分之几的范围之内,做出不同判断。

解决方案 »

  1.   

    我觉得你对面向对象的编程思想  根本就不了解,或者说你根本还没有理解  面向对象的编程思想  是什么意思。
    或许是因为你刚刚学习JAVA 但是我希望你能重新去复习一下我说话比较直白,但是我只能这样说因为只要对  面向对象的编程思想  有一点认识的程序员 就会很快做出来继续努力吧
      

  2.   

    第一题我声明了两个类,一个篮子,一个物品。我用数组把物品放进篮子。可是我实在是想不出来怎么再从数组中拿出来其中一个我刚学JAVA不要笑我。
      

  3.   

    我也是初学者,也曾有过叫天天不应,叫地地不灵的时候,我写了一段第二题的程序,供你参考。
    // Person 类
    public class Person
    {
    String name;
    String gender;
    String zone;
    double stature;
    double weight;

    public Person(String name)
    {
    this.name = name;
    }

    public Person(String gender, String zone)
    {
    this.gender = gender;
    this.zone = zone;
    }

    public void setName(String name)
    {
    this.name = name;
    }
    public String getName()
    {
    return name;
    }

    public void setGender(String gender)
    {
    this.gender = gender;
    }
    public String getGender()
    {
    return gender;
    }

    public void setZone(String zone)
    {
    this.zone = zone;
    }
    public String getZone()
    {
    return zone;
    }

    public void setStature(double stature)
    {
    this.stature = stature;
    }
    public double getStature()
    {
    return stature;
    }

    public void setWeight(double weight)
    {
    this.weight = weight;
    }
    public double getWeight()
    {
    return weight;
    }
    }// 计算类
    public class StandardWeight
    {
    public static double getConstant(String zone, String gender)
    {
    double constant = 0; // 公式中的常数项
    if(zone == "North")
    {
    if(gender == "male")
    {
    constant = 50;
    }
    else
    {
    constant = 48;
    }
    }
    else
    {
    if(gender == "male")
    {
    constant = 48;
    }
    else
    {
    constant = 46;
    }
    }

    return constant;
    }

    // 计算标准体重
    public static void getStandardWeight(Person person)
    {
    double standardWeight = (person.getStature() - 150) * 0.6 + getConstant(person.getZone(), person.getGender());
    if(person.getWeight() > standardWeight)
    {
    System.out.println("体重超标,须减肥");
    }
    else if(person.getWeight() == standardWeight)
    {
    System.out.println("体重符合标准,身材不错");
    }
    else
    {
    System.out.println("体重未达标,须增肥");
    }
    }
    }// 测试类
    public class Main
    {
    public static void main(String[] args)
    {
    Person person = new Person("XXXX");
    person.setZone("South");
    person.setGender("male");
    person.setStature(168);
    person.setWeight(58);

    StandardWeight.getStandardWeight(person);
    }
    }至于第一题,你可以把每一中物品写成一个类,里面有 name 和 price 属性,另外小票也应写成一个类,具体怎么实现你再想想吧,如果仍不清楚,继续讨论吧
      

  4.   


    第一题,你可以参考下面的代码。
    三个类:人,商品,收银员import java.util.ArrayList;
    import java.util.List;
    /**
     * 人
     * @author ibm
     *
     */
    public class Person {
    private String name = null;
    List<Product> listProduct = new ArrayList<Product>(); public Person(String name) {
    this.name = name;
    } //买商品
    public void buyProduct(Product product) {
    this.listProduct.add(product);
    } //不买了,放回去
    public void backProduct(int index) {
    this.listProduct.remove(index);
    } //取得总共买的商品
    public List<Product> getEveryThing() {
    return listProduct;
    } // 开始模拟
    public static void main(String[] args) { Person person = new Person("xiaowang");
    Counter counter = new Counter(); Product xiMianNai = new Product("xiMainNai", 28.5);
    Product tiZi = new Product("tiZi", 3.9);
    Product piJiu = new Product("piJiu", 46.0);
    Product cd = new Product("cd", 62.0); person.buyProduct(xiMianNai);
    person.buyProduct(tiZi);
    person.buyProduct(piJiu);
    person.buyProduct(cd); // 啤酒不买了
    person.backProduct(2); counter.print(person.getEveryThing());
    counter.calculate(person.getEveryThing()); }}/**
     * 商品
     * 
     * @author ibm
     * 
     */
    class Product {
    private String name = null;
    private Double price = 0.0; public Product(String name, Double price) {
    this.name = name;
    this.price = price;
    } public String toString() {
    return name + " : " + price;
    } public Double getPrice() {
    return this.price;
    }
    }
    /**
     * 收银员
     * @author ibm
     *
     */
    class Counter { //计算总价格并打印
    public void calculate(List<Product> listProduct) {
    Double returnValue = 0.0;
    for (Product product : listProduct) {
    returnValue += product.getPrice();
    }
    System.out.println("total : "+returnValue);
    } //打印商品名和单价
    public void print(List<Product> listProduct) {
    for (Product product : listProduct) {
    System.out.println(product);
    }
    }
    }
      

  5.   

    第一题我觉得两个类
    客户类,商品类
    其中商品类中封装了商品名,数量,价格
    使用get set方法来设置,商品的数量最后通过客户类调用,来实现相应的操作
      

  6.   

    谢谢FAFA2008和noone_1983~~我得继续努力了 感谢大家回帖