0% found this document useful (0 votes)
13 views

Q6 Quizapp

Uploaded by

nikhil patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Q6 Quizapp

Uploaded by

nikhil patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Q6. Create an Android application, which show to the user 5-10 quiz questions.

All
questions have 4 possible options and one right option exactly. Application counts
and shows to the user how many answers were right and shows the result to user.

//QuizActivity

package com.example.pract_6mark2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class QuizActivity extends AppCompatActivity {


private TextView questionTextView;
private RadioButton option1, option2, option3, option4;
private Button submitButton;

private int currentQuestionIndex = 0;


private int correctAnswers = 0;

private QuizQuestion[] questions = {


new QuizQuestion("Q1. What is the capital of France?",
new String[]{"London", "Paris", "Rome", "Berlin"}, 1),
new QuizQuestion("Q2. Who wrote 'Romeo and Juliet'?",
new String[]{"William Shakespeare", "Charles Dickens", "Mark Twain",
"Jane Austen"}, 0),
new QuizQuestion("Q3. What is the powerhouse of the cell?",
new String[]{"Nucleus", "Ribosome", "Mitochondrion", "Endoplasmic
reticulum"}, 2),
new QuizQuestion("Q4. Which planet is known as the Red Planet?",
new String[]{"Jupiter", "Venus", "Mars", "Saturn"}, 2),
new QuizQuestion("Q5. What is the chemical symbol for water?",
new String[]{"H2O", "CO2", "O2", "H2SO4"}, 0)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
questionTextView = findViewById(R.id.questionTextView);
option1 = findViewById(R.id.option1);
option2 = findViewById(R.id.option2);
option3 = findViewById(R.id.option3);
option4 = findViewById(R.id.option4);
submitButton = findViewById(R.id.submitButton);
showQuestion();
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer();
}
});
}

private void showQuestion() {


QuizQuestion currentQuestion = questions[currentQuestionIndex];
questionTextView.setText(currentQuestion.getQuestion());
String[] options = currentQuestion.getOptions();
option1.setText(options[0]);
option2.setText(options[1]);
option3.setText(options[2]);
option4.setText(options[3]);
}
private void checkAnswer() {
QuizQuestion currentQuestion = questions[currentQuestionIndex];
int selectedOptionIndex = -1;

if (option1.isChecked()) {
selectedOptionIndex = 0;
} else if (option2.isChecked()) {
selectedOptionIndex = 1;
} else if (option3.isChecked()) {
selectedOptionIndex = 2;
} else if (option4.isChecked()) {
selectedOptionIndex = 3;
}

if (selectedOptionIndex == currentQuestion.getCorrectOptionIndex()) {
correctAnswers++;
}

if (currentQuestionIndex < questions.length - 1) {


currentQuestionIndex++;
showQuestion();
} else {
showResult();
}
}
private void showResult() {
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
intent.putExtra("CORRECT_ANSWERS", correctAnswers);
intent.putExtra("TOTAL_QUESTIONS", questions.length);
startActivity(intent);
finish();
}
}

//QuizQuestion

package com.example.pract_6mark2;
public class QuizQuestion {
private String question;
private String[] options;
private int correctOptionIndex;

public QuizQuestion(String question, String[] options, int correctOptionIndex) {


this.question = question;
this.options = options;
this.correctOptionIndex = correctOptionIndex;
}

public String getQuestion() {


return question;
}

public String[] getOptions() {


return options;
}

public int getCorrectOptionIndex() {


return correctOptionIndex;
}
}

//ResultActivity
package com.example.pract_6mark2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends AppCompatActivity {


private TextView resultTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);

resultTextView = findViewById(R.id.resultTextView);

int correctAnswers = getIntent().getIntExtra("CORRECT_ANSWERS", 0);


int totalQuestions = getIntent().getIntExtra("TOTAL_QUESTIONS", 0);

resultTextView.setText("You got " + correctAnswers + " out of " +


totalQuestions + " questions correct.");
}
}

//SplashActivity

package com.example.pract_6mark2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashActivity extends AppCompatActivity {

private static final int SPLASH_DISPLAY_LENGTH = 2000; // Splash screen in


milliseconds

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

new Handler().postDelayed(new Runnable() {


@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, QuizActivity.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}

You might also like