Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm making a quiz program. First step, I'm taking the quiz questions and their correct answers from the teacher/ user. I have a child class named TrueFalseQuestion which takes the boolean model answer and the string question as a parameter. I've made an array of TrueFalseQuestion type and I'm stuck at this part where I run the code, insert a question and no matter whether the model answer I've inserted is true or false, it's always stored as false when I print it out. HELP?
Here's this part of my code:
System.out.println("How many true or false questions would you like to include in your quiz?");
int l=s.nextInt();
TrueFalseQuestion[] qu2= new TrueFalseQuestion[l];
int x;
for(x=0;x<l;x++){
System.out.println("Please insert question "+(x+1)+":\n");
String Q2=s.next();
System.out.println("Please insert the correct answer");
boolean A2=s.nextBoolean();
qu2[x]=new TrueFalseQuestion(Q2,A2);
System.out.println(qu2[x].GetCorrectAnswer());
}
EDIT: Here's the TrueFalseQuestion code
public class TrueFalseQuestion extends Question {
private boolean CorrectB;
public TrueFalseQuestion(String qu, boolean b){
super(qu);
}
#Override
public void GetQuestion() {
System.out.println(getMyText()+"\n Is this statement true or false?");
}
#Override public String GetAnswer() {
System.out.println("Insert Answer: ");
boolean MyAnswer=s.nextBoolean();
return Boolean.toString(MyAnswer);
}
#Override public String CheckAnswer() {
return Boolean.toString(GetAnswer().equalsIgnoreCase(Boolean.toString(GetCorrectAnswer())));
}
/** * return the MyAnswer / /* * return the CorrectAnswer / public boolean GetCorrectAnswer() { return CorrectB; } /* * return the MyAnswer */
}
your problem is your constructor:
public TrueFalseQuestion(String qu, boolean b){
super(qu);
}
You don't set the field CorrectB so it defaults to its default value which in java is false
the fix is
public TrueFalseQuestion(String qu, boolean b){
super(qu);
CorrectB = b;
}
Problem is nextInt() leaves "\n" in the stream and when next() executes in
String Q2=s.next();
It reads "\n" and doesn't gives you an opportunity to input Question.
Try this:
System.out.println("How many true or false questions would you like to include in your quiz?");
int l=Integer.parseInt(nextLine());
TrueFalseQuestion[] qu2= new TrueFalseQuestion[l];
int x;
for(x=0;x<l;x++){
System.out.println("Please insert question "+(x+1)+":\n");
String Q2=s.nextLine();
System.out.println("Please insert the correct answer");
boolean A2=s.nextBoolean();
qu2[x]=new TrueFalseQuestion(Q2,A2);
System.out.println(qu2[x].GetCorrectAnswer());
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
so sorry if my question is dumb but i haven't done java in 2 years. I'm working in my hw right now and i'm one method away from being done. but one of my constructor is wrong and idk where. thanks in advance!
the value variable is a private integer.
so far i have:
public FancyInt(String a) {
value = "";
a = this.value;
}
basically this constructor takes a string and initalizes the value to the appropriate int
Firstly if you want to equal "value" TO "a", you must write:
this.value = a;
in Java (and a lot of programming languages), you must write variable that you change before equal mark.
Second, if you will make "value" Integer. You must change it to int first:
try {
this.value = Integer.parseInt(a);
} catch (NumberFormatException e) {
e.printStackTrace();
}
Third, if "value" is an Integer. You must define that with numbers:
value = ""; //if value is an Integer, delete this line.
If you say that the variable "value" is a Integer, then your code cannot compile because you are assigning a String (in this case, an empty string "") to a Integer.
According to your last sentence, I think your code should look like this:
public FancyInt(String a) {
this.value = isInteger(a)? Integer.parseInt(a) : 0;
}
private boolean isInteger(String val) {
try{
Integer.parseInt(val);
return true;
}
catch (NumberFormatException e) {
return false;
}
}
You can change the implementation of the isInteger method, in this link Determine if a String is an Integer in Java you have multiple options that could help you.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
public LinkedList<MazeCoord> getPath() {
return getPath();
}
public class MazeCoord {
final private int row; // final (non-static) means it can't be updated once
final private int col; // it's initialized
// create a MazeCoord with the given row and column
public MazeCoord(int row, int col) {
this.row = row;
this.col = col;
}
// get the row of this MazeCoord
public int getRow() { return row; }
// get the col of this MazeCoord;
public int getCol() { return col; }
}
Why when I try to run this in my Eclipse, the console prompts me stack over flow?
Can any one tell me the reason?
getPath() is calling itself, which causes an infinite chain of calls, that ends when the stack overflows. A correct recursive method must have a stopping condition.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Writing this code, I'm getting 'unreachable statement' error when I try to compile, any time I try to reach a[x] in the recursive method.
public class recursion
{
public static boolean match (int [] a, int [] pattern)
{
if(pattern.length==0)
return true;
boolean x;
x=match(a,pattern,0,0);
if(x==true)
return true;
return false;
}
public static boolean match (int [] a, int [] pattern,int aCounter,int ptCounter)
{
int count=0;
int x=aCounter;
if(x==a.length);
{
if(count==pattern.length)
return true;
else return false;
}
if(a[x]>100)
{
count=0;
return match(a,pattern,aCounter+1,0);
}
else if(((pattern[ptCounter]==1)||(pattern[ptCounter]==0))&&((a[x]>-10)&&(a[x]<10)))
{
count++;
return match(a,pattern,aCounter+1,ptCounter+1);
}
else if(((pattern[ptCounter]==2)||(pattern[ptCounter]==0))&&(((a[x]<-10)&&(a[x]>-100))||((a[x]>9)&&(a[x]<100))))
{
count++;
return match(a,pattern,aCounter+1,ptCounter+1);
}
}
}
Would appreciate input regarding this issue and also about the calling of recursive method. Thank you!
Your problem is an unnecessary ; :
if(x==a.length); // here
{
if(count==pattern.length)
return true;
else return false;
}
This ; closes the if statement, so the following bock is always executed (and returns true or false), and the code after that block becomes unreachable.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
This is my example code to test HashSet.
I expect that the result is [3K,1K] but the this code results in [1K,3K,3K]
Could you let me know why the code not call equals?
import java.util.HashSet;
class SutdaCard{
private int num;
private boolean isKwang;
SutdaCard(){
this(1,true);
}
SutdaCard(int num, boolean isKwang){
this.num = num;
this.isKwang = isKwang;
}
public String toString(){
return num+(isKwang ? "K":"");
}
public boolean equals(Object obj){
String compareValue = obj.toString();
String thisValue = toString();
System.out.println("equals");
return thisValue.equals(compareValue);
}
public int hashcode(){
return toString().hashCode();
}
}
class exercise11_11 {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<SutdaCard> set = new HashSet<SutdaCard>();
set.add(new SutdaCard(3,true));
set.add(new SutdaCard(3,true));
set.add(new SutdaCard(1,true));
System.out.println(set);
}
}
You haven't overridden hashCode() properly. Try:
#Override
public int hashCode() {
return this.toString().hashCode();
}
Since the Set uses the hashCode() from Object in your code, the two SutdaCards hash codes won't match, and equals() is never called.
If you add the #Override annotation, the compiler will check that you are actually overriding something, and generate a warning if you have a spelling error.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Why don't I get 20 after delivering e.g. "Obsidian" to Test(String pStr) if I call getMatInt() ?
Also tried .toString() after all String-declariations, also declarated e.g. "Obsidian" as new String a. Nothing works.
getBonus is aways returning a 0 instead of a 20/30/... .
I already tried "Obsidian" and "obsidian", both doesnt work for me ...
public class test
{
private String str;
private int matInt;
private int bonus;
private int magic;
public test(int pMagic, String pStr)
{
int magic = pMagic;
str = pStr;
}
private void materialEquals()
{
if(str.equals("Obsidian"))
{
matInt = 20;
}
.....
}
private void calcBonus()
{
materialEquals();
bonus = magic * matInt;
}
public int getBonus()
{
calcBonus();
return bonus;
}
}
try:
public int getMatInt()
{
materialEquals();
return matInt;
}
There is no reason for this in your constructor: str = new String(pStr);, just use str = pStr.
In fact, you might be better off setting matInt in your constructor:
public test(String pStr)
{
str = pStr;
materialEquals();
}
And depending on how many materials you have, you might want to look into using enumeration.
Ok after your edits:
public int getBonus()
{
calcBonus(); //bonus won't be calculated otherwise
return bonus;
}
After further edits:
Your constructor is wrong, you're not initializing int magic. Try this constructor instead.
public test(int pMagic, String pStr)
{
this.magic = pMagic; //int magic = pMagic was a new variable only in the constructor scope
this.str = pStr;
calcBonus();
}
Also you might as well calculate the bonus on construction.
You will need to call materialEquals() so that 20 can be assigned to matInt upon equals comparison, as integers are always initialized to default value 0 upon declaration.