In: Computer Science
Write a program that does the following: Assume the canvas size of 500X500. The program asks the user to enter a 3 digit number. The program then checks the value of the first and last digit of the number. If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. If the two digits are odd, it makes the background red and displays the three digit number at the mouse pointer. If the first digit is odd and the third is even, it makes the background blue and display the three digit number at the mouse pointer. The number entered by the user is displayed at the mouse pointer at all times irrespective of the location of the mouse pointer. Hint: import javax.swing.JOptionPane; JOptionPane.showInputDialog("")
please help me solve this using processing java pde
Working code implemented in Java PDE and appropriate comments provided for better understanding.
main.pde:
import javax.swing.JOptionPane; int number; color c; void setup(){ size(500,500); //asking, reading number number=Integer.parseInt(JOptionPane.showInputDialog("Enter a three digit number: ")); //assuming user will always enter a valid three digit number //fetching first digit by integer division of number by 100 int first=number/100; //fetching last digit by modulo division by 10 int last=number%10; //if first and last digits are even, assigning green color to c if(first%2==0 && last%2==0){ c=color(0,255,0); } //if first and last digits are odd, assigning red color to c else if(first%2!=0 && last%2!=0){ c=color(255,0,0); } //if first digit is odd and last digits is even, assigning blue color to c else if(first%2!=0 && last%2==0){ c=color(0,0,255); } //there is still one more case which is not mentioned in the question, that is //if first digit is even and second is odd, since not specified any other, I'm //using a default gray color else{ c=color(200,200,200); } } void draw(){ //using c as background color background(c); //using black fill color fill(0); //drawing number at mouse position text(""+number,mouseX,mouseY); }
Sample Output Screenshots: