Results 1 to 8 of 8

Thread: Java program error - help!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    IZob's Avatar Citizen
    Join Date
    Aug 2009
    Location
    Australia
    Posts
    9,829

    Default Java program error - help!

    Hello,
    First I'll say thankyou for viewing this post, secondly thankyou if you can assist me with this java compile error, it will be most appreciated.

    Ok so there are 4 files.

    - main file:
    JavaQuiz.java <---- this file cannot be edited.
    - class files:
    Question.java
    QuestionLibrary.java
    Student.java

    The Question.java, QuestionLibrary.java and Student.java files are in a package togther called au.edu.uow.csci213quiz (each dot represents a new folder directory). JavaQuiz.java is on the desktop.

    As far as I know there is no problem with the package.

    Here is the error I'm getting while trying to compile JavaQuiz.java
    Code:
    C:\Users\name\Desktop>javac JavaQuiz.java
    javaQuiz.java:(line50): error: incompatible types
    Question question = new QuestionLibrary();
     
    required: Question
    found: QuestionLibrary
    1 error
    Here is the code in JavaQuiz.java file:
    Spoiler Alert, click show to read: 
    Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import au.edu.uow.csci213quiz.*;
     
    /**
     * Note: You should not modify this program. This program will use
     *       classes you created in the package au.edu.uow.csci213quiz .
     */
    public class JavaQuiz {
     
        /**
         * This constant specifies the number of questions in each quiz.
         * It should not be larger than the total number of questions in 
         * the question library.
         */
        final private static int NoOfQuestions = 5;
        /**
         * This is the buffered reader required to receive user answers
         */
        private static BufferedReader breader;
        /**
         * This is the entry point of the application
         * @param args Command line options. Not used
         * @throws IOException The exception for errors from reading user answers
         */
        public static void main(String[] args) throws IOException{
     
            //Preparing to receive user answers
            breader = new BufferedReader(new InputStreamReader(System.in));
            //Getting student details  
            Student student = new Student();
            System.out.println("What is your name?");
            String name = breader.readLine(); 
            student.setName(name);
     
            //Creating the question library
            Question question = new QuestionLibrary();
     
            // Variable to hold the student answer
            int ans=0;
     
            //The quiz starts here
            for(int i=0; i< NoOfQuestions; i++){
     
            //Printing the question and choices
            System.out.println("\nQuestion No "+ (i+1) +":");
            question.printQuestion();
            System.out.println("\nAnswer Choices:");
            question.printChoices();
            //Getting the answer
            System.out.println("\nChoose your answer number:");
            ans = Integer.parseInt(breader.readLine());
            //Recording the student answer
            student.recordScore(question.compareAnswer(ans));
     
            //Resetting the answer variable. 
        }
     
        //Printing the quiz result
        System.out.println("\n\nResult of "
          + student.getName()
          +": " + student.getScore()
          +" out of "
          + NoOfQuestions);
        }
    }


    Here is the code inside the Question.java file:
    Spoiler Alert, click show to read: 
    Code:
    package au.edu.uow.csci213quiz;
    //unfinished.
    public interface Question 
    {
        void printQuestion();
        /**
         * This method prints a question. 
         * @see printChoices()
         * @see compareAnswer(int)
         */
     
        void printChoices();
        /**
         * This method prints the choices to answer question. 
         * @see printQuestion()
         * @see compareAnswer(int)
         */
     
     boolean compareAnswer(int ans);
        /**
         * This method compares the student's answer to the standard answer. 
         * @see printQuestion()
         * @see printChoices()
         * @param ans The student's answer
         * @return True for correct answer; false for incorrect answer.
         */
    }


    Here is the code inside the QuestionLibrary.java file:
    Spoiler Alert, click show to read: 
    Code:
    package au.edu.uow.csci213quiz;
    import java.io.*;
     
    /**
    *this file should provide questions and answers (both correct and incorrect) for the user to answer.
    *compare the users answer to the correct answer in the questionlibrary data files.
    *the QuestionLibrary should have two classes, one for t/f questions. another for multi choice questions.
    *you can add more supporting clsses if appropiate.
    */
    public class QuestionLibrary//implements Question
    {
     
    }


    As you can see QuestionLibrary does nonthing yet. I am lost as to why there is a problem.

    JavaQuiz does not call QuestionLibrary If I am not mistaken, yet there is a 'incompatiable error'.

    Most of the code is also unrelated to the error, such as the code in student.java (which works fine atm).

    So thats all of the important info I can give to help I think.
    Thanyou in advance.
    Last edited by IZob; September 04, 2011 at 09:48 PM.
    Contact me on Steam: steamcommunity.com/id/IZob/ or send a PM.

  2. #2
    english tyrant's Avatar Domesticus
    Join Date
    Dec 2006
    Location
    Colchester
    Posts
    2,112

    Default Re: Java program error - help!

    I may be wrong.
    But doesn't your constructor have to have exactly the same name as your class...
    //Creating the question library
    Question question = new QuestionLibrary();
    should be:

    //Creating the question library
    Question question = new Question();

    -----------------------------------------------------------------------

    Edit:
    Actually if you are creating a QuestionLibary don't you want:

    //Creating the question library
    QuestionLibrary question = new QuestionLibrary();

  3. #3
    IZob's Avatar Citizen
    Join Date
    Aug 2009
    Location
    Australia
    Posts
    9,829

    Default Re: Java program error - help!

    That would be the logical way but I wasn't the one who typed the main class. Maybe I don't know its full purpose yet ... but apparently there is a way to use it as it is.
    Contact me on Steam: steamcommunity.com/id/IZob/ or send a PM.

  4. #4
    Juvenal's Avatar love your noggin
    Patrician Content Emeritus

    Join Date
    Apr 2006
    Location
    The Home Counties
    Posts
    3,465

    Default Re: Java program error - help!

    Look at the statement
    Code:
    Question question = new QuestionLibrary();
    The compiler will require QuestionLibrary to have implements or extends Question.

    In this case Question is an interface, so you need implements.

    But you've commented the implements clause out of your declaration of QuestionLibrary.
    Code:
    public class QuestionLibrary//implements Question
    So try removing the comment marks.
    imb39 ...is my daddy!
    See AARtistry in action: Spite of Severus and Severus the God

    Support the MAARC!
    Tale of the Week Needs You!


  5. #5
    Squid's Avatar Opifex
    Patrician Artifex Technical Staff

    Join Date
    Feb 2007
    Location
    Frozen waste lands of the north
    Posts
    17,751
    Blog Entries
    3

    Default Re: Java program error - help!

    Just note uncommenting the implements clause will cause some compile errors because you haven't implemented all the methods of the Question interface. The easiest way around this without actually coding is to create a bunch of method stubs that return default value, if needed, but otherwise don't do anything.

    Example method stubs:

    Code:
    //method stub for a void method, it doesn't do anything
    public void printQuestion() {}
    
    //method stub for a boolean value, it just returns true
    public bool compareAnswer(int ans) { return true; }
    
    etc. etc.
    Under the patronage of Roman_Man#3, Patron of Ishan
    Click for my tools and tutorials
    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe." -----Albert Einstein

  6. #6
    Juvenal's Avatar love your noggin
    Patrician Content Emeritus

    Join Date
    Apr 2006
    Location
    The Home Counties
    Posts
    3,465

    Default Re: Java program error - help!

    Quote Originally Posted by Squid View Post
    Just note uncommenting the implements clause will cause some compile errors because you haven't implemented all the methods of the Question interface. The easiest way around this without actually coding is to create a bunch of method stubs that return default value, if needed, but otherwise don't do anything.
    True enough, but some IDEs will add these stubs automatically (NetBeans certainly does).
    imb39 ...is my daddy!
    See AARtistry in action: Spite of Severus and Severus the God

    Support the MAARC!
    Tale of the Week Needs You!


  7. #7
    Squid's Avatar Opifex
    Patrician Artifex Technical Staff

    Join Date
    Feb 2007
    Location
    Frozen waste lands of the north
    Posts
    17,751
    Blog Entries
    3

    Default Re: Java program error - help!

    Only if you request it I believe.
    Under the patronage of Roman_Man#3, Patron of Ishan
    Click for my tools and tutorials
    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe." -----Albert Einstein

  8. #8
    IZob's Avatar Citizen
    Join Date
    Aug 2009
    Location
    Australia
    Posts
    9,829

    Default Re: Java program error - help!

    Cool that helps, thanks guys.
    Contact me on Steam: steamcommunity.com/id/IZob/ or send a PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •