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.
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.
import java.util.Scanner;
publicclass 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
*/
publicvoid setScanner(Scanner testScanner)
{
sc = testScanner;
}
/**
* Displays the menu to the user.
*
* @return The option that the user picked
*/
publicint 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
*/
publicdouble 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
*/
publicdouble 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
*/
publicdouble 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
*/
publicdouble 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
*/
publicdouble 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.
*/
publicvoid run()
{
int selection = displayMenu();
}
/**
* The main method for the program.
*/
publicstaticvoid 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).
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.
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.
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.