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 7 of 7

Thread: return

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default return

    class aa7
    {
    public static void main (String[] args)
    {
    System.out.println("HOURS IN A DAY: "+Hours(int days=2));
    }
    public class Hours{
       private static final int NUMBER_OF_HOURS_IN_A_DAY = 24;
     
       public int calculateHoursInDays(int days)
      {
         return days * NUMBER_OF_HOURS_IN_A_DAY;
       }
    }
    }


    I want printout :48



    Errors :

    --------------------Configuration: <Default>--------------------
    C:\aa7.java:5: error: '.class' expected
    System.out.println("HOURS IN A DAY: "+Hours(int days=2));
    ^
    C:\aa7.java:5: error: ';' expected
    System.out.println("HOURS IN A DAY: "+Hours(int days=2));
    ^
    2 errors

    Process completed.

    General output :
    Error: Could not find or load main class aa7


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: return

    when the error says could not load main class aa7, it probably should be an issue with the name of your file. You should know that in java, the name of the main class should be same as the file name. Here you file name should be aa7.java.

    Next
    System.out.println("HOURS IN A DAY: "+Hours(int days=2));
    This is completely a wrong statement in java.
    you do not declare anything where parameter is passed. It should only be Hours(2).

    Rename public class Hours as public static class Hours and public int calculateHoursInDays(int days) as public static int calculateHoursInDays(int days)

    This should help you.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: return

    Code after changes :

    class b
    {
    public static void main (String[] args)
    {
    System.out.println("HOURS IN A DAY: "+Hours(2));
    }
    public static class Hours{
       private static final int NUMBER_OF_HOURS_IN_A_DAY = 24;
     
       public static int calculateHoursInDays(int days)
      {
         return days * NUMBER_OF_HOURS_IN_A_DAY;
       }
    }
    }


    Output/errors :

    C:\Documents and Settings\dipak\Desktop>javac b.java
    b.java:5: error: cannot find symbol
    System.out.println("HOURS IN A DAY: "+Hours(2));
    ^
    symbol: method Hours(int)
    location: class b
    1 error

    C:\Documents and Settings\dipak\Desktop>

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: return

    dipakshah8944
    sorry for my rude question: Are you a Java rookie? Your program is garbage. Here is how your program should be:
    class b {
    	public static void main (String[] args) {
    		System.out.println("HOURS IN A DAY: "+Hours(2));
    	}
    	public static int Hours(int day)  {
    		final int NUMBER_OF_HOURS_IN_A_DAY = 24;
    		return day * NUMBER_OF_HOURS_IN_A_DAY;
    	}
    }

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: return

    class b
    {
    public static void main (String[] args)
    {
    b class1 = new b();
    b.Hours class2 = new b.Hours();
     
    System.out.println("HOURS IN A DAY: "+ class2.calculateHoursInDays(2));
    }
     
    public static class Hours{
       private final int NUMBER_OF_HOURS_IN_A_DAY = 24;
     
       public  int calculateHoursInDays(int days)
      {
         return days * NUMBER_OF_HOURS_IN_A_DAY;
       }
    }
    }
    Try this code
    When you have a class within another class, this is how you call the method
    b class1 = new b();
    b.Hours class2 = new b.Hours();

  6. The Following User Says Thank You to kindk12 For This Useful Post:

    dipakshah8944 (July 15th, 2012)

  7. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: return

    Thanks kindk12.

  8. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: return

    Quote Originally Posted by Voodoo View Post
    dipakshah8944
    sorry for my rude question: Are you a Java rookie? Your program is garbage. Here is how your program should be:
    class b {
    	public static void main (String[] args) {
    		System.out.println("HOURS IN A DAY: "+Hours(2));
    	}
    	public static int Hours(int day)  {
    		final int NUMBER_OF_HOURS_IN_A_DAY = 24;
    		return day * NUMBER_OF_HOURS_IN_A_DAY;
    	}
    }
    java rookie? garbage? sorry? are these java ?

Similar Threads

  1. return help
    By Kalle in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 21st, 2012, 05:33 PM
  2. return;
    By tarkal in forum Java Theory & Questions
    Replies: 3
    Last Post: November 26th, 2011, 10:53 AM
  3. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  4. [SOLVED] Return statement.
    By Melawe in forum Java Theory & Questions
    Replies: 18
    Last Post: June 20th, 2011, 11:54 AM
  5. how to return value ..
    By gr8mind in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 11th, 2011, 05:27 AM