3 Replies - 503 Views - Last Post: 10 May 2010 - 04:01 PM Rate Topic: -----

#1 johnathanc456  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 53
  • Joined: 05-May 10

char variable confusion

Posted 10 May 2010 - 02:23 PM

alright so i have a method that takes two variables, an int and a char.
 	public double sumOrProd (int howFar, char sOrP) 


here is what sumOrProd is supposed to do :

A method that takes two parameters: an integer that must be between 1 and 30, inclusive, and a character that must be one of the characters 'S', 's', 'P', or 'p'. If both parameters contain legal values, then calculate and return the sum or product (as specified in sOrP) of the numbers from 1 to howFar. If sOrP is 'S' or 's', return the sum, if 'P' or 'p', return the product.
If either parameter contains an illegal value, return 0.0.

Parameters:
howFar - the range of integers to calculate
sOrP - sum or product indicator
Returns:
the calculated sum or product for legal inputs. 0.0 otherwise.

i am stuck getting the sOrP to work, to me it seems that instead of sOrP being a char it should be a boolean, but thats just me.
here is the method code, any help or suggestions is greatly appreciated.

Thanks.


 	public double sumOrProd (int howFar, char sOrP)
		{
			double sum;
			double product;
			
			
					
			// A method that takes two parameters: an integer that must be between 1 and 30, inclusive, and a character that must be one of the characters 'S', 's', 'P', or 'p'.
			// If both parameters contain legal values, then calculate and return the sum or product (as specified in sOrP) of the numbers from 1 to howFar. If sOrP is 'S' or 's', return the sum, if 'P' or 'p', return the product.	
		
		
			// determines if howFar is between 1 and 30, if not returns 0.0
			while (howFar <= 30)
			{
					if (howFar >=1)
					{
						if (sOrP == s || sOrp == S)
						{
							
							
							return sum;
						}
						
						if (sOrP == p||sOrP == P)
						{
							
							
							return product;
						}
											
					}
			}						
				// if either paramiter contain an illigal value then return 0.0
			//the calculated sum or product for legal inputs. 0.0 otherwise.
		return 0;	
			
		}



Is This A Good Question/Topic? 0
  • +

Replies To: char variable confusion

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: char variable confusion

Posted 10 May 2010 - 02:33 PM

A char literal is a single one character enclosed in single quote marks like this:
char ch1 = 's';
char ch2 = '4';
char ch3 = '&';

so when you want to check if a char variable is equal to the char literal 's', or 's', you'll do it like:
if (sOrP == 's' || sOrp == 'S')


note:
the way you checked, as:
if (sOrP == s || sOrp == S) 

The compiler was actually looking for variables named s, or S.
Was This Post Helpful? 1
  • +
  • -

#3 divinespirit9671  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 30
  • Joined: 01-May 10

Re: char variable confusion

Posted 10 May 2010 - 02:37 PM

sOrP should be a char and not boolean.

You have not written the code to calculate sum or product,that is error;
here is a sample code to calculate sum:-
int s=0;
for(int i=1;i<=howfar;i++)
{
   sum=sum+i;
}



Similarly write 'for' loop for product and put the 'for' loops inside corresponding 'if' blocks
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: char variable confusion

Posted 10 May 2010 - 04:01 PM

What is the use of a while loop here either? You need a for loop to calculate sumj and product though...You can write this program much easier using if statements:

if (howFar <= 30 && howFar >= 1) {
    if (sOrP == 's' || sOrP == 'S') {
        for (int i = 1; i < howFar; i++) {
            sum += i;
        }
        return sum;
    }
    else if (sOrP == 'p' || sOrP == 'P') { 
        ... I'll leave this to you
    }
    else {
        System.out.println("Invalid Option");
        return 0;
    }
}   
// If the number is not between 1 and 30, return 0 
else {
    return 0;
}



Now that you see how I have done sum, try to figure out how to do product.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1