1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.*; import java.util.*; public class Question1 extends World { /** * Constructor for objects of class Questions. * */ public Question1() { // Create a new world with 600x600 cells with a cell size of 1x1 pixels with a quit button. super ( 600 , 600 , 1 ); addObject( new Quit(), 75 , 30 ); double [] cash = new double [] { 52.50 , 99.99 , 36.00 , 10.35 , 72.11 }; double inWallet = cash[(( int )(Math.random() * 4 + 0 ))]; String q = "Hannah had $" + inWallet + " in her wallet. She spent a quarter of that\nwhen she went out with her friends. She owed one quarter\nof what was remaining to her mother so she paid her back.\nHow much did she have left?\n(Round final ANSWER only to 2 decimal places)" ; // Image of Text is 20 font size, black Text, white background GreenfootImage ImageText = new GreenfootImage(q, 20 , Color.BLACK,Color.WHITE); GreenfootImage bg = getBackground(); bg.drawImage(ImageText, 100 , 100 ); // upper left corner of image is at 100, 100. double spent = 0.25 *inWallet; double left1 = inWallet-spent; double mom = 0.25 *left1; double answer = left1-mom; double result = ( double )Math.round(answer * 100000d) / 100000d; Greenfoot.delay( 500 ); String inputstring = JOptionPane.showInputDialog( "Your answer here:" ); if (inputstring.equals(Double.toString(result))) { Greenfoot.setWorld( new Question2()); Greenfoot.playSound( "ding.mp3" ); } else { Greenfoot.playSound( "Wrong.mp3" ); addObject( new Wrong(), 300 , 300 ); Greenfoot.delay( 500 ); Greenfoot.setWorld( new Question2()); } } } |

