Results 1 to 5 of 5
  1. #1
    gellegbs is offline Member
    Join Date
    Apr 2013
    Location
    USA
    Posts
    7
    Rep Power
    0

    Default Can you have methods within the main method?

    More specifically can you have methods within a main method and call on each non-main method separately and independently from each other? I'm not sure how to have each calculator act independently of the other (ei running areaOfCircle and not slope). I was grasping at straws when i added the "private final class" declarations for slope and areaOfCircle classes. This was really a wild guess to see what would work but I'm stuck. I hope I'm not sounding too confusing... here's the code

    Java Code:
    public class apples {
    	public static void main(String args[]){
    		
    		private final class slope{
    			slope slopeObject = new slope();
    			slopeObject.slopeMessage();
    		}
    		
    		private final class areaOfCircle{
    			areaOfCircle areaOfCircleObject = new areaOfCircle();
    			areaOfCircleObject.areaOfCircleMessage();
    		}
    	}
    }
    next is calculator for slope

    Java Code:
    import java.util.Scanner;
    
    public class slope extends apples{ 
    	public void slopeMessage(){
    		Scanner calc = new Scanner(System.in);
    		
    	double y1, y2, x1, x2;
    	System.out.println("Enter first Y coordinate ");
    	y1 = calc.nextInt();
    	System.out.println("Enter second Y coordinate ");
    	y2 = calc.nextInt();
    	System.out.println("Enter first X coordinate ");
    	x1 = calc.nextInt();
    	System.out.println("Enter second X coordinate ");
    	x2 = calc.nextInt();
    	
    	
    	double yDif = (y2 - y1);
    	double xDif = (x2 - x1);
    	double m = (yDif / xDif);
    	
    	
    	
    	System.out.println ("The slope is " +m);
    	calc.close();
    	}
    	
    
    }
    and then calculator for area of circle
    Java Code:
    import java.util.Scanner;
    import java.lang.Math;
    
    public class areaOfCircle extends apples{
    	public void areaOfCircleMessage(){
    	Scanner aCalc = new Scanner(System.in);
    		//Area of a circle, A=(Pi)r^2
    		double Pi = 3.14159;
    		double r;
    		System.out.println("Enter radius: ");
    		r = aCalc.nextInt();
    		
    		double r2 = Math.pow(r, 2);
    		double A =(r2 *= Pi);
    		
    		
    		
    		System.out.println("The area of this circle is " +A);
    	
    	
    	aCalc.close();
    	}
    }

    Perhaps I need more guided exercises. If anyone could direct me to a place where there are exercises I could practice on I would be grateful.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Can you have methods within the main method?

    You can't define methods directly within main() or any other method. Your example shows something else: you define classes within main(). This is perfectly possible, and almost certainly not what you should be doing.

    So, what are you trying to do? In particular what does mean to cause methods to run "separately and independently from each other"? Perhaps it would help to describe precisely what you expect the output (or other program behaviour) to be for some specified input.

    ---

    It would help clarify things if you used standard Java coding conventions. Classes start with an upper case letter and generally take the form of a noun - because classes define a type of thing. Methods start with a lower case letter and generally take the (imperative) form of verbs (== orders or commands): eg, displayAreaOfACircle(). The main() method is an honourable exception because it was called that long before the thing/command distinction was made the centerpeice of coding and programmatic thinking. Classes, methods, variables all take names that are descriptive of what they are or do.

  3. #3
    gellegbs is offline Member
    Join Date
    Apr 2013
    Location
    USA
    Posts
    7
    Rep Power
    0

    Default Re: Can you have methods within the main method?

    Ok sorry I didn't know about the naming standards. I was trying to make practice problems for myself, but I am failing at that lol. I wanted to run separate calculators and when i ran the slope calc java I would get asked for y coordinates... and really just a total mess. My main problem is I don't understand how methods and classes are organized and apparently I'm getting methods confused with classes. I'm trying not to resort to copying someone else's code and not understanding it (which is what is happening with the video tuts I'm doing). Unfortunately I don't have the means to go to school for this or get any formal training (single mom with 4 kids) so i can only do this as a hobby and maybe get materials from the library or something for practice, but I don't really have any good beginner Java practice guides. There's tons of beginner tuts (I'm using NewBoston's vids) but I find myself understanding when he's talking and when I try it on my own I feel like I don't quite grasp why and what. I have done a google search with keywords like "java practice for beginners" etc. and the results aren't very good or the practice is way over my head.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Can you have methods within the main method?

    Check out the tutorial link in my signature. It's a good start and starts with the basics and moves on up.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    gellegbs is offline Member
    Join Date
    Apr 2013
    Location
    USA
    Posts
    7
    Rep Power
    0

    Default Re: Can you have methods within the main method?

    I do use oracle's Java tuts along with the video tuts. I am hoping to find practice problems somewhere.
    When I do copy the code from the tuts I find myself asking why are they using this and what if I do that...and then I get confused


    Ok I think I get my misunderstanding with the classes. Oracle tuts said:
    By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.
    So in a project all the classes should work together like a bike does, they cannot work independently like a bike gear and a car's fuel filter. Even though both vehicles get you places the project and package only pertain to the bike not the bike and car. Am I right?
    Last edited by gellegbs; 05-01-2013 at 06:07 AM.

Similar Threads

  1. Replies: 4
    Last Post: 03-28-2013, 08:40 PM
  2. No main methods ?
    By Niehm in forum New To Java
    Replies: 2
    Last Post: 04-22-2012, 03:16 AM
  3. how can main call other methods?
    By rahuld.exe in forum New To Java
    Replies: 1
    Last Post: 04-06-2012, 01:19 PM
  4. Replies: 9
    Last Post: 11-18-2011, 10:47 PM
  5. Classes and main methods?
    By CyberFrog in forum New To Java
    Replies: 17
    Last Post: 05-26-2009, 04:47 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
  •