Results 1 to 6 of 6
- 11-01-2012, 01:16 PM #1
Lazy
- Join Date
- Nov 2012
- Location
- Sindh
- Posts
- 3
- Rep Power
- 0
can this java calculator program be modified for my learning purpose?
This works just fine, but as you can see their is no "constructor" in this program! And their is only a scanner object! Can this program be more simplified in a way it has constructors and methods, as an example for me to learn ?
import java.util.Scanner;
public class calc {
public static void main(String args[]){
Scanner calculator = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = calculator.nextDouble();
System.out.println("Enter second number: ");
snum = calculator.nextDouble();
System.out.println("enter any operator: ");
String op = calculator.next();
switch (op){
case ("x") :
answer = fnum * snum;
System.out.println(answer);
break;
case ("/") :
answer = fnum / snum;
System.out.println(answer);
break;
case ("+") :
answer = fnum + snum;
System.out.println(answer);
break;
case ("-") :
answer = fnum - snum;
System.out.println(answer);
break;
}
}
}
- 11-01-2012, 01:55 PM #2
Re: can this java calculator program be modified for my learning purpose?
Please use code tags when posting code.
Sure, you could use OO principles to improve this code. What have you tried?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-01-2012, 02:27 PM #3
Lazy
- Join Date
- Nov 2012
- Location
- Sindh
- Posts
- 3
- Rep Power
- 0
Re: can this java calculator program be modified for my learning purpose?
Im sorry ... im new to this forum world
If u can give me an example ... easy for me to understand or change my code adding constructors and methods, it would really be appreciated ... im trying to learn about methods , objects and constructors using simple examples, a calculator in above case ..
- 11-01-2012, 02:32 PM #4
Re: can this java calculator program be modified for my learning purpose?
Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
Why don't you read through that and try to apply the ideas yourself, then we'll go from there.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-01-2012, 08:02 PM #5
Lazy
- Join Date
- Nov 2012
- Location
- Sindh
- Posts
- 3
- Rep Power
- 0
Re: can this java calculator program be modified for my learning purpose?
I am already following those lessons , but the practical examples given are not very helpful because they are limited ..
Thats why i posted this simple calculator program , which i learned from Bucky's java video tutorials..
I just want a simple program to demonstrate , "objects" , "methods" and "constructors" ... !!
so that i can make my logic clear on them ... my theory on these 3 is clear although ..
- 11-02-2012, 04:23 PM #6
Re: can this java calculator program be modified for my learning purpose?
Well, you could create a Calculator class that has methods for doing the different operations, and then call those methods based on user input.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Help with a java program, I'm just learning...
By pittypatsfan in forum New To JavaReplies: 1Last Post: 10-12-2012, 04:46 AM -
general purpose calculator
By j3sr2010 in forum NetBeansReplies: 3Last Post: 06-27-2011, 07:19 PM -
Getting Backspace on a java calculator program to work
By marylanddem in forum New To JavaReplies: 1Last Post: 12-09-2010, 01:01 AM -
unreachable statement - Java calculator program
By V2001Gordon in forum New To JavaReplies: 3Last Post: 12-13-2008, 12:57 AM -
How to get the last modified date of a file from a java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks