SlideShare a Scribd company logo
Improve your App
As you probably have noticed, the code is absolutely
the same in each onClickListener except of the
sign operation between two variables.
The additional challenge in programming is to
decrease complexity and length of the code, and to
make as readable and concise as possible.
Mostly its done by swapping repeatable code with
functions, reusable piece of code.
The following function <calculate>, will:
1. Takes input <sign> (with type String).
2.Takes both calculator's input variables.
3. Through <if statements> defines the sign variable and
calculates result appropriately
4. Prints off result in <answer> TextField
private void calculate(String sign) {
firstVar = Integer.parseInt(firstInput.getText().toString());
secondVar = Integer.parseInt(secondInput.getText().toString());
if (sign == "+") result = firstVar + secondVar;
else if (sign == "-") result = firstVar - secondVar;
else if (sign == "/") result = firstVar / secondVar;
else if (sign == "*") result = firstVar * secondVar;
answer.setText(String.valueOf(result));
}
Now we can remove
previous code inside
onClick method with
function
calculate(<math sign
relatively to the button>)
plus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
calculate("+");
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
calculate("-");
}
});
mult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
calculate("*");
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
calculate("/");
}
});
Program is shorter but result is the
same.
Thats what we aim for other apps you
are going to make as well.
Now try to fix bug when you make
calculation without assigning
variables yourself.
(hint: use if statements to illuminate null value of the variables)
Program is shorter but result is the
same.
Thats what we aim for other apps you
are going to make as well.
Now try to fix bug when you make
calculation without assigning
variables yourself.
(hint: use if statements to illuminate null value of the variables)

More Related Content

PDF
selection structures
PDF
escape sequences and substitution markers
PDF
csharp repitition structures
PDF
A simple program C# program
DOCX
Compiler lab final report writing
PPT
DOCX
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
PPT
C chap02
selection structures
escape sequences and substitution markers
csharp repitition structures
A simple program C# program
Compiler lab final report writing
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
C chap02

What's hot (20)

PPT
C chap02
PPTX
Pseudocode
PPT
Effective Java - Always override toString() method
PPT
Program design techniques
PPTX
Pseudocode
PPT
05 control structures 2
PPT
C sharp_basic_ideas
PPTX
Data annotation validation (ASP.net)
PPTX
2.overview of c#
PDF
Visual basic asp.net programming introduction
DOCX
Bca2030 object oriented programming – c++
PPTX
Unit 7: Built-In Functions
PPTX
LISP: Errors In Lisp
PPT
Pseudocode algorithim flowchart
PPTX
C++ Programming Club-Lecture 2
PDF
Ge6161 lab manual
PDF
Basic c# cheat sheet
PPT
Best Techniques To Design Programs - Program Designing Techniques
PDF
Pseudocode By ZAK
PPTX
New features in C# 6
C chap02
Pseudocode
Effective Java - Always override toString() method
Program design techniques
Pseudocode
05 control structures 2
C sharp_basic_ideas
Data annotation validation (ASP.net)
2.overview of c#
Visual basic asp.net programming introduction
Bca2030 object oriented programming – c++
Unit 7: Built-In Functions
LISP: Errors In Lisp
Pseudocode algorithim flowchart
C++ Programming Club-Lecture 2
Ge6161 lab manual
Basic c# cheat sheet
Best Techniques To Design Programs - Program Designing Techniques
Pseudocode By ZAK
New features in C# 6
Ad

Viewers also liked (20)

ODP
Android tutorials7 calculator_intro
PPTX
Help your scrum team strike oil!
ODP
Android tutorials7 calculator_packageexploirer
PPT
видеоэкскурсия в красный берег(полная)
ODP
ER hypertensive crisis-respiratory distress
PPTX
Услуги стадиона «Динамо»
ODP
Android tutorials2 android_tools_on_eclipse
ODP
Patient safety program scd
PPSX
PPTX
Ray Kirby: Lectures are STILL Appropriate
ODP
Android tutorials1 install_ide
ODP
Github tutorial1
PDF
Wasicunwitkotko
ODP
Preventing the spread of infection
PDF
Construction Risk Management ABC's
PPT
уголовная ответственность
PPT
электронные физминутки
PPTX
Android tutorials7 calculator_intro
Help your scrum team strike oil!
Android tutorials7 calculator_packageexploirer
видеоэкскурсия в красный берег(полная)
ER hypertensive crisis-respiratory distress
Услуги стадиона «Динамо»
Android tutorials2 android_tools_on_eclipse
Patient safety program scd
Ray Kirby: Lectures are STILL Appropriate
Android tutorials1 install_ide
Github tutorial1
Wasicunwitkotko
Preventing the spread of infection
Construction Risk Management ABC's
уголовная ответственность
электронные физминутки
Ad

Similar to Android tutorials7 calulator_improve (20)

PPTX
Fundamental of programming Fundamental of programming
PDF
Maxbox starter
PPT
keyword
PPT
keyword
PPTX
C++ lecture 01
PPT
C++ functions
PPT
C++ functions
PPTX
Python Lecture 2
PPTX
Programming Fundamentals in Python - Sequence Structure
PPTX
C Programming Unit-1
PDF
1588147798Begining_ABUAD1.pdf
PDF
Consequences of using the Copy-Paste method in C++ programming and how to dea...
PPT
Programming For As Comp
PPT
Programming For As Comp
PPT
Visual basic 6.0
PPT
C++ Overview
PPT
Building blocks 2
Fundamental of programming Fundamental of programming
Maxbox starter
keyword
keyword
C++ lecture 01
C++ functions
C++ functions
Python Lecture 2
Programming Fundamentals in Python - Sequence Structure
C Programming Unit-1
1588147798Begining_ABUAD1.pdf
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Programming For As Comp
Programming For As Comp
Visual basic 6.0
C++ Overview
Building blocks 2

More from Vlad Kolesnyk (8)

ODP
Mobile andwebapps
ODP
Mech eng presentation
ODP
Android tutorials8 todo_list
ODP
Android tutorials8 todo_list
ODP
Android tutorials6 run_your_app
ODP
Android tutorials7 calculator
ODP
Android tutorials7 calculator_basiclayout
ODP
Android tutorials7 calculator_javaprogramming
Mobile andwebapps
Mech eng presentation
Android tutorials8 todo_list
Android tutorials8 todo_list
Android tutorials6 run_your_app
Android tutorials7 calculator
Android tutorials7 calculator_basiclayout
Android tutorials7 calculator_javaprogramming

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
PDF
Approach and Philosophy of On baking technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Spectroscopy.pptx food analysis technology
Approach and Philosophy of On baking technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Understanding_Digital_Forensics_Presentation.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.
Big Data Technologies - Introduction.pptx
NewMind AI Weekly Chronicles - August'25 Week I

Android tutorials7 calulator_improve

  • 2. As you probably have noticed, the code is absolutely the same in each onClickListener except of the sign operation between two variables. The additional challenge in programming is to decrease complexity and length of the code, and to make as readable and concise as possible. Mostly its done by swapping repeatable code with functions, reusable piece of code.
  • 3. The following function <calculate>, will: 1. Takes input <sign> (with type String). 2.Takes both calculator's input variables. 3. Through <if statements> defines the sign variable and calculates result appropriately 4. Prints off result in <answer> TextField private void calculate(String sign) { firstVar = Integer.parseInt(firstInput.getText().toString()); secondVar = Integer.parseInt(secondInput.getText().toString()); if (sign == "+") result = firstVar + secondVar; else if (sign == "-") result = firstVar - secondVar; else if (sign == "/") result = firstVar / secondVar; else if (sign == "*") result = firstVar * secondVar; answer.setText(String.valueOf(result)); }
  • 4. Now we can remove previous code inside onClick method with function calculate(<math sign relatively to the button>) plus.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub calculate("+"); } }); minus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub calculate("-"); } }); mult.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub calculate("*"); } }); div.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub calculate("/"); } });
  • 5. Program is shorter but result is the same. Thats what we aim for other apps you are going to make as well. Now try to fix bug when you make calculation without assigning variables yourself. (hint: use if statements to illuminate null value of the variables)
  • 6. Program is shorter but result is the same. Thats what we aim for other apps you are going to make as well. Now try to fix bug when you make calculation without assigning variables yourself. (hint: use if statements to illuminate null value of the variables)