Programming II
Rules
• Time – 10 minutes allowance (attendance)
• Scoring Procedure – Written and Hands-on(rubrics-discuss)
• Evaluation
30% - Quizzes 20%-Quizzes
100%
40%- M.E 20%-M.E
20%- Projects/Assignment 50%-Performance/Projects
10%-Attendance
10%- Attendance
LECTURE LABORATORY
Critical Tips to Learn
1. Learn by doing. Always play with the code while learning
with every new subject, the sooner you start playing with code,
the faster you will learn the given concepts.
2. Grasp the fundamentals for long-term benefits
programming fundamentals always need to come first: the better
you understand them, the easier it is to learn more advanced
concepts.
3. Code by hand. It sharpens proficiency and you’ll need it to get a
job
Critical Tips to Learn
4. Ask for help. You’ll need it
what may seem like an immovable bug or an unlearnable topic
could be quickly alleviated by a fresh pair of eyes or a new
interpretation of the subject.
5. Seek out more online resources. There’s a wealth of content
if a particular concept doesn’t make sense, be it on
codeacademy, in a textbook, or during class lecture, maintain
your confidence and look for alternative online resources to
learn the same content.
Critical Tips to Learn
6. Don’t just read the sample code. Tinder with it!
Reading sample code is not enough to understand how it works.
To develop a true understanding, you need to actually run the
code and tinker with it.
7. Take breaks when debugging
when debugging, its easy to go down the rabbit hole for hours,
and there’s no guarantee that you will fix the problem.
8. Conclusion: Keep Calm and Keep on Coding
the most important ingredient to learn programming faster is to
remain confident.
THE SOFTWARE DEVELOPMENT METHOD
1. Specify the problem requirements
2. Analyze the problem
3. Design the algorithm to solve the problem
4. Implement the algorithm
5. Test and verify the completed program
6. Maintain and update the software
1. PROBLEM
Specify the problem requirements forces to state the problem clearly
and unambiguously and to gain a clear understanding of what is
required for its solution.
2. ANALYSIS
Analyzing the problem involves identifying the problem (a) inputs,
that is, the data you have to work with; (b) outputs, that is the desired
results; and (c) any additional requirements or constraints on the
solution.
3. DESIGN
Designing the algorithm to solve the problem requires you to develop
a list of steps called algorithm to solve the problem and then verify
that the algorithm solves the problem as intended.
4. IMPLEMENTATION
Implementing the algorithm ( step 4 in the software development
method) involves writing it as a program. You must convert each
algorithm step into one or more statements in a programming
language.
JAVA
Difference between println(), print() and
printf()
JAVA Identifiers
Identifier refers to name given to entities such as variables,
functions, structures etc.
Identifier must be unique. They are created to give unique name
to entity it during the execution of the program. For example:
int money;
double accountBalance;
Here, money and accountBalance are identifiers.
Rules for writing an identifier
1. You cannot use any special characters other than underscore(_).
2. They must not be a reserved word or keyword in Java, like true,
false, while. Etc.
3. Identifiers must not begin with digits
4. Identifiers can be of any length
5. Java is case sensitive, so uppercase and lowercase identifiers are
treated differently
Concatenation Sentence Builder Challenge
Create a program that will only require the
programmer to change the variable values in order
to create dynamic sentence using the sentence in
the sample output;
The process of joining
Strings together with Sample Output
the plus operator. Hi my name is David
I am 24 Years old
My GPA is 2.25
My blood type is B
Declaring Variables
Syntax:
datatype identifier;
datatype identifier = value;
Reassigning Variables
Syntax:
Identifier = value;
Variables
Variables are used to store up temporary data to be used in our
program’s runtime
Data Type
The Type of Data inside our variable
Ex: int speed;
Here, speed is a variable, and the data type of variable is int.
The int data type determines that the speed variable can only contain
integers.
Built-In Package
Java API has many pre-written classes to help the
programmer manage input, database and etc.
Importing Packages
IMPORT SPECIFIC CLASS
Import packagename.classname
IMPORT WHOLE PACKAGE
Import packagename.*
Java Input
java.util.Scanner
A class in the java.util package that helps programmers
handle input from user.
User Input
String x;
Scanner s=new Scanner(System.in);
x=s.nextline();
User Input
• nextLine()
• nextInt()
• nextShort()
• nextLong()
• nextByte()
• nextBoolean()
• nextDouble()
• nextFloat()
BASIC MATH METHODS
Java Math class
Java Math class provides several
methods to work on math
calculations like min(), max(),
avg(), sin(), cos(), tan(), round(),
ceil(), floor(), abs() etc.
Reference:
https://www.javatpoint.com/jav
a-math
Java Math class
Java Math class provides several methods to work
on math calculations like min(), max(), avg(), sin(),
cos(), tan(), round(), ceil(), floor(), abs() etc.
Print Formatting: printf()
Print Formatting: printf()
Print Formatting: printf()
Composition of printf()
Composition of printf()
Composition of printf()
Create a program that read strings and numeric data
Java Programming
Arithmetic Operators
Java Arithmetic Operators
/Division Operator
Order of Precedence
a. Operator precedence rule: Operators in the same expression are
evaluated in the following order.
➢ *,/,% first
➢ binary +,- last
b. Associativity rule: Binary operators in the same subexpression and
at the same precedence level (such as + and - are evaluated left to
right(left associativity).
Operator Associativity
*/% left to right
+- left to right
Two rules apply when evaluating a mixed
expression:
1. When evaluating an operator in a mixed expression:
a) if the operator has the same types of operands (that is, both are
integers or both are floating-point numbers), the operator is
evaluated according to the type of the operand. Integer operands
yield an integer result; floating point numbers yield a floating point
number result.
b) If the operator has both type of operands(that is, one is an integer
and the other is floating-point number), during calculation the
integer is treated temporarily as a floating point number with the
decimal part of zero, and then the operator is evaluated. The result
is a floating-point number.
Two rules apply when evaluating a mixed
expression:
2. The entire expression is
evaluated according to the
precedence rules. The
multiplication, division and
modulus operators are
evaluated before the addition
and subtraction operators.
Operators having same level
of precedence are evaluated
from left to right. Grouping is
allowed for clarity.
Type conversion (Casting) (dataTypeName) expression
Type conversion (Casting)
Increment and Decrement
Java also provides increment and decrement operators: ++ and --
respectively. ++ increases the value of the operand by 1, while -- decrease it by 1.
For example,
One Function Calculator
Create a program that will make the User Input the
2 Numbers and perform one of the arithmetic
operators excluding increment and decrement
Java Assignment Operators
Assignment operators are used in Java to assign values to variables. For example,
Java Relational Operators
Relational operators are used to check the relationship between two operands.
Java Logical Operators
Logical operators are used to check whether an expression is true or false. They are
used in decision making.
Java Ternary Operator
The ternary operator (conditional operator) is shorthand for the if-then-
else statement. For example,
Using Dialog Boxes for Input/Output
The syntax to use the method showInputDialog is:
str = JOptionPane.showInputDialog(stringExpression);
Consider the following statement(suppose that name is a String variable):
name = JOptionPane.showInputDialog(“Enter your name and press ok”);
Using Dialog Boxes for Input/Output
Now that you know how to use an input dialog box, let’s turn to the method showMessageDialog for
output.
The syntax to use the method showMessageDialog is:
JOptionPane.showMessageDialog(parentComponent,messageStringExpression,
boxTitleString,messageType);
boxTitleString
messageStringExpression
JOptionPane.INFORMATION_MESSAGE icon
Parsing Numeric Strings
A string consisting of only an integer or a floating-point number, optionally preceded by a minus sign
is called a numeric string. For example, the following are numeric strings:
“6723”
“-823”
“345.78”
“-782.873”
1. To convert a string consisting of an integer to a value of the type int, we use the following
expression:
Integer.parseInt(strExpression)
For example:
Integer.parseInt(“6723”) = 6723
Integer.parseInt(“-823”) = -823
2. To convert a string consisting of a floating-point number to a value of the type float, we use
the following expression:
Float.parseFloat(strExpression)
For example:
Float.parseFloat(“34.56”) = 34.56
Float.parseFloat(“-542.97”) = -542.97
3. To convert a string consisting of a floating-point number to a value of the type double, we
use the following expression:
Double.parseDouble(strExpression)
For example:
Double.parseDouble(“34.56”) = 34.56
Double.parseDouble(“-542.97”) = -542.97
Example
1. Integer.parseInt(“34”) = 34
Integer.parseInt(“-456”) = -456
Double.parseDouble(“754.89”) = 34.56
2. Integer.parseInt(“34”) + Integer.parseInt(“75”) = 34 + 75 = 109
Integer.parseInt(“87”) + Integer.parseInt(“-67”) = 87 - 67= 20
3. Double.parseDouble(“754.89”) - Double.parseDouble(“87.34”) = 754.89 – 87.34 = 667.55
Problem:
Computer the area and circumference of circle
Problem solving technique includes analyzing a problem, outlining the problem
requirements and designing steps called Algorithm.
A step-by-step problem-solving process in which a solution is arrived at in a finite amount
of time.
In the programming environment, the problem-solving process involves the following
steps:
1. Analyze the problem and outline the problem and its solution requirements.
2. Design an algorithm to solve the problem
3. Implement the algorithm in a programming language, such as Java.
4. Verify that the algorithm works.
5. Maintain the program by using and improving it and modifying it if the problem
domain changes.
Calculate the monthly paycheck of a salesperson at a
local department store. 1. Get baseSalary
2. Get noOfServiceYears
Every salesperson has a base salary. The salesperson 3. Calculate bonus using following formula:
also receives a bonus at the end of each month, if(noOfServiceYear <= 5)
bonus = 10 * noOfServiceYears
based on the following criteria: if the salesperson has otherwise
been with the store for five years or less, the bonus is bonus = 20 * noOfServiceYears
10 for each year that he or she has worked there. If 4. Get totalSales
the salesperson has been with the store for more 5. Calculate additionalBonus using the following formula:
if(totalSales < 5000)
than five years, the bonus is 20 for each year that he additionalBonus = 0
or she has worked there. The salesperson can earn an otherwise
additional bonus as follows: if the total sales made by if((totalSales >=5000)&&(totalSales <10000))
additionalBonus = totalSales * (0.03)
the salesperson for the month are greater than or otherwise
equal to 5,000 but less than 10000, he or she additionalBonus = totalSales * (0.06)
receives a 3% commission on the sale. If the total 6. Calculate paycheck using the following equation:
sales made by the salesperson for the month are at paycheck = baseSalary + bonus + additionalBonus
least 10,000, he or she receives a 6% commission on
the sale.