COE318 Lecture Notes Week 4 (Sept 26, 2011)

Size: px
Start display at page:

Download "COE318 Lecture Notes Week 4 (Sept 26, 2011)"

Transcription

1 COE318 Software Systems Lecture Notes: Week 4 1 of 11 COE318 Lecture Notes Week 4 (Sept 26, 2011) Topics Announcements Data types (cont.) Pass by value Arrays The + operator Strings Stack and Heap details Announcements Quiz: Monday October 3, 2011 Midterm: Monday, October 17, 2011 Counselling hours cancelled this Thursday (September 29). (I will be out of town.) Data types There are 2 general data types: 1. primitive, and 2. reference Primitive data types are int, float, double, short, long, byte, boolean and char. When a primitive data type is declared, the memory required to hold the data is allocated and that memory contains the value of the data. For example, the declaration byte i; allocates one byte of memory for the variable i. The assignment statement i = 5; would set that memory location's value to 5. Every class (and there are thousands built into the java library) is also a data type. But it is a reference data type. The reference points to an area in memory where the actual object of the type resides.

2 COE318 Software Systems Lecture Notes: Week 4 2 of 11 The most common built-in reference data type is String. When a reference data type is declared, memory is set aside to hold the reference. This is always 4 bytes no matter how big the actual data object is. The reference is instantiated by assigning it to an object (which may need to be created) of the correct data type. For example: Robot robbie; robbie = new Robot(); An actual Robot object is created with new Robot() (which invokes the Robot class's constructor) and it resides somewhere in memory (and uses many more than 4 bytes!). The reference value robbie points to the starting address of the actual Robot object. The place in memory where objects exist is called the heap. The Java Virtual Machine (JVM) keeps track of how many references there are to objects in the heap. When the reference count becomes zero, it is no longer possible to access the object. It has become garbage. The JVM periodically runs a Garbage Collector which frees up the space occupied by garbage objects. Where variable names are used Variable names can be used for instance variables, parameter names and local variables. Instance variables are declared in a class body. They can be accessed by any method of the class. They are usually private. Parameter names are the arbitrary names given to the passed parameters of methods or constructors. Parameters only exist during the execution of the method or constructor and cannot be seen by other methods. (If 2 methods have the same name for a parameter, no interference results.) If a parameter name and an instance variable name are the same, the name all by itself refers to the parameter. To access the instance variable of the same name, use this.name. Local variables are declared and used inside methods. They only exist during the execution of the method. To be even more specific, on entry to a method, space for all the local variables is allocated on the stack. When a method ends, the stack memory for local variables is released.

3 COE318 Software Systems Lecture Notes: Week 4 3 of 11 Java is pass by value When a method with parameters (arguments) is invoked, the method receives a copy of the actual argument. The method can modify the parameter, but this has no effect on the original. For example, consider: public int f(int i) { i = i + 5; //modify parameter i: increment it by 5 return i; //return the modified parameter.. int i = 3; //this is a different i! int k = f(i);//sets k to 8 //the value of i is still 3 When a parameter is a mutable object, the method receives a copy of the parameter. However,

4 COE318 Software Systems Lecture Notes: Week 4 4 of 11 the method may modify mutable instance variables and these changes will be reflected in the object by any reference variable to it. Consider the following example: public class C { public void f(int i, Id id, Id j) { i = i + 5; id.setid(id.getid() + 10); j = id; j.setid(j.getid() + 20); public static void main(string[] args) { int i = 2; Id id = new Id(6); Id id2 = new Id(3); System.out.println("id: " + id + " id2: " + id2 + " i: " + i + " id.id: " + id.getid()); C c = new C(); c.f(i, id, id2); System.out.println("id: " + id + " id2: " + id2 + " i: " + i + " id.id: " + id.getid()); The output is: id: Id@1be2d65 id2: Id@9664a1 i: 2 id.id: 6 id: Id@1be2d65 id2: Id@9664a1 i: 2 id.id: 36 Arrays In Java, arrays are always objects. Consequently, a declaration like int[] arr; states that arr is a reference to an array of ints. (It is quite common and OK to say that arr is an array of integers; but sometimes the programmer has to be aware that arr is only a reference to the actual array.) The declaration allocates 4 bytes of memory for the reference. (If it is a local variable, the space is allocated on the stack.) The array itself, however, does not yet exist! Since the array is an object, it will be in the heap memory area. The array is created with the keybord new. For example, arr = new int[4];

5 COE318 Software Systems Lecture Notes: Week 4 5 of 11 Note: it is very common to combine the declaration and array creation in ome line. For example: int[] arr = new int[5]; This creates the array in the heap and sets its size to 4. The elements are indexed from 0 to 3. A simple example: int[] primes = new int[5]; primes[0] = 2; primes[1] = 3; primes[2] = 5; primes[3] = 7; primes[4] = 11; System.out.println(primes.length); //outputs 5 System.out.println(primes[3]); //outputs 7 Note that the size of an array is obtained with array.length not array.length(). i.e. length is a read-only instance variable of an array. (You can achieve the same effect for immutable instance variables by making them public final. It is also possible to use curly braces to initialize and set the size of an array. For example: int[] primes = {2, 3, 5, 7, 11; String[] messages = {"Mr.", "Ms.", "Mrs.", "Miss"; The + operator We are not surprised that Java evaluates to be 3. The '+' simply implies ordinary arithmetic addition. But '+' can also be used in a non-numeric context such as "foo" + "bar" to yield the new string "foobar". In this case, the '+' operator indicates the concatenation (or joining) of two strings into a single one. The rules for determining if '+' means addition or concatenation are: If either side of the '+' is a String then both the left and right operands are converted to Strings and these are concatenated into a new String. Note that parsing a line with more than one '+' proceeds from left to right. Note also that expressions inside parenthesis are evaluated first in their own context. Some examples: "1" + "2" "12" 1 + "2" "12"

6 COE318 Software Systems Lecture Notes: Week 4 6 of 11 "1" + 2 "12" "" "12" "" (1<2) "1true" "" (1+2) "13" "" *(1+2) "16" "" "3" "" *2 "14" 1 + "" + 2*2 "14" More about Strings String objects are special in Java and are directly supported by the language.simply putting letters in double quotes ("like this") creates a String object. (There is no need for the word new.) Strings are immutable. There are many methods for String objects; the most commonly used include charat(int i): returns the character at position i of the String. (Counting starts at zero.) For example: String s = abcde ; System.out.println(s.charAt(2)); //prints 'c' touppercase(): Creates a new String with the same letters as the original String but all converted to upper case. For example: String s = abcde ; System.out.println(s.toUpperCase());//prints ABCDE //Note that s is still abcdef tolowercase(): Creates a new String with the same letters but all lowercase. equalsignorecase(string s): Returns true if the 2 strings have the same letters irrespective of case; false otherwise. For example: abcd.equalsignorecase( abcd ) is true.

7 COE318 Software Systems Lecture Notes: Week 4 7 of 11 length(): Returns the number of characters in the String. For example: Stack and Heap String s = abcde ; System.out.println(s.length());//prints 5 Questions 1. What is printed for each of the following statements? System.out.println(1 + 2); System.out.println( ""); System.out.println("" ); System.out.println("x" + (1 + 2)); System.out.println("" (3 > 5? 2 : 4)); System.out.println(1 + (5 > 3? 2 : 4)); System.out.println("" (5 > 3? 2 : 4)); Answer: x Consider the following code fragment: public class Id { private final int id; public Id(int id) { this.id = id; public static void main(string[] args) { int i; Id id = null; Id id2 = null ; for(i = 0; i < 5; i++) { id2 = id; id = new Id(i);

8 COE318 Software Systems Lecture Notes: Week 4 8 of 11 System.out.println(id.id + + id2.id); Answer: a. When the for loop finishes, how many Id objects have been created? How many of them are garbage? (Assume garbage collection has not occurred.) b. What is the output from the println statement. 5 Id objects created; 3 are garbage The output is: What is the output when main(...) of the following class is executed? public class Foo { private final int value; private final Foo other; public Foo(int val, Foo oth) { value = val; other = oth; public int getv() { int x = 1; if (other!= null) { x = other.getv(); return x * value; public static void main(string[] args) { Foo a = new Foo(3, null); Foo b = new Foo(2, a); Foo c = new Foo(1, b); System.out.println(c.getV());

9 COE318 Software Systems Lecture Notes: Week 4 9 of 11 Answer: 6 Explanation: One can, of course, mechanically go through the code, pretending you are the Java Virtual Machine and figuring out what happens as each line of code is executed. Indeed, this is a good exercise and you can use the single-stepping feature of the debugger to see if you are right. But you might also recognize a similarity with the Counter lab. If other is null, then getv() simply returns the object's value. But if other is not null, getv() returns the object's value multiplied by other's getv(). So there is a chain: getv()returns the product of all the values in the chain. Since c's chain is 1->2->3, its getv() returns 1*2*3=6. Suppose the chain had 100 elements. If any one of them were 0 (zero), then the answer would be 0 irrespective of the values of the other 99 elements. 4. What is the output when main(...) of the following class is executed? public class A { private int i; public A(int i) { this.i = i; public int geti() { return i; public void seti(int i) { this.i = i; public int foo(int k) { i++; k++; System.out.println("foo k: " + k); return k + i; public static void main(string[] args) { int k, j = 2; A a = new A(5); k = a.foo(j);

10 COE318 Software Systems Lecture Notes: Week 4 10 of 11 System.out.println("j: " + j + ", i: " + a.i + ", k: " + k); Answer: foo k: 3 j: 2 i: 6 k: 9 5. What is the output when main(...) of the following class is executed? Indicate the reference count for each Id object just before the method terminates and indicate which (if any) Id objects are garbage. public class B { Answer: public static void main(string[] args) { Id[] ids = new Id[5]; int i; for (i = 0; i < ids.length; i++) { ids[i] = new Id(i + 1); ids[2] = ids[3]; for (i = 0; i < ids.length; i++) { System.out.println("ids[" + i + "]: " +ids[i].getid()); 6. For each comment //? replace the? with instance variable, local variable, constructor, method or local variable for the immediately preceding declaration. public class G { private int i; //? public void f() { //? int j; //? public G() { //?

11 COE318 Software Systems Lecture Notes: Week 4 11 of 11 Answer: public class G { private int i; //instance variable public void f() { //method int j; //local variable public G() { //Constructor

COE318 Lecture Notes Week 3 (Sept 19, 2011)

COE318 Lecture Notes Week 3 (Sept 19, 2011) COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Sept 19, 2011) Topics Announcements TurningRobot example PersonAge example Announcements The submit command now works. Quiz: Monday October

More information

COE318 Lecture Notes Week 5 (Oct 3, 2011)

COE318 Lecture Notes Week 5 (Oct 3, 2011) COE318 Software Systems Lecture Notes: Week 5 1 of 6 COE318 Lecture Notes Week 5 (Oct 3, 2011) Topics Announcements Strings static and final qualifiers Stack and Heap details Announcements Quiz: Today!

More information

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) Announcements Quiz (5% of total mark) on Wednesday, September 26, 2012. Covers weeks 1 3. This includes both the

More information

COE318 Lecture Notes Week 3 (Week of Sept 15, 2014)

COE318 Lecture Notes Week 3 (Week of Sept 15, 2014) COE318 Lecture Notes: Week 3 1 of 17 COE318 Lecture Notes Week 3 (Week of Sept 15, 2014) Announcements (REPEAT!) Quiz (5% of total mark) on Wednesday, September 24, 2014. Covers weeks 1 3 and labs 1 3.

More information

COE318 Lecture Notes Week 6 (Oct 10, 2011)

COE318 Lecture Notes Week 6 (Oct 10, 2011) COE318 Software Systems Lecture Notes: Week 6 1 of 8 COE318 Lecture Notes Week 6 (Oct 10, 2011) Topics Announcements final qualifiers Example: An alternative to arrays == vs..equals(...): A first look

More information

Selected Questions from by Nageshwara Rao

Selected Questions from  by Nageshwara Rao Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)

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

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Java Identifiers, Data Types & Variables

Java Identifiers, Data Types & Variables Java Identifiers, Data Types & Variables 1. Java Identifiers: Identifiers are name given to a class, variable or a method. public class TestingShastra { //TestingShastra is an identifier for class char

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003 1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 7, 2003 Name: Email Address: TA: Section: You have 90 minutes to complete this exam. For coding questions, you do not need to

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 24 March 18, 2016 The Java ASM What is the value of ans at the end of this program? Counter[] a = { new Counter(), new Counter() ; Counter[] b = {

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line A SIMPLE JAVA PROGRAM Class Declaration The Main Line INDEX The Line Contains Three Keywords The Output Line COMMENTS Single Line Comment Multiline Comment Documentation Comment TYPE CASTING Implicit Type

More information

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming Exam 1 Prep Dr. Demetrios Glinos University of Central Florida COP3330 Object Oriented Programming Progress Exam 1 is a Timed Webcourses Quiz You can find it from the "Assignments" link on Webcourses choose

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Computer Science 1 Ah

Computer Science 1 Ah UNIVERSITY OF EDINBURGH course CS0077 COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS Computer Science 1 Ah Resit Examination Specimen Solutions Date: Monday 1st September 2003 Time: 09:30 11:00

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4 Assignments Lecture 6 Complete for Project 1 Reading: 3.1, 3.2, 3.3, 3.4 Summary Program Parts Summary - Variables Class Header (class name matches the file name prefix) Class Body Because this is a program,

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

13 th Windsor Regional Secondary School Computer Programming Competition

13 th Windsor Regional Secondary School Computer Programming Competition SCHOOL OF COMPUTER SCIENCE 13 th Windsor Regional Secondary School Computer Programming Competition Hosted by The School of Computer Science, University of Windsor WORKSHOP I [ Overview of the Java/Eclipse

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

Using System.out.println()

Using System.out.println() Programming Assignments Read instructions carefully Many deduction on Program 3 for items in instructions Comment your code Coding conventions 20% of program grade going forward Class #23: Characters,

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

University of Palestine. Mid Exam Total Grade: 100

University of Palestine. Mid Exam Total Grade: 100 First Question No. of Branches (5) A) Choose the correct answer: 1. If we type: system.out.println( a ); in the main() method, what will be the result? int a=12; //in the global space... void f() { int

More information

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Scope and Lifetime Type Checking Referencing Environments Named Constants Names Used for variables, subprograms

More information

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() { Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they

More information

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

More on variables and methods

More on variables and methods More on variables and methods Robots Learning to Program with Java Byron Weber Becker chapter 7 Announcements (Oct 12) Reading for Monday Ch 7.4-7.5 Program#5 out Character Data String is a java class

More information

Computer Science II Data Structures

Computer Science II Data Structures Computer Science II Data Structures Instructor Sukumar Ghosh 201P Maclean Hall Office hours: 10:30 AM 12:00 PM Mondays and Fridays Course Webpage homepage.cs.uiowa.edu/~ghosh/2116.html Course Syllabus

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

More information

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types. Class #07: Java Primitives Software Design I (CS 120): M. Allen, 13 Sep. 2018 Two Types of Types So far, we have mainly been dealing with objects, like DrawingGizmo, Window, Triangle, that are: 1. Specified

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I have decided to keep this site for the whole semester I still hope to have blackboard up and running, but you

More information

Computer Science II (20082) Week 1: Review and Inheritance

Computer Science II (20082) Week 1: Review and Inheritance Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Syntax and Semantics of Formal (e.g. Programming) Languages Syntax

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2 CS321 Languages and Compiler Design I Winter 2012 Lecture 2 1 A (RE-)INTRODUCTION TO JAVA FOR C++/C PROGRAMMERS Why Java? Developed by Sun Microsystems (now Oracle) beginning in 1995. Conceived as a better,

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

1 class Lecture2 { 2 3 Elementray Programming / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 41 / 68 Example Given the radius

More information

Introduction to Java https://tinyurl.com/y7bvpa9z

Introduction to Java https://tinyurl.com/y7bvpa9z Introduction to Java https://tinyurl.com/y7bvpa9z Eric Newhall - Laurence Meyers Team 2849 Alumni Java Object-Oriented Compiled Garbage-Collected WORA - Write Once, Run Anywhere IDE Integrated Development

More information

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Computer Programming, I. Laboratory Manual. Final Exam Solution

Computer Programming, I. Laboratory Manual. Final Exam Solution Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Final Exam Solution

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 2 Data types type: A category or set of data

More information

Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class

Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class G51PRG: Introduction to Programming Second semester Lecture 2 Remedial classes Monday 3-5 in A32 Contact Yan Su (yxs) Ordinary labs start on Tuesday Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk

More information

Datatypes, Variables, and Operations

Datatypes, Variables, and Operations Datatypes, Variables, and Operations 1 Primitive Type Classification 2 Numerical Data Types Name Range Storage Size byte 2 7 to 2 7 1 (-128 to 127) 8-bit signed short 2 15 to 2 15 1 (-32768 to 32767) 16-bit

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

4 Programming Fundamentals. Introduction to Programming 1 1

4 Programming Fundamentals. Introduction to Programming 1 1 4 Programming Fundamentals Introduction to Programming 1 1 Objectives At the end of the lesson, the student should be able to: Identify the basic parts of a Java program Differentiate among Java literals,

More information

Java Basic Programming Constructs

Java Basic Programming Constructs Java Basic Programming Constructs /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2 This

More information

Test methods Testing exceptions Common known states Slides by Mark Hancock (adapted from notes by Craig Schock)

Test methods Testing exceptions Common known states Slides by Mark Hancock (adapted from notes by Craig Schock) JUnit Summary Test methods (@Test) Testing exceptions Common known states (@Before) 1 By the end of this lecture you will be able to implement unit tests using JUnit in Java. 2 1 What steps are involved

More information

JUnit Summary. Unit Test 3/18/2009. Test methods Testing exceptions Common known states

JUnit Summary. Unit Test 3/18/2009. Test methods Testing exceptions Common known states JUnit Summary Test methods (@Test) Testing exceptions Common known states (@Before) By the end of this lecture you will be able to implement unit tests using JUnit in Java. 1 2 Unit Test What steps are

More information

JUnit Summary. Test methods Testing exceptions Common known states Slides by Mark Hancock (adapted from notes by Craig Schock)

JUnit Summary. Test methods Testing exceptions Common known states Slides by Mark Hancock (adapted from notes by Craig Schock) JUnit Summary Test methods (@Test) Testing exceptions Common known states (@Before) 1 By the end of this lecture you will be able to implement unit tests using JUnit in Java. 2 What steps are involved

More information

1.00 Lecture 4. Promotion

1.00 Lecture 4. Promotion 1.00 Lecture 4 Data Types, Operators Reading for next time: Big Java: sections 6.1-6.4 Promotion increasing capacity Data Type Allowed Promotions double None float double long float,double int long,float,double

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 Copyright 2009 by Pearson Education Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 Copyright

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

CS 2340 Objects and Design - Scala

CS 2340 Objects and Design - Scala CS 2340 Objects and Design - Scala Objects and Operators Christopher Simpkins chris.simpkins@gatech.edu Chris Simpkins (Georgia Tech) CS 2340 Objects and Design - Scala Objects and Operators 1 / 13 Classes

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

16. Give a detailed algorithm for making a peanut butter and jelly sandwich. COSC120FinalExamReview2010 1. NamethetwotheoreticalmachinesthatCharlesBabbagedeveloped. 2. WhatwastheAntikytheraDevice? 3. Givethecodetodeclareanintegervariablecalledxandthenassignitthe number10. 4. Givethecodetoprintout

More information

Compiling Techniques

Compiling Techniques Lecture 10: Introduction to 10 November 2015 Coursework: Block and Procedure Table of contents Introduction 1 Introduction Overview Java Virtual Machine Frames and Function Call 2 JVM Types and Mnemonics

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

Expressions & Flow Control

Expressions & Flow Control Objectives Distinguish between instance and local variables 4 Expressions & Flow Control Describe how instance variables are initialized Identify and correct a Possible reference before assignment compiler

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

EXERCISES SOFTWARE DEVELOPMENT I. 04 Arrays & Methods 2018W

EXERCISES SOFTWARE DEVELOPMENT I. 04 Arrays & Methods 2018W EXERCISES SOFTWARE DEVELOPMENT I 04 Arrays & Methods 2018W Solution First Test DATA TYPES, BRANCHING AND LOOPS Complete the following program, which calculates the price for a car rental. First, the program

More information

Chapter 02: Using Data

Chapter 02: Using Data True / False 1. A variable can hold more than one value at a time. ANSWER: False REFERENCES: 54 2. The int data type is the most commonly used integer type. ANSWER: True REFERENCES: 64 3. Multiplication,

More information

Join the course group CS230-S18! Post questions, answer them if you know the answer!

Join the course group CS230-S18! Post questions, answer them if you know the answer! http://cs.wellesley.edu/~cs230 Spring 2018 Join the course group CS230-S18! Post questions, answer them if you know the answer! Assignment 1 is available and due at 11:59pm Wednesday February 7th See schedule

More information

CS Programming I: Arrays

CS Programming I: Arrays CS 200 - Programming I: Arrays Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Array Basics

More information

CS 211: Methods, Memory, Equality

CS 211: Methods, Memory, Equality CS 211: Methods, Memory, Equality Chris Kauffman Week 2-1 So far... Comments Statements/Expressions Variable Types little types, what about Big types? Assignment Basic Output (Input?) Conditionals (if-else)

More information

COMP-202 Unit 9: Exceptions

COMP-202 Unit 9: Exceptions COMP-202 Unit 9: Exceptions Announcements - Assignment 4: due Monday April 16th - Assignment 4: tutorial - Final exam tutorial next week 2 Exceptions An exception is an object that describes an unusual

More information

Java+- Language Reference Manual

Java+- Language Reference Manual Fall 2016 COMS4115 Programming Languages & Translators Java+- Language Reference Manual Authors Ashley Daguanno (ad3079) - Manager Anna Wen (aw2802) - Tester Tin Nilar Hlaing (th2520) - Systems Architect

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments CMSC433, Spring 2004 Programming Language Technology and Paradigms Java Review Jeff Foster Feburary 3, 2004 Administrivia Reading: Liskov, ch 4, optional Eckel, ch 8, 9 Project 1 posted Part 2 was revised

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

McGill University School of Computer Science COMP-202A Introduction to Computing 1

McGill University School of Computer Science COMP-202A Introduction to Computing 1 McGill University School of Computer Science COMP-202A Introduction to Computing 1 Midterm Exam Thursday, October 26, 2006, 18:00-20:00 (6:00 8:00 PM) Instructors: Mathieu Petitpas, Shah Asaduzzaman, Sherif

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202

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