Luokka ongelma

testaja

Luo Kuvio-luokka ja johda siitä Suorakaide-luokka. Laita sitten 5 suorakaidetta taulukkoon ja hae pinta-alaltaan suurin suorakaide ja tulosta sen tiedot.

Tommonen olisi tehtävänä. Miten oisi helpoin tehdä?

4

501

    Vastaukset

    Anonyymi (Kirjaudu / Rekisteröidy)
    5000
    • hosnimuparakki

      Eli haluaisit jonkun tekevän koulutehtäväsi valmiiksi puolestasi?

      Tehtävän anto on mielestäni selvä eikä siinä pitäisi olla mitään ongelmia mikäli peruskäsiteet on selvillä.

      Tässäpä kuitenkin jotain:
      1. Tee luokka "Kuvio"
      2. Tee luokka "Suorakaide" joka perii luokan Kuvio.
      3. Tee pääohjelmaluokka - vaikka nimeltä "Kotitehtava"
      4. Muodosta pääohjelmassa 5 instanssia luokasta suorakaide ja tallenna ne taulukkoon.
      5. Etsi taulukosta suorakaide olio jonka pinta-ala on suurin ja tulosta sen tiedot näytölle.

      Perinnän ideahan on uudelleenkäyttää koodia. Siispä mieti mitä muuttujia tai metodeja kannattaa toteuttaa luokassa Kuvio joita Suorakaidekin voi hyödyntää.

      Vastaavasti Suorakaide -luokkaan määrittele vain suorakaiteelle yksilölliset muuttujat tai metodit.

    • osa 1

      metriikkaa ei oltu määritelty, joten metriikkana voi olla eiffel-torni, kokispullo yms. Voidaan myös jatkokehittää sisältämään Linus Torvalds, kyynärä yms. Helppo tapa konffata miten printataan ja miten kuviot luodaan. Postaan useammassa osassa koska forumin viestit on rajoitettu 5000 merkkiin. Se miten printataan ja miten kuviot luodaan annetaan komentorivin parametreina esim "java TestPrinter FiveRectanglesFactory MetaDataAndSizeFactory"

      interface Command{
      public void execute();
      }

      class BiggestShapePrinter implements Command{
      public static final String ESCAPECHARACTER="\\";
      public static final String PERIODCHARACTER=".";
      public static final String FILESEPARATOR="/";
      private Shape shape=null;
      private SystemPrinterFactory printerFactory=null;
      public BiggestShapePrinter(Shape s,SystemPrinterFactory p){
      shape=s;
      printerFactory=p;
      }
      public void execute(){
      SystemPrinter printer=printerFactory.getPrinter();
      printer.print(shape);
      }
      }

      interface SystemPrinterFactory{
      public SystemPrinter getPrinter();
      }

      interface SystemPrinter{
      public void print(Shape s);
      }

      interface DatePrinter{
      //might be deprecated on future
      public void print(Shape s);
      }

      class MetaDataPrinter implements SystemPrinter{
      private SystemPrinter other=null;
      public MetaDataPrinter(SystemPrinter p){
      other=p;
      }

      public void print(Shape s){
      System.out.println("shape type=" s.getType());
      System.out.println("shape name=" s.getName());
      if(other!=null){
      other.print(s);
      }
      }
      }
      class SizePrinter implements SystemPrinter{
      private SystemPrinter other=null;
      public SizePrinter(SystemPrinter p){
      other=p;
      }

      public void print(Shape s){
      ISurface f=s.getSurface();
      MetricSystem m=f.getMetrics();
      double sz=f.getWidth()*f.getLength();
      System.out.println("shape size=" sz " " m.getName());
      if(other!=null){
      other.print(s);
      }
      }
      }
      class MetaDataAndSizeFactory implements SystemPrinterFactory{
      public SystemPrinter getPrinter(){
      return new SizePrinter(new MetaDataPrinter(null));
      }
      }

      interface ShapeFactory{
      public Shape[] getShapes();
      }

      class FiveRectanglesFactory implements ShapeFactory{
      /*
      * this method returns an array of Shape objects
      * @ params none
      */
      public Shape[] getShapes(){
      Shape shapes[]=new Shape[5];

      MetricSystem bill=new BillGates();
      MetricSystem eiffel=new EiffelTorni();
      MetricSystem kaapio=new MaailmanPisinKaapio();
      MetricSystem kokis=new KokisPullo();
      MetricSystem teho=new TehoTolkki();
      ISurface eka=new Surface(bill,1.0,0.5);
      ISurface toka=new Surface(eiffel,1.0,0.5);
      ISurface kolmas=new Surface(kaapio,1.0,1.0);
      ISurface neljas=new Surface(kokis,1.0,0.1);
      ISurface viides=new Surface(teho,10.0,1);
      shapes[0]=new Rectangle(eka,"1 Bill Gates");
      shapes[1]=new Rectangle(toka,"0.5 part of Eiffel..");
      shapes[2]=new Rectangle(kolmas,"alykaapio..");
      shapes[3]=new Rectangle(neljas,"kokispullo..");
      shapes[4]=new Rectangle(viides,"10 tehotolkkia..");
      return shapes;
      }
      }

      interface Shape extends Comparable{
      public String getType();
      public String getName();
      public ISurface getSurface();
      }

      interface ISurface {
      public MetricSystem getMetrics();
      public double getWidth();
      public double getLength();
      }

      class Surface implements ISurface{
      //todo put comments here
      private double witdh=0.0;
      private double heihgt=0.0;
      private MetricSystem metricsystem=null;
      public Surface(MetricSystem m,double w,double h){
      witdh=w;
      heihgt=h;
      metricsystem=m;
      }
      public MetricSystem getMetrics(){
      return metricsystem;
      }
      public double getWidth(){
      return witdh;
      }
      public double getLength(){
      return heihgt;
      }
      }

      interface MetricSystem{
      public double getAsMetres(); //palauttaa metreissa..esim kokispullo on 0.33333333334 metria..
      public String getName();
      }

      class TehoTolkki implements MetricSystem{
      public double getAsMetres(){
      return 0.1;
      }
      public String getName(){
      return "Tehotolkkia";
      }
      }

      class KokisPullo implements MetricSystem{
      public double getAsMetres(){
      return 0.333333334;
      }
      public String getName(){
      return "Kokispulloa";
      }
      }

      class BillGates implements MetricSystem{
      public double getAsMetres(){
      return 1.7526;
      }
      public String getName(){
      return "Bill Gatesia";
      }
      }

      • osa 2

        class EiffelTorni implements MetricSystem{
        public double getAsMetres(){
        return 300.65;
        }
        public String getName(){
        return "Eiffeltornia";
        }
        }

        class MaailmanPisinKaapio implements MetricSystem{
        public double getAsMetres(){
        return 1.1;
        }
        public String getName(){
        return "Kaapiota";
        }
        }

        class Rectangle implements Shape{
        private String name="";
        private ISurface surface=null;
        public Rectangle(ISurface s,String n){
        name=n;
        surface=s;
        }
        public final String getType(){
        return "rectangle";
        }
        public final String getName(){
        return name;
        }
        public ISurface getSurface(){
        return surface;
        }
        public int compareTo(Object otherShape){
        Shape other=(Shape)otherShape;
        double thissize= getSz(this);
        double othersize= getSz(other);
        if(thissize>othersize){
        return -1;
        }
        if(othersize>thissize){
        return 1;
        }
        return 0;
        }

        private double getSz(Shape s){
        ISurface f=s.getSurface();
        MetricSystem m=f.getMetrics();
        double sz=f.getWidth()*f.getLength();
        double retv=sz*m.getAsMetres();
        return retv;
        }
        }

        public class TestPrinter{
        public static void main(String args[])throws Exception{
        ShapeFactory shapefactory=(ShapeFactory)Class.forName(args[0]).newInstance();
        //System.out.println(shapefactory.getClass());
        Shape shapes[]=shapefactory.getShapes();
        java.util.Arrays.sort(shapes);
        Shape biggest=shapes[0];
        SystemPrinterFactory printerfactory=(SystemPrinterFactory)Class.forName(args[1]).newInstance();
        Command command=new BiggestShapePrinter(biggest,printerfactory);
        command.execute();
        }
        }


        TOIMII!!


      • .
        osa 2 kirjoitti:

        class EiffelTorni implements MetricSystem{
        public double getAsMetres(){
        return 300.65;
        }
        public String getName(){
        return "Eiffeltornia";
        }
        }

        class MaailmanPisinKaapio implements MetricSystem{
        public double getAsMetres(){
        return 1.1;
        }
        public String getName(){
        return "Kaapiota";
        }
        }

        class Rectangle implements Shape{
        private String name="";
        private ISurface surface=null;
        public Rectangle(ISurface s,String n){
        name=n;
        surface=s;
        }
        public final String getType(){
        return "rectangle";
        }
        public final String getName(){
        return name;
        }
        public ISurface getSurface(){
        return surface;
        }
        public int compareTo(Object otherShape){
        Shape other=(Shape)otherShape;
        double thissize= getSz(this);
        double othersize= getSz(other);
        if(thissize>othersize){
        return -1;
        }
        if(othersize>thissize){
        return 1;
        }
        return 0;
        }

        private double getSz(Shape s){
        ISurface f=s.getSurface();
        MetricSystem m=f.getMetrics();
        double sz=f.getWidth()*f.getLength();
        double retv=sz*m.getAsMetres();
        return retv;
        }
        }

        public class TestPrinter{
        public static void main(String args[])throws Exception{
        ShapeFactory shapefactory=(ShapeFactory)Class.forName(args[0]).newInstance();
        //System.out.println(shapefactory.getClass());
        Shape shapes[]=shapefactory.getShapes();
        java.util.Arrays.sort(shapes);
        Shape biggest=shapes[0];
        SystemPrinterFactory printerfactory=(SystemPrinterFactory)Class.forName(args[1]).newInstance();
        Command command=new BiggestShapePrinter(biggest,printerfactory);
        command.execute();
        }
        }


        TOIMII!!

        Vau.


    Ketjusta on poistettu 0 sääntöjenvastaista viestiä.

    Luetuimmat keskustelut

    1. SDP jo 100 % suositumpi kuin persut

      Kertoo Hesarin uusin kannatuskysely. Demareiden kannatus on miltei tuplat verrattuna persuihinl. Suomen kansa ei selväst
      Maailman menoa
      90
      10654
    2. Voiko normaali ihminen ryhtyä vasemmistolaiseksi?

      Tätä jäin pohdiskelemaan.
      Maailman menoa
      245
      4576
    3. SDP haluaa 40 000 nettomaahanmuuttajaa

      SDP:n Suunnanmuutos-vaihtoehtobudjetissa, käy ilmi, että demarit itse asiassa vaativat räjähdysmäistä ”työperäisen” maah
      Maailman menoa
      163
      3943
    4. Orpo: Velkajarrua vastustavaa puoluetta vaikea ajatella hallitukseen

      No Minja Koskelan kommunistipuolue jäi ulos tuosta. Kaikki eduskuntapuolueet vasemmistoliittoa lukuun ottamatta sopivat
      Maailman menoa
      169
      3449
    5. PS ylivoimainen nousija myös HS:n gallupissa, SDP laskee taas

      https://www.verkkouutiset.fi/a/hs-gallup-sdpn-suosio-laskee-ps-nousussa/#0a7d2507 Ylivoimainen viime kuukausien nousija
      Maailman menoa
      72
      3262
    6. Valtavasti suomalaisia asunnottomina, mutta ei yhtään somalia

      tai muuta kehitysmaalaista. Mites tässä näin kävi? Tiedän hyvin, että esim. somaleita lentää ulos mm. Hekan asunnoista,
      Maailman menoa
      70
      3024
    7. Mikä tämä henkilö mahtaa touhuta Parkanossa

      Kamalaa https://www.ylasatakunta.fi/teksti/pirkanmaan-karajaoikeus-vangitsi-koiran-tappamisesta-epaillyn-6.68.127794.b58
      Parkano
      32
      2742
    8. Ikävä sinua mies

      Vuosia kuluu, mutta tunteet ei ole hävinnyt. Tasoittuneet toki, kun ei olla nähty. Järki palannut päähän kuitenkin. Se i
      Ikävä
      27
      2310
    9. Hienoa! Eduskunta luopui käteisen käytöstä

      Nyt tuo sama muutos pitää saada myös muuhun yhteiskuntaan. Käteistähän ei tarvitse tänä päivänä enää kuin rikolliset.
      Maailman menoa
      65
      1766
    10. Kiinnostaa - ei kiinnosta - kiinnostaapas

      Selittäkää hämmentyneelle miksi miehiä ei ikinä kiinnosta silloin, kun sitä olisi itsekin kiinnostunut? Sitten kun siirt
      Sinkut
      164
      1510
    Aihe