The Artima Developer Community
Sponsored Link

Java Answers Forum
calling methods from main

6 replies on 1 page. Most recent reply: Mar 2, 2006 2:35 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

calling methods from main Posted: Mar 1, 2006 6:04 AM
Reply to this message Reply
Advertisement
I'm having trouble figuring out how to call these methods from my main lethod (or maybe from the run method, not sure where it belongs).

Here's the gist of what I've got.

public class lab3
{
private scanner sc
}

//creating the scanner object. Fine.

public lab3()
{
sc = new Scanner(System.in);
}
//constructor, also fine
public void setScanner(Scanner testScanner)
{
sc = testScanner;
}
//making the class use the scanner. FIne I think.
public int displayMenu()
{
int ret = o;
System.out.println(stuff);
return ret;
}
//I shortened this, but it's a menut that gives options 1-6.
//They call volume calculators and 6 exits.
//I think that I may need to set up a sc.nextInt for the
//ret variable, but every time I try I get compile errors.
//I also think I may need some if else statments here to
//run the menu, though I may need them in the run method.
//(I then have calcVolum methods for a bunch of shapes).
public void run()
{
int selection = displayMenu();
}
//runs the menu. Fine.
public static void Main(Strings [] args)
{
Lab3 c = new Lab3();
c.run();
}


That's all she wrote.

Right now the menu runs, but I'm not sure how to take hte user input and use it to call a method. I think I need an if else series of statments, but what I've got now (basically if ret == X double answer = calcVolume method). That's not working, though I'm not sure if it's syntax or if I'm just doing it wrong.

Thoughts? Help? Please?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: calling methods from main Posted: Mar 1, 2006 6:51 AM
Reply to this message Reply
1. reformat your code using the java tagsd as shown on the right side of the input field, so someone might actually read it.

2. Which compile errors do you get?
Post the code you' tried to write and put a mark on the line where the error occured.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: calling methods from main Posted: Mar 1, 2006 6:52 AM
Reply to this message Reply
Just to be clear: Reformatting does not mean to copy the code from your previous message, but from the compiler.
Put the java tags before and after the code.

joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

Re: calling methods from main Posted: Mar 1, 2006 7:04 AM
Reply to this message Reply
import java.util.Scanner;
 
 
public class Lab3
{
    private Scanner sc;
 
    /*
     * add other instance fields as necessary.
     */
    
    /**
     * Constructor for objects of class Lab3.
     */
    public Lab3()
    {
        sc = new Scanner(System.in);
        /*
         * add lines as necessary.
         */
    }
    
    /**
     * Makes Lab3 use the given Scanner.
     * @param  testScanner Scanner to use
     */
    public void setScanner(Scanner testScanner)
    {
        sc = testScanner;
    }
    
    /**
     * Displays the menu to the user.
     * 
     * @return The option that the user picked
     */
    public int displayMenu()
    {       
        int ret = 0;
        System.out.println("Please use the menu below to select a shape.");
        System.out.println("You will be prompted to provide object variables.");
        System.out.println("");
        System.out.println("1. Build a Block.");
        System.out.println("2. Build a Cube.");
        System.out.println("3. Build a Pyramid.");
        System.out.println("4. Build a Sphere.");
        System.out.println("5. Build a Torus.");
        System.out.println("6. Quit the program.");
        System.out.println("");
        System.out.print("Please enter your selection: ");
        int menuSelection = sc.nextInt();
        //Students fill in here
        // Option 1 should be to build a block
        // Option 2 should be to build a cube
        // Option 3 should be to build a pyramid
        // Option 4 should be to build a sphere
        // Option 5 should be to build a torus
        // Option 6 should be to quit the program
        // Any other selection is illegal and the menu should be displayed,
        //and the user prompted for another response until a legal value is entered.
 
        return menuSelection;
        
        if (menuSelection == 1)
        {
            double blockThing = calcBlockVolume();
        }
        else
        {
            int selection = displayMenu();
        }
    }
  
    /**
     * Calculates the volume of a torus.
     * 
     * @return     The volume of the torus
     */
    public double calcTorusVolume()
    {
        System.out.print("Please enter the inner radius:");
        double innerRadius = sc.nextDouble();
        System.out.print("Please enter the outer radius:");
        double outerRadius = sc.nextDouble();
        
        double ret = 0.0;
        //fill in here        
        return ret;
    }
    
    /**
     * Calculates the volume of a pyramid.
     * 
     * @return     The volume of the pyamid
     */
    public double calcPyramidVolume()
    {
        System.out.print("Please enter the side length:");
        double l = sc.nextDouble();
        System.out.print("Please enter the width:");
        double w = sc.nextDouble();
        System.out.print("Please enter the height:");
        double h = sc.nextDouble();
        
        double ret = 0.0;
        //fill in here        
        return ret;
    }
    
    /**
     * Calculates the volume of a sphere.
     * 
     * @return     The volume of the sphere
     */
    public double calcSphereVolume()
    {
        System.out.print("Please enter the radius:");
        double innerRadius = sc.nextDouble();
        
        double ret = 0.0;
        //fill in here        
        return ret;
    }
    
    /**
     * Calculates the volume of a cube.
     * 
     * @return     The volume of the cube
     */
    public double calcCubeVolume()
    {
        System.out.print("Please enter the side length:");
        double length = sc.nextDouble();
        
        double ret = 0.0;
        //fill in here        
        return ret;
    }
    
    /**
     * Calculates the volume of a block.
     * 
     * @return     The volume of the block
     */
    public double calcBlockVolume()
    {
        System.out.print("Please enter the side length:");
        double l = sc.nextDouble();
        System.out.print("Please enter the width:");
        double w = sc.nextDouble();
        System.out.print("Please enter the height:");
        double h = sc.nextDouble();
        
        double ret = 0.0;
        //fill in here        
        return ret;
    }
   
  
    /**
     * The run method for the class.
     */
    public void run()
    {
        
        int selection = displayMenu();
        
    }
    
    /**
     * The main method for the program.
     */
    public static void main(String [] args)
    {
        Lab3 c = new Lab3();
        c.run();
    }
}0


Sorry, the code was origionally on another system. Took some transfering. In the above I get an unreacheable statment error at the if statment of the menu.

If I remove hte if statment, it runs fine, but I just enter a value and that's it. An if else statment of some sort will be needed to make hte menu actually work, but is that where I need it? if not, how can I implement the ret variable elsewhere (if I try to place it anywhere else I get an unknown variable error).

Sorry about the formatting.

Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: calling methods from main Posted: Mar 1, 2006 12:55 PM
Reply to this message Reply
There is this statement before the if block:
return menuSelection;

Anything after the return statement is essentially dead code - there is no way for the program to execute the if block. That is what the error message says very clearly.

joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

Re: calling methods from main Posted: Mar 1, 2006 1:39 PM
Reply to this message Reply
All right. I see that. Here's a question.

In the original code I had what is effectively a code skeleton provided. It had int ret = 0 at the start and int return ret at the end (where the menuSelection is now).

Does that mean that I can use the ret variable provided as long as the if else statments come before the return line?

My instinct was to assume that the return line simply passed off the user's response to be inperpreted. Does it actually have the effect of clearing it instead? If so, can you maybe touch on why (does it return it to void? Or does return, here, simply mean somethething different than what I'm assuming)?

I'll try to drop the if statments in before the return and see how that goes.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: calling methods from main Posted: Mar 2, 2006 2:35 AM
Reply to this message Reply
The return line obviously returns the method's return value and - even more important - returns the program pointer to the method caller, it exits the method.

Every non final variable inside this method is removed from the system memory after that (not exactly, but it comes close to what really happens).
So if you call the method again, all variables will be reset.

In your case it would be best to create a while loop in the main program which repeats displayMenu as long as the returnvalue is not 6.

Or you create this loop inside the displayMenu method and return nothing (set the return type to void). Then you can eliminate the return line, because return without a return value is executed atomatically when the end of a method is reached.

Flat View: This topic has 6 replies on 1 page
Topic: calling a JApplet from JSP Previous Topic   Next Topic Topic: how to read jsp tag in using xml

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use