Java quiz game ⌛

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 เม.ย. 2020
  • Java how to make a multiple choice quiz/test in Java tutorial
    #Java #quiz #tutorial #game #beginner
    Coding boot camps hate him! See how he can teach you to code with this one simple trick...
    Bro Code is the self-proclaimed #1 tutorial series on coding in various programming languages and other how to videos in the known universe.
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 209

  • @BroCodez
    @BroCodez  4 ปีที่แล้ว +123

    // ---------------------------- Main ----------------------------
    public class Main {
    public static void main(String[] args) {

    Quiz quiz = new Quiz();
    }
    }
    // ---------------------------- Quiz ----------------------------
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class Quiz implements ActionListener{

    String[] questions = {
    "Which company created Java?",
    "Which year was Java created?",
    "What was Java originally called?",
    "Who is credited with creating Java?"
    };
    String[][] options = {
    {"Sun Microsystems","Starbucks","Microsoft","Alphabet"},
    {"1989","1996","1972","1492"},
    {"Apple","Latte","Oak","Koffing"},
    {"Steve Jobs","Bill Gates","James Gosling","Mark Zuckerburg"}
    };
    char[] answers = {
    'A',
    'B',
    'C',
    'C'
    };
    char guess;
    char answer;
    int index;
    int correct_guesses =0;
    int total_questions = questions.length;
    int result;
    int seconds=10;

    JFrame frame = new JFrame();
    JTextField textfield = new JTextField();
    JTextArea textarea = new JTextArea();
    JButton buttonA = new JButton();
    JButton buttonB = new JButton();
    JButton buttonC = new JButton();
    JButton buttonD = new JButton();
    JLabel answer_labelA = new JLabel();
    JLabel answer_labelB = new JLabel();
    JLabel answer_labelC = new JLabel();
    JLabel answer_labelD = new JLabel();
    JLabel time_label = new JLabel();
    JLabel seconds_left = new JLabel();
    JTextField number_right = new JTextField();
    JTextField percentage = new JTextField();

    Timer timer = new Timer(1000, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
    seconds--;
    seconds_left.setText(String.valueOf(seconds));
    if(seconds=total_questions) {
    results();
    }
    else {
    textfield.setText("Question "+(index+1));
    textarea.setText(questions[index]);
    answer_labelA.setText(options[index][0]);
    answer_labelB.setText(options[index][1]);
    answer_labelC.setText(options[index][2]);
    answer_labelD.setText(options[index][3]);
    timer.start();
    }
    }
    @Override
    public void actionPerformed(ActionEvent e) {

    buttonA.setEnabled(false);
    buttonB.setEnabled(false);
    buttonC.setEnabled(false);
    buttonD.setEnabled(false);

    if(e.getSource()==buttonA) {
    answer= 'A';
    if(answer == answers[index]) {
    correct_guesses++;
    }
    }
    if(e.getSource()==buttonB) {
    answer= 'B';
    if(answer == answers[index]) {
    correct_guesses++;
    }
    }
    if(e.getSource()==buttonC) {
    answer= 'C';
    if(answer == answers[index]) {
    correct_guesses++;
    }
    }
    if(e.getSource()==buttonD) {
    answer= 'D';
    if(answer == answers[index]) {
    correct_guesses++;
    }
    }
    displayAnswer();
    }
    public void displayAnswer() {

    timer.stop();

    buttonA.setEnabled(false);
    buttonB.setEnabled(false);
    buttonC.setEnabled(false);
    buttonD.setEnabled(false);

    if(answers[index] != 'A')
    answer_labelA.setForeground(new Color(255,0,0));
    if(answers[index] != 'B')
    answer_labelB.setForeground(new Color(255,0,0));
    if(answers[index] != 'C')
    answer_labelC.setForeground(new Color(255,0,0));
    if(answers[index] != 'D')
    answer_labelD.setForeground(new Color(255,0,0));

    Timer pause = new Timer(2000, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

    answer_labelA.setForeground(new Color(25,255,0));
    answer_labelB.setForeground(new Color(25,255,0));
    answer_labelC.setForeground(new Color(25,255,0));
    answer_labelD.setForeground(new Color(25,255,0));

    answer = ' ';
    seconds=10;
    seconds_left.setText(String.valueOf(seconds));
    buttonA.setEnabled(true);
    buttonB.setEnabled(true);
    buttonC.setEnabled(true);
    buttonD.setEnabled(true);
    index++;
    nextQuestion();
    }
    });
    pause.setRepeats(false);
    pause.start();
    }
    public void results(){

    buttonA.setEnabled(false);
    buttonB.setEnabled(false);
    buttonC.setEnabled(false);
    buttonD.setEnabled(false);

    result = (int)((correct_guesses/(double)total_questions)*100);

    textfield.setText("RESULTS!");
    textarea.setText("");
    answer_labelA.setText("");
    answer_labelB.setText("");
    answer_labelC.setText("");
    answer_labelD.setText("");

    number_right.setText("("+correct_guesses+"/"+total_questions+")");
    percentage.setText(result+"%");

    frame.add(number_right);
    frame.add(percentage);

    }
    }

    • @AshishArjun
      @AshishArjun 3 ปีที่แล้ว +4

      Bro u r just awesome... I love ur content 🤙🏻🤙🏻🤙🏻🤙🏻

    • @jaylordjl6337
      @jaylordjl6337 3 ปีที่แล้ว +1

      Really love your content bro, more more videos Thankyou

    • @ANGEL_GFREENS
      @ANGEL_GFREENS 3 ปีที่แล้ว

      LOVE U BRO CODEEEE MILLIONS THANKS FOR THIS

    • @eesha.paruchuri8878
      @eesha.paruchuri8878 2 ปีที่แล้ว

      Brooo i need the class diagram for this code . Please 🙏

    • @_Anna_Nass_
      @_Anna_Nass_ 2 ปีที่แล้ว

      We love you, bro ❤️

  • @jcarc5701
    @jcarc5701 3 ปีที่แล้ว +38

    Hey mate, this is the most valuable tutorial on Java. The reason most people give up on coding is they never start because they want to understand more before starting.
    This is very straight forward and allows to actually start coding and before you realize it you are actually a coder.

  • @TheTechdude88
    @TheTechdude88 3 ปีที่แล้ว +9

    easily one of the most underrated coding channels, i hope you make it big one day :)

  • @VikramKumar1111.
    @VikramKumar1111. 3 ปีที่แล้ว +21

    Thank sir for giving us the quality education for free of cost 🥺
    And the video is awesome😊

  • @stephenlai5102
    @stephenlai5102 3 ปีที่แล้ว +2

    Dude, ur tutorials are what I am really looking for, ez for understanding , ez to follow! Best Tutorial for beginners!

  • @amarshgautam636
    @amarshgautam636 3 ปีที่แล้ว +21

    I made this for my Univerity project , nice project and teaching ideas

    • @zainulabideen1511
      @zainulabideen1511 2 ปีที่แล้ว +1

      Brother I am also thinking about it plz can you send your source code

  • @mr_robert1723
    @mr_robert1723 3 ปีที่แล้ว +1

    Keep sharing these wonderful works
    I'll always support you🌹🌹

  • @adityabasanti4415
    @adityabasanti4415 2 ปีที่แล้ว +2

    This video explains all concept of swing. Thanks for making this amazing video. I understand a lot👍👍

  • @rjmp7324
    @rjmp7324 2 ปีที่แล้ว +2

    Bro, your projects are really helping me understand Java. TYVM.

  • @joffwillardvaldez1596
    @joffwillardvaldez1596 3 ปีที่แล้ว +2

    best channel so far. Thanks mate! helps us a lot!

  • @FukSN
    @FukSN 2 ปีที่แล้ว +9

    Great tutorial.
    I am excited to take away this foundation and start adding to it.
    Thank you Bro. *HighFive*
    Easy to do additions to the code for practice (Bro Code vids will explain how to do it, they are already on the channel):-
    Can add a welcome page and start button.
    Change the icon in the top left to your own.
    Add some images (if you want to posh it up).
    Change the layout (if your not a fan of this original).
    Remove almost all the buttons and labels (of not results and percentage and title label) when the quiz finishes to make it a little more tidy.
    Options are endless. Get practicing,...after you leave a like and comment of course :D

  • @superTramp572
    @superTramp572 ปีที่แล้ว

    The very best java tutorial channel on YT..ok maybe in World 🤔
    Thank you for all the effort you put in your videos!:)

  • @Re9sTwitch
    @Re9sTwitch 3 ปีที่แล้ว +3

    You Sir, are a genius! That was great, thank you.

  • @WretchedByNature
    @WretchedByNature ปีที่แล้ว +1

    This was awesome! I think I am going to try to take this and run with it, and expand it to really follow the rules of OOP, and encapsulate everything into the proper classes and methods. This is a great starting place!

  • @Quest-Giver
    @Quest-Giver ปีที่แล้ว

    Hey man, I'm just jumping back into programming and I'm really enjoying messing around with this and refactoring it to try and make a real game out of it, thanks for posting this!

  • @stephanieezat-panah7750
    @stephanieezat-panah7750 ปีที่แล้ว

    This is an amazing tutorial. much work! thank you for all that you do

  • @ethioboy8955
    @ethioboy8955 2 ปีที่แล้ว

    You are a great teacher! You tell us step by step for free! I subscribed you with 3 accounts! Keep it up you will get millions of subs soon!

  • @taiwoawe889
    @taiwoawe889 2 ปีที่แล้ว

    Great One Bro! Thanks for this.

  • @marxLz
    @marxLz 2 ปีที่แล้ว +1

    Hello Bro, I'm about to finish the whole series about Java programming, thank you so much!!!
    I now trying to alter or add some syntax to the sample programs you have introduce, by using the coding styles you have taught on the previous videos.
    I may start learing python next after finishing this one. From my research, java and python are the programming languages commonly used in our country.

  • @girl6994
    @girl6994 4 ปีที่แล้ว +3

    Yes! I really love quiz!

  • @aruldilip9430
    @aruldilip9430 2 ปีที่แล้ว +1

    Dropping my comment bro !!! 👍🎄Merry Christmas n Happy New year🎄

  • @supreme-soft
    @supreme-soft ปีที่แล้ว +5

    The perfect video that I was looking for ! Bro, will you also start teaching android app development from zero to hero level !! 😎 Both offline & online apps. Thanks in advance 👍

  • @abhilashLeader_07
    @abhilashLeader_07 2 ปีที่แล้ว +2

    Thumbnail: Will you click on this video ?
    Me: yea

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 3 ปีที่แล้ว +4

    this was amazing

  • @Poojagupta-dz8qr
    @Poojagupta-dz8qr ปีที่แล้ว

    this video is very helpful and awesome..... thanks lot sir

  • @jamanas4692
    @jamanas4692 ปีที่แล้ว

    Hello bro
    You make programming easy for me. Thank you!

  • @Sportzzz2991
    @Sportzzz2991 2 ปีที่แล้ว +1

    Thank you so much for making this video🙏 very helpful ☺️

  • @vincentchoo7471
    @vincentchoo7471 3 ปีที่แล้ว +1

    Great video, thanks.

  • @ChristianRodriguez-tm3jg
    @ChristianRodriguez-tm3jg ปีที่แล้ว

    wow..... thanks bro for your kind support for us who are traying to get into this tough path....

  • @SvetolikKljajic
    @SvetolikKljajic ปีที่แล้ว

    Well done! Great tutorial! :)

  • @zari_723
    @zari_723 6 หลายเดือนก่อน

    I am using your videos to make a base for my own projects, thank you for these tutorials

  • @Juleismaster
    @Juleismaster 2 ปีที่แล้ว

    ¡Amazing!
    You are great Bro... code!

  • @ericdonkor7129
    @ericdonkor7129 3 ปีที่แล้ว

    Wow I really learnt so much . Thank you sir

  • @renecabuhan1675
    @renecabuhan1675 3 ปีที่แล้ว +2

    Bro why ur so smart.. thank you.

  • @ergitopllari1318
    @ergitopllari1318 2 ปีที่แล้ว

    You are fantastic Bro!

  • @NEO-wl9ox
    @NEO-wl9ox ปีที่แล้ว

    Long enervating and mesmerizing project.mate!

  • @lubos.hodossy
    @lubos.hodossy 2 ปีที่แล้ว

    wow thanks Bro !
    this is perfect

  • @raannyaana2230
    @raannyaana2230 2 ปีที่แล้ว +2

    If GIGA-CHAD was a programmer, he would be Bro Code!

  • @Shalve94
    @Shalve94 3 ปีที่แล้ว

    Thanks for the tutorial!

  • @minatouzumaki6
    @minatouzumaki6 ปีที่แล้ว

    Always motivate me

  • @sewek1513
    @sewek1513 2 ปีที่แล้ว

    That was great lesson!

  • @unda25
    @unda25 7 หลายเดือนก่อน

    I SUBSCRIBE AND I COMMENT IS THE BEST GUY WHO EXPLAIN ON THE INTERNET

  • @xXMaDGaMeR
    @xXMaDGaMeR 2 ปีที่แล้ว

    love the content man keep it up !

  • @pradhoshss1640
    @pradhoshss1640 3 ปีที่แล้ว

    Genius Bro ;)

  • @adedejiadebowale9205
    @adedejiadebowale9205 3 ปีที่แล้ว +1

    Hello bro code , nice work, appreciate it alot. However, I would like to add and display
    1. Diagrams to my quiz app
    2. Solutions to calculations after displayed answers.
    Thanks bro

  • @dreamy665
    @dreamy665 ปีที่แล้ว

    Thank you I have tried this and learnt so much!!!

  • @ludovicbocquet9783
    @ludovicbocquet9783 10 หลายเดือนก่อน

    Nice video again, merci beaucoup !

  • @aqibishaq885
    @aqibishaq885 3 ปีที่แล้ว +1

    waiting for next video bro bring face detection in java and more games in java please

  • @stephanieezat-panah7750
    @stephanieezat-panah7750 ปีที่แล้ว +2

    I found the constructor a bit overwhelming, so I did this:
    set_frame();
    set_textfield();
    set_textarea();
    set_buttons();
    set_answer_labels();
    set_timer();
    set_seconds_left();
    set_number_right();
    set_percentage();
    frame_add();
    and one method as an example:
    public void set_frame()
    {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600,600);
    frame.getContentPane().setBackground(new Color(150,150,150));
    frame.setLayout(null);
    frame.setResizable(false);
    }

  • @wanyoikedanny472
    @wanyoikedanny472 3 ปีที่แล้ว

    The explanation is lit😎😎😎😎

  • @somayaelgendy8824
    @somayaelgendy8824 ปีที่แล้ว

    Thank you so much! This is the only vid I used for my uni Java course final project🤩

  • @karankanjar7441
    @karankanjar7441 ปีที่แล้ว

    Bro your just osm

  • @benjietreyes1743
    @benjietreyes1743 3 ปีที่แล้ว +1

    Super cool bro btw where can u put looping when you wanted to ask the user of how many questions you wanted to answer and also how many subjects. thanks in advance

  • @ishandorge5366
    @ishandorge5366 2 ปีที่แล้ว

    Amazing 😍

  • @sharifullahdanish4480
    @sharifullahdanish4480 3 ปีที่แล้ว

    Great work bro

  • @365motivation.9
    @365motivation.9 ปีที่แล้ว

    bro you are a genius.

  • @opgamedev3067
    @opgamedev3067 2 ปีที่แล้ว

    Thank youuuuu its very helpfulllll

  • @ahbabshahinchowdhury4162
    @ahbabshahinchowdhury4162 3 ปีที่แล้ว +1

    Awesome

  • @user-cj4zy1lm1r
    @user-cj4zy1lm1r ปีที่แล้ว

    thank , I got a lot of knowledge

  • @awindustries1672
    @awindustries1672 2 ปีที่แล้ว

    awesome bro

  • @itzplayzzhd7290
    @itzplayzzhd7290 3 ปีที่แล้ว

    Can you pls do another video of the Quiz where you add a joker, like the 50/50 joker or sth like this. Would be really amazing!

  • @golivee2767
    @golivee2767 3 ปีที่แล้ว +3

    I really enjoyed this project. Fun way to enhance my java skills. Now, if i wanted to take something like this live, or share it...would I need to place it on the app store? if so, how would i do this?

  • @ThanhLe-et1gp
    @ThanhLe-et1gp 3 ปีที่แล้ว

    Awesome bro

  • @Blackshadowfizz
    @Blackshadowfizz 2 ปีที่แล้ว

    thank you so much

  • @souvikdatta3065
    @souvikdatta3065 3 ปีที่แล้ว +5

    Sir can you make a tutorial on java networking?
    Love from india

  • @ethioactive5661
    @ethioactive5661 2 ปีที่แล้ว

    It is great

  • @darkermcgamer5482
    @darkermcgamer5482 2 ปีที่แล้ว

    Thanks for your code a lot. I'm new to this so it's difficult for me to understand this.

  • @vklmao8677
    @vklmao8677 3 ปีที่แล้ว

    hurray you just passed 100k....!😀

  • @odinmaster6677
    @odinmaster6677 2 ปีที่แล้ว

    you sir are amazing

  • @anonimanonim1223
    @anonimanonim1223 2 ปีที่แล้ว

    Like it.

  • @vladimirtimofte6828
    @vladimirtimofte6828 3 ปีที่แล้ว

    Best QUIZ app till now. JAVA. Congratulations!

  • @fifaham
    @fifaham 9 หลายเดือนก่อน

    Thank you Java Guru

  • @minatouzumaki6
    @minatouzumaki6 ปีที่แล้ว

    i love you Bro

  • @kemann3815
    @kemann3815 2 ปีที่แล้ว

    Thanks a lot!

  • @gialacnguyen5426
    @gialacnguyen5426 4 ปีที่แล้ว

    niceeeeee

  • @developerjunior446
    @developerjunior446 3 ปีที่แล้ว

    Best quiz

  • @shigeruyamamoto53
    @shigeruyamamoto53 3 ปีที่แล้ว +1

    thank you sir for this tutorial ... can you make ATM Interface using java sir?

  • @2ysh650
    @2ysh650 2 ปีที่แล้ว

    Yaayy!!

  • @augischadiegils.5109
    @augischadiegils.5109 3 ปีที่แล้ว

    Thanks bro :)

  • @ctluwua7695
    @ctluwua7695 3 ปีที่แล้ว

    your thumbnail
    ME : yes !

  • @mikeafter5
    @mikeafter5 3 ปีที่แล้ว

    Thanks!

  • @mosabxgamer3335
    @mosabxgamer3335 2 ปีที่แล้ว

    Thanks

  • @xhichun123liew2
    @xhichun123liew2 4 ปีที่แล้ว +3

    can make a tutorial on how to make a leaderboard :D

  • @user-io8xv1lz8o
    @user-io8xv1lz8o 3 ปีที่แล้ว

    Thank you.

  • @AdhyFvnkys11
    @AdhyFvnkys11 7 หลายเดือนก่อน

    yoo thank you bro, finally my schoolwork is finished.... yeayyy..... 😅

  • @marybenish9716
    @marybenish9716 2 ปีที่แล้ว

    thanks!

  • @PIYUSHKUMAR-eo9fj
    @PIYUSHKUMAR-eo9fj 3 ปีที่แล้ว

    Thank u Man

  • @iMODEY22
    @iMODEY22 3 ปีที่แล้ว

    Wow
    I learned too many thing
    But one of them. When java was created and what company did😂😂😂😂

  • @abhishek-gu6qt
    @abhishek-gu6qt ปีที่แล้ว

    one thing i learnt that mark zuckerberg is hiding something 😂

  • @brooklyn-fv3gk
    @brooklyn-fv3gk ปีที่แล้ว

    yeeeee

  • @FJ.Y
    @FJ.Y 3 ปีที่แล้ว

    Thanks bro! I followed along until the end while documenting almost every line (took me 2 hrs) . At the end after I added the last actionPerformed method and hit run, the the textfield/area appear empty, as well as the choices. The buttons appear enabled and the counter "10". When I click on a button it goes to the second question and everything seems to work fine but the last choices all change to red when clicked (result shows 3/4, 75%) , I have no idea where I messed up, any thoughts where I should check? I went through you code and mine side by side but no luck, also your code works fine too! Also, where did you use the char guess variable (don't think it was used?).

    • @FJ.Y
      @FJ.Y 3 ปีที่แล้ว

      Also, what do I say the "this" key term is referring to? the actionPerformed method? how do I describe how that work in simple English

    • @FJ.Y
      @FJ.Y 3 ปีที่แล้ว +1

      Wow, I figured out the first problem :) I somehow removed "nextQuestion();" from the constructor lol. Disregard all the above, just about the keyword "this"

    • @koetimoep
      @koetimoep 2 ปีที่แล้ว +1

      @@FJ.Y I realize the comment is 1 year old, but here you go anyways.
      The only time the "this" key word is used in this code is when you add an ActionListener to a button. In our code "this" is a reference variable and just refers to the current object.
      buttonA.addActionListener(this); has the same meaning as buttonA.addActionListener(buttonA);

  • @medos2012
    @medos2012 2 ปีที่แล้ว +2

    what if you want to add a "Congratulations" when you answer the correct question? how to do that? and lastly, calculate the questions that you correctly answer and the percentage.

    • @marxLz
      @marxLz 2 ปีที่แล้ว

      Hello dude, if you happen to watch some of his tutorial in this series, you can come up with an idea to add "congratulations" in your program,
      for instance, you can create an If statement that if a correct answer has been clicked from the actionlistener, you can trigger another window (which may be another class or warning window) that will display the greetings you like.
      I'm also trying to experiment with his program samples by altering some syntax on the code, and sometimes, adding additional triggers just to see how the program will work properly or how will a program will malfunction.

  • @MO-dd3cs
    @MO-dd3cs 4 ปีที่แล้ว

    شكر thinke

  • @YTMarcoYT
    @YTMarcoYT 3 ปีที่แล้ว

    Please make the same video with JavaFX

  • @nigistgebremeskel9519
    @nigistgebremeskel9519 ปีที่แล้ว

    🥰🥰🥰🥰🥰🥰🥰🥰

  • @fantom9392
    @fantom9392 7 หลายเดือนก่อน

    مسكين طارق مشكور على الفيديو

  • @nehalayaaz9406
    @nehalayaaz9406 4 ปีที่แล้ว +1

    🤓

  • @tomriley9051
    @tomriley9051 2 ปีที่แล้ว

    Class video tbf

  • @jorgebulnes96
    @jorgebulnes96 3 ปีที่แล้ว

    Question @Bro Code, I got a project like this, but instead of typing the questions and answers, I got a File with the questions and answers, how do I implement that on my code? for example, my file is question.txt... if you can help, I would really appreciate it!

  • @sergeyiwanov9771
    @sergeyiwanov9771 2 ปีที่แล้ว

    sir make a tutorial about android