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


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

Help with methods, classes and parameters

From Scott Dunning <scottwd01@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Help with methods, classes and parameters
Date 2014-07-29 22:58 -0700
Organization A noiseless patient Spider
Message-ID <lra1dp$9jh$1@dont-email.me> (permalink)

Show all headers | View raw


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