Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: need help prog skipping method

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default [solved]- need help prog skipping method

    Hi this is the question i was given

    Write a menu driven program that allows the user to enter a value for x and provides three options:
    i. Find the square of the number
    ii. Find the cube of the number
    iii. The third option allows the user to quit.
    Make use of methods in your program for each option above. Note the methods to find the square and cube of the number should receive one parameter of type double and return the answer.

    And here is my attemt. It compiles but it always cubes the number entered even if i choose option a to square. I've also tweaked the code to valadiate that the correct letters have been entered.
    If you could highlight where im going wrong in red please.Any help would be greately appricated.
    class WS1Q3
    {
    	public static void main(String[] args)
    	{
    		double x, result;
    		char ans;
    		System.out.println("enter a number");
    		x=Keyboard.readDouble();
    		System.out.println("press 'A' to square the number, 'B' to cube or 'C' to quit");
    		ans=Keyboard.readChar();
    		if(ans!='A' && ans !='B' && ans!='C' && ans!='a' && ans !='b' && ans!='c')
    		{
    			do // valadating correct letters entered
    			{
    				System.out.print("Error enter either A,B or C");
    				ans=Keyboard.readChar();
    			}while(ans!='A' && ans!='B' && ans!='C' && ans!='a' && ans !='b' && ans!='c');
    		}
     
    		// use of if statment to use requested method
     
    		if(ans == 'A' || ans == 'a')
    		{
    			result=square(x);
    		}
    		if(ans =='B' || ans =='b');
    		{
    			result=cube(x);
    		}
    		if(ans == 'C' || ans =='c')
    		{
    		System.out.println("Goodbye");
    		}
    		System.out.println("the result is "+result);
    		System.out.println("Goodbye");
    	}//close main
    		static double square(double x)
    		{
    			double result;
    			result = x*x;
    			return result;
    		}//close method square
    		static double cube(double x)
    		{
    			double result;
    			result= x*x*x*x;
    			return result;
    		}//close method cube
    }//close class
    Last edited by Pulse_Irl; February 11th, 2010 at 07:46 AM. Reason: making as solved


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: need help prog skipping method

    Hi, Please tell what is Keyboard here. I am unable to resolve it. Is it some other class or an object

  3. #3
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: need help prog skipping method

    I debugged your program and found following errors.

    1.You have used semicolon in if second if statement
    if(ans =='B' || ans =='b');
    Remove this semicolon and write statement as
    if(ans =='B' || ans =='b')

    2. In cube you are multiplying the number 4 times. It should be 3 times like as
    result= x*x*x;

    3. Initialize your result to 0. other wise there will be an error.

    Enjoy buddy and say click on thanks link..

  4. The Following User Says Thank You to jassi For This Useful Post:

    Pulse_Irl (February 11th, 2010)

  5. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: need help prog skipping method

    Cheers jassi guessed you figured out that it's a class file that reads the input from the user.
    Q. is there any benefits from writting the keyboard class file a different way i.e read.console() - i think i seen this 1

  6. #5
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: need help prog skipping method

    I think its not. You can use Scanner class to get data from Keyboard. I have seen many apps where in Scanner is used.

Similar Threads

  1. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 06:55 PM