Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,207,511 members, 7,999,280 topics. Date: Sunday, 10 November 2024 at 11:24 PM

Experienced Java Developer , Please Help ! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Experienced Java Developer , Please Help ! (1199 Views)

Java Developer Needed / Need A Web Developer? Please Call 09058718256 / 7 Ways To Earn Money As Java Developer (2) (3) (4)

(1) (Reply) (Go Down)

Experienced Java Developer , Please Help ! by shegzhkn: 12:17am On Dec 26, 2019
CLOSED
Re: Experienced Java Developer , Please Help ! by stanliwise(m): 12:46am On Dec 26, 2019
shegzhkn:
Hello folks , happy Christmas and to the experienced Java programmer here , i need a hand.

I wanted to build a Multiple Choice Question console app and i create two class a Question class and another with the execution point to run the app,
i didn't want to repeat a question that will be generated by random class but i am getting problem , below is the data for the app.

Question.java

public class Question {

private String prompt;
private String answer;

public Question(String question, String answer) {
this.prompt = question;
this.answer = answer;
}
public String getQuestion() {
return prompt;
}
public String getAnswer() {
return answer;
}
public String toString() {
return prompt;
}
}

And then TestQuestion.java

import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class TestQuestion {

private static Scanner input;

public static void main(String[] args) {

input = new Scanner(System.in);

String response = "y";

String q1 = "Who is the longest serving president of the United States ? \n(a) Ronald Reagan"
+ "\n(b) John Quincy Adam \n(c) Franklin Delano Roosevelt \n(d) Abraham Lincoln";
String q2 = "What is the capital of Saudi Arabia ? \n(a) Jeddah \n(b) Riyardh" + "\n(c) Dubai \n(d) Kabul";
String q3 = "Which of the country is not part of NATO ? \n(a) France\n(b) Turkey"
+ "\n(c) United States\n(d) North Korea";
String q4 = "Which of the following country is Europe economy powerhouse ?\n(a) France\n(b) Japan"
+ "\n(c) Germany\n(d) Norway";
String q5 = "Which of the following is largest e-commerce company ?\n(a) Amazon\n(b) JD"
+ "\n(c) Alibaba\n(d) eBay";
String q6 = "Who is the richest man in North America?\n(a) Jack Ma\n(b) Elon Musk"
+ "\n(c) Jeff Bezos\n(d) Bill Gates";
String q7 = "Which of the following is not part of UNSC?\n(a) Russia\n(b) United States"
+ "\n(c) South Korea\n(d) China";
String q8 = "Who is the wife of 44th POTUS ?\n(a) Michelle Obama\n(b) Melania Trump"
+ "\n(c) Laura Bush\n(d) Hillary Clinton";
String q9 = "Which of the following Nation has largest land mass ?\n(a) Qatar\n(b) Russia"
+ "\n(c) South Africa\n(d) Mexico";
String q10 = "Which of the following conglomerate has the highest Market Capitalization?\n(a) Amazon Inc\n(b) Alphabet Inc"
+ "\n(c) Berkshire Hathaway Inc\n(d) Apple Inc";
String q11 = "Which of the following currency is the most valued ?\n(a) US Dollar\n(b) Kuwaiti Dinar"
+ "\n(c) Pound Sterling\n(d) Euro";
String q12 = "Which year is Facebook founded ?\n(a) 1997\n(b) 1984" + "\n(c) 2004\n(d) 1975";
String q13 = "Which of the following conglomerate is the largest software maker\n(a) IBM systems\n(b) Alphabet Inc"
+ "\n(c) Adobe Systems\n(d) Microsoft Inc";
String q14 = "Which year is Microsoft founded ?\n(a) 1976\n(b) 1995" + "\n(c) 1975\n(d) 1999";
String q15 = "Which year is Amazon founded ?\n(a) 1999\n(b) 1995" + "\n(c) 1982\n(d) 1975";
String q16 = "Which year is Google founded ?\n(a) 1979\n(b) 1997" + "\n(c) 1970\n(d) 1980";
String q17 = "Which year did US became an independent nation ?\n(a) 1923\n(b) 1776" + "\n(c) 1770\n(d) 1875";
String q18 = "Which is the financial capital of Germany?\n(a) Berlin\n(b) Frankfurt"
+ "\n(c) Moskow\n(d) Paris";
String q19 = "Which state is 'Silicon Valley' in ?\n(a) Texas\n(b) California"
+ "\n(c) Rhodes Island\n(d) Virginia";
String q20 = "Which of this Company is Japan Auto Maker ?\n(a) Tesla\n(b) General Motors"
+ "\n(c) Toyota\n(d) Hyundai";

Question[] vault = { new Question(q1, "c"wink, new Question(q2, "b"wink, new Question(q3, "d"wink, new Question(q4, "c"wink,
new Question(q5, "a"wink, new Question(q6, "c"wink, new Question(q7, "c"wink, new Question(q8, "a"wink,
new Question(q9, "b"wink, new Question(q10, "d"wink, new Question(q11, "b"wink, new Question(q12, "c"wink,
new Question(q13, "d"wink, new Question(q14, "c"wink, new Question(q15, "a"wink, new Question(q16, "b"wink,
new Question(q17, "b"wink, new Question(q18, "b"wink, new Question(q19, "b"wink, new Question(q20, "c"wink };

while (response.equalsIgnoreCase("y"wink) {

takeTest(vault);

System.out.println("Wanna take test again (y/n)? "wink;

response = input.nextLine();

input.nextLine();
}

System.out.println("Thanks for taking the test, bye!"wink;
}

public static void takeTest(Question[] vault) {
int scores = 0;
String answer = "";
Question [] questions = new Question[10];
List<Integer> numbies = new ArrayList<>();
while (numbies.size() <= questions.length) {
int count = 0;
Random rand = new Random();
int ctr = rand.nextInt(vault.length);
if (!numbies.contains(ctr)) {
numbies.add(ctr);
questions[count] = vault[ctr];
count++;
}
}
for (int i = 0; i < questions.length; i++) {
System.out.println(questions[i]);
answer = input.nextLine();
if (answer.equalsIgnoreCase(questions[i].getAnswer()))
scores += 1;
}
System.out.println("You got " + scores + "/" + questions.length + " correctly"wink;

}

}

Please folks i need urgent response ASAP.
Thanks.
I didn't read your code sha. but once you randomize throught the questions, for each you have selected, just delete from the the array and stor it in another array.
List_of_questions
answered_questions.

use Arraylist data structure, it is more flexible.
Re: Experienced Java Developer , Please Help ! by crunchyDope(m): 12:49am On Dec 26, 2019
shegzhkn:

what error(s) are you getting?
Re: Experienced Java Developer , Please Help ! by shegzhkn: 1:06am On Dec 26, 2019
crunchyDope:

what error(s) are you getting?

Maybe you should run the code sire , at some point am getting null pointer exception , your input is appreciated sire.
Re: Experienced Java Developer , Please Help ! by shegzhkn: 1:07am On Dec 26, 2019
stanliwise:
I didn't read your code sha. but once you randomize throught the questions, for each you have selected, just delete from the the array and stor it in another array.
List_of_questions
answered_questions.

use Arraylist data structure, it is more flexible.

Lol, well we've said you didn't read the code, i did utilize it anyways and you might want to take a close look at it.
Re: Experienced Java Developer , Please Help ! by Nobody: 5:14am On Dec 26, 2019
Not readable, sorry I can't contribute.

Next time, add comments for easy reading.
Re: Experienced Java Developer , Please Help ! by shegzhkn: 8:17am On Dec 26, 2019
2kaybiel:
Not readable, sorry I can't contribute.

Next time, add comments for easy reading.

Goddammit , can i reach you through other medium , walai NL has long way to go - if nairaland had special code input blueprint we won't be having this conversation.
Re: Experienced Java Developer , Please Help ! by lincsnuel: 8:20am On Dec 26, 2019
shegzhkn:


Maybe you should run the code sire , at some point am getting null pointer exception , your input is appreciated sire.

Your problem lies here:

while (numbies.size() <= questions.length) {

Remove "<=" and use only "<" or use:
while (numbies.size() <= (questions.length -1)) {

Remember arrays start from 0, this implies that the first Question object is assigned the value 0. Since your arraylist is 10 (i.e 0-9), then numbies should be less than questions.length
Re: Experienced Java Developer , Please Help ! by lincsnuel: 8:30am On Dec 26, 2019
To avoid repetition, create a new ArrayList maybe, answerList and for any question answered, remove from questions and store in answerList. The Random object will then randomly pick from the ones in question not already answered. There's another way but try this one.
Re: Experienced Java Developer , Please Help ! by shegzhkn: 8:45am On Dec 26, 2019
lincsnuel:


Your problem lies here:

while (numbies.size() <= questions.length) {

Remove "<=" and use only "<" or use:
while (numbies.size() <= (questions.length -1) {

Remember arrays start from 0, this implies that the first Question object is assigned the value 0. Since your arraylist is 10 (i.e 0-9), then numbies should be less than questions.length

Thanks boss man , God bless.
Re: Experienced Java Developer , Please Help ! by crunchyDope(m): 8:47am On Dec 26, 2019
shegzhkn:
Hello folks , happy Christmas and to the experienced Java programmer here , i need a hand.

I wanted to build a Multiple Choice Question console app and i create two class a Question class and another with the execution point to run the app,
i didn't want to repeat a question that will be generated by random class but i am getting problem , below is the data for the app.

Question.java

public class Question {

private String prompt;
private String answer;

public Question(String question, String answer) {
this.prompt = question;
this.answer = answer;
}
public String getQuestion() {
return prompt;
}
public String getAnswer() {
return answer;
}
public String toString() {
return prompt;
}
}

And then TestQuestion.java

import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class TestQuestion {

private static Scanner input;

public static void main(String[] args) {

input = new Scanner(System.in);

String response = "y";

String q1 = "Who is the longest serving president of the United States ? \n(a) Ronald Reagan"
+ "\n(b) John Quincy Adam \n(c) Franklin Delano Roosevelt \n(d) Abraham Lincoln";
String q2 = "What is the capital of Saudi Arabia ? \n(a) Jeddah \n(b) Riyardh" + "\n(c) Dubai \n(d) Kabul";
String q3 = "Which of the country is not part of NATO ? \n(a) France\n(b) Turkey"
+ "\n(c) United States\n(d) North Korea";
String q4 = "Which of the following country is Europe economy powerhouse ?\n(a) France\n(b) Japan"
+ "\n(c) Germany\n(d) Norway";
String q5 = "Which of the following is largest e-commerce company ?\n(a) Amazon\n(b) JD"
+ "\n(c) Alibaba\n(d) eBay";
String q6 = "Who is the richest man in North America?\n(a) Jack Ma\n(b) Elon Musk"
+ "\n(c) Jeff Bezos\n(d) Bill Gates";
String q7 = "Which of the following is not part of UNSC?\n(a) Russia\n(b) United States"
+ "\n(c) South Korea\n(d) China";
String q8 = "Who is the wife of 44th POTUS ?\n(a) Michelle Obama\n(b) Melania Trump"
+ "\n(c) Laura Bush\n(d) Hillary Clinton";
String q9 = "Which of the following Nation has largest land mass ?\n(a) Qatar\n(b) Russia"
+ "\n(c) South Africa\n(d) Mexico";
String q10 = "Which of the following conglomerate has the highest Market Capitalization?\n(a) Amazon Inc\n(b) Alphabet Inc"
+ "\n(c) Berkshire Hathaway Inc\n(d) Apple Inc";
String q11 = "Which of the following currency is the most valued ?\n(a) US Dollar\n(b) Kuwaiti Dinar"
+ "\n(c) Pound Sterling\n(d) Euro";
String q12 = "Which year is Facebook founded ?\n(a) 1997\n(b) 1984" + "\n(c) 2004\n(d) 1975";
String q13 = "Which of the following conglomerate is the largest software maker\n(a) IBM systems\n(b) Alphabet Inc"
+ "\n(c) Adobe Systems\n(d) Microsoft Inc";
String q14 = "Which year is Microsoft founded ?\n(a) 1976\n(b) 1995" + "\n(c) 1975\n(d) 1999";
String q15 = "Which year is Amazon founded ?\n(a) 1999\n(b) 1995" + "\n(c) 1982\n(d) 1975";
String q16 = "Which year is Google founded ?\n(a) 1979\n(b) 1997" + "\n(c) 1970\n(d) 1980";
String q17 = "Which year did US became an independent nation ?\n(a) 1923\n(b) 1776" + "\n(c) 1770\n(d) 1875";
String q18 = "Which is the financial capital of Germany?\n(a) Berlin\n(b) Frankfurt"
+ "\n(c) Moskow\n(d) Paris";
String q19 = "Which state is 'Silicon Valley' in ?\n(a) Texas\n(b) California"
+ "\n(c) Rhodes Island\n(d) Virginia";
String q20 = "Which of this Company is Japan Auto Maker ?\n(a) Tesla\n(b) General Motors"
+ "\n(c) Toyota\n(d) Hyundai";

Question[] vault = { new Question(q1, "c"wink, new Question(q2, "b"wink, new Question(q3, "d"wink, new Question(q4, "c"wink,
new Question(q5, "a"wink, new Question(q6, "c"wink, new Question(q7, "c"wink, new Question(q8, "a"wink,
new Question(q9, "b"wink, new Question(q10, "d"wink, new Question(q11, "b"wink, new Question(q12, "c"wink,
new Question(q13, "d"wink, new Question(q14, "c"wink, new Question(q15, "a"wink, new Question(q16, "b"wink,
new Question(q17, "b"wink, new Question(q18, "b"wink, new Question(q19, "b"wink, new Question(q20, "c"wink };

while (response.equalsIgnoreCase("y"wink) {

takeTest(vault);

System.out.println("Wanna take test again (y/n)? "wink;

response = input.nextLine();

input.nextLine();
}

System.out.println("Thanks for taking the test, bye!"wink;
}

public static void takeTest(Question[] vault) {
int scores = 0;
String answer = "";
Question [] questions = new Question[10];
List<Integer> numbies = new ArrayList<>();
while (numbies.size() <= questions.length) {
int count = 0;
Random rand = new Random();
int ctr = rand.nextInt(vault.length);
if (!numbies.contains(ctr)) {
numbies.add(ctr);
questions[count] = vault[ctr];
count++;
}
}
for (int i = 0; i < questions.length; i++) {
System.out.println(questions[i]);
answer = input.nextLine();
if (answer.equalsIgnoreCase(questions[i].getAnswer()))
scores += 1;
}
System.out.println("You got " + scores + "/" + questions.length + " correctly"wink;

}

}

Please folks i need urgent response ASAP.
Thanks.
just ran your code

the bug is in the "take test" method

you initialized your random questions to 10 but when you create a random question you save it to just the first index (0) because you keep initializing "count=0" over again

solution
place "int count = 0 " outside the iteration

goodluck
Re: Experienced Java Developer , Please Help ! by shegzhkn: 8:50am On Dec 26, 2019
crunchyDope:

just ran your code

the bug is in the "take test" method

you initialized your random questions to 10 but when you create a random question you save it to just the first index (0) because you keep initializing "count=0" over again

solution
place "int count = 0 " outside the iteration

goodluck

Thanks boss , i really appreciate.

Coding can be really crazy , after quite sometimes of drinking coffee(Java) i still made such a simple error - keep giving count value of zero after every iteration and it never crossed my mind after several attempt to debug .

Once again thanks.
Re: Experienced Java Developer , Please Help ! by stanliwise(m): 11:36am On Dec 26, 2019
shegzhkn:


Thanks boss , i really appreciate.

Coding can be really crazy , after quite sometimes of drinking coffee(Java) i still made such a simple error - keep giving count value of zero after every iteration and it never crossed my mind after several attempt to debug .

Once again thanks.

Next time you paste coding problems, please place the error messages.
Not everyone has time to run your codes especially when it has dependencies or compatibility requirements.
Re: Experienced Java Developer , Please Help ! by shegzhkn: 11:45am On Dec 26, 2019
stanliwise:

Next time you paste coding problems, please place the error messages.
Not everyone has time to run your codes especially when it has dependencies or compatibility requirements.

Affirmative sire, thanks.
Re: Experienced Java Developer , Please Help ! by crunchyDope(m): 4:44pm On Dec 26, 2019
shegzhkn:


Thanks boss , i really appreciate.

Coding can be really crazy , after quite sometimes of drinking coffee(Java) i still made such a simple error - keep giving count value of zero after every iteration and it never crossed my mind after several attempt to debug .

Once again thanks.

it happens to everyone

(1) (Reply)

Learn With Any Udemy Course / Which IT Course Can I Study Without Disrupting My Banking Career. / See Laptop Wey Person Wan Sell For 160k On Nairaland(pics)

Viewing this topic: 1 guest(s)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 53
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.