Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #29434 > unrolled thread
| Started by | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| First post | 2014-07-29 22:58 -0700 |
| Last post | 2014-07-30 17:57 -0400 |
| Articles | 13 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| Date | 2014-07-29 22:58 -0700 |
| Subject | Help with methods, classes and parameters |
| Message-ID | <lra1dp$9jh$1@dont-email.me> |
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
[toc] | [next] | [standalone]
| From | Josip Almasi <joe@vrspace.org> |
|---|---|
| Date | 2014-07-30 09:37 +0200 |
| Message-ID | <lra7be$l2s$1@gregory.bnet.hr> |
| In reply to | #29434 |
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
>
[toc] | [prev] | [next] | [standalone]
| From | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| Date | 2014-07-30 16:59 -0700 |
| Message-ID | <lrc0n6$nqt$1@dont-email.me> |
| In reply to | #29436 |
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);
}
}
[toc] | [prev] | [next] | [standalone]
| From | Josip Almasi <joe@vrspace.org> |
|---|---|
| Date | 2014-07-31 10:42 +0200 |
| Message-ID | <lrcvh8$qe8$1@gregory.bnet.hr> |
| In reply to | #29445 |
On 07/31/2014 01:59 AM, Scott Dunning wrote: > > 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? A loop allright. And exit the loop with case 0: break; (to answer the other question) Regards...
[toc] | [prev] | [next] | [standalone]
| From | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| Date | 2014-07-30 17:29 -0700 |
| Message-ID | <lrc2g8$1ph$1@dont-email.me> |
| In reply to | #29436 |
Actually you can scratch the question about Action.addValues and how that's all working I think I understand now. If you can help me with that other question I'd appreciate it! Thanks, Scott
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-07-30 20:54 -0400 |
| Message-ID | <53d993b9$0$299$14726298@news.sunsite.dk> |
| In reply to | #29436 |
On 7/30/2014 3:37 AM, Josip Almasi wrote: > first, search 'static' and delete it all. Nothing should be static, > except static void main. > 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. This is good advice for original poster. This is good advice for any beginner. One should just be aware that there are some (few but some) cases where static and inner classes make sense. Arne
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-07-30 18:43 -0700 |
| Message-ID | <lrc70j$rht$1@dont-email.me> |
| In reply to | #29447 |
On 7/30/2014 5:54 PM, Arne Vajhøj wrote:
> One should just be aware that there are some (few but some) cases
> where static and inner classes make sense.
Just to toss out an example of static methods, when a class's purpose is
only to provide functions (and sometimes helpful utility methods), you
should make all the methods static, and make the class final and
non-instantiable.
For example, the java.lang.Math class contains only mathematical
functions. All of its methods are static, and the class itself it final
and non-instantiable.
package myproject;
public class MyUtilities {
// all private ctors makes the class final and
// non-instantiable.
private MyUtilities() {}
public static void doThis() { ...
}
public static double calculateThat() { ...
}
}
[toc] | [prev] | [next] | [standalone]
| From | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| Date | 2014-07-30 22:01 -0700 |
| Message-ID | <lrcidd$ndj$1@dont-email.me> |
| In reply to | #29449 |
On 7/30/14, 6:43 PM, markspace wrote:
> On 7/30/2014 5:54 PM, Arne Vajhøj wrote:
>
>> One should just be aware that there are some (few but some) cases
>> where static and inner classes make sense.
>
> Just to toss out an example of static methods, when a class's purpose is
> only to provide functions (and sometimes helpful utility methods), you
> should make all the methods static, and make the class final and
> non-instantiable.
>
> For example, the java.lang.Math class contains only mathematical
> functions. All of its methods are static, and the class itself it final
> and non-instantiable.
>
> package myproject;
>
> public class MyUtilities {
>
> // all private ctors makes the class final and
> // non-instantiable.
>
> private MyUtilities() {}
>
> public static void doThis() { ...
> }
>
> public static double calculateThat() { ...
> }
>
> }
>
>
Thanks everyone, you helped me understand everything a lot more!
I have another quick question. One of the options in in the menu is if
the user enter 0 to quit the program. I'm not sure how to go about that
in the switch statement?
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-07-31 09:45 -0700 |
| Message-ID | <lrdrqp$u82$1@dont-email.me> |
| In reply to | #29454 |
On 7/30/2014 10:01 PM, Scott Dunning wrote:
> I have another quick question. One of the options in in the menu is if
> the user enter 0 to quit the program. I'm not sure how to go about that
> in the switch statement?
You can execute a 'return' anywhere in a method to simply exit the
method. From 'main' it will just exit the program.
switch (optionInt) {
case 0:
return;
break;
I left in the break because when you edit a program, sometimes you
forget and change things, and the break helps prevent an unexpected fall
through when you make a change. It doesn't hurt anything to leave it in
even though the break is extraneous.
[toc] | [prev] | [next] | [standalone]
| From | Steven Simpson <ss@domain.invalid> |
|---|---|
| Date | 2014-08-01 09:00 +0100 |
| Message-ID | <c1urab-rlh.ln1@s.simpson148.btinternet.com> |
| In reply to | #29457 |
On 31/07/14 17:45, markspace wrote:
> switch (optionInt) {
> case 0:
> return;
> break;
>
> I left in the break because when you edit a program, sometimes you
> forget and change things, and the break helps prevent an unexpected
> fall through when you make a change. It doesn't hurt anything to
> leave it in even though the break is extraneous.
Hmm?
public class ReturnBreak {
public static void test(int code) {
switch (code) {
case 10:
return;
break;
}
}
}
$ javac ReturnBreak.java
ReturnBreak.java:6: error: unreachable statement
break;
^
1 error
--
ss at comp dot lancs dot ac dot uk
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-08-01 09:07 -0700 |
| Message-ID | <lrgdvn$1sg$1@dont-email.me> |
| In reply to | #29479 |
On 8/1/2014 1:00 AM, Steven Simpson wrote: > $ javac ReturnBreak.java > ReturnBreak.java:6: error: unreachable statement > break; Yup, my mistake. I didn't test that code, so I messed up.
[toc] | [prev] | [next] | [standalone]
| From | Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> |
|---|---|
| Date | 2014-08-01 12:02 +0300 |
| Message-ID | <lvegx07fch.fsf@saunalahti.fi> |
| In reply to | #29457 |
markspace <markspace@nospam.nospam> writes:
> switch (optionInt) {
> case 0:
> return;
> break;
> I left in the break because when you edit a program, sometimes you forget
> and change things, and the break helps prevent an unexpected fall through
> when you make a change. It doesn't hurt anything to leave it in even
> though the break is extraneous.
However, I suspect it may cause a warning about unreachable code and
even prevent compilation with some IDE settings.
--
Jukka Lahtinen
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-07-30 17:57 -0400 |
| Message-ID | <lrbq9m$if1$1@dont-email.me> |
| In reply to | #29434 |
On 07/30/2014 01: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. > This may seem abrupt, but no disrespect is intended. Again I'll suggest paper and a pencil. Draw a picture. Please become familiar with the fine manual. Java Platform, Standard Edition (Java SE) 8 <http://docs.oracle.com/javase/8/> The Java documentation Home page. The Java Tutorials <http://docs.oracle.com/javase/tutorial> The Java Tutorials Home page. Trail: Learning the Java Language <http://docs.oracle.com/javase/tutorial/java/index.html> Please start here. All of the questions you raise in this post are answered here. Java™ Platform, Standard Edition 8 API Specification <http://docs.oracle.com/javase/8/docs/api/index.html> Please become adept at reading the API documentation.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web