Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you. Members have full access to the forums.
Advertisements are removed for registered users. Remember Me?
Java GUI Calculator Source Code
Advanced Search. Tip Calculator for newbie Results 1 to 3 of 3. Thread: Tip Calculator for newbie Hey all. I hope I'm posting in the right place. I'm pretty new to Java meaning this is only my third program besides 'hello world'.
Subscribe to RSS
I have a tip calculator I'm working on for an assignment. I'm not getting an 'error' as such - the program runs, but the result is consistently wrong. I'm wondering if it's to do with the 'split' variable being a double, since the 'total' amount seems to be correct when run. The issue seems to be when it tries to divide the 'total' by 'split' - and consistently returns 'infinity' in the console.
I have my program set up in two classes: tipCalc1 and tipCalc2 no points for creative names of course. Here's what I have so far. Any assistance appreciated, thanks. Last edited by Nootoojaava; May 19th, at AM. Reason: Added highlight tags. Related threads: There must be a better way Do While Loop Help! Newbie's Avatar Where am I going wrong?GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Skip to content. Permalink Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. Branch: master. Find file Copy path. No definitions found in this file. Cannot retrieve contributors at this time. Raw Blame History. SplitPaymentCalculator ; import java. ActionEvent ; import java. ActionListener ; import java. NumberFormat ; import java.
ParseException ; import javax. ComboBoxModel ; import javax. DefaultComboBoxModel ; import javax. JButton ; import javax. JCheckBox ; import javax.
Intro To Java Methods
JComboBox ; import javax. JFormattedTextField ; import javax. JLabel ; import javax.
JOptionPane ; import javax.After the "Hello World! The reason for this is because of the simplicity of its structure in addition to covering most of the basic concepts in programming. Follow the steps below and you too will be able to build your own fully functioning calculator! Did you use this instructable in your classroom? Add a Teacher Note to share how you incorporated it into your lesson.
All software programs are written on Development Environments, programs made specifically to build and compile software. For the programming language we're using, Java, the program Dr. Java is an excellent and simple introductory program to use. Start by downloading the program from www. After you first open Dr.Android Tip Calculator Tutorial, Chapter 3: Java - Part 12
Java, you'll be greeted with a blank screen of nothing. So beautiful and pristine, we're going to cover this baby with a lot of code. Start off by declaring a public Calculator class and then inside of it, a main method, exactly like shown in the picture. In Java, a class is simply an object which holds information.
In our case, the object we're building is a calculator so we have named it as such. A method is a process which performs a certain task. All methods follow the IPO structure as we will learn later. Brackets in java denote what is contained in what. In our example, our calculator object contains the method we are going to build. The brackets of the method is where our code is going to be contained.A Java method is a collection of statements that are grouped together to perform an operation.
When you call the System. Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design. Method definition consists of a method header and a method body. The method signature consists of the method name and the parameter list. These are optional, method may contain zero parameters.
Here is the source code of the above defined method called min. For using a method, it should be called. There are two ways in which a method is called i. The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method.
The methods returning void is considered as call to a statement. The void keyword allows us to create methods which do not return a value. Here, in the following example we're considering a void method methodRankPoints. This method is a void method, which does not return any value. Call to a void method must be a statement i. It is a Java statement which ends with a semicolon as shown in the following example.
While working under calling process, arguments is to be passed. These should be in the same order as their respective parameters in the method specification. Parameters can be passed by value or by reference. Passing Parameters by Value means calling a method with a parameter. Through this, the argument value is passed to the parameter.
The following program shows an example of passing parameter by value. The values of the arguments remains the same even after the method invocation. When a class has two or more methods by the same name but different parameters, it is known as method overloading. It is different from overriding. In overriding, a method has the same method name, type, number of parameters, etc. Then the concept of overloading will be introduced to create two or more methods with the same name but different parameters.
Overloading methods makes program readable. Here, two methods are given by the same name but with different parameters. The minimum number from integer and double types is the result. Sometimes you will want to pass some information into a program when you run it.
This is accomplished by passing command-line arguments to main.
Java - Methods
A command-line argument is the information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy.
It only takes a minute to sign up. I've built a fairly simple tip calculator. What I have so far is a bunch of functions to calculate each tip amount and calling each with eventListener. It works however trying to see if there's a better approach that would modularize the code. I'm thinking possibilities include passing functions as arguments or functions calling other functions but can't figure it out yet.
A golden coding rule is Don't Repeat Yourself D. Y which outwardly may seam a simple rule to follow but for the novice coder this is not as straight forward as it sounds. Repeated code does not always look like repeated code. You can use search and replace to change most repeated code, repeated code is seldom the same text repeated over and over.
Most frequently delivered to the client device separately. Repetition is commonly exasperated by the need to communicate between the two. They are clearly not that repeating, a bit of a pattern that may not always stand outsome repeated tokens function returnetc that is unavoidable. The function names need to be defined and then called, thus at min require two copies. One source of repetition is logic being applied. Ignoring the numbers both functions do the same thing, add values, with each function handling custom data values.
Much as you have done with calcTencalcFifteenetc they all do the same thing, calculate a percentage and display it, but you have done it 4 times. There are just 4 values 10, 20, 30, 40, each repeated 7 times. In your code you have 4 values 3 fixed and one variable. Not only have you repeated that value 9 times, you have done so in about as many formats as possible. For the effort of one more key the 0 is worthy readability.
The thing you are on the right track to good dry code if you look at the custom value, it can take any percentage and format a result. At the most basic level you could have just used the click events to set the custom. The data billtip percent quick options, and resulting display text and format are mostly semantic and belong on the page.Methods are used to perform certain actions, and they are also known as functions.
A method must be declared within a class. It is defined with the name of the method, followed by parentheses. Java provides some pre-defined methods, such as System. To call a method in Java, write the method's name followed by two parentheses and a semicolon. In the following example, myMethod is used to print a text the actionwhen it is called:. Inside maincall the myMethod method:. In the next chapter, Method Parametersyou will learn how to pass data parameters into a method.
Insert the missing part to call myMethod from main. If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:. HOW TO. Your message has been sent to W3Schools. W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
I have made a basic calculator using methods. This is my first attempt at using methods and would like to see if I can improve on this as there is a lot of repeated code. Redundancy: Any time you are copy-pasting code, alarm bells should go off in your head. You do this in multiple places. Most notable is:.
What that means is that this is a prime candidate for encapsulation into its own method. Something like this:. Now we can simply call this method whenever we need to get numbers from the user. Much easier, and we've reduced the complexity of our code. You really only need to do this once.
If it were me, I'd have it as a static field for the entire class, e. This is the basic rule for how constants are defined in Java.
This would also be a perfect use-case for enums, but that my be getting a bit too complicated for a starting project.
Small final note: I think you have a bug. The line where you display the results always says "sum", even when you're doing division, multiplication, or subtraction. I think you should ask for the first number then ask for the operation and then ask for the third number. Eventually you can add something to this so that it distinguishes the operators separately and the user can write an expression rather than answer questions about the expression, I think that is really the goal right?
Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Very basic calculator using methods Ask Question.