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


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

Re: Help with methods, classes and parameters

From Josip Almasi <joe@vrspace.org>
Newsgroups comp.lang.java.programmer
Subject Re: Help with methods, classes and parameters
Date 2014-07-30 09:37 +0200
Organization B.net Hrvatska d.o.o.
Message-ID <lra7be$l2s$1@gregory.bnet.hr> (permalink)
References <lra1dp$9jh$1@dont-email.me>

Show all headers | View raw


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

On 07/30/2014 07:58 AM, Scott Dunning wrote:
> So I am super new to programming in general and I'm only a few weeks
> into a beginning summer course on Java and I'm super confused on this
> assignment for a simple java calculator that we need to use three
> classes in.
>
> I decided to write the code and get "working" in one class and then try
> and split it up into the necessary multiple classes. However, I'm having
> trouble doing that. I guess I'm more confused then I thought I was on
> how methods and parameters work. I'd greatly appreciate any help
> explaining how they work particularly in this program or something
> similar so that I can understand it more clearly.
>
> Here are a part of the instructions that I'm having a hard time with....
> "The Driver class is the only class wit hmain(String[]
> args)method.Driver class should call a method in the Menu class to print
> the menu, and from that method there should be calls to methods in
> Actions class for respective functionality from the menu."
>
> The Menu class and the method calls are working correctly and I planned
> on basically adding the rest to the Action class but I just keep
> confusing myself more every time I mess with it. I don't know if it
> makes a difference but I thought about adding the switch to the Menu
> class, also if I do, do I need to make it a method to call the switch?
> How would that work?
>
> import java.util.Scanner;
> import java.io.*;
>
> public class Driver {
>
> public static void main(String[] args) {
>
>      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");
>
> class Action {
>      int optionInt = Integer.parseInt(option);
>      double result = 0;
>
>      switch (optionInt) {
>      case 1:
>          result = addValues(s1, s2);
>          break;
>      case 2:
>          result = subtractValues(s1, s2);
>          break;
>      case 3:
>          result = multiplyValues(s1, s2);
>          break;
>      case 4:
>          result = divideValues(s1, s2);
>          break;
>
>      default:
>          System.out.println("You entered an incorrect value");
>      }
>
> public static String(
>      System.out.println("The answer is " + result);
> }
>
> private static double divideValues(String s1, String s2) {
>      double d1 = Double.parseDouble(s1);
>      double d2 = Double.parseDouble(s2);
>      double result = d1 / d2;
>      return result;
> }
>
> private static double multiplyValues(String s1, String s2) {
>      double d1 = Double.parseDouble(s1);
>      double d2 = Double.parseDouble(s2);
>      double result = d1 + d2;
>      return result;
> }
>
> private static double subtractValues(String s1, String s2) {
>      double d1 = Double.parseDouble(s1);
>      double d2 = Double.parseDouble(s2);
>      double result = d1 - d2;
>      return result;
> }
>
> private static double addValues(String s1, String s2) {
>      double d1 = Double.parseDouble(s1);
>      double d2 = Double.parseDouble(s2);
>      double result = d1 + d2;
>      return result;
>    }
>   }
>
> }
>
> class Menu {
>      public static String getInput(String prompt) {
>          String option;
>          Scanner scan = new Scanner(System.in);
>          System.out.println(prompt);
>          return option = scan.nextLine();
>      }
> }
> Thanks for any help!!
>
> Scott
>

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