Java Language Basics.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
The switch statement: an N-way selection statement.
SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
DAT602 Database Application Development Lecture 5 JAVA Review.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Java operatoriai sandbolts/operators.html
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Primitive Variables.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CompSci 230 S Programming Techniques
Lecture 4b Repeating With Loops
University of Central Florida COP 3330 Object Oriented Programming
The switch Statement, and Introduction to Looping
University of Central Florida COP 3330 Object Oriented Programming
Control Statements: Part 2
Selenium WebDriver Web Test Tool Training
University of Central Florida COP 3330 Object Oriented Programming
Primitive Data, Variables, Loops (Maybe)
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Java Review Most of these slides are based on
JavaScript: Control Statements.
JavaScript: Control Statements I
Java operatoriai
CMSC 202 Static Methods.
Control Statement Examples
Introduction to Programming in Java
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
An Introduction to Java – Part I, language basics
Variables ICS2O.
In this class, we will cover:
Java Language Basics.
Classes and Objects 5th Lecture
3 Control Statements:.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Expressions and Assignment
elementary programming
The System.exit() Method
Java Review Most of these slides are based on
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Classes and Objects Static Methods
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
In this class, we will cover:
Classes, Objects and Methods
Introduction to Programming
Chap 7. Advanced Control Statements in Java
Review of Previous Lesson
Loops CGS3416 Spring 2019 Lecture 7.
Loops and Iteration CS 21a: Introduction to Computing I
Presentation transcript:

Java Language Basics

Recap Last Class Introduction into Object-Oriented Programming Objects State Behaviour Classes Java Language Variables - Capture states Methods - Capture behaviour

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?

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?

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.

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, 42325123, etc. Boolean - True or False values. Double - 42.2345678901234567 decimal values. String - “This is a string” value. Also, google search other data types in Java.

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.

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?

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.

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.

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.

Control Flow Statements

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.

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); } }

Assignment If-then-else Use the if-then-else statement in one of the methods of your Human Class

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;

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.

Assignment Switch Statement Use the switch statement in one of the methods of your Human Class

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); } }

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:

Assignment while statement Use the while and do-while statement in one of the methods of your Human Class

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) }

for-statement Demo class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } } }

Assignment For Loop statement Use the For Loop statements in one of the methods of your Human Class

Language Operators

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

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); } }

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 1 -- Decrement operator; decrements a value by 1 ! Logical complement operator; inverts the value of a boolean

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); } }

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

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"); } }

Conditional Opertors The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. && Conditional-AND || Conditional-OR

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"); } }