Sie sind auf Seite 1von 42

Faculty of Information Science and Technology Multimedia University

TCP 1311 OBJECT-ORIENTED PROGRAMMING


Trimester 1, Session 2009/2010

LECTURER: MR. MD. SHOHEL SAYEED


TUTORIAL SECTION: TMG 06 D

PROJECT TITLE: Children

learning system

STUDENT NAME: MOHGAMBIHAI A/P MOGANALINGAM

ID: 1081100406

TCP 1311

No 1. 2.

Content System Introduction Assumptions UML Diagrams A: Use Case Diagram


B: Class Diagram C: Sequence Diagram D: Collaboration Diagram E: State Diagram

Page 3 4

3.

Appendix 4. A: Source Code


B: Sample Interface Screen C: Sample Input/Output Screens

5.

Appreciation

42

TCP 1311

(I)

System Introduction

The Children Learning System is a system that gives children of ages 4 to 6 on how to use an interactive application. This interactive system will enable children see what happens when they rollover their mouse over the buttons in the menus and in the options. Moreover, clicking an option to select the answer and then summiting the answer for checking, is in real time. Result after solving three questions will be displayed for each category. The system deals with attractive images for children to match with their corresponding names. Matching images which include animals, fruits, and colors. The system allows simple mathematical operation for children to fill in the blanks for the correct answer. The numbers menu is designed for counting, addition/subtraction and multiplication/division. The frame uses java Swings UI Manager to manage defaults and extensively implements java GUI. The compiled program is to be run using command prompt. The system has robust coding which can include additional features such as background and effects sound and welcoming front page that has not been implemented. The system contains no errors as all exceptions are handled carefully.

Children Learning System


3

TCP 1311

(II) Assumptions
The Children Learning system assumes that the children should explore the menus and it with the activities on their own as this would promote their self-learning of interactive system. Since the system aims to provide basic knowledge on interactive systems, there are simple activities provided for the specified age group. The system involves a java application program. The program can be compiled using javac cls/ChildrenLearningSystem.java from the src folder and then run with java cls.ChildrenLearningSystem command at the command prompt. It would be better if there were a jar executable file but here the application only can be run from the command prompt. Since it is an application, it will not run from a web browser unless there is the JNLP file that can launch the application through the java web start. Thanks to the recent java SE update 10, however, that would require server to hold the file. That Java application mentioned runs as standalone, outside the browser offering a secure environment. On the other hand, applets, according to Sun Java Tutorial Websites, is a special kind of Java program that a browser enabled with Java technology can download from the internet and run within it. An applet is typically embedded inside a web-page and runs in the context of the browser. An applet must be a subclass of the java.applet.Applet class, which provides the standard interface between the applet and the browser environment. The Children Learning System can be transform into an applet by simply removing the packaging. Although the program runs in the html file within that directory, but that would causes exception in the web browser as that supplies application kind of coding.

TCP 1311

(III) UML Diagrams

Use Case Diagram

TCP 1311

Class Diagram

Child User

-selectsObjectFrom 1 1

Matching

-selectsObjectFrom

SetMatchingQuestion

Number

SetNumberQuestion

TCP 1311

Sequence Diagram

Menu: Object

View:Object

Examine:Object

The Child User selectsFromMenu()

typeOfObject () chooses()

Input()
inputanswer() numberOfCorrect() validateAnswer()

TCP 1311

Collaboration Diagram

Examine:Object Child User selectsFromMenu() 2.1: input()

2:chooses() Menu:Object View:Object

1:typeOfObject()

State Diagram
input()
object examine

validate()

TCP 1311 (IV)

Appendix

(A) Source code


// ChildrenLearningSystem.java
package cls; import cls.frames.WelcomeFrame; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.UIManager; public class ChildrenLearningSystem { boolean packFrame = false; //Construct the application public ChildrenLearningSystem() { WelcomeFrame frame = new WelcomeFrame(); //Validate frames that have preset sizes //Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) { frame.pack(); } else { frame.validate(); } //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height frameSize.height) / 2); frame.setVisible(true); } //Main method public static void main(String[] args) { 9

TCP 1311 try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } new ChildrenLearningSystem(); } }

//AboutBox.java
package cls.about; import javax.swing.JDialog; import java.awt.event.ActionListener; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.AWTEvent; import javax.swing.BorderFactory; import java.awt.event.WindowEvent; import java.awt.event.ActionEvent; public class AboutBox extends JDialog implements ActionListener { JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel insetsPanel1 = new JPanel(); JPanel insetsPanel2 = new JPanel(); JPanel insetsPanel3 = new JPanel(); JButton button1 = new JButton(); JLabel imageLabel = new JLabel(); JLabel label1 = new JLabel(); JLabel label2 = new JLabel(); JLabel label3 = new JLabel(); JLabel label4 = new JLabel(); ImageIcon image1 = new ImageIcon(); BorderLayout borderLayout1 = new BorderLayout(); BorderLayout borderLayout2 = new BorderLayout(); FlowLayout flowLayout1 = new FlowLayout(); 10

TCP 1311 GridLayout gridLayout1 = new GridLayout(); String product = "Children Learning System"; String version = "1.0"; String copyright = "Copyright (c) 2009"; String comments = "Simple learning system for children of ages between 4 to 10 years."; public AboutBox() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /* WelcomeFrame_AboutBox() { this(null); } */ //Component initialization private void jbInit() throws Exception { image1 = new ImageIcon(cls.about.AboutBox.class.getResource("about.png")); imageLabel.setIcon(image1); this.setTitle("About"); panel1.setLayout(borderLayout1); panel2.setLayout(borderLayout2); insetsPanel1.setLayout(flowLayout1); insetsPanel2.setLayout(flowLayout1); insetsPanel2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); gridLayout1.setRows(4); gridLayout1.setColumns(1); label1.setText(product); label2.setText(version); label3.setText(copyright); label4.setText(comments); insetsPanel3.setLayout(gridLayout1); insetsPanel3.setBorder(BorderFactory.createEmptyBorder(10, 60, 10, 10)); button1.setText("Ok"); button1.addActionListener(this); insetsPanel2.add(imageLabel, null); panel2.add(insetsPanel2, BorderLayout.WEST); this.getContentPane().add(panel1, null); insetsPanel3.add(label1, null); insetsPanel3.add(label2, null); insetsPanel3.add(label3, null); insetsPanel3.add(label4, null); 11

TCP 1311 panel2.add(insetsPanel3, BorderLayout.CENTER); insetsPanel1.add(button1, null); panel1.add(insetsPanel1, BorderLayout.SOUTH); panel1.add(panel2, BorderLayout.NORTH); setResizable(false); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) { cancel(); } super.processWindowEvent(e); } //Close the dialog void cancel() { dispose(); } //Close the dialog on a button event public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { cancel(); } } }

//Matching.java
package cls.core; public class Matching { public Matching() { } private String question; private String correctAnswer; private String option1; private String option2; private String option3; private String option4; private String option5;

public void setQuestion(String question) { 12

TCP 1311 this.question = question; } public String getQuestion() { return this.question; } public void setCorrectAnswer(String correctAnswer) { this.correctAnswer = correctAnswer; } public String getCorrectAnswer() { return this.correctAnswer; } public void setOption1(String option1) { this.option1 = option1; } public String getOption1() { return this.option1; } public void setOption2(String option2) { this.option2 = option2; } public String getOption2() { return this.option2; } public void setOption3(String option3) { this.option3 = option3; } public String getOption3() { return this.option3; } public void setOption4(String option4) { this.option4 = option4; } public String getOption4() { return this.option4; } public void setOption5(String option5) { this.option5 = option5; } 13

TCP 1311 public String getOption5() { return this.option5; }

public boolean checkAnswer(String givenAnswer) { if (correctAnswer.compareTo(givenAnswer)==0) return true; else return false; } }

//Numbers.java
package cls.core; public class Numbers { public Numbers() { } private String operand1; private String operand2; private String operator; private String correctAnswer; public void setOperand1(String operand1){ this.operand1 = operand1; } public String getOperand1(){ return this.operand1; } public void setOperand2(String operand2) { this.operand2 = operand2; } public String getOperand2() { return this.operand2; } 14

TCP 1311 public void setOperator(String operator) { this.operator = operator; } public String getOperator() { return this.operator; } public void setCorrectAnswer(String correctAnswer) { this.correctAnswer = correctAnswer; } public String getCorrectAnswer() { return this.correctAnswer; } public boolean checkAnswer(String givenAnswer){ if (correctAnswer.compareTo(givenAnswer)==0) return true; else return false; } }

//SetMatchingQuestion.java
package cls.core.matching; import cls.core.Matching; public class SetMatchingQuestion extends Matching{ public SetMatchingQuestion(String question, String correctAns, String opt1, String opt2, String opt3, String opt4, String opt5) { super.setQuestion(question); super.setCorrectAnswer(correctAns); super.setOption1(opt1); super.setOption2(opt2); super.setOption3(opt3); super.setOption4(opt4); super.setOption5(opt5); } 15

TCP 1311 }

//SetNumberQuestion.java
package cls.core.numbers; import cls.core.Numbers;

public class SetNumberQuestion extends Numbers{ public SetNumberQuestion(String operand1, String operand2, String operator, String correctAns) { super.setOperand1(operand1); super.setOperand2(operand2); super.setOperator(operator); super.setCorrectAnswer(correctAns); } }

//AdditionSubtractionQuestions.java
package cls.core.questions; import cls.core.numbers.SetNumberQuestion; import cls.core.Numbers; public class AdditionSubtractionQuestions{ public int totalQInt = 3; public Numbers[] setQuestions(){ /*To add more questions just assign the correct no of questions to totalQInt and add one more same patern object of SetNumberQuestion */ Numbers[] questions = new SetNumberQuestion[totalQInt]; //(operand1, operand2, operator, correctAns) questions[0] = new SetNumberQuestion("3","4","+","7"); questions[1] = new SetNumberQuestion("8","6","+","14"); questions[2] = new SetNumberQuestion("5","2","-","3"); 16

TCP 1311 return questions; } }

//AnimalQuestions.java
package cls.core.questions; import cls.core.matching.SetMatchingQuestion; import cls.core.Matching; public class AnimalQuestions{ public int totalQInt = 3; public Matching[] setQuestions(){ /*To add more questions just assign the correct no of questions to totalQInt and add one more same patern object of SetMatchingQuestion */ Matching[] questions = new SetMatchingQuestion[totalQInt]; //(image path, correctAnswer, opt1, opt2, opt3, opt4, opt5) questions[0] = new SetMatchingQuestion("images/matching/animals/animal0.jpg","Buffalo","Lion","Elephant","Buffalo","Mon key","Camel"); questions[1] = new SetMatchingQuestion("images/matching/animals/animal1.jpg","Lion","Lion","Elephant","Buffalo","Monke y","Camel"); questions[2] = new SetMatchingQuestion("images/matching/animals/animal2.jpg","Elephant","Lion","Elephant","Buffalo","Mo nkey","Camel"); return questions; } }

17

TCP 1311

//ColorQuestions.java
package cls.core.questions; import cls.core.matching.SetMatchingQuestion; import cls.core.Matching; public class ColorQuestions{ public int totalQInt = 3; public Matching[] setQuestions(){ /*To add more questions just assign the correct no of questions to totalQInt and add one more same patern object of SetMatchingQuestion */ Matching[] questions = new SetMatchingQuestion[totalQInt]; //(image path, actualAnswer, opt1, opt2, opt3, opt4, opt5) questions[0] = new SetMatchingQuestion("images/matching/colors/color0.jpg","Green","Yellow","Green","Orange","Blue","R ed"); questions[1] = new SetMatchingQuestion("images/matching/colors/color1.jpg","Blue","Yellow","Green","Orange","Blue","Red "); questions[2] = new SetMatchingQuestion("images/matching/colors/color2.jpg","Yellow","Yellow","Green","Orange","Blue"," Red"); return questions; } }

//CountingQuestions.java
package cls.core.questions; import cls.core.numbers.SetNumberQuestion; import cls.core.Numbers; public class CountingQuestions{ public int totalQInt = 3; public Numbers[] setQuestions(){ 18

TCP 1311 /*To add more questions just assign the correct no of questions to totalQInt and add one more same patern object of SetNumberQuestion */ Numbers[] questions = new SetNumberQuestion[totalQInt]; //(img path, "", "", correctAns) questions[0] = new SetNumberQuestion("images/numbers/counting/count0.JPG","","","3"); questions[1] = new SetNumberQuestion("images/numbers/counting/count1.JPG","","","5"); questions[2] = new SetNumberQuestion("images/numbers/counting/count2.JPG","","","6"); return questions; } }

//FruitQuestions.java
package cls.core.questions; import cls.core.matching.SetMatchingQuestion; import cls.core.Matching; public class FruitQuestions{ public int totalQInt = 3; public Matching[] setQuestions(){ /*To add more questions just assign the correct no of questions to totalQInt and add one more same patern object of SetMatchingQuestion */ Matching[] questions = new SetMatchingQuestion[totalQInt]; //(image path, correctAnswer, opt1, opt2, opt3, opt4, opt5) questions[0] = new SetMatchingQuestion("images/matching/fruits/fruit0.jpg","Orange","Mango","Orange","Banana","Grapes", "Guava"); questions[1] = new SetMatchingQuestion("images/matching/fruits/fruit1.jpg","Mango","Mango","Orange","Banana","Grapes", "Guava"); questions[2] = new SetMatchingQuestion("images/matching/fruits/fruit2.jpg","Grapes","Mango","Orange","Banana","Grapes", 19

TCP 1311 "Guava"); return questions; } }

//MultiplicationDivisionQuestions.java
package cls.core.questions; import cls.core.numbers.SetNumberQuestion; import cls.core.Numbers; public class MultiplicationDivisionQuestions{ public int totalQInt = 3; public Numbers[] setQuestions(){ /*To add more questions just assign the correct no of questions to totalQInt and add one more same patern object of SetNumberQuestion */ Numbers[] questions = new SetNumberQuestion[totalQInt]; //(operand1, operand2, operator, correctAns) questions[0] = new SetNumberQuestion("2","2","*","4"); questions[1] = new SetNumberQuestion("6","2","*","12"); questions[2] = new SetNumberQuestion("9","3","/","3"); return questions; } }

//MatchingFrame.java
package cls.frames; import java.awt.event.ActionListener; import javax.swing.JPanel; import javax.swing.JButton; 20

TCP 1311 import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.AWTEvent; import java.awt.event.WindowEvent; import java.awt.event.ActionEvent; import javax.swing.JFrame; import java.awt.Toolkit; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Color; import cls.menubar.CLSMenuBar; import cls.core.questions.FruitQuestions; import cls.core.questions.AnimalQuestions; import cls.core.Matching; import cls.core.questions.ColorQuestions; public class MatchingFrame extends JFrame { Matching questions[]; private int qNo; private int totalQInt; private int totalRAnsInt; private String catStr; JPanel contentPane; JLabel imgLbl = new JLabel(); ImageIcon imgIcon; JButton optionBtn1 = new JButton(); JButton optionBtn2 = new JButton(); JButton optionBtn3 = new JButton(); JButton optionBtn4 = new JButton(); JButton optionBtn5 = new JButton(); JLabel ansLbl = new JLabel(); JButton nextBtn = new JButton(); JLabel totalRAnsLbl = new JLabel(); JLabel currQLbl = new JLabel(); JLabel totalQLbl = new JLabel(); //Construct the frame public MatchingFrame(String matchingCatStr) { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { this.catStr = matchingCatStr; jbInit(); } catch(Exception e) { e.printStackTrace(); 21

TCP 1311 } }//End of constructor private void setQuestions(){ if(catStr.compareTo("fruit")==0){ FruitQuestions questions = new FruitQuestions(); totalQInt = questions.totalQInt; //setQuestions also sets the questions and return those questions in array of Matching Class objects this.questions = questions.setQuestions(); } else if (catStr.compareTo("animal") == 0) { AnimalQuestions questions = new AnimalQuestions(); totalQInt = questions.totalQInt; this.questions = questions.setQuestions(); } else if (catStr.compareTo("color") == 0) { ColorQuestions questions = new ColorQuestions(); totalQInt = questions.totalQInt; this.questions = questions.setQuestions(); } } //Component initialization private void jbInit() throws Exception { //Set questions setQuestions(); //Construct the MenuBar and set it for this frame CLSMenuBar clsMenuBar = new CLSMenuBar(this); this.setJMenuBar(clsMenuBar.getMenu()); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(null); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setSize(new Dimension(screenSize.width , screenSize.height-100)); //Uncomment the above line after using design view and set the below line commented. //this.setSize(new Dimension(700, 650)); this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH); this.setTitle("Children Learning System"); 22

TCP 1311 //To display the imageicon in the label imgLbl.setBounds(new Rectangle(53, 80, 238, 234)); //Display 1st Question (starts from 0) displayQuestion(qNo); //To display total question no. totalQLbl.setBounds(new Rectangle(30, 21, 163, 15)); totalQLbl.setFont(new java.awt.Font("Dialog", 1, 14)); totalQLbl.setText("Total Questions: " + totalQInt); //To display current question no. currQLbl.setBounds(new Rectangle(55, 50, 110, 15)); currQLbl.setFont(new java.awt.Font("Dialog", 1, 12)); currQLbl.setText("Question No: " + (qNo+1)); //To display the label answer is wrong or right. ansLbl.setBounds(new Rectangle(84, 425, 243, 26)); ansLbl.setFont(new java.awt.Font("Dialog", 1, 14)); ansLbl.setVisible(false); //To display Next Question Button nextBtn.setBounds(new Rectangle(440, 420, 137, 39)); nextBtn.setFont(new java.awt.Font("Dialog", 1, 14)); nextBtn.setText("Next Question"); nextBtn.setVisible(false); //To display total right ans lable totalRAnsLbl.setBounds(new Rectangle(440, 415, 137, 39)); totalRAnsLbl.setForeground(Color.magenta); totalRAnsLbl.setFont(new java.awt.Font("Dialog", 1, 18)); totalRAnsLbl.setVisible(false); //Add action listeners when answer is pressed. and next question button is pressed optionBtn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { whenAnswered(optionBtn1.getText()); } } ); optionBtn2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { whenAnswered(optionBtn2.getText()); } 23

TCP 1311 } ); optionBtn3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { whenAnswered(optionBtn3.getText()); } } ); optionBtn4.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { whenAnswered(optionBtn4.getText()); } } ); optionBtn5.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { whenAnswered(optionBtn5.getText()); } } ); nextBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { qNo = qNo + 1; currQLbl.setText("Question No: " + (qNo+1)); nextBtn.setVisible(false); ansLbl.setVisible(false); enableOptBtns(); displayQuestion(qNo); } } ); //Add components to JPanel contentPane.add(optionBtn1, null); contentPane.add(optionBtn2, null); contentPane.add(optionBtn3, null); contentPane.add(optionBtn4, null); contentPane.add(optionBtn5, null); 24

TCP 1311 contentPane.add(imgLbl, null); contentPane.add(ansLbl, null); contentPane.add(nextBtn, null); contentPane.add(totalRAnsLbl, null); contentPane.add(totalQLbl, null); contentPane.add(currQLbl, null); }//End of jbInit Method private void displayQuestion(int qNo){ //Pass question's image path directly in the getResouce to edit it in design view imgIcon = new ImageIcon(cls.ChildrenLearningSystem.class.getResource(questions[qNo].getQuestion())); imgLbl.setIcon(imgIcon); optionBtn1.setBounds(new Rectangle(440, 75, 137, 39)); optionBtn1.setFont(new java.awt.Font("Dialog", 1, 14)); optionBtn1.setText(questions[qNo].getOption1()); optionBtn2.setBounds(new Rectangle(440, 125, 137, 39)); optionBtn2.setFont(new java.awt.Font("Dialog", 1, 14)); optionBtn2.setText(questions[qNo].getOption2()); optionBtn3.setBounds(new Rectangle(440, 175, 137, 39)); optionBtn3.setFont(new java.awt.Font("Dialog", 1, 14)); optionBtn3.setText(questions[qNo].getOption3()); optionBtn4.setBounds(new Rectangle(440, 225, 137, 39)); optionBtn4.setFont(new java.awt.Font("Dialog", 1, 14)); optionBtn4.setText(questions[qNo].getOption4()); optionBtn5.setBounds(new Rectangle(440, 275, 137, 39)); optionBtn5.setFont(new java.awt.Font("Dialog", 1, 14)); optionBtn5.setText(questions[qNo].getOption5()); } private void whenAnswered(String givenAns){ //disable option buttons disableOptBtns(); //enable ansLbl ansLbl.setVisible(true); if (questions[qNo].checkAnswer(givenAns)) { totalRAnsInt = totalRAnsInt + 1; ansLbl.setForeground(Color.blue); ansLbl.setText("Good, Answer is Right!"); } else { ansLbl.setForeground(Color.red); ansLbl.setText("Sorry, Wrong Answer!"); 25

TCP 1311 } //enable next question button till last question done if(qNo<totalQInt-1)//if not last question nextBtn.setVisible(true); else{// all question done whenAllQDone(); } } private void whenAllQDone(){ totalRAnsLbl.setText("Total Marks: "+ totalRAnsInt + "/" + totalQInt); totalRAnsLbl.setVisible(true); } private void disableOptBtns(){ optionBtn1.setEnabled(false); optionBtn2.setEnabled(false); optionBtn3.setEnabled(false); optionBtn4.setEnabled(false); optionBtn5.setEnabled(false); } private void enableOptBtns(){ optionBtn1.setEnabled(true); optionBtn2.setEnabled(true); optionBtn3.setEnabled(true); optionBtn4.setEnabled(true); optionBtn5.setEnabled(true); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } }//End of class MatchingFrame

26

TCP 1311

//NumberFrame.java
package cls.frames; import java.awt.event.ActionListener; import java.awt.AWTEvent; import java.awt.event.WindowEvent; import java.awt.event.ActionEvent; import java.awt.Toolkit; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Color; import cls.menubar.CLSMenuBar; import cls.core.questions.AdditionSubtractionQuestions; import cls.core.Numbers; import javax.swing.*; import cls.core.questions.MultiplicationDivisionQuestions; import cls.core.questions.CountingQuestions; public class NumberFrame extends JFrame { Numbers questions[]; private int qNo; private int totalQInt; private int totalRAnsInt; private String catStr; JPanel contentPane; JButton submitBtn = new JButton(); JLabel ansLbl = new JLabel(); JButton nextBtn = new JButton(); JLabel totalRAnsLbl = new JLabel(); JLabel currQLbl = new JLabel(); JLabel totalQLbl = new JLabel(); JLabel operandOneLbl = new JLabel(); JLabel operatorLbl = new JLabel(); JLabel operandTwoLbl = new JLabel(); JTextField ansTxtF = new JTextField(); JLabel equalLbl = new JLabel(); JLabel imgLbl = new JLabel(); ImageIcon imgIcon;

//Construct the frame public NumberFrame(String numberCatStr) { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { 27

TCP 1311 this.catStr = numberCatStr; jbInit(); } catch(Exception e) { e.printStackTrace(); } }//End of constructor private void setQuestions(){ if(catStr.compareTo("additionSubtraction")==0){ AdditionSubtractionQuestions questions = new AdditionSubtractionQuestions(); totalQInt = questions.totalQInt; this.questions = questions.setQuestions(); } if(catStr.compareTo("multiplyDivide")==0){ MultiplicationDivisionQuestions questions = new MultiplicationDivisionQuestions(); totalQInt = questions.totalQInt; this.questions = questions.setQuestions(); } if(catStr.compareTo("counting")==0){ CountingQuestions questions = new CountingQuestions(); totalQInt = questions.totalQInt; this.questions = questions.setQuestions(); } } //Component initialization private void jbInit() throws Exception { //Set questions setQuestions(); //Construct the MenuBar and set it for this frame CLSMenuBar clsMenuBar = new CLSMenuBar(this); this.setJMenuBar(clsMenuBar.getMenu()); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(null); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setSize(new Dimension(screenSize.width , screenSize.height-100)); //Uncomment the above line after using design view and set the below line commented. //this.setSize(new Dimension(700, 650)); this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH); 28

TCP 1311 this.setTitle("Children Learning System"); // Question area operandOneLbl.setFont(new java.awt.Font("Dialog", 1, 60)); operandOneLbl.setBounds(new Rectangle(75, 220, 115, 55)); operatorLbl.setFont(new java.awt.Font("Dialog", 1, 60)); operatorLbl.setBounds(new Rectangle(200, 220, 49, 55)); operandTwoLbl.setBounds(new Rectangle(332, 220, 115, 55)); operandTwoLbl.setFont(new java.awt.Font("Dialog", 1, 60)); equalLbl.setFont(new java.awt.Font("Dialog", 1, 60)); equalLbl.setText("="); equalLbl.setBounds(new Rectangle(472, 217, 41, 55)); ansTxtF.setFont(new java.awt.Font("Dialog", 1, 40)); ansTxtF.setBounds(new Rectangle(528, 220, 115, 55)); //To display the imageicon in the label imgLbl.setBounds(new Rectangle(53, 85, 372, 319)); submitBtn.setBounds(new Rectangle(537, 310, 97, 39)); submitBtn.setFont(new java.awt.Font("Dialog", 1, 18)); submitBtn.setText("Submit"); //Display 1st Question (starts from 0) displayQuestion(qNo); //To display total question no. totalQLbl.setBounds(new Rectangle(30, 21, 163, 15)); totalQLbl.setFont(new java.awt.Font("Dialog", 1, 14)); totalQLbl.setText("Total Questions: " + totalQInt); //To display current question no. currQLbl.setBounds(new Rectangle(55, 50, 110, 15)); currQLbl.setFont(new java.awt.Font("Dialog", 1, 12)); currQLbl.setText("Question No: " + (qNo+1)); //To display the label answer is wrong or right. ansLbl.setBounds(new Rectangle(84, 425, 243, 26)); ansLbl.setFont(new java.awt.Font("Dialog", 1, 14)); ansLbl.setVisible(false); //To display Next Question Button nextBtn.setBounds(new Rectangle(440, 420, 137, 39)); nextBtn.setFont(new java.awt.Font("Dialog", 1, 14)); nextBtn.setText("Next Question"); nextBtn.setVisible(false); //To display total right ans label totalRAnsLbl.setBounds(new Rectangle(440, 415, 137, 39)); 29

TCP 1311 totalRAnsLbl.setForeground(Color.magenta); totalRAnsLbl.setFont(new java.awt.Font("Dialog", 1, 18)); totalRAnsLbl.setVisible(false);

//Add action listeners when submit is pressed. and next question button is pressed submitBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { whenAnswered(ansTxtF.getText().trim()); } } ); nextBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { qNo = qNo + 1; currQLbl.setText("Question No: " + (qNo+1)); nextBtn.setVisible(false); ansLbl.setVisible(false); submitBtn.setVisible(true); ansTxtF.setEditable(true); ansTxtF.setText(""); displayQuestion(qNo); } } ); //Add components to JPanel contentPane.add(submitBtn, null); contentPane.add(ansLbl, null); contentPane.add(nextBtn, null); contentPane.add(totalRAnsLbl, null); contentPane.add(totalQLbl, null); contentPane.add(currQLbl, null); contentPane.add(operandOneLbl, null); contentPane.add(operandTwoLbl, null); contentPane.add(ansTxtF, null); contentPane.add(operatorLbl, null); 30

TCP 1311 contentPane.add(equalLbl, null); contentPane.add(imgLbl, null); contentPane.add(submitBtn, null); }//End of jbInit Method private void displayQuestion(int qNo){ if(catStr.compareTo("counting")==0){ imgIcon = new ImageIcon(cls.ChildrenLearningSystem.class.getResource(questions[qNo].getOperand1())); imgLbl.setIcon(imgIcon); }else{ operandOneLbl.setText(questions[qNo].getOperand1()); operatorLbl.setText(questions[qNo].getOperator()); operandTwoLbl.setText(questions[qNo].getOperand2()); } } private void whenAnswered(String givenAns){ //disable submit btn submitBtn.setVisible(false); ansTxtF.setEditable(false); //enable ansLbl ansLbl.setVisible(true); if (questions[qNo].checkAnswer(givenAns)) { totalRAnsInt = totalRAnsInt + 1; ansLbl.setForeground(Color.blue); ansLbl.setText("Good, Answer is Right!"); } else { ansLbl.setForeground(Color.red); ansLbl.setText("Sorry, Wrong Answer!"); } //enable next question button till last question done if(qNo<totalQInt-1)//if not last question nextBtn.setVisible(true); else{// all question done whenAllQDone(); } } private void whenAllQDone(){ 31

TCP 1311 totalRAnsLbl.setText("Total Marks: "+ totalRAnsInt + "/" + totalQInt); totalRAnsLbl.setVisible(true); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } }//End of class NumberFrame

//WelcomeFrame.java
package cls.frames; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.AWTEvent; import java.awt.event.WindowEvent; import javax.swing.JFrame; import java.awt.Toolkit; import java.awt.Dimension; import cls.menubar.CLSMenuBar; import java.awt.BorderLayout; public class WelcomeFrame extends JFrame { JPanel contentPane; JLabel statusBar = new JLabel(); BorderLayout borderLayout1 = new BorderLayout(); //Construct the welcome frame public WelcomeFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } }//End of constructor //Component initialization private void jbInit() throws Exception { 32

TCP 1311 //Construct the MenuBar and set it for this frame CLSMenuBar clsMenuBar = new CLSMenuBar(this); this.setJMenuBar(clsMenuBar.getMenu()); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(borderLayout1); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setSize(new Dimension(screenSize.width , screenSize.height-100)); this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH); this.setTitle("Children Learning System"); statusBar.setText("Children Learning System"); contentPane.add(statusBar, BorderLayout.SOUTH); }//End of jbInit Method

//Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } }//End of class WelcomeFrame

//CLSMenuBar.java
package cls.menubar; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import java.awt.Toolkit; import java.awt.Dimension; import java.awt.Dimension; import java.awt.Toolkit; import cls.about.AboutBox; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import cls.frames.MatchingFrame; import cls.frames.NumberFrame; 33

TCP 1311

public class CLSMenuBar { JMenuBar jMenuBar = new JMenuBar(); JFrame jFrame; //The constructor. public CLSMenuBar(JFrame jframe) { this.jFrame = jframe; //Create each menu and item. JMenu jMenuFile = new JMenu(); JMenuItem jMenuFileExit = new JMenuItem(); JMenu jMenuHelp = new JMenu(); JMenuItem jMenuHelpAbout = new JMenuItem(); JMenu jMenuMatching = new JMenu(); JMenuItem jMenuMatchingFruits = new JMenuItem(); JMenuItem jMenuMatchingAnimals = new JMenuItem(); JMenuItem jMenuMatchingColors = new JMenuItem(); JMenu jMenuNumbers = new JMenu(); JMenuItem jMenuNumAddSub = new JMenuItem(); JMenuItem jMenuNumMultDiv = new JMenuItem(); JMenuItem jMenuNumCount = new JMenuItem();

//Setup the menu items jMenuFile.setText("File"); jMenuFileExit.setText("Exit"); jMenuHelp.setText("Help"); jMenuHelpAbout.setText("About"); jMenuMatching.setText("Matching"); jMenuMatchingFruits.setText("Fruits"); jMenuMatchingAnimals.setText("Animals"); jMenuMatchingColors.setText("Colors"); jMenuNumbers.setText("Numbers"); jMenuNumAddSub.setText("Addition/Subtraction"); jMenuNumMultDiv.setText("Multiplication/Division"); jMenuNumCount.setText("Counting Objects"); //Add action listeners for the menu items. 34

TCP 1311 //File | Exit action performed jMenuFileExit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } } ); //Matching | Fruits action performed jMenuMatchingFruits.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jFrame.dispose(); MatchingFrame matchingFruitsFrame = new MatchingFrame("fruit"); matchingFruitsFrame.setVisible(true); } } ); //Matching | Animals action performed jMenuMatchingAnimals.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jFrame.dispose(); MatchingFrame matchingAnimalsFrame = new MatchingFrame("animal"); matchingAnimalsFrame.setVisible(true); } } ); //Matching | Colors action performed jMenuMatchingColors.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jFrame.dispose(); MatchingFrame matchingColorFrame = new MatchingFrame("color"); matchingColorFrame.setVisible(true); } } ); //Numbers | Addition/Subtraction action performed jMenuNumAddSub.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jFrame.dispose(); NumberFrame numberAdditionSubFrame = new NumberFrame("additionSubtraction"); numberAdditionSubFrame.setVisible(true); 35

TCP 1311 } } ); //Numbers | Multiplication/Subtraction action performed jMenuNumMultDiv.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jFrame.dispose(); NumberFrame numberMultipDivFrame = new NumberFrame("multiplyDivide"); numberMultipDivFrame.setVisible(true); } } ); //Numbers | Counting Objects action performed jMenuNumCount.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jFrame.dispose(); NumberFrame numberCountingFrame = new NumberFrame("counting"); numberCountingFrame.setVisible(true); } } );

//Help | About action performed jMenuHelpAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AboutBox dlg = new AboutBox(); Dimension dlgSize = dlg.getPreferredSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); dlg.setLocation((screenSize.width - dlgSize.width) / 2, (screenSize.height - dlgSize.height) / 2); dlg.setModal(true); dlg.pack(); dlg.show(); } } ); //Organize all the items into a single menu. jMenuFile.add(jMenuFileExit); jMenuMatching.add(jMenuMatchingFruits); 36

TCP 1311 jMenuMatching.add(jMenuMatchingAnimals); jMenuMatching.add(jMenuMatchingColors); jMenuNumbers.add(jMenuNumAddSub); jMenuNumbers.add(jMenuNumMultDiv); jMenuNumbers.add(jMenuNumCount); jMenuHelp.add(jMenuHelpAbout); jMenuBar.add(jMenuFile); jMenuBar.add(jMenuMatching); jMenuBar.add(jMenuNumbers); jMenuBar.add(jMenuHelp); } //End of constructor.

//Method to get the menu bar from this menu. public JMenuBar getMenu() { return jMenuBar; } }//End of class CLSMenuBar

(B) Sample interface screen

37

TCP 1311

(C) Sample input /output screens


A user can select from options in the menus.

Once User selects fruit from matching, this will appear and user has to choose the correct answer. Along each options the mouse rollover has effect.

38

TCP 1311 After selecting an option an output message will clarify the answer.

When user selects wrong a option the system will notify that the answer was wrong.

39

TCP 1311

There is some counting too.

40

TCP 1311

41

TCP 1311

A token of Appreciation I, Mohgambihai A/P Moganalingam will like to thank my lecturer Mr. Md. Shohel Sayeed for helping me wonderfully in my entire understanding about Java and Object-Oriented Programming. Thank You Mr. Md. Shohel Sayeed

42

Das könnte Ihnen auch gefallen