Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CIS Computer Programming Logic
Lists in Python.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Flow of Control Part 1: Selection
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to Java Java Translation Program Structure
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Lecture 6: Midterm Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
Lecture 2: 1226 Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.
Lecture 2: 1226 Review II Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Lecture 10: Object Oriented Programming Tami Meredith.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS 1428 Final Exam Review. Exam Format 200 Total Points – 60 Points Writing Programs – 45 Points Tracing Algorithms and determining results – 20 Points.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Java Primer 1: Types, Classes and Operators
Multiple variables can be created in one declaration
CS 1428 Exam I Review.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
Programming Funamental slides
CS 1428 Final Exam Review.
Chapter 2 Programming Basics.
CSC 142 Arrays [Reading: chapter 12].
Primitive Types and Expressions
Unit 3: Variables in Java
CS 1428 Exam I Review.
Presentation transcript:

Midterm Review Tami Meredith

Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with a decimal point, 3.14,.333, 99.9,... char Alphabetic symbols, 'a', 'b', '%',... boolean Truth values: true, false void The empty set, nothing, no values

Complex/Composite Types Arrays Ordered, fixed length, groups of identically typed data Strings Special type that is very like an array of char Classes User defined collections of other types and methods As an OOPL, Java relies heavily on the use of classes

Exercise Define (in Java) a variable to store the following data. Where necessary, define any needed classes or arrays. 1) The weight of Jessica's new daughter (in kg). 2) 10 possible baby names. 3) A blanket colour, as a set of 3 integer RGB values. 4) A birth date (day, month, year).

Solution 1) The weight of Jessica's new daughter (in kg). double weight; 2) 10 possible baby names. String[] names = new String[10]; 3) A blanket colour, as a set of 3 integer RGB values. int[] colour = new int[3]; 4) A birth date (day, month, year). public class Date { int day, month, year; } Date d = new Date();

Variable Manipulation After defining variables we assign them values We use these values in computation to create new values It is critical that we know what every variable stores at all points in time

Exercise 1. int i, j = 1, k; 2. double a = j; 3. k = squareRoot(64); 4. for (i = 0; i < k; i = i + 2) { 5. j = i * i; 6. System.out.println("j is " + j"); 7. } A. How many times are lines 5 and 6 executed? B. What are the values of i, j, k, and a after lines 1, 2, and 3. C. What are their values after line 7? D. What are their values at the END of each loop iteration? E. What is printed out? You may use a table if you wish.

Solution 1. int i, j = 1, k; 2. double a = j; 3. k = squareRoot(64); 4. for (i = 0; i < k; i = i + 2) { 5. j = i * i; 6. System.out.println("j is "+j); 7. } Prints: j is 0 j is 2 j is 4 j is 6 Lineijka 1?1?n/a 2?1?1.0 3?18 Loop 10, Loop 22, Loop 34, Loop 46, After

Programming We program algorithms with only a few basic elements: 1. Assignments and expressions – to manipulate data and memory 2. Conditional (if) statements - to make choices 3. Loops - to repeat things 4. Blocks - to group things 5. Method calls - to use code that is in another place We combine these things to create programs

Syntax if (condition) statement if (condition) statement 1 else statement 2 do statement while (condition); while (condition) statement for (preloop ; condition ; postbody) statement { statement 1 statement 2... statement n } variable = expression; method(arguments);

Exercise An array called nums contains 20 integers Determine the number of positive and negative integers in the array and store these in variables you must define called p and n int[] nums = new int[20]; getData(nums); // Your code here

Solution int[] nums = new int[20]; getData(nums); int p = 0, n = 0, i; for (i = 0; i < 20; i = i + 1) { if (nums[i] < 0) { n = n + 1; } else if (nums[i] > 0) { p = p + 1; }

Solution (ICK!) int[] nums = new int[20]; getData(nums); int p,n,i; for (p=n=i=0;i<nums.length;i++) if (nums[i]<0) n++; else if (nums[i]>0) p++;

Exercise An integer, n, is prime if it is not evenly divisible by any of 2, 3, 4, 5,..., n-1 if n % m equals zero, n is evenly divisible by m write code to determine if an integer "n" is prime and print either "Prime" or "Not prime" to the screen int n = keyboard.nextInt(); // Your code here

Solution int n = keyboard.nextInt(); int m; boolean prime = true; for (m=2; m < n; m++) { if ((n % m) == 0) { prime = false; } if (prime == true) { System.out.println("Prime"); } else{ System.out.println("Not Prime"); }

Methods Methods are named blocks of code that Take input as parameters Generate a return value Format: public static opt return-type name (parameters) { statement(s) }

An Example public static char last (String str) { int len = str.length(); return (str.charAt(len – 1)); } Method name: last Required Input: A string Return type: character Return value: The last character in the input string

Exercise Write a method that counts the number of spaces in a string. Your method should be public and static.

Solution Write a method that counts the number of spaces in a string. Your method should be public and static. public static int spaces (String s) { int i, num = 0; char c; for (i = 0; i < s.length(); i++) { c = s.charAt(i); if (c == ' ') { num = num + 1; } return (num); }

Exercise It is common in computing to keep track of time as the number of seconds since midnight. Write a method that takes an array of 3 integers (hours, minutes, seconds) and returns it as the number of seconds since midnight. E.g., If time = {4, 5, 6 } seconds = (4 * 60 * 60) + (5 * 60) + 6

Solutions public static int sinceMidnight(int[] t) { int s; s = t[0] * 60 * 60; s = s + (t[1] * 60); s = s + t[2]; return (s) } public static int sinceMidnight (int[] t) { return ((t[0]*3600)+(t[1]*60)+t[2]); }

Exercise Write a method called equals that takes two arrays of integers as parameters and returns true if the arrays are the same and false otherwise.

Solution public static boolean equals (int[] a1, int[] a2) { if (a1.length != a2.length) return (false); for (int i = 0; i < a1.length; i++) { if (a1[i] != a2[i]) return (false); } return (true); }

A final exercise Write a complete Java program that counts the number of lines in a file. Here is an example of its use: File? arrays.java arrays.java: 22 lines

The Midterm Tracing code (fill in the table) Writing Variable Definitions Find the errors: syntax >> error when you "Build File" algorithm >> does not "work" properly Write code to... Write a complete program to...

The Usuals Nothing = Zero – don't leave questions blank, ANYTHING is better than nothing! There are no penalties for incorrect answers SHOTGUN approach – write anything and everything you think is possibly relevant Pseudo code, point-form, flow charts... If you don't know the Java, answer it some other way For T/F, Multiple-Choice, Matching, etc. GUESS if you have to – no negative scoring! Copy parts of the textbook that you think might work, don't worry about perfection, just about getting a small part right Leave lots of space, add stuff later when you think of it Write code in pencil, bring an eraser Answer questions in any order – do easiest stuff first