Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48
  1. #21
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    Here's an example of how to use a method.

    Java Code:
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main( String[] args ) {
    
    		int a = 5;
    		int b = twoTimesInput( a );
    		System.out.println( b );
    	}
    
    	private static int twoTimesInput( int a ) {
    
    		return a * 2;
    	}
    
    }

  2. #22
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by racerxadam View Post
    Here's an example of how to use a method.

    Java Code:
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main( String[] args ) {
    
    		int a = 5;
    		int b = twoTimesInput( a );
    		System.out.println( b );
    	}
    
    	private static int twoTimesInput( int a ) {
    
    		return a * 2;
    	}
    
    }
    thank you im trying....

  3. #23
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by racerxadam View Post
    Here's an example of how to use a method.

    Java Code:
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main( String[] args ) {
    
    		int a = 5;
    		int b = twoTimesInput( a );
    		System.out.println( b );
    	}
    
    	private static int twoTimesInput( int a ) {
    
    		return a * 2;
    	}
    
    }
    Could you please write an example which is a bit similar to my program as your example is so basic and it dose not have (if statement.).
    Thanks you
    Last edited by afrttoh; 11-28-2008 at 04:21 AM.

  4. #24
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    No. Try something and post the code along with any error message.

    Java Code:
    private static int twoTimesInput( int a ) {
    
    		return a * 2;
    	}
    is a method, you can do anything you want in there as long as the return statement contains the same datatype as the method declaration, in this case int. Do your if/else and calculation logic in the method and return the value to be printed out.

  5. #25
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Thank you,
    i know i am doing it wrong ,but here is what i have done!!

    Java Code:
    import javax.swing.JOptionPane;
    public class PayCheck
    {
    	public static void main(String[] args)
    	{
    		String input = JOptionPane.showInputDialog("What's your name?");
    		String input1 = JOptionPane.showInputDialog("What's your hourly wage?");
    		 private static double n = Double.parseDouble(input1);
    		String input2 = JOptionPane.showInputDialog("how many hours you worked in the past week?");
    		private static double m = Double.parseDouble(input2);
    	   	
    		if (m > 40)
    		{
    			JOptionPane.showMessageDialog (null,"your wage this week is:  "+ return((n*m)*1.5));
    		}
    
    		else
    		{
    			JOptionPane.showMessageDialog (null,"your  wage this week is:  " return + (n*m));
    		}
    		
    		     
    		
    	}
    }
    errors :

    ---------- Compile Java Source ----------
    PayCheck.java:8: illegal start of expression
    private static double n = Double.parseDouble(input1);
    ^
    PayCheck.java:8: illegal start of expression
    private static double n = Double.parseDouble(input1);
    ^
    PayCheck.java:8: ';' expected
    private static double n = Double.parseDouble(input1);
    ^
    PayCheck.java:10: illegal start of expression
    private static double m = Double.parseDouble(input2);
    ^
    PayCheck.java:10: illegal start of expression
    private static double m = Double.parseDouble(input2);
    ^
    PayCheck.java:10: ';' expected
    private static double m = Double.parseDouble(input2);
    ^
    PayCheck.java:14: illegal start of expression
    JOptionPane.showMessageDialog (null,"your wage this week is: "+ return((n*m)*1.5));
    ^
    PayCheck.java:14: ';' expected
    JOptionPane.showMessageDialog (null,"your wage this week is: "+ return((n*m)*1.5));
    ^
    PayCheck.java:14: not a statement
    JOptionPane.showMessageDialog (null,"your wage this week is: "+ return((n*m)*1.5));
    ^
    PayCheck.java:14: ';' expected
    JOptionPane.showMessageDialog (null,"your wage this week is: "+ return((n*m)*1.5));
    ^
    PayCheck.java:19: ')' expected
    JOptionPane.showMessageDialog (null,"your wage this week is: " return + (n*m));
    ^
    PayCheck.java:19: not a statement
    JOptionPane.showMessageDialog (null,"your wage this week is: " return + (n*m));
    ^
    PayCheck.java:19: ';' expected
    JOptionPane.showMessageDialog (null,"your wage this week is: " return + (n*m));
    ^
    13 errors

    Output completed (3 sec consumed)
    Last edited by afrttoh; 11-28-2008 at 05:34 AM.

  6. #26
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #27
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    import javax.swing.JOptionPane;
    public class PayCheck
    {
    public static void main(String[] args)
    {
    String input = JOptionPane.showInputDialog("What's your name?");
    String input1 = JOptionPane.showInputDialog("What's your hourly wage?");
    double n = Double.parseDouble(input1);
    String input2 = JOptionPane.showInputDialog("how many hours you worked in the past week?");
    double m = Double.parseDouble(input2);
    double h = (n*m)*1.5;
    double t = (n*m);

    if (m > 40)
    {
    JOptionPane.showMessageDialog (null,"your wage this week is: " + (h));
    }
    return h;
    else
    {
    JOptionPane.showMessageDialog (null,"your wage this week is: " + (t));
    }

    return t;

    }
    }


    this is what i have done now where do i put the private variable ?

  8. #28
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What's purpose of a private variable. You just define it inside a method, so any other members cannot access that variable.

  9. #29
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    errors errors and more errors!

    i think i need to see a similar example as I am a complete beginner.

  10. #30
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  11. #31
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Look at this code segment first.

    Java Code:
        public static void main(String[] args) {
    
        // User inputs
            String strName = JOptionPane.showInputDialog("What's your name? ");
            String strWage = JOptionPane.showInputDialog("What's your hourly wage? ");
            String strTime = JOptionPane.showInputDialog("How many working hours? ");
    
        // Convertion
            double wage = Double.parseDouble(strWage);
            double time = Double.parseDouble(strTime);
    
        // Calculation
            double salary;
            if(time > 40) {
                salary = (wage * time) * 1.5;
            }
            else {
                salary = (wage * time);
            }
    
        // Result
            JOptionPane.showMessageDialog(null, "Salaray of " + strName + " is: " +
                    salary, "Salary", JOptionPane.INFORMATION_MESSAGE);
    
        }

  12. #32
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Look at this code segment first.

    Java Code:
        public static void main(String[] args) {
    
        // User inputs
            String strName = JOptionPane.showInputDialog("What's your name? ");
            String strWage = JOptionPane.showInputDialog("What's your hourly wage? ");
            String strTime = JOptionPane.showInputDialog("How many working hours? ");
    
        // Convertion
            double wage = Double.parseDouble(strWage);
            double time = Double.parseDouble(strTime);
    
        // Calculation
            double salary;
            if(time > 40) {
                salary = (wage * time) * 1.5;
            }
            else {
                salary = (wage * time);
            }
    
        // Result
            JOptionPane.showMessageDialog(null, "Salaray of " + strName + " is: " +
                    salary, "Salary", JOptionPane.INFORMATION_MESSAGE);
    
        }

    yeas its much better than the one i have and u done it in a better way as well..

  13. #33
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    here is what i have done now:

    import javax.swing.JOptionPane;
    public class PayCheck
    {
    public static void main(String[] args)
    {
    String strName = JOptionPane.showInputDialog("What's your name? ");
    String strWage = JOptionPane.showInputDialog("What's your hourly wage? ");
    String strTime = JOptionPane.showInputDialog("How many working hours? ");

    double wage = Double.parseDouble(strWage);
    double time = Double.parseDouble(strTime);

    double salary;
    if(time > 40) {
    salary = (wage * time) * 1.5;
    }
    else {
    salary = (wage * time);
    }
    private static double salary
    {

    return JOptionPane.showMessageDialog(null, "Salaray of " + strName + " is: " +
    salary, "Salary", JOptionPane.INFORMATION_MESSAGE);


    }
    }
    ---------- Compile Java Source ----------
    PayCheck.java:20: illegal start of expression
    private static double (salary ) {
    ^
    PayCheck.java:20: illegal start of expression
    private static double (salary ) {
    ^
    PayCheck.java:20: ';' expected
    private static double (salary ) {
    ^
    PayCheck.java:20: not a statement
    private static double (salary ) {
    ^
    PayCheck.java:20: ';' expected
    private static double (salary ) {
    ^
    PayCheck.java:27: reached end of file while parsing
    }
    Output completed (1 sec consumed))
    Last edited by afrttoh; 11-28-2008 at 07:07 AM.

  14. #34
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This is the same error as before and has already been solved. Surely you can see that the solution is the same.

  15. #35
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This is the same error as before and has already been solved. Surely you can see that the solution is the same.
    thank you!

  16. #36
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    A lesson here is to read the error messages carefully as the more familiar you become with them, the more helpful they will become to you. They really aren't that cryptic and they often tell you all you need to know to solve the problem.

  17. #37
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    A lesson here is to read the error messages carefully as the more familiar you become with them, the more helpful they will become to you. They really aren't that cryptic and they often tell you all you need to know to solve the problem.
    thanks ,
    im trying hard ;) lets see..

  18. #38
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, now you are going to implement a new method to return the value. So do this first and see,

    1. Remove the return statement from that method and add the following, and run the application.

    Java Code:
    return 0;
    2. Remove the word static and run the application.

  19. #39
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Ok, now you are going to implement a new method to return the value. So do this first and see,

    1. Remove the return statement from that method and add the following, and run the application.

    Java Code:
    return 0;
    2. Remove the word static and run the application.
    import javax.swing.JOptionPane;
    public class PayCheck
    {
    public static void main(String[] args)
    {
    String strName = JOptionPane.showInputDialog("What's your name? ");
    String strWage = JOptionPane.showInputDialog("What's your hourly wage? ");
    String strTime = JOptionPane.showInputDialog("How many working hours? ");
    double wage = Double.parseDouble(strWage);
    double time = Double.parseDouble(strTime);

    double salary;
    if(time > 40) {
    salary = (wage * time) * 1.5;
    }
    else {
    salary = (wage * time);
    }
    JOptionPane.showMessageDialog(null, "Salaray of " + strName + " is: " +
    salary, "Salary", JOptionPane.INFORMATION_MESSAGE);

    private double(double salary)
    return 0;

    }
    }

    ---------- Compile Java Source ----------
    PayCheck.java:22: illegal start of expression
    private double(double salary)
    ^
    PayCheck.java:22: not a statement
    private double(double salary)
    ^
    PayCheck.java:22: ';' expected
    private double(double salary)
    ^
    PayCheck.java:22: ';' expected
    private double(double salary)
    ^
    4 errors

    Output completed (2 sec consumed)

    .................................................. ....................................




    import javax.swing.JOptionPane;
    public class PayCheck
    {
    public static void main(String[] args)
    {
    String strName = JOptionPane.showInputDialog("What's your name? ");
    String strWage = JOptionPane.showInputDialog("What's your hourly wage? ");
    String strTime = JOptionPane.showInputDialog("How many working hours? ");
    double wage = Double.parseDouble(strWage);
    double time = Double.parseDouble(strTime);

    double salary;
    if(time > 40) {
    salary = (wage * time) * 1.5;
    }
    else {
    salary = (wage * time);
    }
    JOptionPane.showMessageDialog(null, "Salaray of " + strName + " is: " +
    salary, "Salary", JOptionPane.INFORMATION_MESSAGE);

    private double (double salary)
    return salary;

    }
    }
    ---------- Compile Java Source ----------
    PayCheck.java:22: illegal start of expression
    private double (double salary)
    ^
    PayCheck.java:22: not a statement
    private double (double salary)
    ^
    PayCheck.java:22: ';' expected
    private double (double salary)
    ^
    PayCheck.java:22: ';' expected
    private double (double salary)
    ^
    4 errors

    Output completed (2 sec consumed)

  20. #40
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Still you are going in wrong way lol.

    Lest start from the point, which I gave the code. It's working fine. Then add the other method, name calculatePay() as you said, and send the complete code here.

    Keep in mind that calculatePay() return a value of type double.

    Do it first.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Pls Help me for Newton-Raphson method by Java
    By kienph2004 in forum Advanced Java
    Replies: 3
    Last Post: 08-13-2008, 06:07 PM
  2. Call Java Method
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-15-2008, 08:22 AM
  3. Any method for improving of java thread
    By code453 in forum Java Applets
    Replies: 0
    Last Post: 12-20-2007, 09:29 AM
  4. Java constructor method
    By carl in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:15 PM
  5. The main method in java...
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •