Java Review. Fundamentals of Computer Science

Size: px
Start display at page:

Download "Java Review. Fundamentals of Computer Science"

Transcription

1 Java Review Fundamentals of Computer Science

2 Link to Head First pdf File ead-first-java-2nd-edition.pdf

3 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

4 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;

5 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.

6 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?

7 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.

8 Arrays How many dimensions can an array have?

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

10 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

11 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

12 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

13 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

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

15 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.

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

17 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.

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

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

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

21 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.

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

23 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.

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

25 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.

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

27 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).

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

29 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.

30 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?

31 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.

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

33 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.

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

35 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.

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

37 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.

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

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

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

41 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.

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

43 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.

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

45 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.

46 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

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

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen Other conditional and loop constructs Fundamentals of Computer Science Keith Vertanen Overview Current loop constructs: for, while, do-while New loop constructs Get out of loop early: break Skip rest of

More information

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

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

Michele Van Dyne Museum 204B  CSCI 136: Fundamentals of Computer Science II, Spring Michele Van Dyne Museum 204B mvandyne@mtech.edu http://katie.mtech.edu/classes/csci136 CSCI 136: Fundamentals of Computer Science II, Spring 2016 1 Review of Java Basics Data Types Arrays NEW: multidimensional

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

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

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes 1 CS257 Computer Science II Kevin Sahr, PhD Lecture 5: Writing Object Classes Object Class 2 objects are the basic building blocks of programs in Object Oriented Programming (OOP) languages objects consist

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)

More information

AP CS Fall Semester Final

AP CS Fall Semester Final Name: Class: Date: AP CS Fall Semester Final Multiple Choice Identify the choice that best completes the statement or answers the question. 1. What is printed by the following code? int k = 3; for (int

More information

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

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 Epic Test Review 1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4 Write a line of code that outputs the phase Hello World to the console without creating a new line character. System.out.print(

More information

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

Classes, interfaces, & documentation. Review of basic building blocks Classes, interfaces, & documentation Review of basic building blocks Objects Data structures literally, storage containers for data constitute object knowledge or state Operations an object can perform

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

CS 302 Week 9. Jim Williams

CS 302 Week 9. Jim Williams CS 302 Week 9 Jim Williams This Week P2 Milestone 3 Lab: Instantiating Classes Lecture: Wrapper Classes More Objects (Instances) and Classes Next Week: Spring Break Will this work? Double d = new Double(10);

More information

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

Week 6: Review. Java is Case Sensitive

Week 6: Review. Java is Case Sensitive Week 6: Review Java Language Elements: special characters, reserved keywords, variables, operators & expressions, syntax, objects, scoping, Robot world 7 will be used on the midterm. Java is Case Sensitive

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Unit 1 Lesson 4. Introduction to Control Statements

Unit 1 Lesson 4. Introduction to Control Statements Unit 1 Lesson 4 Introduction to Control Statements Essential Question: How are control loops used to alter the execution flow of a program? Lesson 4: Introduction to Control Statements Objectives: Use

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations

More information

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

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software. CISC-124 20180129 20180130 20180201 This week we continued to look at some aspects of Java and how they relate to building reliable software. Multi-Dimensional Arrays Like most languages, Java permits

More information

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

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

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

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable CISC-124 20180122 Today we looked at casting, conditionals and loops. Casting Casting is a simple method for converting one type of number to another, when the original type cannot be simply assigned to

More information

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

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase Today s Lesson plan (10) Retrospective Previous lesson Theory: Nested if.. then..

More information

Enumerations. Fundamentals of Computer Science

Enumerations. Fundamentals of Computer Science Enumerations Fundamentals of Computer Science Outline Avoiding magic numbers Variables takes on a small set of values Use descriptive names instead of literal values Java enumerations Using in a switch

More information

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

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

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

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

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs. Java SE11 Development Java is the most widely-used development language in the world today. It allows programmers to create objects that can interact with other objects to solve a problem. Explore Java

More information

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

1007 Imperative Programming Part II

1007 Imperative Programming Part II Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

More information

VARIABLES AND TYPES CITS1001

VARIABLES AND TYPES CITS1001 VARIABLES AND TYPES CITS1001 Scope of this lecture Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Primitive types Every piece of data

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

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

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

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

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while Pull Lecture Materials and Open PollEv Poll Everywhere: pollev.com/comp110 Lecture 12 else-if and while loops Once in a while Fall 2016 if-then-else Statements General form of an if-then-else statement:

More information

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

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

University of Massachusetts Amherst, Electrical and Computer Engineering

University of Massachusetts Amherst, Electrical and Computer Engineering University of Massachusetts Amherst, Electrical and Computer Engineering ECE 122 Midterm Exam 1 Makeup Answer key March 2, 2018 Instructions: Closed book, Calculators allowed; Duration:120 minutes; Write

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

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

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated 1. What are the 2 types of short circuit operators. 1. Short circuit AND (&&) = if first condition is false then the second is never evaluated because true and false gives you false only. 2. Short circuit

More information

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

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday

More information

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

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2. Class #10: Understanding Primitives and Assignments Software Design I (CS 120): M. Allen, 19 Sep. 18 Java Arithmetic } Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = 2 + 5 / 2; 3.

More information

Assignment 4: Dodo gets smarter

Assignment 4: Dodo gets smarter Assignment 4: Dodo gets smarter Algorithmic Thinking and Structured Programming (in Greenfoot) 2017 Renske Smetsers-Weeda & Sjaak Smetsers 1 Contents Introduction 1 Learning objectives 1 Instructions 1

More information

Comments are almost like C++

Comments are almost like C++ UMBC CMSC 331 Java Comments are almost like C++ The javadoc program generates HTML API documentation from the javadoc style comments in your code. /* This kind of comment can span multiple lines */ //

More information

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CSE 142 Su 04 Computer Programming 1 - Java. Objects Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request

More information

Java Programming: from the Beginning

Java Programming: from the Beginning CSc 2310: Principle of Programming g( (Java) Spring 2013 Java Programming: from the Beginning Chapter 5 Arrays 1 Copyright 2000 W. W. Norton & Company. All rights reserved. Outline 5.1 Creating and Using

More information

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

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types Classes and objects A foundation for programming any program you might want to write objects functions and modules build even bigger programs and reuse code http://www.flickr.com/photos/vermegrigio/5923415248/

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification

More information

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

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017 This week the main activity was a quiz activity, with a structure similar to our Friday lecture activities. The retrospective for the quiz is in Quiz-07- retrospective.pdf This retrospective explores the

More information

Robots. Byron Weber Becker. chapter 6

Robots. Byron Weber Becker. chapter 6 Using Variables Robots Learning to Program with Java Byron Weber Becker chapter 6 Announcements (Oct 3) Reminder: Reading for today y( (Oct 3 rd ) Ch 6.1-6.4 Reading for Wednesday (Oct 3 rd ) The rest

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Introduction to the Java Basics: Control Flow Statements

Introduction to the Java Basics: Control Flow Statements Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared

More information

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

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved. Java Foundations 7-2 Copyright 2015, Oracle and/or its affiliates. All rights reserved. Objectives This lesson covers the following objectives: Understand the memory consequences of instantiating objects

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming (Spring 2012) Lecture #31: Software Reuse through Inheritance Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

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

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

1 Inheritance (8 minutes, 9 points)

1 Inheritance (8 minutes, 9 points) Name: Career Account ID: Recitation#: 1 CS180 Spring 2011 Exam 2, 6 April, 2011 Prof. Chris Clifton Turn Off Your Cell Phone. Use of any electronic device during the test is prohibited. Time will be tight.

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 02 / 15 / 2016 Instructor: Michael Eckmann Questions? Comments? Loops to repeat code while loops for loops do while loops Today s Topics Logical operators Example

More information

Principles of Computer Science I

Principles of Computer Science I Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120A - Fall 2004 Lecture Unit 7 Review Chapter 4 Boolean data type and operators (&&,,) Selection control flow structure if, if-else, nested

More information

In Java there are three types of data values:

In Java there are three types of data values: In Java there are three types of data values: primitive data values (int, double, boolean, etc.) arrays (actually a special type of object) objects An object might represent a string of characters, a planet,

More information

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

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 ); Sample Final Exam 1. Evaluate each of the following expressions and show the result and data type of each: Expression Value Data Type 14 % 5 1 / 2 + 1 / 3 + 1 / 4 4.0 / 2.0 Math.pow(2.0, 3.0) (double)(2

More information

AP CS Unit 4: Classes and Objects Programs

AP CS Unit 4: Classes and Objects Programs AP CS Unit 4: Classes and Objects Programs 1. Copy the Bucket class. Make sure it compiles (but you won t be able to run it because it does not have a main method). public class Bucket { private double

More information

CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions

More information

CS1083 Week 2: Arrays, ArrayList

CS1083 Week 2: Arrays, ArrayList CS1083 Week 2: Arrays, ArrayList mostly review David Bremner 2018-01-08 Arrays (1D) Declaring and using 2D Arrays 2D Array Example ArrayList and Generics Multiple references to an array d o u b l e prices

More information

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

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com CHAPTER 8 OBJECTS AND CLASSES Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals To understand the concepts of classes, objects and encapsulation To implement instance variables,

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

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

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers Today s Lesson plan (3) 10 min Looking back What did we learn last week? Blocks of: Theory Exercises

More information

CSCI 161 Introduction to Computer Science

CSCI 161 Introduction to Computer Science CSCI 161 Introduction to Computer Science Department of Mathematics and Computer Science Lecture 2b A First Look at Class Design Last Time... We saw: How fields (instance variables) are declared How methods

More information

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

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Robots. Byron Weber Becker. chapter 6

Robots. Byron Weber Becker. chapter 6 Using Variables Robots Learning to Program with Java Byron Weber Becker chapter 6 Announcements (Oct 5) Chapter 6 You don t have to spend much time on graphics in Ch6 Just grasp the concept Reminder: Reading

More information

Computer Programming I - Unit 5 Lecture page 1 of 14

Computer Programming I - Unit 5 Lecture page 1 of 14 page 1 of 14 I. The while Statement while, for, do Loops Note: Loop - a control structure that causes a sequence of statement(s) to be executed repeatedly. The while statement is one of three looping statements

More information

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.

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. Enums Introduction In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed. The Final Tag To display why this is useful, I m going to

More information

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

Chapter 5: Arrays. Chapter 5. Arrays. Java Programming FROM THE BEGINNING. Copyright 2000 W. W. Norton & Company. All rights reserved. Chapter 5 Arrays 1 5.1 Creating and Using Arrays A collection of data items stored under a single name is known as a data structure. An object is one kind of data structure, because it can store multiple

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

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

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations. Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn

More information

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

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.! True object-oriented programming: Dynamic Objects Reference Variables D0010E Object-Oriented Programming and Design Lecture 3 Static Object-Oriented Programming UML" knows-about Eckel: 30-31, 41-46, 107-111,

More information

Introduction to Python (All the Basic Stuff)

Introduction to Python (All the Basic Stuff) Introduction to Python (All the Basic Stuff) 1 Learning Objectives Python program development Command line, IDEs, file editing Language fundamentals Types & variables Expressions I/O Control flow Functions

More information

public static String[] manyme() {

public static String[] manyme() { UNCA CSCI 202 Middle Exam 2013 9 April, 2013 This is a closed book and closed notes exam. It is a 100-minute exam to be given within the regular class meeting time. Calculators, PDA s, cell phones, and

More information

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

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

Section 1.3 Adding Integers

Section 1.3 Adding Integers Section 1.3 Adding Integers Objectives In this section, you will learn to: To successfully complete this section, you need to understand: Represent integers as vectors. The integer number line (1.2) Add

More information

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

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

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

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1 CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Singleton Pattern Mar. 13, 2007 What is it? If you need to make sure that there can be one and only one instance of a class. For example,

More information

Lecture Transcript While and Do While Statements in C++

Lecture Transcript While and Do While Statements in C++ Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some

More information

The Arithmetic Operators

The Arithmetic Operators The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

More information

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

More information

Objects as a programming concept

Objects as a programming concept Objects as a programming concept IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4:

More information

Recommended Group Brainstorm (NO computers during this time)

Recommended Group Brainstorm (NO computers during this time) Recommended Group Brainstorm (NO computers during this time) Good programmers think before they begin coding. Part I of this assignment involves brainstorming with a group of peers with no computers to

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

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

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators, Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators, Expressions, Statements and Arrays. Java technology is: A programming

More information

BM214E Object Oriented Programming Lecture 4

BM214E Object Oriented Programming Lecture 4 BM214E Object Oriented Programming Lecture 4 Computer Numbers Integers (byte, short, int, long) whole numbers exact relatively limited in magnitude (~10 19 ) Floating Point (float, double) fractional often

More information

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

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

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

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal APCS A Midterm Review You will have a copy of the one page Java Quick Reference sheet. This is the same reference that will be available to you when you take the AP Computer Science exam. 1. n bits can

More information

[2:3] Linked Lists, Stacks, Queues

[2:3] Linked Lists, Stacks, Queues [2:3] Linked Lists, Stacks, Queues Helpful Knowledge CS308 Abstract data structures vs concrete data types CS250 Memory management (stack) Pointers CS230 Modular Arithmetic !!!!! There s a lot of slides,

More information

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

In either case, remember to delete each array that you allocate. CS 103 Path-so-logical 1 Introduction In this programming assignment you will write a program to read a given maze (provided as an ASCII text file) and find the shortest path from start to finish. 2 Techniques

More information

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

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #16: Java conditionals/loops, cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Midterms returned now Weird distribution Mean: 35.4 ± 8.4 What

More information