10 Replies Latest reply: Jan 31, 2008 2:37 PM by 807601 RSS

    Practicing classes - a test program

    807601
      I'm practicing classes, but still have questions.
      Unfortunately, I'm stuck and don't quite know where to
      start or what question to ask first.

      The program:
      Output: English word.
      Input: Norwegian translation. The answer is tested to see if it is the correct word.

      Compiler response:
      cannot find symbol
      symbol: constructor Words(int)
      location: class Words
      Words nor = new Words(1);

      cannot find symbol
      symbol: constructor Words(int)
      location: class Words
      Words nor = new Words(2);

      Words2(int) in Words cannot be applied to ()
      String[] liste_eng = eng.Words2();

      Words2(int) in Words cannot be applied to ()
      String[] liste_nor = nor.Words;
      import java.io.*;
      
      /* Practice English words. 6 words each week */
      
      class FindWords{
        public static void main(String[] arg) throws IOException 
           { 
           int ii, jj;
           jj=0;
      
           InputStreamReader isr = new InputStreamReader(System.in);
           BufferedReader stdin = new BufferedReader(isr);
       
           Words eng = new Words(1); 
           Words nor = new Words(2);
      
           String[] list_eng = eng.Words2();
           String[] list_nor = nor.Words2();
      
           for(ii=0; ii<5; ii++) {
                        System.out.print("What is " + list_eng[ii] + " in Norwegian?");
             String guess = stdin.readLine();  
             while((guess != list_nor[ii]) && jj<5) {         
             System.out.print("Sorry, no, try again.");
                        guess = stdin.readLine();
               jj++;
               }
             System.out.println("Right!");
             }
           }
      }
      
      
      class Words {
           public String[] listofwords = new String[6];
      
           public String[] Words2(int language) { // returns 1 for English, 2 for Norwegian
             if(language==1){
                  listofwords[0]="sunshine";     
                                      listofwords[1]="thunder"; // and so on until listofwords[5]
             }
             else { // Norwegian
                listofwords[0]="solskinn"; 
                                      listofwords[1]="torden";  // and so on
             }
                      }
      
      }
        • 1. Re: Practicing classes - a test program
          807601
          maryme wrote:
               Words eng = new Words(1); 
               Words nor = new Words(2);
          You're calling a constructor that does not exist, just like the error message says. Look at your Words class. When you call Words(1), where do you think that '1' is supposed to go?
          • 2. Re: Practicing classes - a test program
            807601
            The plan was to send the 1 to Words2 in Words, thus spesifying "English".

            From your respons, I'm guessing I got it wrong.
            • 3. Re: Practicing classes - a test program
              807601
              If I have a class like this
              public class MyClass {
                  int value;
                
                  public MyClass(int v) {
                      value = v;
                  }
                  public int getValue() {
                      return value;
                  }
              }
              and then want to use that class, I have to first create an instance of it, like this:
              MyClass myclass = new MyClass(5);
              What that does, is tell the Java compiler to go find the MyClass class, and look for a constructor called MyClass() that takes a single integer (in this case, 5). If it does not find that constructor, it gets an error. In this case, it finds the constructor I wrote and creates the myclass object.

              Now I can use the methods in that class, in a similar way
              int myvalue = myclass.getValue();
              This goes to the myclass class, and searches for a method called getValue that takes no parameters. If it doesn't find it, an exception occurs.
              • 4. Re: Practicing classes - a test program
                807601
                You have to call Words2() separately, which you're doing.

                But you're passing your parameters into the constructors instead of the Words2 functions, which is causing the errors.
                • 5. Re: Practicing classes - a test program
                  807601
                  Of course! Thanks!
                  Now the compiler complains: "missing return statement", last line in the file.
                  The return statement is to send the listofwords from Words to FindWords - am I right?
                  Either way, I can't figure out how and where to put the return statement.
                  • 6. Re: Practicing classes - a test program
                    807601
                    I think what's confusing you is probably having dealt with implied constructors. If you create a class with no constructor:
                    public class MyClass {
                        public int doStuff() {
                            return 10;
                        }
                    }
                    Then you can create an instance of it with
                    MyClass myclass = new MyClass();
                    Because every class has a default constructor that takes no parameters, if you don't overwrite it with your own version. But in your code you tried passing an integer to the default constructor, and it blew up since there is no default constructor that takes an integer.
                    • 7. Re: Practicing classes - a test program
                      807601
                      maryme wrote:
                      Of course! Thanks!
                      Now the compiler complains: "missing return statement", last line in the file.
                      The return statement is to send the listofwords from Words to FindWords - am I right?
                      Either way, I can't figure out how and where to put the return statement.
                      Your return statement should be the last line of your Words2 method.
                      return listofwords;
                      • 8. Re: Practicing classes - a test program
                        807601
                        10 years ago, I leared SIMULA - object based programming. I've forgotten most of it. The parts I remember don't go well with the few things I've understood in java. The mix is really confusing! Right now I can't tell the old stuff from the new. Thanks for answering my questions - it helps me sort everything out!

                        If I give the for-loop in FindWords a name (this would be making it a method? syntax question..), I can add a return at the end - returning listofwords to FindWords?? Just thinking out loud before I turn my program upside down.
                        • 9. Re: Practicing classes - a test program
                          807601
                          A method is to Java what would be called a 'function' in most other languages. Here's a basic method:
                          public int getSquared(int num) {
                              int square = num * num;
                              return square;
                          }
                          Look at the first line of this method:

                          public -> any object that creates an instance of the class containing this method can call this method
                          int -> the method's type. This means that the method MUST return a value of this type, no matter what
                          getSquared -> the name of the method
                          (int num) -> the parameter list, in this case a single int parameter

                          If you remove the last line in the method, your code will not compile, as it MUST return an int.
                          • 10. Re: Practicing classes - a test program
                            807601
                            Ooops; the return statement in place resulted in:

                            Expection in thread "main" java.lang.NoClassDefFoundError: FindWords
                            Caused by: java.lang.ClassNotFoundException: FindWords
                            at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
                            at java.security.AccessController.doPrivileged(NAtive Method)
                            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                            at sun.misc.Launcher$AppClassLoader.laodClass(Launcher.java:276)
                            at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)