View Single Post
  #1 (permalink)  
Old 08-02-2010, 04:40 PM
SnowCrashed SnowCrashed is offline
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 12:06 PM. Reason: Please use code tags
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence