PDA

View Full Version : My first calculater



osmm
03-09-2008, 05:36 PM
Its not that great, you can only do the basics, + - / and *. Also if the answer is a decimal it will automaticly show 0. Other wise it is prety good. Right now it only handles 2 numbers. I coulda make it use more but then stuff would be all messed up. Anyways to anyone who actually looks at this section here it is:



public class Calculator
{
public static void main(String[]args)
{
String symbol1 = "division";
/* what type of math to do;
* Addition
* Subtraction
* Multiplaction
* Division
*/
int num1 = 2; // number one here
int num2 = 2; // number two here
//-------------------------------------------------------\\
//-------------------------------------------------------\\
//-------------------------------------------------------\\
if (symbol1 == "multiplaction")
{
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
}
if (symbol1 == "division")
{
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
}
if (symbol1 == "subtraction")
{
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
}
if (symbol1 == "addition")
{
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
}
}

Yakman
03-09-2008, 06:29 PM
i see you've done things like this


if (symbol1 == "multiplaction")


String is an object, objects should not be checked for equality with the "==" operator, that is only for privative types (int, byte, long, short, char and boolean)

here is one useful fact
In java, when you have a variable containing an object, you dont have the object, you only have a referance to that object

when you use "==" on objects, you are only checking if they are stored in the same location, that is not the same as checking if they are equal

this code will check if the strings are really equal, it uses the method equals()


if (symbol1.equals("multiplaction"))
{
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
}
if (symbol1.equals("division"))
{
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
}
if (symbol1.equals("subtraction"))
{
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
}
if (symbol1.equals("addition"))
{
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}

osmm
03-09-2008, 07:05 PM
Thanks yakman, but I got a question. Where is the library type thing such as SRL has SRL Manual. I know there has to be one for things too look up like system.out.println. The only way I got that one was from tutorials. Basicly I want to know what types of codes there are and stuff....Yea I explained it bad, but do you get what I am asking for?

(Using Eclips).

[Off-topic] how the hell does Open Source = Equality, Fairness, Inevitable, and especially Comunism? I like open source for small things like our scripts on the site, but bigger things I'd never do open source.

Yakman
03-10-2008, 05:00 PM
i get what you're asking, the JDK6 docs http://java.sun.com/javase/6/docs/
for your example of System.out.println(), "System" is a class with a field called "out", http://java.sun.com/javase/6/docs/api/java/lang/System.html#out
"out" is an instance of the class PrintStream, which contains the method println() http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html#println(java.lang.String)


as for the open source part
In my opinion, open source is similar to communism because in closed-source, the program is effectivly owned by the company which makes it, they sell the application for their own profit,
in open source, the program is owned by all, everyone can easily used it, learn from it, improve it.

the reason i think its inevitable is because the open source movement is gaining momentum over the past few years, the entire java platform is open source, theres Linux, Firefox, Wikipedia, Apache HTTP server, MySQL, PHP, the GNU Compilers which are all open source.
Apache serves 70% of the worlds websites, whole forums are written in PHP, servers are mostly run on linux operating systems

^^these are big things, not the small scripts we make on this site, and they are all open source, tell me why you wouldnt do open source for big things?

I do stress the, "In my opinion" part, there are many many people in this forum who disagree with me.

P1nky
03-12-2008, 02:30 AM
You Got To Teaaaaaaach Me!

yargo
03-15-2008, 09:51 PM
hey you guys are using eclipse right? to set up documentation (so that you can view it for a particular class just by highlighting it in your script, go to
Window -> Preferences
Expand java, then click Installed JREs
Highlight jre1.6.### and click edit
Find rt.jar, highlight it and click javadoc location
put this url there without the quotes: "http://java.sun.com/javase/6/docs/api/"

Now when you click something like println() it will show you the javadocs for that method/class. Useful, eh?