CS 211: More Syntax, Memory, Equality

Similar documents
CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Introduction to Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Crash Course in Java

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

J a v a Quiz (Unit 3, Test 0 Practice)

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Sample CSE8A midterm Multiple Choice (circle one)

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Moving from CS 61A Scheme to CS 61B Java

Java Basics: Data Types, Variables, and Loops

Chapter 2 Introduction to Java programming

In this Chapter you ll learn:

Programming Languages CIS 443

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

AP Computer Science Java Subset

Example of a Java program

Stacks. Linear data structures

Java Interview Questions and Answers

CS 106 Introduction to Computer Science I

The C Programming Language course syllabus associate level

Sources: On the Web: Slides will be available on:

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Introduction to Java. CS 3: Computer Programming in Java

ECE 122. Engineering Problem Solving with Java

Chapter 2: Elements of Java

CMSC 202H. ArrayList, Multidimensional Arrays

Topic 11 Scanner object, conditional execution

Building Java Programs

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Stack vs. Heap. Introduction to Programming. How the Stack Works. Primitive vs. Reference Types. Stack

LAB4 Making Classes and Objects

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Building Java Programs

Arrays. Introduction. Chapter 7

Building Java Programs

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Lecture 5: Java Fundamentals III

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

CSE 8B Midterm Fall 2015

AP Computer Science Java Mr. Clausen Program 9A, 9B

CS 111 Classes I 1. Software Organization View to this point:

Java Crash Course Part I

Java from a C perspective. Plan

Object Oriented Software Design

Section 6 Spring 2013

CS170 Lab 11 Abstract Data Types & Objects

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

8.1. Example: Visualizing Data

Visual Basic Programming. An Introduction

Object Oriented Software Design

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Homework/Program #5 Solutions

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Part I. Multiple Choice Questions (2 points each):

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Software Engineering Techniques

C Compiler Targeting the Java Virtual Machine

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Building a Multi-Threaded Web Server

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Introduction to Data Structures

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Introduction to Object-Oriented Programming

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

The Interface Concept

Java CPD (I) Frans Coenen Department of Computer Science

Install Java Development Kit (JDK) 1.8

LOOPS CHAPTER CHAPTER GOALS

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

JAVA ARRAY EXAMPLE PDF

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Lecture J - Exceptions

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Chapter 3. Input and output. 3.1 The System class

An Incomplete C++ Primer. University of Wyoming MA 5310

Building Java Programs

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Using Files as Input/Output in Java 5.0 Applications

Transcription:

CS 211: More Syntax, Memory, Equality Chris Kauffman Week 3-1

Logisitics This Week Lab 3 exercises Some labs consolidated Project 1 Due Wednesday: Questions? Goals Today A few course mechanics More java syntax Memory diagrams Reading BJP Ch 1-7 Lab Manual chapters

More Mechanics Textbook and PracticeIt! Honors Section Tools and DrJava Cheating

Array and Function Practice Good exercises: functions that manipulate arrays BJP3 Self-Check 7.28: arraymystery5 BJP3 Exercise 7.6: stdev BJP3 Exercise 7.12: priceisright BJP3 Exercise 7.13: longestsortedsequence

Last Time Comments Statements/Expressions Variable Types little types, what about Big types? Assignment Basic Output (Input?) Conditionals (if-else) Iteration (loops) Aggregate data (arrays, structs, objects, etc) Function Declarations Library System

Easy Exam Questions to Write Convert to for double tol = 1e-4; double S = 45.0; double x = 45.0/2; double err; err = (S - x*x)*(s - x*x); while(err > tol){ x = (x + S/x) / 2.0; err = (S - x*x)*(s - x*x); Answers in code pack Convert to while int x = 48; int f = -1; boolean found = false; for(int i=x-1; i>1 &&!found; i--) { if(x % i == 0){ f = i; found = true;

break exits a loop int guess, correct = 22; while(true){ guess = input.nextint(); if(guess == correct){ System.out.println("You guessed right"); break; System.out.println("You guessed wrong"); System.out.println("Game over"); There is also a continue which skips to the next loop iteration which is sometimes useful

Array Goodies Declare and Initialize int a[] = {1, 2, 3, 4; Initialize Dynamically int b[];... b = new int[]{7, 6, 5; myfunc(new int[]{3,1,4,1,5,9);

Functions Are parameterized code Referred to as methods in java jargon Give me some stuff (arguments) I ll give you something back (return value) Java: specify types for arguments and return User return to finish function and give value back Immediately ends function (even inside loop) Useful for project 1

Functions Live inside classes, see FunctionDemo.java // Sum up an array public static int sumintarray(int a[]){ int sum = 0; for(int i=0; i<a.length; i++){ sum += a[i]; return sum; For now, use the magic word static for functions Omitting static changes the meaning of functions significantly We ll start doing that soon

Legacy of the void Sometimes a method gives nothing as an answer. Return type is void In void methods, return is optional public static void downhere(){ System.out.println("Calling down here"); // no return required static int anumber = 0; public static void maybeincrease(int myarg){ if(myarg <= 0){ return; // return immediately anumber++; return; // optional return

Playing with Functions It s easy to play with static functions in DrJava s interactive loop. Make sure to use ClassName.functionName(param,parm2). Welcome to DrJava. Working directory is... > CountDescents.countDescents(new int[]{) 0 > CountDescents.countDescents(new int[]{3,2,1,5,4,3,2,1) 2 > CountDescents.countDescents(new int[]{3,2,1,2,3) 3 > int x = CountDescents.countDescents(new int[]{1,2,3,4,5) > x 5 > int [] a = {5,4,3,3,3,3,3,2,1,5,4,4,4,3,2,2,2,1; > CountDescents.countDescents(a) 2

What s the difference between #1 and #2? Defined Used public static void doubler1(int x){ x = 2*x; public static void doubler2(int x[]){ x[0] = 2*x[0]; public static void main(string args[]){ int r = 10; int s[] = {20; doubler1(r); System.out.println(r); doubler2(s); System.out.println(s[0]); Code is in Doubler.java To understand the difference, we need to draw memory diagrams of the function call stack and heap

Two Kinds of types: Primitive and References Primitives Little types are primitives int, double, char, boolean, long, short, float... Live directly inside a memory cell Each primitive type has its own notion of a zero value: know what they are as all arrays are initialized to these values Only a small number of primitive types, can t make new ones References Big types including types you ll create String, Scanner, File, Sauce, Exception,... And all arrays Contents of memory cell refer to another spot in memory where the thing actually resides Usually refer to a heap location Identical to a pointer but operations are limited Have a single zero-value: null which points nowhere

Another Tricky Example What s the difference? What gets printed? Defined Used public static boolean intequals1(int x, int y){ return x==y; public static boolean intequals2(int x[], int y[]){ return x==y; public static void main(string args[]){ boolean result; int a=1, b=1; result = intequals1(a,b); System.out.println(result); int aa[]={20, bb[]={20; result = intequals2(aa,bb); System.out.println(result); result = aa==bb; System.out.println(result);

Equality == does shallow comparisons: compare the contents of two memory boxes. Many times this is not what is desired Instead want a deep comparison which compares multiple parts For that will typically have x.equals(y) methods Can also write static functions that do similar things Array Equality Write a function public static boolean intarrayequals(int x[], int y[]) which checks whether two integer arrays are deeply equal to one another. Write a function public static boolean intarrayidentical(int x[], int y[]) which checks whether two integer arrays are the same array.