|
|
Регистрация Восстановить пароль |
Повторная активизация e-mail |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
|
Опции темы | Поиск в этой теме |
13.10.2011, 23:32 | #1 |
Пользователь
Регистрация: 30.11.2010
Сообщений: 48
|
классы
Помогите изменить ниже приведенный код. контейнер MyContainer реализован с помощью коллекции. помогите изменить и реализовать с помощью массивов. так как в яве нет динамических массивов то идея в том что при добавлнии нового объекта предыдущий массив переписывается в новый массив больший на 1 дописанный объект. помогите хотя бы один метод add() изменить а дальше я сама, спасибо заранее..
|
13.10.2011, 23:36 | #2 |
Пользователь
Регистрация: 30.11.2010
Сообщений: 48
|
import java.util.*;
abstract class Goods implements Comparable<Goods>{ private double cost; private String name; public void setCost(double cost){ this.cost = cost; } public double getCost(){ return cost; } public void setName(String name){ this.name = name; } public String getName(){ return name; } abstract public String toString(); public int compareTo(Goods o) { if (this.cost < o.getCost()) return -1; else if (this.cost == o.getCost()) return 0; else return 1; } } class Chair extends Goods{ private boolean isNew; static private int count = 0; static private double commonCost = 0; public Chair() { this.setCost(10); this.setName("wodoo"); this.setNew(true); count++; commonCost += 10; } public Chair(double cost, String name, boolean isNew){ this.setCost(cost); this.setName(name); this.setNew(isNew); count++; commonCost += cost; } public void setNew(boolean isNew){ this.isNew = isNew; } public boolean getNew(){ return isNew; } public String toString(){ return "It's a chair with cost = " + getCost() + ". Its name = " + getName() + ". It's new? " + isNew; } static public void avgCost(){ System.out.println(commonCost/count); } } class Table extends Goods{ private String material; static private int count = 0; static private double commonCost = 0; public Table() { this.setCost(15); this.setName("wodoo"); this.setMaterial("Oak"); count++; commonCost += 15; } public Table(int cost, String name, String material){ this.setCost(cost); this.setName(name); this.setMaterial(material); count++; commonCost += cost; } public void setMaterial(String material){ this.material = material; } public String getMaterial(){ return material; } public String toString(){ return "It's a table with cost = " + getCost() + ". Its name = " + getName() + " and it was create from " + material; } static public void avgCost(){ System.out.println(commonCost/count); } } class MyContainer{ private List<Goods> al = new ArrayList<Goods>(); public void add(Goods newGood){ al.add(newGood); } public int count(){ return al.size(); } public Goods get(int index){ return al.get(index); } public void sortByCost(){ Collections.sort(al); } public void print(){ for(Goods a : al) System.out.println(a); } } public class Test{ public static void main(String[] args) { Chair c1 = new Chair(); Table t1 = new Table(); System.out.println(t1.toString()); System.out.println(c1.toString()); Chair c2 = new Chair(60, "lovely rabbit", false); Table t2 = new Table(30, "good table", "fir"); System.out.println(t2.toString()); System.out.println(c2.toString()); Table.avgCost(); Chair.avgCost(); System.out.println("_______________ ___"); MyContainer myC = new MyContainer(); myC.add(c1); myC.add(t1); myC.add(c2); myC.add(t2); myC.print(); System.out.println("_______________ ___"); myC.sortByCost(); myC.print(); } } |
Похожие темы | ||||
Тема | Автор | Раздел | Ответов | Последнее сообщение |
С++ классы | Azgor | Помощь студентам | 3 | 26.05.2010 22:42 |
С++ , классы | Djes92 | Общие вопросы C/C++ | 6 | 21.05.2010 00:01 |
Классы в С++ | Rammashka | Помощь студентам | 4 | 01.05.2010 20:45 |
Классы | honest | Помощь студентам | 5 | 21.09.2009 21:23 |