Download presentation
Presentation is loading. Please wait.
1
Java Language Basics
2
Recap Last Class Introduction into Object-Oriented Programming Objects
State Behaviour Classes Java Language Variables - Capture states Methods - Capture behaviour
3
Questions from Last Class
What is meant by an instance? Does have to have parameter passing? How to write decimals? How to maintain a group of people? + how to maintain a list How to use Loops? How advanced will the programming go? Website link for data types? What other programming languages would we learn through Summer school? Why are the “()” needed e.g. Frank.increaseAge();?. Why do you need 2 different classes to be able to run the application? Why can’t you just do all the coding in 1 class?
4
Variables In the previous lesson, we saw the Class Human had the variables: int age; String name; This code must have raised a few questions: What are the other data types in Java? What are the rules and conventions for naming variables? Do the variables need to be initialized (have an initial value) when declared? What are the types of variables?
5
Naming Convention for Variables
Use CamelCase,e.g. thisIsMyVariableName Variable names are Case Sensitive. White spaces is not allowed in variables. Try to use only mostly letters. Avoid using "_" or “$”. Use full words instead of cryptic abbreviations.
6
Data Types All variables in Java must declared with a type, e.g. int Age. Doing so will tell your program that a variable named "age" exists, holds numerical data. You will use these data types in our lessons: Int - Numerical value, , etc. Boolean - True or False values. Double decimal values. String - “This is a string” value. Also, google search other data types in Java.
7
Variables Kinds In the JAVA programming language there are FOUR kinds of variables: Instance Variables Class Variables Local Variables Parameters We will learn what these are in the following slides.
8
Instance Variable Instance Variables
Non-static variables are known as “Instance variables”. This means their values are unique to each Instance of a class. For example, in the Human Class, age is an instance variable. Since the current age of one person can independant or different from the age of another person. Another example of an Instance variable in our Human class?
9
Class Variable Any variable declared with a static modifier is a class variable. This states that there is exactly one copy of this variable in existence for any number of instances created. For example, In the human class we can introduce a class variable to indicate the maximum age for a human: static int maxAge = 120; Additionally, the keyword final could be added to indicate that the max age will never change.
10
Local Variable A Class may have many methods, and each method may store its own temporary states as local variables. There is no distinctive way of declaring a local variable. It is determined by it’s location within your code. Can you find a local variable within the Human class code? Assignment: Change the Human class to increase age by 3.
11
Parameter You have already seen many examples of parameters.
For example, in the Class Human: void setNameandAge(String parameterName,...) Assignment: Change the huname class - Remove Name and instead introduce First Name and Last Name as the methods parameters. Make the necessary changes for the parameter.
12
Control Flow Statements
13
Control Flow Statements
The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution. They by employing decision making, looping, and branching This enables your program to conditionally execute particular blocks of code.
14
if-then Demo class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
15
Assignment If-then-else
Use the if-then-else statement in one of the methods of your Human Class
16
switch Demo case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "Septembe"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid”; break; } System.out.println(monthString); }} public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break;
17
If-then-else or Switch
Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing. An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object.
18
Assignment Switch Statement
Use the switch statement in one of the methods of your Human Class
19
while and do-while Demo
class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } } class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } }
20
Difference: while and do-while
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program:
21
Assignment while statement
Use the while and do-while statement in one of the methods of your Human Class
22
The for Statement The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows: for (initialization; termination; increment) { statement(s) }
23
for-statement Demo class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } } }
24
Assignment For Loop statement
Use the For Loop statements in one of the methods of your Human Class
25
Language Operators
26
Arithmetic Operators Java programming language provides operators that perform addition, subtraction, multiplication, and division additive operator (also used for String concatenation) subtraction operator * multiplication operator / division operator % remainder operator
27
ArithmeticDemo class ArithmeticDemo { public static void main (String[] args){ int result = 1 + 2; System.out.println(result); result = result - 1; System.out.println(result); result = result * 2; System.out.println(result); result = result / 2; System.out.println(result); result = result + 8; result = result % 7; System.out.println(result); } }
28
Unary Operators The unary operator requires only one operand.
Unary plus operator; indicates positive value Unary minus operator; negates an expression Increment operator; increments a value by Decrement operator; decrements a value by 1 ! Logical complement operator; inverts the value of a boolean
29
UnaryDemo class UnaryDemo { public static void main(String[] args){ int result = +1; System.out.println(result); result--; System.out.println(result); result++; System.out.println(result); result = -result; System.out.println(result); boolean success = false; System.out.println(success); System.out.println(!success); } }
30
Equality and Relational Operators
The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. == equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
31
ComparisonDemo class ComparisonDemo { public static void main(String[] args){ int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 > value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 <= value2) System.out.println("value1 <= value2"); } }
32
Conditional Opertors The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. && Conditional-AND || Conditional-OR
33
ComparisonDemo class ComparisonDemo { public static void main(String[] args){ int value1 = 1; int value2 = 2; if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1"); } }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.