creating a class in java

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1464 Views - Last Post: 14 February 2012 - 07:00 PM Rate Topic: -----

#1 cstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 11

creating a class in java

Posted 13 February 2012 - 05:22 PM

I need to create a class in java for an exam question. I am not understanding what I am doing wrong with my constructor.
package exam;

/**
 *
 * @author stephens0728
 */
import java.util.*;
import java.lang.String;
 public class Exam {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      
       
       String q1, q2;
       q1 = new Question("What state do you live in?","A. Oregon", "B. California", "C. Montana", "D. Georgia", "A");
       q1= get.Answer("");
    }
}

THIS IS MY CLASS

public class Question {
    //Data Member
    private String testQuestion, A, B, C, D, cA;
  
    //constructor
    public Question(String testQuestion, String A, String B, String C, String D, String cA){
      this.testQuestion = testQuestion;
      this.A = A;
      this.B = B;
      this.C = C;
      this.D = D;
      this.cA = cA;
      
    }
    public String getAnswer(){
        return Answer;
        
        
    }
    
}

I am not declaring something right.
Thank you

This post has been edited by smohd: 13 February 2012 - 08:37 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: creating a class in java

#2 jimmyo88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 147
  • Joined: 27-February 11

Re: creating a class in java

Posted 13 February 2012 - 05:31 PM

put your code in

brackets please. It makes it much easier to read.

One thing i noticed. Your getAnswer() method returns a value called Answer.

You have not declared a variable called Answer anywhere.
Was This Post Helpful? 1
  • +
  • -

#3 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: creating a class in java

Posted 13 February 2012 - 05:34 PM

There are several things wrong here. The first thing is that q1 has been declared as a String. You are trying to assign a Question to it. A Question is not a String so this won't work. You can instead declare q1 as a Question, and then this assignment will work. If you need a String value from a question, considering overriding the toString() method from the Object class in your question class.

Another thing I noticed is that you are returning Answer in your getAnswer() method, but I don't see the identifier 'Answer' declared anywhere.
Was This Post Helpful? 1
  • +
  • -

#4 cstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 11

Re: creating a class in java

Posted 13 February 2012 - 05:37 PM

View Postjimmyo88, on 13 February 2012 - 05:31 PM, said:

put your code in

brackets please. It makes it much easier to read.

One thing i noticed. Your getAnswer() method returns a value called Answer.

You have not declared a variable called Answer anywhere.

I fixed that, but my problem is that for some reason new Question is not recognized and is underlined and I dont know how to fix it.
Was This Post Helpful? 0
  • +
  • -

#5 cstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 11

Re: creating a class in java

Posted 13 February 2012 - 05:43 PM

View PostSheph, on 13 February 2012 - 05:34 PM, said:

There are several things wrong here. The first thing is that q1 has been declared as a String. You are trying to assign a Question to it. A Question is not a String so this won't work. You can instead declare q1 as a Question, and then this assignment will work. If you need a String value from a question, considering overriding the toString() method from the Object class in your question class.

Another thing I noticed is that you are returning Answer in your getAnswer() method, but I don't see the identifier 'Answer' declared anywhere.

Can you please give me an example of declaring the q1 as a Question, I understand but not sure how to do it. I am very new at this.
Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: creating a class in java

Posted 13 February 2012 - 05:50 PM

Question q1; // declare
q1 = new Question("What state do you live in?","A. Oregon", "B. California", "C. Montana", "D. Georgia", "A"); //initialize

// or both at the same time

Question q1 = new Question("What state do you live in?","A. Oregon", "B. California", "C. Montana", "D. Georgia", "A");

This post has been edited by Sheph: 13 February 2012 - 05:50 PM

Was This Post Helpful? 1
  • +
  • -

#7 cstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 11

Re: creating a class in java

Posted 13 February 2012 - 08:04 PM

Thank you your advice worked. Now if I want q1 to appear as I put it in the constructor do I use System.out.println(q1)?
Was This Post Helpful? 0
  • +
  • -

#8 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: creating a class in java

Posted 13 February 2012 - 08:09 PM

Assuming you have an Answer variable in the Question class, use
System.out.println(q1.getAnswer());

Was This Post Helpful? 1
  • +
  • -

#9 cstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 11

Re: creating a class in java

Posted 13 February 2012 - 08:15 PM

 public String getAnswer(){
        return Answer;
this is what I have. Is this right?
Was This Post Helpful? 0
  • +
  • -

#10 cstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 11

Re: creating a class in java

Posted 13 February 2012 - 08:20 PM

class Question {
     //Data Member
    Question q1;
    private String testQuestion, A, B, C, D, cA, Answer;
  
    //constructor
    public Question(String testQuestion, String A, String B, String C, String D, String cA){
      this.testQuestion = testQuestion;
      this.A = A;
      this.B = B;
      this.C = C;
      this.D = D;
      this.cA = cA;
      
    }
    public String getAnswer(){
        return Answer;
        
        
    }
    
} 


import java.util.*;
import java.lang.String;
 public class Exam {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      
       
       Question q1;
       Question q2;
      q1 = new Question("What state do you live in?","A. Oregon", "B. California", "C. Montana", "D. Georgia", "A");
      q2 = new Question("Where would you like to live", "A. Oregon", "B. California", "C. Montana", "D. Georgia","A");
      
      //get user answer
      System.out.println(q1.getAnswer());
// It comes back null. I need it to display the question with the options, like a multiple choice.
Was This Post Helpful? 0
  • +
  • -

#11 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: creating a class in java

Posted 13 February 2012 - 08:25 PM

View Postcstephens, on 13 February 2012 - 07:15 PM, said:

 public String getAnswer(){
        return Answer;
this is what I have. Is this right?

That is correct, but I do not see the variable Answer in your Question class.

This may make it easier:
class Question {
     //Data Member
    Question q1;
    private String testQuestion, A, B, C, D, cA, Answer;
  
    //constructor
    public Question(String testQuestion, String A, String B, String C, String D, String cA){
      this.testQuestion = testQuestion;
      this.A = A;
      this.B = B;
      this.C = C;
      this.D = D;
      this.cA = cA;
      
    }
    public String getAnswer(){
        System.out.println("Please enter your answer: ");
        this.Answer = (new Scanner(System.in).readLine());

        return this.Answer;
        
        
    }
    
} 




Was This Post Helpful? 1
  • +
  • -

#12 cstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 11

Re: creating a class in java

Posted 13 February 2012 - 08:26 PM

I think I have one now. I posted all of my code that I have now.
Was This Post Helpful? 0
  • +
  • -

#13 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: creating a class in java

Posted 13 February 2012 - 08:31 PM

And if you want to display the question, why not use System.out.println() and print out each variable in the class? Like:
System.out.println(q1.getQuestion());
System.out.println(q1.getA());
System.out.println(q1.getB());
System.out.println(q1.getC());
System.out.println(q1.getD());



But then you would need to add this to the question class:
class Question {
     //Data Member
    Question q1;
    private String testQuestion, A, B, C, D, cA, Answer;
  
    //constructor
    public Question(String testQuestion, String A, String B, String C, String D, String cA){
      this.testQuestion = testQuestion;
      this.A = A;
      this.B = B;
      this.C = C;
      this.D = D;
      this.cA = cA;
      
    }
    public String getAnswer(){
        System.out.println("Please enter your answer: ");
        this.Answer = (new Scanner(System.in).readLine());

        return this.Answer;
        
        
    }

    public String getQuestion() {
        return testQuestion;
    }

    public String getA() {
        return A;
    }

    public String getB() {
        return B;
    }

    public String getC() {
        return C;
    }

    public String getD() {
        return D;
    }
    
} 






Was This Post Helpful? 1
  • +
  • -

#14 cstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-October 11

Re: creating a class in java

Posted 13 February 2012 - 08:37 PM

So If I do it like that, do I need to make methods in the class for each one of those gets?
Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: creating a class in java

Posted 13 February 2012 - 08:46 PM

Or you can simply just override the toString() method and make all the question to be displayed like:
public String toString(){
   String st = testQuestion + "\n" + A + "\n" + B + "\n" + C + "\n" + D;
   return st;
}

Then you will just have to say:
System.out.println(q1);


This will simplify the life of having a lot of getters here
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2