Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29445

Re: Help with methods, classes and parameters

From Scott Dunning <scottwd01@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Help with methods, classes and parameters
Date 2014-07-30 16:59 -0700
Organization A noiseless patient Spider
Message-ID <lrc0n6$nqt$1@dont-email.me> (permalink)
References <lra1dp$9jh$1@dont-email.me> <lra7be$l2s$1@gregory.bnet.hr>

Show all headers | View raw


On 7/30/14, 12:37 AM, Josip Almasi wrote:
> Scott,
>
> first, search 'static' and delete it all. Nothing should be static,
> except static void main.
> Nothing will work then. But then, make your main like this:
>
> Menu menu = new Menu();
> String s1 = menu.getInput("Enter a numeric value: ");
> String s2 = menu.getInput("Enter a numeric value: ");
> String option = menu.getInput("Enter: \n 1=Add \n 2=Subtract \n
> 3=Multiply \n 4=Divide");
>
> See, static methods are as close to functions as it gets in java.
> They work on classes. But consider this:
>
> How many menus you can make with such a Menu class?
> Only one, right?
>
> Well, once you move methods from classes to objects (that's what
> removing 'static' does), you can have many simultaneous objects.
> Each object operates on it's own variables.
>
> That's important difference, and untill you get it, go no further.
> Point of these exercises is to understand this.
>
> Allright, then the code will not work, and time to understand the
> visibility.
> You have to ensure that Action and Menu class work together.
> Usually, it's done like this:
>
> Action a = new Action(menu);
>
> Action class then needs a constructor like this:
>
> public class Action {
>    Menu menu;
>    public Action( Menu menu ) {
>      this.menu = menu;
>    }
> }
>
> After that, all methods in Action class can access member variable menu,
> and call it's methods: menu.whatever().
>
> OK let's suppose you now understand class/object difference.
> Next thing troubling you is visibility.
> It's hard to reach variables and methods of one object from the other,
> right?
> Compiler is bitching about whatever you try, so you make it all static
> and it suddenly works.
>
> Simple hint about visibility: {}
> Whatever is between is visible, can reach each other.
> That hint applies to classes, objects, statements.
>
> Finally, avoid inner classes.
> (your Menu and Action are inner classes of Driver class)
> Always put one class in one java file.
> You will find it harder to code this way, but you will learn visibility
> faster.
>
> Regards...
>

Thanks Josip, you've helped a lot!  I used a lot of your advice and 
changed up my code and got it working!  I'd love to get some advice to 
see if I did it correctly and if there are any major changes you'd make. 
  I have a couple questions that I hope you can help with.  Having an 
IDE like Eclipse is great because it helps but it also hinders in 
learning correctly.  So, I made all of my methods non-static and 
specifically in action, I'm calling those methods in menu but, I'm not 
sure how they're working together.  I thought it should be 
Action.addValues(num1, num2); but ince the methods are no longer static 
Eclipse didn't like that and I had to essentially create a new variable 
but, how does that help communicate with the methods in Action?  Can you 
help me figure out why instead of Action.addValues I needed to create 
(option) option.addValues.   I also moved all of my classes to separate 
files, and you're right it actually helped me because things were less 
cluttered.  I also changed all of my methods from Strings to double so 
that I didn't have to then convert everything from a String to a double, 
was that a good move?  Also, why the m in Menu m = new Menu() & 
m.getMenu. Could I just call it like this...Menu.getMenu(); instead of 
what I guess is creating a new variable (m)?



This is my last question...In the switch with the default being if they 
enter an option that is not listed, how can I get it to restart the 
switch if they enter the wrong option and not have to rerun the program? 
Can I do that inside the switch or would I need to put the switch inside 
a while loop?


public class Driver {

	public static void main(String[] args) {
	
		Menu m = new Menu();
	    m.getMenu();
	
	}
}		
	
import java.util.Scanner;

     class Menu {	
	    public void getMenu() {
	    	Scanner scan = new Scanner(System.in);
	        System.out.println("Enter the first number :");
	        double num1 = scan.nextDouble();
	        System.out.println("Enter the second number :");
	        double num2 = scan.nextDouble();
	        System.out.println("Please select an operation :");
	        System.out.println("1 - Add " + "\n" + "2 - Subtract" + "\n"
	                + "3 - Multiply" + "\n" + "4 - Divide");
	
	        int optionInt = scan.nextInt();
	        double result;
	        Action option = new Action();
			
			
			switch (optionInt) {
			case 1:
				result = option.addValues(num1, num2);
				option.displayResult(result);
				break;
			case 2:
				result = option.subtractValues(num1, num2);
				option.displayResult(result);
				break;
			case 3:
				result = option.multiplyValues(num1, num2);
				option.displayResult(result);
				break;	
			case 4:
				result = option.divideValues(num1, num2);
				option.displayResult(result);
				break;	

			default:
				System.out.println("You entered an incorrect option");
			}
	    	
	    	
	    	
	    	
	    	
	    	
		}
     }


class Action {
	
	public double divideValues(double d1, double d2) {
		double result = d1 / d2;
		return result;
	}

	public double multiplyValues(double d1, double d2) {
		double result = d1 + d2;
		return result;
	}

	public double subtractValues(double d1, double d2) {
		double result = d1 - d2;
		return result;
	}

	public double addValues(double d1, double d2) {
		double result = d1 + d2;
		return result;
     }
	public void displayResult(double result) {
		System.out.println("The answer is " + result);
	}
	
  }


Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Help with methods, classes and parameters Scott Dunning <scottwd01@gmail.com> - 2014-07-29 22:58 -0700
  Re: Help with methods, classes and parameters Josip Almasi <joe@vrspace.org> - 2014-07-30 09:37 +0200
    Re: Help with methods, classes and parameters Scott Dunning <scottwd01@gmail.com> - 2014-07-30 16:59 -0700
      Re: Help with methods, classes and parameters Josip Almasi <joe@vrspace.org> - 2014-07-31 10:42 +0200
    Re: Help with methods, classes and parameters Scott Dunning <scottwd01@gmail.com> - 2014-07-30 17:29 -0700
    Re: Help with methods, classes and parameters Arne Vajhøj <arne@vajhoej.dk> - 2014-07-30 20:54 -0400
      Re: Help with methods, classes and parameters markspace <markspace@nospam.nospam> - 2014-07-30 18:43 -0700
        Re: Help with methods, classes and parameters Scott Dunning <scottwd01@gmail.com> - 2014-07-30 22:01 -0700
          Re: Help with methods, classes and parameters markspace <markspace@nospam.nospam> - 2014-07-31 09:45 -0700
            Re: Help with methods, classes and parameters Steven Simpson <ss@domain.invalid> - 2014-08-01 09:00 +0100
              Re: Help with methods, classes and parameters markspace <markspace@nospam.nospam> - 2014-08-01 09:07 -0700
            Re: Help with methods, classes and parameters Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2014-08-01 12:02 +0300
  Re: Help with methods, classes and parameters Jeff Higgins <jeff@invalid.invalid> - 2014-07-30 17:57 -0400

csiph-web