Results 1 to 2 of 2
  1. #1
    amalina is offline Member
    Join Date
    Jul 2009
    Posts
    17
    Rep Power
    0

    Exclamation my new problem..!!

    hai as usual im always post thread with a problem to solve..
    i get the task from my teacher..
    she ask me to write a coding using JApplet with using switch statement..
    like this the output:
    when i enter any alphabet is will say whether it is a consonant or a vowel..
    for example:
    when i enter 'a'/'e'/'i'/'o'/'u':this is a vowel.
    when i enter other than the above:this is a consonant..
    u get what u mean..??

    i've done 1 of my simple task using if..else..
    the coding is:
    package ifelse;

    import javax.swing.JApplet;
    import java.awt.Graphics;
    import javax.swing.JOptionPane;

    /**
    *

    */
    public class exercise extends JApplet {

    double num1;
    String a;

    @Override

    /**
    * Initialization method that will be called after the applet is loaded
    * into the browser.
    */
    public void init() {

    String num;

    num=JOptionPane.showInputDialog("please insert a number:");

    num1=Double.parseDouble(num);

    if(num1%2==0)
    {
    a=("this is an even number!");
    }
    else{
    a=("this is an odd number!");
    }
    }
    @Override

    public void paint(Graphics g){
    super.paint(g);

    g.drawRect(12,10,270,20);
    g.drawString("the number is " +a,55,25);
    }


    // TODO overwrite start(), stop() and destroy() methods

    }

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    //take the input in the same way what you have done for the previous code
    Java Code:
    String a;
    public void init(){
    String num;
    
    num=JOptionPane.showInputDialog("please insert a character:");
    
    char c=num.charAt(0);
    switch(c){
       case 'a': a="Vowel";
                    break;
       //apply same logic for the other vowels
       default: a="Consonant";
       
    }
    
    @Override
    
    public void paint(Graphics g){
    super.paint(g);
    
    g.drawRect(12,10,270,20);
    g.drawString("the number is " +a,55,25);
    }
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •