Köll & Sam Classroom Blog

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    What outpouring support, guys, twas a brain tease at best for me and I agree Jos, 'the remote control' scenario is what makes it work best when dealing with arrays, rest assured I was kidding about adding it to [ ] Well, you know I did, not denying that, and did it out of anger, after muscling through it a bit and could not figure it out:-) but this works fine:

    [CODE=JAVA]

    Automobile [] automobile; //declare automobile array
    automobile = new Automobile[5]; //create automobile array

    [/CODE]

    And I seem to understand how it works. I call it fixed, but if it is as:

    [CODE=JAVA]

    int cStyle[];
    int[] javaStyle;

    [/CODE]

    I was under the impression it was somewhat not-fixed, because I would give user the power to decide the size of the array, not setting it as in the case above it...

    I am not too savvy in these types of arrays just yet, I had to set the above by hand, from the book, but by hand..

    This should have worked, in my mind, it should have:
    [CODE=JAVA]

    public static void main (String[] args) {

    Automobile[] automobile; //declare the Automobile array...
    automobile = new Automobile[automobile]; //and then create Automobile array...

    String name; //declare variables...
    double rating1; //declare variables...
    double rating2; //declare variables...
    double rating3; //declare variables...
    float averageRatingSc ale; //declare variables...

    for (int i = 0; i < automobile.leng th; i++) { //run loop as if there's no tomorrow:-)

    automobile = Integer.parseIn t(
    JOptionPane.sho wInputDialog(nu ll,"Enter Number of Automobiles:")) ; //grab Automobile instances
    name = JOptionPane.sho wInputDialog(nu ll,"Enter Favourite Automobile name:"); //demand for automobile name or preference
    rating1 = Double.parseDou ble(
    JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto # 1:")); //ask to enter rating scale 1 of favourite auto
    rating2 = Double.parseDou ble(
    JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto# 2:")); //ask to enter rating scale 2 of favourite auto
    rating3 = Double.parseDou ble(
    JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto # 3:")); //ask to enter rating scale 3 of favourite auto
    //create a new Automobile and assign values to each instance

    Automobile = new int[automobile];
    automobile[i] = new Automobile (); //create your new Automobile

    automobile[i].setName ( name ); //create instances of each favourite new Automobile
    automobile[i].setrating1 ( rating1 ); //create instances of each favourite new Automobile rating scale 1
    automobile[i].setrating2 ( rating2 ); //create instances of each favourite new Automobile rating scale 2
    automobile[i].setrating3 ( rating3 ); //create instances of each favourite new Automobile rating scale 3

    float sum = 0;
    for (int i = 0; i < automobile.leng th; i++) {

    sum += automobile[i].getRating1();
    }

    for (int i = 0; i < automobile.leng th; i++) {

    sum += automobile[i].getRating2();
    }

    for (int i = 0; i < automobile.leng th; i++) {

    sum += automobile[i].getRating3();
    }




    averageRatingSc ale = sum / (float) Automobile.leng th / 3;
    System.out.prin tln("Maximum Rating Scale: " + sum);
    System.out.prin tln("Average Rating Scale: " + averageRatingSc ale);

    //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...

    Automobile undoubtablySpor ty, //points to the Semi-Sporty Automobile
    stupendouslySpo rty; //points to the Sportiest Automobile

    undoubtablySpor ty = stupendouslySpo rty = automobile[0];

    for (int i = 1; i < automobile.leng th; i++) {

    if ( automobile[i].getRating1() < undoubtablySpor ty.getRating1() ) {
    //found a weakest Automobile
    undoubtablySpor ty = Automobile[i];
    }
    else if ( automobile[i].getRating1() > stupendouslySpo rty.getRating1( ) ) {
    //found a strongest Automobile in this class...
    stupendouslySpo rty = automobile[i];
    }

    } //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...



    System.out.prin tln("Rating Scale for Automobile named: " + stupendouslySpo rty.getName()
    + " is " + (stupendouslySp orty.getRating1 ()+ stupendouslySpo rty.getRating2( )+ stupendouslySpo rty.getRating3( ))
    / 3 + " grade point average.");
    System.out.prin tln("Rating Scale for Automobile: " + undoubtablySpor ty.getName()
    + " is " + (undoubtablySpo rty.getRating1( )+ undoubtablySpor ty.getRating2() + undoubtablySpor ty.getRating3() )
    / 3 + " grade point average.");

    //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...


    String fecthAuto = JOptionPane.sho wInputDialog(nu ll,"Name Automobile to fetch:");

    int i = 0;

    while ( i < automobile.leng th && //still more Automobiles to search
    !automobile[i].getName().equa ls( fecthAuto ) ) {
    i++;
    }

    if (i == automobile.leng th) {
    //not found - unsuccessful search
    System.out.prin tln( fecthAuto + " was not in the array" );

    } else {
    //found - successful search
    System.out.prin tln("Looks like we found " + fecthAuto + i + " level in the storage " + );
    System.out.prin tln("Looks like we found " + fecthAuto + " having " + averageRatingSc ale);
    }

    }


    }


    }


    [/CODE]

    I think I lost it after that, so thank you BigDaddyLH for posting and to let me know I am not that far off, or pretty close, the above is pretty uggly, of course you know it does not work. But your examples/adivce will surely help pin it down


    [CODE=JAVA]

    Automobile [] automobile;
    int automobileSize = ...; //compute size
    automobile = new Automobile[automobileSize];

    or

    int automobileSize = ...; //compute size
    Automobile [] automobile = new Automobile[automobileSize];

    [/CODE]

    My book is a true exercise-based book, where you get mismathed bit of code and some explaination on how it works and why, and it moves on to examples and samples of non-related code, important code like the one posted.

    But you guys are right, I am probably reading too much into it. There's probably no reason to make the code do what it is not supposed to do.

    Thanks for posting, I'll give examples a whirl;-)

    Will let you know:-)

    Köll
    Last edited by Dököll; Apr 8 '08, 01:11 AM. Reason: Will let you know:-)

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      It would not have worked Gang, I need to enter the size before making up each Auto, my noodle was probably a bit sketchy, back to drawing board...

      In a bit;-)

      Comment

      • Dököll
        Recognized Expert Top Contributor
        • Nov 2006
        • 2379

        Almost there; can anyone tell me why only one instance of an Automobile is being read:

        [CODE=JAVA]

        No errors but I am not telling it to do it properly...


        public static void main (String[] args) {


        String name; //declare variables...
        double rating1; //declare variables...
        int numTypeAuto = 0; //declare variables...
        double rating2; //declare variables...
        double rating3; //declare variables...
        float averageRatingSc ale; //declare variables...

        numTypeAuto = Integer.parseIn t(
        JOptionPane.sho wInputDialog(nu ll,"Enter Number of Automobiles:")) ; //grab Automobile instances
        name = JOptionPane.sho wInputDialog(nu ll,"Enter Favourite Automobile name:"); //demand for automobile name or preference
        rating1 = Double.parseDou ble(
        JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto Type # 1:")); //ask to enter rating scale 1 of favourite auto
        rating2 = Double.parseDou ble(
        JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto Type # 2:")); //ask to enter rating scale 2 of favourite auto
        rating3 = Double.parseDou ble(
        JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto Type # 3:")); //ask to enter rating scale 3 of favourite auto
        //create a new Automobile and assign values to each instance


        for (int i = 0; i < automobile.leng th; i++) { //run loop as if there's no tomorrow:-)


        Automobile[] automobile; //declare the Automobile array...
        automobile = new Automobile[numTypeAuto]; //and then create Automobile array...

        automobile[i].setName ( name ); //create instances of each favourite new Automobile
        automobile[i].setrating1 ( rating1 ); //create instances of each favourite new Automobile rating scale 1
        automobile[i].setrating2 ( rating2 ); //create instances of each favourite new Automobile rating scale 2
        automobile[i].setrating3 ( rating3 ); //create instances of each favourite new Automobile rating scale 3

        automobile[i].setNumTypeArra y ( arraySize ); //create instances of several favourite Automobiles


        float sum = 0;
        for (int i = 0; i < automobile.leng th; i++) {

        sum += automobile[i].getRating1();
        }

        for (int i = 0; i < automobile.leng th; i++) {

        sum += automobile[i].getRating2();
        }

        for (int i = 0; i < automobile.leng th; i++) {

        sum += automobile[i].getRating3();
        }




        averageRatingSc ale = sum / (float) Automobile.leng th / 3;
        System.out.prin tln("Maximum Rating Scale: " + sum);
        System.out.prin tln("Average Rating Scale: " + averageRatingSc ale);

        //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...

        Automobile undoubtablySpor ty, //points to the Semi-Sporty Automobile
        stupendouslySpo rty; //points to the Sportiest Automobile

        undoubtablySpor ty = stupendouslySpo rty = automobile[0];

        for (int i = 1; i < automobile.leng th; i++) {

        if ( automobile[i].getRating1() < undoubtablySpor ty.getRating1() ) {
        //found a weakest Automobile
        undoubtablySpor ty = Automobile[i];
        }
        else if ( automobile[i].getRating1() > stupendouslySpo rty.getRating1( ) ) {
        //found a strongest Automobile in this class...
        stupendouslySpo rty = automobile[i];
        }

        } //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...



        System.out.prin tln("Rating Scale for Automobile named: " + stupendouslySpo rty.getName()
        + " is " + (stupendouslySp orty.getRating1 ()+ stupendouslySpo rty.getRating2( )+ stupendouslySpo rty.getRating3( ))
        / 3 + " grade point average.");
        System.out.prin tln("Rating Scale for Automobile: " + undoubtablySpor ty.getName()
        + " is " + (undoubtablySpo rty.getRating1( )+ undoubtablySpor ty.getRating2() + undoubtablySpor ty.getRating3() )
        / 3 + " grade point average.");

        //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...


        String fecthAuto = JOptionPane.sho wInputDialog(nu ll,"Name Automobile to fetch:");

        int i = 0;

        while ( i < automobile.leng th && //still more Automobiles to search
        !automobile[i].getName().equa ls( fecthAuto ) ) {
        i++;
        }

        if (i == automobile.leng th) {
        //not found - unsuccessful search
        System.out.prin tln( fecthAuto + " was not in the array" );

        } else {
        //found - successful search
        System.out.prin tln("Looks like we found " + fecthAuto + i + " level in the storage " + );
        System.out.prin tln("Looks like we found " + fecthAuto + " having " + averageRatingSc ale);
        }

        }


        }


        }


        [/CODE]

        Will be here all night. Thanks for you help, will report my findings, I'm pumped:-)
        Last edited by Dököll; Apr 10 '08, 04:46 AM. Reason: Type...

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          Why are you creating that automobile array *inside* a loop that needs the length
          of the array? Does that code even compile? btw, you're not creating Automobiles
          for each arrray entry; i.e. your code erroneously assumes they are just there.
          If your code compiles it crashes horribly at runtime.

          kind regards,

          Jos

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            Originally posted by JosAH
            If Java were a boat this would'be been the slick, smooth development experience.

            kind regards,

            Jos ;-)
            Actually, some of those disembarkations looked similar to the beginnings of my Java projects: “Ah, here it is JList…now add some more items to the list… what, why is it immutable?!xxx! ?” Or the time I said, “just need a couple of option buttons… ah,JRadioButton … now,click on the other option button… arggg they’re both on!”

            XML is going smoothly, but the multiple-guess questions are already killing me. The weather is getting warm and I’m itching to go off-road biking again, hopefully, I won’t do any more of these falls (halfway down, just after the jet ski), but it’s sort of part of the fun when the trail is wet. Took the road bike for 14 miles last night, then I was going to run. Ran the first block which was up hill and immediately realized that I could barely walk, so I went back home. Training for a triathlon is not fun, but I am losing weight.

            Comment

            • Dököll
              Recognized Expert Top Contributor
              • Nov 2006
              • 2379

              Originally posted by JosAH
              Why are you creating that automobile array *inside* a loop that needs the length
              of the array? Does that code even compile? btw, you're not creating Automobiles
              for each arrray entry; i.e. your code erroneously assumes they are just there.
              If your code compiles it crashes horribly at runtime.

              kind regards,

              Jos
              It runs quite nicely actually, it just allows me one entry alone even though I enter 3 for example, I wanted user instance to feed the array whereby 3 added to InputBox would allow 3 Automobiles to be entered, then the program would create the Automobiles and their ratings and there's a way to fetch any auto name and reaveal it's position, looks like a done deal. If you can say that three times:-) But what is the direction you think I should be going?

              Thanks for your reply Jos!

              Köll

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                Originally posted by Dököll
                It runs quite nicely actually, it just allows me one entry alone even though I enter 3 for example, I wanted user instance to feed the array whereby 3 added to InputBox would allow 3 Automobiles to be entered, then the program would create the Automobiles and their ratings and there's a way to fetch any auto name and reaveal it's position, looks like a done deal. If you can say that three times:-) But what is the direction you think I should be going?

                Thanks for your reply Jos!

                Köll
                If your code runs nicely then it's not the code you showed us in reply #168. Have
                a look at that loop e.g:

                [code=java]
                for (int i = 0; i < automobile.leng th; i++) { //run loop as if there's no tomorrow:-)


                Automobile[] automobile;
                automobile = new Automobile[numTypeAuto];

                automobile[i].setName ( name );
                [/code]

                Your automobile array doesn't even exist in the header part of your for loop. In
                that loop you create an array at every loop pass but no automobiles for the
                entries of that array. This code doesn't even compile and most certainly doesn't
                run.

                kind regards,

                Jos

                Comment

                • Dököll
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 2379

                  Originally posted by JosAH
                  If your code runs nicely then it's not the code you showed us in reply #168. Have
                  a look at that loop e.g:

                  [code=java]
                  for (int i = 0; i < automobile.leng th; i++) { //run loop as if there's no tomorrow:-)


                  Automobile[] automobile;
                  automobile = new Automobile[numTypeAuto];

                  automobile[i].setName ( name );
                  [/code]

                  Your automobile array doesn't even exist in the header part of your for loop. In
                  that loop you create an array at every loop pass but no automobiles for the
                  entries of that array. This code doesn't even compile and most certainly doesn't
                  run.

                  kind regards,

                  Jos
                  Hey Jos!

                  Yes you have the version that did not work, using both my notebook and home workstation. Anyway, had work out some quirks, have since renamed certain variables to make them more readable:-)

                  I have some constructors running in the main class, the code you are looking at is my package code, I think it is called the OOD technique.

                  My constructors look like like they're doing what they're there for:

                  (1) I get the data I need with the get method,
                  (2) set values with the set method and so on,

                  so the code runs well, I just can't put my finger on the problem:

                  Automobile class

                  [CODE=JAVA]

                  //Get variables to load to program...

                  public String getName( ) {
                  return name;
                  }

                  public int getRating1( ) {
                  return rating1;
                  }



                  public int getRating2( ) {
                  return rating2;
                  }



                  public int getRating3( ) {
                  return rating3;
                  }


                  public int getNumTypeArray ( ) {
                  return numTypeAuto;
                  }



                  //Set variables to load to program...


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

                  public void setNumTypeArray ( int numTypeAuto) {
                  this.numTypeAut o= numTypeAuto;
                  }


                  public void setRating1( int rating1 ) {
                  this.rating1 = rating1;
                  }


                  public void setRating2( int rating2 ) {
                  this.rating2 = rating2;
                  }



                  public void setRating3( int rating3 ) {
                  this.rating3 = rating3;
                  }




                  [/CODE]

                  Data do get loaded in, still cannot shake it. But is it is the week-end, by midnight, I am dracula on Java, I'll get it to work. Surely appreciate your throwing your hat in:-)

                  In a bit...

                  Köll
                  Last edited by Dököll; Apr 12 '08, 01:39 AM. Reason: rating...

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    Originally posted by Dököll
                    Hey Jos!

                    Yes you have the version that did not work, using both my notebook and home workstation. Anyway, had work out some quirks, have since renamed certain variables to make them more readable:-)

                    I have some constructors running in the main class, the code you are looking at is my package code, I think it is called the OOD technique.
                    This is never going to work out: you post code that is not the code what you're
                    talking about; you post wallpapers full of code and ramble about it; we can't help
                    you like that. Post a short self-contained compilable example, better known as a
                    SSCCE and we can discuss matters adequately.

                    kind regards,

                    Jos

                    Comment

                    • Dököll
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 2379

                      Originally posted by JosAH
                      This is never going to work out: you post code that is not the code what you're
                      talking about; you post wallpapers full of code and ramble about it; we can't help
                      you like that. Post a short self-contained compilable example, better known as a
                      SSCCE and we can discuss matters adequately.

                      kind regards,

                      Jos
                      I was posting the part with the problem Jos, isn't that the rule when it involves school work, I wanted to get an idea if my construction of the array looked right so you got bits and pieces of the code...

                      the code works...

                      I have to rewrite it on this machine, my wife has the notebook.

                      Should only be a second

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        Originally posted by Dököll
                        I was posting the part with the problem Jos, isn't that the rule when it involves school work, I wanted to get an idea if my construction of the array looked right so you got bits and pieces of the code...

                        the code works...
                        Not the code you have shown us; we are not psychic and can't know that you
                        have some code somewhere on some computer that happens to work nicely.
                        Programming and debugging doesn't work that way. You can't just dump random
                        pieces of code and say "the problem is in there" while the rest of the code
                        doesn't make any sense. That's why I asked you to post an SSCCE (see my
                        previous reply) if you want any adequate help.

                        kind regards,

                        Jos

                        Comment

                        • Dököll
                          Recognized Expert Top Contributor
                          • Nov 2006
                          • 2379

                          Alright, Jos, stop it now will you!

                          Weren't you able to provide assistance with minimal information, the above post 164, or 169 regarding arrays; that was good, I gave minimal info, you helped, I learned something from it.

                          As said, I wanted to do it on my own so adding as little information would allow me to achieve it, thought this was for site rules; when it regards school activities, direction, examples and so on, must show ability to come with ideas on your own! No, isn't that it?

                          Still, Jos, I cannot loop to grab all instances added to an input dialog that would allow user to decide how many automobiles to enter, and that would make up the size of the array... instead I get one instance/chance and one result:

                          (1) Below code is that of our Automobile class
                          (2) Code is associated with a template
                          (3) Template code follows main class code

                          Automobile class

                          [CODE=JAVA]

                          package processAutomobi le;

                          /**
                          * @author Köll S. Cherizard
                          * @professor :-)
                          * @Lab 7
                          * @version 200800000408
                          */

                          /**
                          * The Automobile class used in Chapter 10
                          */
                          class Automobile {

                          /**
                          * The name of this automobile
                          */
                          private String name;

                          /**
                          * The Rating Scale of each Auto being entered
                          */
                          private int rating1;
                          private int rating2;
                          private int rating3;
                          private char autoSpeed;

                          private int numTypeAuto;

                          /**
                          * Default constructor
                          */
                          public Automobile() {
                          this("Not Given", 0,'U');
                          }

                          /**

                          * @param name name of the new automobile
                          */
                          public Automobile(Stri ng name, int numTypeAuto, char autoSpeed) {

                          this.name = name;
                          this.numTypeAut o = numTypeAuto;
                          this.autoSpeed = autoSpeed;

                          }

                          /**
                          * Returns the Rating 1 of each automobile.
                          */
                          public int getRating1( ) {
                          return rating1;
                          }


                          /**
                          * Returns the Rating 2 of each automobile.
                          */
                          public int getRating2( ) {
                          return rating2;
                          }


                          /**
                          * Returns the Rating 3 of each automobile.
                          */
                          public int getRating3( ) {
                          return rating3;
                          }

                          /**
                          * Returns the number of automobiles being entered...
                          */
                          public int getSizeOfArray( ) {
                          return numTypeAuto;
                          }

                          /**
                          * Returns the name of this automobile.
                          */
                          public String getName( ) {
                          return name;
                          }



                          public char autoSpeed( ) {
                          return autoSpeed;
                          }

                          /**
                          * Set Rating of each automobile.
                          */
                          public void setRating1( int rating1 ) {
                          this.rating1 = rating1;
                          }

                          /**
                          * Set Rating of each automobile.
                          */
                          public void setRating2( int rating2 ) {
                          this.rating2 = rating2;
                          }

                          /**
                          * Set Rating of each automobile.
                          */
                          public void setRating3( int rating1 ) {
                          this.rating1 = rating1;
                          }

                          /**
                          * Set the name of this automobile.
                          */
                          public void setName( String name ) {
                          this.name = name;
                          }

                          public void setSizeOfArray( int numTypeAuto ) {
                          this.numTypeAut o = numTypeAuto;
                          }

                          public void setAutoSpeed( char autoSpeed) {
                          this.autoSpeed = autoSpeed;
                          }

                          }


                          [/CODE]


                          Automobile template code...

                          [CODE=JAVA]


                          package processAutomobi le;

                          /**
                          * @author Köll S. Cherizard
                          * @professor :-)
                          * @Lab 7
                          * @version 200800000408
                          */

                          import javax.swing.*;




                          class Ch10ProcessAuto mobileArray {

                          public static void main (String[] args) {
                          String name, speed;
                          int numTypeAuto = 0; //declare variables...
                          int rating1; //declare variables...
                          int rating2; //declare variables...
                          int rating3; //declare variables...
                          char autoSpeed;
                          //read in data values
                          numTypeAuto = Integer.parseIn t(JOptionPane.s howInputDialog( null,"Enter Number of Automobiles:")) ;

                          name = JOptionPane.sho wInputDialog(nu ll,"Enter Favourite Automobile name:");

                          rating1 = Integer.parseIn t(JOptionPane.s howInputDialog
                          (null,"Enter Rating Scale for Endurance (Example: 5 through 10):")); //ask to enter rating scale 1 of favourite auto
                          rating2 = Integer.parseIn t(JOptionPane.s howInputDialog
                          (null,"Enter Rating Scale for Handling (Example: 5 through 10):")); //ask to enter rating scale 2 of favourite auto
                          rating3 = Integer.parseIn t(JOptionPane.s howInputDialog
                          (null,"Enter Rating Scale for Dependability (Example: 5 through 10):")); //ask to enter rating scale 3 of favourite auto

                          speed = JOptionPane.sho wInputDialog(nu ll,"Enter Speed of this Automobile name:"); //ask to enter speed of each favourite auto
                          autoSpeed = speed.charAt(0) ;


                          Automobile[] automobile; //declare the Automobile array
                          automobile = new Automobile[numTypeAuto]; //and then create it

                          for (int i = 0; i < automobile.leng th; i++) { //create a new Automobile and assign values

                          automobile[i] = new Automobile( );
                          automobile[i].setSizeOfArray ( numTypeAuto ); //create user-defined number of Automobiles to be entered
                          automobile[i].setName ( name ); //create instances of each favourite new Automobile
                          automobile[i].setRating1 ( rating1 ); //create instances of each favourite new Automobile rating scale 1
                          automobile[i].setRating2 ( rating2 ); //create instances of each favourite new Automobile rating scale 2
                          automobile[i].setRating3 ( rating3 ); //create instances of each favourite new Automobile rating scale 3
                          automobile[i].setAutoSpeed ( autoSpeed ); //create speed level of each favourite new Automobile


                          }

                          //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...

                          Automobile undoubtablySpor ty, //points to the Semi-Sporty Automobile
                          stupendouslySpo rty; //points to the Sportiest Automobile
                          undoubtablySpor ty = stupendouslySpo rty = automobile[0]; //initialize each strength to zero...

                          //loop to make available for Rating Scale 1, 2, and 3
                          for (int i = 1; i < automobile.leng th; i++) {

                          if ( automobile[i].getRating1() < undoubtablySpor ty.getRating1() ) { //found a younger Automobile
                          undoubtablySpor ty = automobile[i];
                          }

                          else if ( automobile[i].getRating1() > stupendouslySpo rty.getRating1( ) ) { //found an older Automobile
                          stupendouslySpo rty = automobile[i];
                          }
                          }
                          System.out.prin tln("Rating Scale for Automobile named : " + stupendouslySpo rty.getName() + " is " + stupendouslySpo rty.getRating1( ));
                          System.out.prin tln("Rating Scale for Automobile named: " + undoubtablySpor ty.getName() + " is " + undoubtablySpor ty.getRating1() );

                          //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...

                          String searchName = JOptionPane.sho wInputDialog(
                          null,"Name to search:");
                          int i = 0;

                          //still more Automobiles to search
                          while ( i < automobile.leng th && !automobile[i].getName().equa ls( searchName ) ) {i++;
                          }
                          if (i == automobile.leng th) { //not found - unsuccessful search
                          System.out.prin tln( searchName + " was not in the array" );
                          }
                          else {
                          //found - successful search
                          System.out.prin tln("Found " + searchName + " at position " + i);
                          }
                          }
                          }

                          [/CODE]

                          I simply cannot shake it, runs without compile errors but the results not what I am looking for. Just a little guidance will do just fine, I am patient, I know I'll get it... Thanks for all your help.

                          Köll
                          Last edited by Dököll; Apr 13 '08, 02:23 AM. Reason: text...

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            Originally posted by Dököll
                            Alright, Jos, stop it now will you!
                            Why should I stop and stop with what? You're posting here asking for advice. I'm
                            trying to tell you to stop programming like a senile granny's knitting: she's knitting
                            but forgot what she was knitting but she keeps on knitting and hopes that all ends
                            up well; i.e. it lacks a plan, a design just like your coding.

                            There are a few stone old rules: a class or method should do one thing and it
                            should do it well. This immediately implies that a program that's supposed to
                            do quite a few things should have quite a few classes with quite a few methods.

                            There's another 'linquistic' trick: write down what you want in enough detail; the
                            verbs you're using are the methods and the nouns are the classes. Give it a try:

                            - ask user for numberOfCars
                            - setup storage for numberOfCars Cars
                            - for numberOfCars
                            --- ask CarDetails and store them

                            That's about it and this fragment basically translates to:

                            [code=java]
                            int numberOfCars= askNumberOfCars ();
                            Car[] cars= new Car[numberOfCars];
                            for (int i= 0; i < numberOfCars; i++)
                            CarDetails carDetails= askCarDetails() ;
                            cars[i]= new Car(carDetails) ;
                            [/code]

                            Note that I used the artifact CarDetails which you may get around by a bit of
                            different coding but it does the job for now. It is called a 'DTO' (Data Transfer
                            Object) and is a well known aid in Java and other languages. In your simple
                            case the Car can be the CarDetails class itself; it's up to you.

                            There are a few simple methods that do their single job and they should do it well.
                            As you can see the program control flow is a one to one bijection between the
                            plans (the design or pseudo code) and the Java implementation. At this level
                            we're not interested in the details of the other methods: they have an isolated
                            functionality that won't interfere with the job at hand.

                            Look at the code above: it's just a few lines of Java and does what it has to do
                            with the help of a few other methods and classes. Keep on doing this in some
                            sort of 'fractal' way until you've designed and implemented it all, e.g. you should
                            already have a 'generic' method available that prompts the user for an integer.

                            If you read this very same thread you must've noticed it already so (re)use it. Stop
                            knitting like a granny and stop hoping that all that wallpaper ends up well; most of
                            the time it doesn't and programming is not knitting.

                            kind rregards,

                            Jos

                            Comment

                            • Dököll
                              Recognized Expert Top Contributor
                              • Nov 2006
                              • 2379

                              Good advice, I know you're kidding about thegranny part... most grannies are actually pretty skilled @ programming...

                              My design is flawless, Jos, you can say,"hey that looks like a lot of effort, but I would try it this way instead"

                              Or,

                              "...you may have a pretty cool design, but I would concentrate in this area..."

                              Something in that kind of a line... The idea is an advice is well retained if one can concentrate on that and not the fact that it was not well said, even though helpful...

                              I could have added classes for the 3 ratings below:
                              [CODE=JAVA] Endurance (Example: 5 through 10):")); //ask to enter rating scale 1 of favourite auto
                              Handling (Example: 5 through 10):")); //ask to enter rating scale 2 of favourite auto
                              Dependability (Example: 5 through 10):")); //ask to enter rating scale 3 of favourite auto "[/CODE]
                              The project at hand did not call for other classes, and I surely could not add what should never be put in, as per our professor... that's not the exercise.

                              I am proud of my work, it stinks, but it's mine, so help me understand where I have faultered, if that's a verb, but don't keep saying only what's not good about it, you do not know the trouble I went through to make the design a reality, for an apprentice by the way:-)

                              Thanks for you help!

                              Köll

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                Originally posted by Dököll
                                I am proud of my work, it stinks, but it's mine, so help me understand where I have faultered, if that's a verb, but don't keep saying only what's not good about it, you do not know the trouble I went through to make the design a reality, for an apprentice by the way:-)
                                Read this.

                                kind regards,

                                Jos

                                Comment

                                Working...