12 Replies - 2037 Views - Last Post: 09 February 2010 - 05:28 PM Rate Topic: -----

#1 Guest_Joshua*


Reputation:

Help with If-Else Statements and methods

Posted 08 February 2010 - 07:09 PM

Hello,

I'm taking an introductory level course in Java and I've been trying to figure out how to continue and what's wrong with my current code. My assignment is to create a program in which the user is asked, "Are you happy?", if they say yes or y a smiley face appears in a window. If they type anything else then a sad face appears.

I have been trying to figure out the correct way to approach this for about 3 hours now but with my professor it has very quickly become sink or swim. If there is any advice, corrections, or suggestions on what to do now I would appreciate it.

Thanks,

Joshua



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;
private char answer;

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);

Scanner keyboard = new Scanner(System.in);



//The following code deals with if-else and asking the user if they are happy

{
char answer;
}



{
System.out.println("Are you happy?");
}

{


if (answer == 'y')
happyFace (canvas);

else sadFace (canvas);


if (answer == 'yes');
happyFace (canvas);

else sadFace (canvas);
{




}















}






{
}
}}

Is This A Good Question/Topic? 0

Replies To: Help with If-Else Statements and methods

#2 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Help with If-Else Statements and methods

Posted 08 February 2010 - 07:15 PM

Hey, We'd love to help, but we can't read this code as-is. Please post your code like this:
[ code] post your code here [ /code] (without the spaces in the tags)

It should look like this:
 post your code here 

Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Help with If-Else Statements and methods

Posted 08 February 2010 - 07:18 PM

No semicolon after if statements:
if (answer == 'yes')//;


Your brackets are a little bizarre here:
{
char answer;
}
{
System.out.println("Are you happy?");
}


Why do you surround those lines with brackets?


As dogstopper said, please use the code tags
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Joshua*


Reputation:

Re: Help with If-Else Statements and methods

Posted 08 February 2010 - 07:22 PM

Sorry about that, also I kept adding brackets when eclipse asked, I can clean that up.


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.  
 *Author: Dalton Vogler
 *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;
	private char answer;
    
    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);
    	
    	Scanner keyboard = new Scanner(System.in);
    
    	
    	
    //The following code deals with if-else and asking the user if they are happy
    
    	{
    		char answer;
      	}
    	
    
    
    	{
    	System.out.println("Are you happy?");
    	}
    	
 {
    		
	 
	 		if (answer == 'y')
    			happyFace (canvas);
			
				else sadFace (canvas);
			
			
			if (answer == 'yes')
			happyFace (canvas);
			
			else  sadFace (canvas);
			{
			
			

			
			}
			   	
    		    	}
      
   
    	{
}
    	}}


Was This Post Helpful? 0

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Help with If-Else Statements and methods

Posted 08 February 2010 - 07:25 PM

 public void sadFace (Graphics canvas)
    {
    }


Put the code for this method inside these braces, not after them
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Joshua*


Reputation:

Re: Help with If-Else Statements and methods

Posted 08 February 2010 - 07:36 PM

Like this?

public void sadFace (Graphics canvas)
    {sadFace (canvas);    }



Should I do the same for the happyFace method? Also, are the if-else statements set up correctly?

Thanks for helping me with this!
Was This Post Helpful? 0

#7 Harlen  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 40
  • Joined: 08-February 10

Re: Help with If-Else Statements and methods

Posted 08 February 2010 - 09:15 PM

Sorry, but I'm taking the introductory course as well.

With the following code of< Guest_Joshua*'s source code:

 {
                
         
                        if (answer == 'y')
                        happyFace (canvas);
                        
                                else sadFace (canvas);
                        
                        
                        if (answer == yes')
                        happyFace (canvas);
                        
                        else  sadFace (canvas);




I haven't done this, but couldn't you just do this?

if (answer == 'y' || 'yes')
happyFace (canvas);

else sadFace (canvos);




I believe that
 || 
is a logical operator for OR. Or would that not compile, or have a run-time error?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Help with If-Else Statements and methods

Posted 08 February 2010 - 10:46 PM

When you use a logical operator, you have to include two distinct boolean conditions. So instead of saying if(varName == val1 || val2), you must say if(varName == val1 || varName == val2). Also, since answer is a char, it can't == 'yes', since (last I checked), there isn't a 'yes' character in the English language. If you want to allow for a "yes" comparison, better to work with Strings (which you should use the String equals() method in that case). Lastly, I think you misspelled canvas at this line: else sadFace (canvos);
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Joshua*


Reputation:

Re: Help with If-Else Statements and methods

Posted 09 February 2010 - 09:03 AM

Okay, so I spent more time last night fixing the code and rearranging some things and I have a few more questions (more specific).

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
{
public static void main(String[] args)
{System.out.println("Are you happy?");
}

{String s1;
s1 = 'yes';

}

{Scanner keyboard = new Scanner(System.in);
}



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() 
    
   
    
    
    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;
  
    	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);
    
    		{ 
    		
	 
	 		if (answer == 'y')
    			happyFace (canvas);
			
				else sadFace (canvas);
			
			
			if (answer == s1)
			happyFace (canvas);
			
			else  sadFace (canvas);
			{
			
			

			
			}
			   	
    		
    	}
       
   
    	{
}
    		}}






1) Am I using Strings correctly? Will s1 turn into yes if a user types it in?
2) Currently when I run the program I get a smiley face with a frowning face overlapping it, how do I keep both from popping up? Shouldn't the if-else statement prevent that?
3) Does the if-else statement look as it should, are there any suggestions?
4) Am I forgetting to define anything currently?

Thanks for the help!
Was This Post Helpful? 0

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Help with If-Else Statements and methods

Posted 09 February 2010 - 01:04 PM

Your methods should work like this. Notice the start and end braces, with the code inside of the method. In your main class block, you should only have method definitions and top-level variable declarations. Any method invocations or logic/flow should be contained in other methods.
public <return-type> methodName(..param list if applicable..){
   ..code goes here..
}



There should be no rogue braces with code in your class, as I show you below. Really, braces should be limited to class or method definitions, conditionals, loops, and try-catch-finally blocks.
class MyClass{

 { //rogue brace
   ..code..
} //rogue brace

  public MyClass(){ //valid brace

  }//valid brace

}//end class



Edit: Added missing code tag

This post has been edited by macosxnerd101: 09 February 2010 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#11 Guest_Joshua*


Reputation:

Re: Help with If-Else Statements and methods

Posted 09 February 2010 - 03:10 PM

Okay, should I just delete all my braces and rewrite the program placing braces where they need to go? Also, I fixed most of the programs in my code but now my problem is that the user can't input when the program runs.

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);}
    				
    				
    			}
	 		
			
			      		

         
    	{
}
    		}



Was This Post Helpful? 0

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Help with If-Else Statements and methods

Posted 09 February 2010 - 03:45 PM

If you want to delete all your braces and start over, that's your choice; just be careful about where you place the new ones so as not to throw off the logic flow or create more errors. Really, the only braces that should be removed are the rogue ones; that is, the ones that aren't used to encapsulate a conditional, loop, method, class, or try-catch block. As for getting input, you'll probably want to do so inside your main() method.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Joshua*


Reputation:

Re: Help with If-Else Statements and methods

Posted 09 February 2010 - 05:28 PM

If I place the Scanner keyboard code in my main method, eclipse highlights the rest of the code in red line, but is that the only way to get input to work?
Was This Post Helpful? 0

Page 1 of 1