Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Control Structures Corresponds with Chapters 3 and 4.
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.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Chapter 05 (Part III) Control Statements: Part II.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Bordoloi and Bock Control Structures: Iterative Control.
Introduction to Java Java Translation Program Structure
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
CSC 212 Object-Oriented Programming and Java Part 2.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Review for Nested loops & Math class methods & User defined methods.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Information and Computer Sciences University of Hawaii, Manoa
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
The switch Statement, and Introduction to Looping
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
Control Structures.
Chapter 2 Basic Computation
Java - Data Types, Variables, and Arrays
Expressions and Assignment
CMPE212 – Reminders The other four assignments are now posted.
Chapter 2 Programming Basics.
Java Programming Language
Peer Instruction 4 Control Loops.
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
Problem 1 Given n, calculate 2n
Presentation transcript:

Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language

Constructors A constructor is a method that constructs objects of its class. When we define a constructor for a class, it is defined much like we define other methods for a class. The rules are as follows: A constructor must have the same name as the class itself. A constructor cannot specifically define a return value. When you create an object you use the following syntax: House mc = new House(); So, we’ve used constructors before, we just were not aware of it. When we created objects of type House we used the default constructor. Call to the Objects default constructor

Constructors If we do not explicitly define our own constructor for a Class, Java automatically generates a default constructor that has no arguments. Default constructors are used simply to create objects of a Class and are called via the new operator. As soon as we declare our own constructor for a Class the default constructor can no longer be used. Why would we define our own constructors? Not only are constructors used to create objects of a particular class, they are also commonly used to initialize the fields (variables) of an object.

Creating Constructors If we were to create a constructor for our MyCar class it may look something like the following: public class House { private int width; private int length public MyCar(int theWidth, int theLength) { length = theLength; length = theWidth; } House mc = new House(42, 35); Lets create a Class that uses a constructor to initialize it’s fields

Wrapper Classes Wrapper Classes are a set of classes that allow us to use primitive types, such as integers, in places where we need an object. When you create objects from Wrapper classes the object ‘wraps’ itself around a primitive data type. You can then use the methods available from that object to manipulate the value wrapped. Every primitive data type has an associated wrapper class in the java.lang package. Why use Wrapper Classes? You may need to convert a String to an actual number. You may need to take a primitive and convert it to some other type. Some classes can only deal with other objects. Not primitives.

WrapperPrimitive Boolean boolean Bytebyte Characterchar Doubledouble Floatfloat Integerint Longlong Shortshort âWrapper Classes Start with a capital letter. âCharacter and Integer wrapper Classes have long names instead of just the short version.

Wrapper Classes An example of the Integer wrapper class: int x = 15; int y; int z; String mark = “85”; Integer m = new Integer(mark); y = m.intValue(); z = x + y; What does z equal? Lets build a Java app that demonstrates wrapper classes.

Java Operators Important Java Operators !Logical Not newObject Creation (type)expressionCasting (Type Conversion) * Multiplication /Division +Addition String Concatenation -Subtraction +=Used for incrementing numeric values or used for concatenating Strings. int a = 10; a+=2; a is now equal to 12. int a = 10; a = a + 2; a is now equal to 12 Same Results

Java Operators <Less Than >Greater Than >=Greater than or equal to <=Less than or equal to = =Equal to !=Not equal to &&Logical AND ||Logical OR =Assignment (Set Equal To)

Casting For primitive data types we have to explicitly use a cast operator if we wish to cast from a larger data to a smaller data type. Example: double a = 3.34d; float b; The following line will fail because we are trying to assign a float value to an int. Floats have a larger range of values and so we have to specify an explicit cast. b = a; Will fail. b = (float)a Will compile. (float) is the cast operator.

Casting If we want to assign a variable of a smaller data type to a variable of a larger data type no explicit cast is needed. Example: long a; int b = 500; a = b; We are assigning a variable of type int (b) to a variable of type long (a) No explicit cast is needed because (a) has a larger range of values than (b). Lets demonstrate with an example.

Control Structures If Statements ([ ] means optional) if (boolean_expression) { statement_or_block } [else { statement_or_block }] The else clause is optional

Control Structures int x = 4; if (x = = 10) { System.out.println(“x is equal to 10”); } else { System.out.println(“x not equal to 10”); } Important Note: Note the difference between the assignment operator and the equality operator. The single ‘=‘ assigns a value to a variable. The ‘==‘ evaluates for equality.

Control Structures if statements You can also nest if statements or use else ifs int x = 125; if (x > 100) { if (x > 150) { System.out.println(“Greater than 150”); } else if (x > 50) { System.out.println(“Greater than 50”); } else { System.out.println(“50 or less”); }

Control Structures switch statements Use the switch statement to select one or more alternative statements or blocks of code to execute. switch (integral_expression) { case selector1: statements or blocks [break;] [case selector2: statements or blocks] [….] [default: statements or blocks] }

Control Structures The expression following the switch statement must have type byte, short, int, or char. Any number of case labels are allowed. The label default is optional. It can be anywhere in the switch statement, but is typically at the end. The integeral_expression is evaluated. Control then goes to the first case label where the value of the selector equals that of the switch expression. No further matching will be done. If none of the labels match the expression and a default label is present, control will pass to the statements following the default label. Using break is optional, but is very common.

Control Structures switch statements int x = 40; switch(x) { case 40: System.out.println(“x = = 40”); break; case 50: System.out.println(“x= = 50”); break; default: System.out.println(“Some other value”); } Output will be: x = = 40

Control Structures for statements Use a for loop when you know in advance how many times to repeat the loop. for (initialization; termination_expression; step) { statement_or_block } for (int i = 0; i < 3; i++) {Output: System.out.println(i); 0 } 1 2

Control Structures If the counter (i) is declared and initialized in the for loop the scope of i is inside the loop only. for (int i = 0; i < 3; i++) { System.out.println(i); } System.out.pritnln(i);Program will fail here. If the counter is declared outside the loop and used as the counter the variable can be seen outside of the for loop. int i; for (i = 0; i < 3; i++) { System.out.println(i); } System.out.println(i);Program will proceed.

Control Structures while statements while (boolean_expression) { statement_or_block } Example: int i = 0; while ( i <= 2) { i++; System.out.println(“i= “ + i); } Output will be: i=1 i=2 i=3

Control Structures do statements Use do statements to program conditional loops when you want to force the body of the loop to execute at least once. int x = 0; Output will be: do { x++;1 System.out.println(x);2 } while (x <=2);3

Control Structures Getting out of loops You can leave loops prematurely with the keywords break and continue. break will terminate the loop and give control to the next statement outside the loop. Continue will terminate the current iteration of the loop only. Control passes to the top of the loop where the boolean expression is evaluated to determine whether to pass through the loop again.

Control Statements Example of break and continue inside loops for (int k = 0; k <=5; k++) { if (k = = 3) { break; } System.out.println(“loop count “ + k); } The output is: loop count 0 loop count 1 loop count 2

Control Structures for (int k = 0; k <=5; k++) { if (k = = 3) { continue; } System.out.println(“loop count “ + k); } The output is: loop count 0 loop count 1 loop count 2 loop count 4 loop count 5