Go Back   Java Programming Forums > Java Standard Edition Programming Help > Loops & Control Statements

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-02-2010, 05:40 PM
Junior Member
 

Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
SnowCrashed is on a distinguished road
Default If-Else Statement help

Hello,

I just recently started a Java programming class and the current assignment is to create a program that asks the user if they are happy. Depending on the response either a smiley face or sad face appears in a window.

I have been struggling to figure out how to set up an if-else statement, and am also unsure of when to use:
public static void main(String[] args)

I know this is probably very basic level stuff but I've tried messing around with the code for over 2 hours now. This is how far I've gotten..

Java Code
import java.awt.Graphics;
import java.util.Scanner;

import javax.swing.JApplet;

/**
 *This is a program that displays different faces
 *based off of different responses.  
 *2/5/2010
 */
public class Program3
extends JApplet {
    public static final int FACE_DIAMETER = 200;
    public static final int X_FACE = 100;
    public static final int Y_FACE = 50;
    public static final int EYE_WIDTH = 10;
    public static final int EYE_HEIGHT = 20;
    public static final int X_RIGHT_EYE = 155;
    public static final int Y_RIGHT_EYE = 100;
    public static final int X_LEFT_EYE = 200;
    public static final int Y_LEFT_EYE = Y_RIGHT_EYE;
    public static final int MOUTH_WIDTH = 100;
    public static final int MOUTH_HEIGHT = 200;
    public static final int X_MOUTH = 150;
    public static final int Y_MOUTH = 160;
    public static final int MOUTH_START_ANGLE = 180;
    public static final int MOUTH_EXTENT_ANGLE = 180;
    
    public void init()
    {
        setSize(500, 500);
    }
    
    
    public void happyFace (Graphics canvas)
    {
        //Draw face outline
        canvas.drawOval (X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
        
        //Draw eyes
        canvas.fillOval (X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
        canvas.fillOval (X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
        
        //Draw mouth
        canvas.drawArc (X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,
                MOUTH_START_ANGLE, MOUTH_EXTENT_ANGLE);
    }
    
    public void sadFace (Graphics canvas)
    {
    }
    	private static final long serialVersionUID = 1L;
    	public static final int FACE_DIAMETER2 = 200;
    	public static final int X_FACE2 = 100;
    	public static final int Y_FACE2 = 50;
    	
    	public static final int EYE_WIDTH2 = 10;
    	public static final int EYE_HEIGHT2 = 20;
    	public static final int X_RIGHT_EYE2 = 155;
    	public static final int Y_RIGHT_EYE2 = 100;
    	public static final int X_LEFT_EYE2 = 200;
    	public static final int Y_LEFT_EYE2 = Y_RIGHT_EYE;
    	
    	public static final int MOUTH_WIDTH2 = 100;
    	public static final int MOUTH_HEIGHT2 = 200;
    	public static final int X_MOUTH2 = 150;
    	public static final int Y_MOUTH2 = 150;
    	public static final int MOUTH_START_ANGLE2 = 150;
    	public static final int MOUTH_EXTENT_ANGLE2 = -120;
  
    	public void paint(Graphics canvas)
    	{
    		//Draw face outline:
    		canvas.drawOval(X_FACE2, Y_FACE2, FACE_DIAMETER2, FACE_DIAMETER2);
    		//Draw eyes:
    		canvas.fillOval(X_RIGHT_EYE2, Y_RIGHT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
    		canvas.fillOval(X_LEFT_EYE2, Y_LEFT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
    		//Draw mouth:
    		canvas.drawArc(X_MOUTH2, Y_MOUTH2, MOUTH_WIDTH2, MOUTH_HEIGHT2,
    				MOUTH_START_ANGLE2, MOUTH_EXTENT_ANGLE2);
    
    	happyFace (canvas);
    	sadFace (canvas);
    	
    	
    //The following code deals with if-else and asking the user if they are happy
    	
    	System.out.println("Are you happy?");
    	
    	if (answer == 'y') || (answer == 'yes')
    	
    		
    	}
     
    	{
}

}
Thanks for any advice!




Last edited by Json; 09-02-2010 at 01:06 PM. Reason: Please use code tags
Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 09-02-2010, 02:50 AM
Junior Member
 

Join Date: Feb 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Zhdophanti is on a distinguished road
Default Re: If-Else Statement help

Hi,

i will try to help you out a bit

The method "public static void main(String[] args)" is basically only needed as entry point for standard java programs, if your applet is run from a browser you wont need that function.

And about the if,i would better write it in a way like

if ((answer.equalsIgnoreCase("y") || (answer.equalsIgnoreCase("yes")) {
....

this ensures String comparison is used, and if the user types Y or YES it will be also valid.
Reply With Quote
  #3 (permalink)  
Old 09-02-2010, 01:08 PM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 861
Thanks: 31
Thanked 112 Times in 108 Posts
Json will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: If-Else Statement help

And to use best practises with this you want to turn it around as well

Java Code
if (("y".equalsIgnoreCase(answer) || ("yes".equalsIgnoreCase(answer)) {
// Json
Reply With Quote
  #4 (permalink)  
Old 09-02-2010, 08:30 PM
Junior Member
 

Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
SnowCrashed is on a distinguished road
Default Re: If-Else Statement help

I tried your suggestions but it says answer cannot be resolved--what does that mean? Also, I'm running this program from eclipse.
Reply With Quote
  #5 (permalink)  
Old 09-02-2010, 10:11 PM
Junior Member
 

Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
SnowCrashed is on a distinguished road
Default Re: If-Else Statement help

Okay everything is working as far as I can tell, BUT...when the program loads it won't allow me to add input using the keyboard. Is it the braces? Or is there some coding wrong?

Java Code
import java.awt.Graphics;
import java.util.Scanner;

import javax.swing.JApplet;

/**
 *This is a program that displays different faces
 *based off of different responses.  
 *2/5/2010
 */
public class Program3 extends JApplet
{
public static void main(String[] args)
{System.out.println("Are you happy?");


}	public static final int FACE_DIAMETER = 200;
    public static final int X_FACE = 100;
    public static final int Y_FACE = 50;
    public static final int EYE_WIDTH = 10;
    public static final int EYE_HEIGHT = 20;
    public static final int X_RIGHT_EYE = 155;
    public static final int Y_RIGHT_EYE = 100;
    public static final int X_LEFT_EYE = 200;
    public static final int Y_LEFT_EYE = Y_RIGHT_EYE;
    public static final int MOUTH_WIDTH = 100;
    public static final int MOUTH_HEIGHT = 200;
    public static final int X_MOUTH = 150;
    public static final int Y_MOUTH = 160;
    public static final int MOUTH_START_ANGLE = 180;
    public static final int MOUTH_EXTENT_ANGLE = 180;
  
    
      
    public void happyFace (Graphics canvas)
    {happyFace (canvas);
        //Draw face outline
        canvas.drawOval (X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
        
        //Draw eyes
        canvas.fillOval (X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
        canvas.fillOval (X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
        
        //Draw mouth
        canvas.drawArc (X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,
                MOUTH_START_ANGLE, MOUTH_EXTENT_ANGLE);
    }
    
    public void sadFace (Graphics canvas)
    {sadFace (canvas);}
    	private static final long serialVersionUID = 1L;
    	public static final int FACE_DIAMETER2 = 200;
    	public static final int X_FACE2 = 100;
    	public static final int Y_FACE2 = 50;
    	
    	public static final int EYE_WIDTH2 = 10;
    	public static final int EYE_HEIGHT2 = 20;
    	public static final int X_RIGHT_EYE2 = 155;
    	public static final int Y_RIGHT_EYE2 = 100;
    	public static final int X_LEFT_EYE2 = 200;
    	public static final int Y_LEFT_EYE2 = Y_RIGHT_EYE;
    	
    	public static final int MOUTH_WIDTH2 = 100;
    	public static final int MOUTH_HEIGHT2 = 200;
    	public static final int X_MOUTH2 = 150;
    	public static final int Y_MOUTH2 = 150;
    	public static final int MOUTH_START_ANGLE2 = 150;
    	public static final int MOUTH_EXTENT_ANGLE2 = -120;
  
    	@SuppressWarnings({ "unused", "null" })
		public void paint(Graphics canvas)
    	{
    		//Draw face outline:
    		canvas.drawOval(X_FACE2, Y_FACE2, FACE_DIAMETER2, FACE_DIAMETER2);
    		//Draw eyes:
    		canvas.fillOval(X_RIGHT_EYE2, Y_RIGHT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
    		canvas.fillOval(X_LEFT_EYE2, Y_LEFT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
    		//Draw mouth:
    		canvas.drawArc(X_MOUTH2, Y_MOUTH2, MOUTH_WIDTH2, MOUTH_HEIGHT2,
    				MOUTH_START_ANGLE2, MOUTH_EXTENT_ANGLE2);
    
    		{ 
    Object answer;
	{Scanner keyboard = new Scanner(System.in);
    answer = keyboard.nextLine();}
    
    			
    			
    			
    			
				if ((answer.equals("y") || (answer.equals("yes")))) Statement:   {
    				happyFace (canvas);
    			}
    			else sadFace (canvas);}
    				
    				
    			}
	 		
			
			      		

         
    	{
}
    		}
Reply With Quote
  #6 (permalink)  
Old 10-02-2010, 12:57 AM
Junior Member
 

Join Date: Feb 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Zhdophanti is on a distinguished road
Default Re: If-Else Statement help

Well i guess it is cause you can't use "System.in" for applets

I have found this link explaining how to get key input in applets:
Applet Tutorial: Keyboard Input

I hope you can use it for JApplet too
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble with an if statement. please help!! humdinger Loops & Control Statements 11 21-01-2010 07:49 PM
If Statement Java Need help Keno888 Loops & Control Statements 5 25-10-2009 06:53 AM
another unreachable statement.. chronoz13 Loops & Control Statements 3 02-10-2009 05:15 PM
unreachable statement? chronoz13 Loops & Control Statements 1 11-09-2009 03:06 PM
If statement Dave Loops & Control Statements 2 27-08-2009 03:11 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java ipod touch java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 01:01 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.