Java Review. Fundamentals of Computer Science

Similar documents
Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Chapter 4 Defining Classes I

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

CS 106 Introduction to Computer Science I

AP CS Fall Semester Final

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Classes, interfaces, & documentation. Review of basic building blocks

APCS Semester #1 Final Exam Practice Problems

CS 302 Week 9. Jim Williams

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Introduction to Programming Using Java (98-388)

Loops. CSE 114, Computer Science 1 Stony Brook University

Week 6: Review. Java is Case Sensitive

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Unit 1 Lesson 4. Introduction to Control Statements

Programming II (CS300)

COMP 250 Winter 2011 Reading: Java background January 5, 2011

ECE 122. Engineering Problem Solving with Java

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Enumerations. Fundamentals of Computer Science

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

1007 Imperative Programming Part II

VARIABLES AND TYPES CITS1001

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

University of Massachusetts Amherst, Electrical and Computer Engineering

CSC Java Programming, Fall Java Data Types and Control Constructs

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Assignment 4: Dodo gets smarter

Comments are almost like C++

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Java Programming: from the Beginning

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Chapter 4: Writing Classes

Control Structures in Java if-else and switch

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Robots. Byron Weber Becker. chapter 6

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Introduction to the Java Basics: Control Flow Statements

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CS 112 Introduction to Programming

Anatomy of a Class Encapsulation Anatomy of a Method

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

1 Inheritance (8 minutes, 9 points)

CS 106 Introduction to Computer Science I

Principles of Computer Science I

In Java there are three types of data values:

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

AP CS Unit 4: Classes and Objects Programs

CS112 Lecture: Working with Numbers

Control Structures in Java if-else and switch

CS1083 Week 2: Arrays, ArrayList

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Course Outline. Introduction to java

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers

CSCI 161 Introduction to Computer Science

QUIZ. What is wrong with this code that uses default arguments?

Robots. Byron Weber Becker. chapter 6

Computer Programming I - Unit 5 Lecture page 1 of 14

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Chapter 5: Arrays. Chapter 5. Arrays. Java Programming FROM THE BEGINNING. Copyright 2000 W. W. Norton & Company. All rights reserved.

Programming with Java

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Introduction to Python (All the Basic Stuff)

public static String[] manyme() {

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Section 1.3 Adding Integers

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

Lecture Transcript While and Do While Statements in C++

The Arithmetic Operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Objects as a programming concept

Recommended Group Brainstorm (NO computers during this time)

Chapter 4 Introduction to Control Statements

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

BM214E Object Oriented Programming Lecture 4

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

[2:3] Linked Lists, Stacks, Queues

In either case, remember to delete each array that you allocate.

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Software Design and Analysis for Engineers

CS1004: Intro to CS in Java, Spring 2005

Transcription:

Java Review Fundamentals of Computer Science

Link to Head First pdf File https://zimslifeintcs.files.wordpress.com/2011/12/h ead-first-java-2nd-edition.pdf

Outline Data Types Arrays Boolean Expressions Conditionals: if-else switch Iteration: while and for loops Object Oriented Design and Programming State Behavior Constructors Accessors and Mutators Other Important Methods API Static Methods http://www.hark.com/clips/sfpbhcvhzh-jeopardy-theme

Data Types Is the following set of statements legal? Why or why not? If not legal, what would make it work? int j, k; double x = 2.0; j = x;

Data Types Is the following set of statements legal? Why or why not? If not legal, what would make it work? int j, k; double x = 2.0; j = x; Not legal you cannot assign a data item that takes up more space in memory (a double) to one that takes up less space (an int). You could type cast x to an integer so that the last line becomes: j = (int) x; This would cause x to be truncated to an integer and would now be legal.

Arrays Let s say you have a set of Duck objects you want to keep track of in an array. There are 7 Ducks. How do you declare the array? Is it ready to use as is, or do you need to do additional work?

Arrays Let s say you have a set of Duck objects you want to keep track of in an array. There are 7 of these Duck objects. How do you declare the array? Is it ready to use as is, or do you need to do additional work? Duck [] ducks = new Duck[7]; for (int i=0; i<7; i++) ducks[i] = new Duck(); The first line initializes the array to hold 7 Ducks, but each Duck object in the array is not yet usable until it has been allocated space on the heap. The for loop allocates that space by calling the Duck constructor for each one in the array.

Arrays How many dimensions can an array have?

Arrays How many dimensions can an array have? As many as you need.

Boolean Expressions What is the result of the following boolean expression? (What is the value of d after this runs?) int a = 4; int b = 6; boolean c = true; boolean d; d = (a < b) &&!c

Boolean Expressions What is the result of the following boolean expression? (What is the value of d after this runs?) int a = 4; int b = 6; boolean c = true; boolean d; d = (a < b) &&!c false (a < b) is true!c is false (a<b) &&!c is equal to true && false which is false

Boolean Expressions How about this one? (What is the value of d after this runs?) int a = 4; int b = 6; boolean c = true; boolean d; d = ((a < b) &&!c) (c && (b < a) c

Boolean Expressions How about this one? (What is the value of d after this runs?) int a = 4; int b = 6; boolean c = true; boolean d; d = ((a < b) &&!c) (c && (b < a) c true (a < b) is true,!c is false so first part is false, c is true, b<a is false, c && (b<a) is false, c is true false or false or true is true

Boolean Expressions Bonus Question: Which one is more efficient and why? int a = 4; int b = 6; boolean c = true; boolean d; // First one d = ((a < b) &&!c) (c && (b < a) c // Second one d = c ((a < b) &&!c) (c && (b < a)

Boolean Expressions Bonus Question: Which one is more efficient and why? int a = 4; int b = 6; boolean c = true; boolean d; // First one d = ((a < b) &&!c) (c && (b < a) c // Second one d = c ((a < b) &&!c) (c && (b < a) The second one, because when we use the double operations (&& instead of &, and instead of ), Java does lazy evaluation. In the second one, as soon as c evaluates to true. other stuff, Java knows the whole thing will be true and doesn t go any further.

Conditionals What is the output of this code? String s = yes ; if (s == yes ) System.out.println( Yes it is. ); else System.out.println( No I don t think so );

Conditionals What is the output of this code? String s = yes ; if (s == yes ) System.out.println( Yes it is. ); else System.out.println( No I don t think so ); OK, trick question you should not use == to compare strings. The if statement should have read if (s.equals( yes )) The answer is that you really don t know what the output will be. The == may compare like you expect, or it might not.

Conditionals What is the output of this code? String s = yes ; int a = 4; if (s.equals( yes )) System.out.println( Yes it is. ); if (a == 3) System.out.println( 3 ); else System.out.println( No I don t think so );

Conditionals What is the output of this code? String s = yes ; int a = 4; if (s.equals( yes )) System.out.println( Yes it is. ); if (a == 3) System.out.println( 3 ); else System.out.println( No I don t think so ); Yes it is. No I don t think so Java doesn t care about how you ve indented the code. An else always goes with the next closest if, in this case, the else goes with if (a == 3)

What is the output of this code? Conditionals final int NORTH = 0; final int SOUTH = 1; final int EAST = 2; final int WEST = 3; int direction = 0; switch (direction) { case NORTH: y--; System.out.println("Walking north"); case SOUTH: y++; System.out.println("Walking south"); case EAST: x++; System.out.println("Walking east"); case WEST: x--; System.out.println( Walking west ); }

What is the output of this code? final int NORTH = 0; final int SOUTH = 1; final int EAST = 2; final int WEST = 3; int direction = 0; switch (direction) { case NORTH: y--; } Conditionals Walking north Walking south Walking east Walking west System.out.println("Walking north"); case SOUTH: y++; System.out.println("Walking south"); case EAST: x++; System.out.println("Walking east"); case WEST: x--; System.out.println( Walking west ); Without a break statement in each of the cases, Java simply executes the next one (and next one and ) until it reaches a break or the end of the switch statement.

Conditionals What is the difference between a conditional, like if or switch and a loop statement like for or while?

Conditionals What is the difference between a conditional, like if or switch and a loop statement like for or while? Conditional statements choose between options they make a decision. Once. The purpose of a loop statement is to iterate to perform a set of actions multiple times.

Iteration / Loops How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; while (a > 0){ a -= 2; counter++; } System.out.println(counter);

Iteration / Loops How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; while (a > 0){ a -= 2; counter++; } System.out.println(counter); 5 On each iteration, 2 is subtracted from a, and the loop ends when a is less than or equal to 0, so it goes through 5 times.

Iteration / Loops Bonus question: How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; while (a > 0){ counter++; } System.out.println(counter);

Iteration / Loops Bonus question: How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; while (a > 0){ counter++; } System.out.println(counter); Well it never ends, so nothing is ever printed out. The condition on which the while loop is based (a > 0) is never changed. It starts out as true, and this loop will run forever (an infinite loop).

Iteration / Loops How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; do { a -= 2; counter++; } while (a > 0); System.out.println(counter);

Iteration / Loops How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; do { a -= 2; counter++; } while (a > 0); System.out.println(counter); 5 In this case, the do while loop iterates exactly the same amount of times the while loop would have. The difference is that if a had been less than 0 to start, the loop body would have executed once.

Iteration / Loops When would you use a for loop instead of a while loop? Can a for loop always be written as (translated to) a while loop? Can a while loop always be written as (translated to) a for loop?

Iteration / Loops When would you use a for loop instead of a while loop? Can a for loop always be written as (translated to) a while loop? Can a while loop always be written as (translated to) a for loop? Use a for loop when you know how many times you need to iterate. Use a while loop when you want to iterate as long as a condition holds true. A for loop can always be rewritten as a while loop, but a while loop cannot always be rewritten as a for loop.

Classes and Objects What is the difference between a class and an object?

Classes and Objects What is the difference between a class and an object? A class is a blueprint for a type of thing. An object is an instantiation (or instance, or example) of that class. A class defines the structure, but there is no memory allocated until an object (or more than one object) is constructed.

Classes and Objects What is a constructor method? What is it s main purpose?

Classes and Objects What is a constructor method? What is it s main purpose? A constructor is a method that has the same name as the class. It can be overloaded and have parameters, and its main purpose is to create an object of type class, that is it allocates the memory for an object on the heap. A constructor may initialize instance variables associated with the object and perform other initialization actions as well.

Classes and Objects What is a method? What is it s main purpose?

Classes and Objects What is a method? What is it s main purpose? In Java, all methods live within a class. The purpose of a method is to isolate the code to perform a single function so that code is not duplicated. Taken together, all the methods within a class define its behavior.

Classes and Objects Regarding an object, what defines its state?

Classes and Objects Regarding an object, what defines its state? The values of its instance variables.

Methods In the following method, what is its signature, what are it parameters, what is its return type, and what information would be included in its API description? public int AddThese(int a, int b) { return a + b; }

Methods In the following method, what is its signature, what are it parameters, what is its return type, and what information would be included in its API description? public int AddThese(int a, int b) { return a + b; } It s signature is its name, and the number and type of its parameters. a and b are its parameters, and it returns an integer. All of these things (name, parameters, return type) plus a description of what the method does would be included in its API description.

Methods What is a mutator method? What is an accessor method? Why do we use these instead of accessing instance variables directly?

Methods What is a mutator method? What is an accessor method? Why do we use these instead of accessing instance variables directly? A mutator method is a setter method, and it changes the value of an instance variable. An accessor method is a getter, and it retrieves the value of an instance variable. We use these to protect the data in instance variables from direct external change we can use these methods to ensure that instance variables are not changed to illegal values.

Static Methods and Variables What does it mean for a method or variable to be static?

Static Methods and Variables What does it mean for a method or variable to be static? It means that there is only one method or variable for the entire class, no matter how many objects have been created. An entire class can be static (for example, the Math class in Java) which means that you can only have one instance of that class.

Summary Data Types Arrays Boolean Expressions Conditionals: if-else switch Iteration: while and for loops Object Oriented Design and Programming State Behavior Constructors Accessors and Mutators Other Important Methods API Static Methods