CS18000: Problem Solving And Object-Oriented Programming

Similar documents
CS18000: Programming I

CS 180 Problem Solving and Object Oriented Programming Fall 2011

University of Palestine. Mid Exam Total Grade: 100

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Array. Prepared By - Rifat Shahriyar

Give one example where you might wish to use a three dimensional array

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

Computer Science II Data Structures

Java Identifiers, Data Types & Variables

AP Computer Science Unit 1. Programs

Casting. References. References

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

CS18000: Problem Solving and Object-Oriented Programming

Programming II (CS300)

Computational Expression

Key Differences Between Python and Java

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

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.

Announcements. CS18000: Problem Solving And Object-Oriented Programming

Course Outline. Introduction to java

CS 1302 Chapter 9 (Review) Object & Classes

I. Variables and Data Type week 3

CS 180 Problem Solving and Object Oriented Programming Fall 2011

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Problem Grade Total

Object-oriented Programming and Software Engineering CITS1001. Multiple-choice Mid-semester Test

CS 106 Introduction to Computer Science I

CS18000: Problem Solving And Object-Oriented Programming

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

CS360 Lecture 10 Multithreading

Program Fundamentals

Getting started with Java

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

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

HST 952. Computing for Biomedical Scientists Lecture 5

The Basic Parts of Java

Getting started with Java

COS 126 General Computer Science Spring Written Exam 1

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

CS 101 Exam 2 Spring Id Name

CS162 Computer Science I Fall 2018 Practice Exam 1 DRAFT (9 Oct.)

Datatypes, Variables, and Operations

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

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

Programming with Java

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Introduction to Programming Using Java (98-388)

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

CS111: PROGRAMMING LANGUAGE II

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Selected Questions from by Nageshwara Rao

AP CS Unit 8: Inheritance Exercises

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Chapter 5 Names, Bindings, Type Checking, and Scopes

Notes on Chapter Three

Princess Nourah bint Abdulrahman University. Computer Sciences Department

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

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Solutions Manual. Data Structures and Algorithms in Java, 5th edition International Student Version. M. T. Goodrich and R.

ESC101 : Fundamental of Computing

CS111: PROGRAMMING LANGUAGE II

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

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

CS 211: Methods, Memory, Equality

Topic 4 Expressions and variables

Lecture 3. Lecture

CMAT Language - Language Reference Manual COMS 4115

Use the template below and fill in the areas in Red to complete it.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS 177 Week 15 Recitation Slides. Review

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Section 2.2 Your First Program in Java: Printing a Line of Text

Arrays. Eng. Mohammed Abdualal

Primitive vs Reference

Options for User Input

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

CS 231 Data Structures and Algorithms, Fall 2016

Computer Science 1 Ah

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Lecture 5: Methods CS2301

UMBC CMSC 331 Final Exam

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

2. [20] Suppose we start declaring a Rectangle class as follows:

Expressions and Variables

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

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

Basics of Java Programming variables, assignment, and input

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

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

CEN 414 Java Programming

Transcription:

CS18000: Problem Solving And Object-Oriented Programming Class (and Program) Structure 31 January 2011 Prof. Chris Clifton Classes and Objects Set of real or virtual objects Represent Template in Java Create Objects created Class Animal animal Class Vehicle AvTech Bus vehicle student flower Class Student student72 dog Class Flower mydog Class Dog marysdog 2 2011 Chris Clifton, Aditya P. Mathur 1

Basic Class Structure Everything in Java starts with a Class One class has a main method May have data associated with it variables constants Statements occur in methods May also have declarations Constructor method called when class created Initializes the object May have arguments class Program public static final String Author = Chris ; Date timerun; Program( String user ) timerun = new Date(); System.out.println( timerun.gettime() ); public static void main (String[ ] args) Program p; p = new Program( args[0] ); System.out.println( p.timerun.gettime() ); CS18000 3 Variables A variable is something whose value may change during program execution. Every variable has a name and a type. Every variable must be declared before it is used. 4 2011 Chris Clifton, Aditya P. Mathur 2

Declarations int age; float height, area; String name boolean int x=1, y=0; String firstname= Harry ; 5 Used to denote classes, objects, data Names Contain characters; must start with a letter, or a $ sign or an underscore. Examples: height, area1, Dog, $great Length unlimited, case sensitive. Dog and dog are different names. Convention: All class names begin with an uppercase letter; all other names begin with a lower case letter. 6 2011 Chris Clifton, Aditya P. Mathur 3

Constants A constant is something that cannot change during program execution. Examples: Integer constants: 0, 1, -1, +24, 29, 300009998, O14, 0x1B Floating point constants: 0.0, -2.345e28, -0.000976512 Boolean constants: true, false Character constants:, a, A, $ String constants:,, Hi!, Alice in Wonderland 7 Named Constants A constant can be named and the name used instead of the constant itself. Examples: final float pi=3.14159; final boolean dogsexist=true; 8 2011 Chris Clifton, Aditya P. Mathur 4

Simple expressions Expressions are used to compute something. float x, y, z; x*y+z; // Arithmetic expression, results in float value x<y; // Boolean expression, results in boolean value String firstname= Mary, lastname= Jones ; firstname+ +lastname; // Results in a string More in Chapter 2! And yet more to come! 9 Assignment statement An assignment statement allows assigning the value of an expression to a variable. float p=x*y+z; // p gets the value of x*y+z boolean q=x<y; // q gets the value of x<y String firstname= Mary, lastname= Jones ; String name= firstname+ +lastname; More in Chapter 2! And yet more to come! 10 2011 Chris Clifton, Aditya P. Mathur 5

Parameters and Arguments Program ( String user ) user is a formal parameter Used like a variable inside the method Value provided when called Program p = new Program ( Chris ) Chris is the argument to the method for this call, in the method, user has the value Chris CS18000 11 Example in a Program: Concurrent distance How can we speed up the Euclidean distance program? We have multiple processors, use them to compute parts of the sum simultaneously! Java Thread class: Set up what a thread is supposed to do (constructor) Start it (returns immediately) Do other things Wait for it to end and get result CS18000 12 2011 Chris Clifton, Aditya P. Mathur 6

What is the problem? What would be a solution? Steps to a solution Input Calculations Output Data representation Function breakdown Now we start coding Problem Solving: Euclidean Distance A a b Get values for a, b Represent as Array Compute distance Sum squares of each dimension Divide into parts Sum each separately Add parts together Take square root Display result 2 B CS18000 13 Using Threads Like running a program run instead of main takes no arguments Object must know what it is supposed to do Constructor Result stored in object Create object with portion of a, b to sum Constructor takes arrays and saves in object Variable in scope of Class main divides and creates objects run does the summation Saves result in variable CS18000 14 2011 Chris Clifton, Aditya P. Mathur 7

Example: Euclidean Distance public class Lec3Distc extends Thread public double distance = 0; private double x[], y[]; // Arrays to compute distance on private int begin,end; // Dimensions to compute on public Lec3Distc(double a[], double b[],int bg, int en) // Set up what this instance is supposed to do when it runs. x = a; y = b; begin = bg; end = en; public void run() // What to do when "start" is called (from Thread class) distance = SumSquareDiff(x,y,begin,end); public static double SumSquareDiff(double[] a, double[] b, int begin, int end) // Requires: a.length = b.length; no null values in a or b // Produces: Euclidean distance between a and b (>0) double sum = 0; for (int i=begin; i<end; i++) sum = sum + (a[i]-b[i])*(a[i]-b[i]); return sum; public static double EuclideanDistance(double[] a, double[] b) // Requires: a.length = b.length; no null values in a or b // Produces: Euclidean distance between a and b (>=0) Lec3Distc first = new Lec3Distc(a,b,0,(int)Math.floor(a.length/2)); Lec3Distc second = new Lec3Distc(a,b,(int)Math.floor(a.length/2)+1,a.length); first.start(); // Start computation on the first half, but don't wait second.start(); // Start computation on the second half, don't wait try first.join(); // Wait for the first half to finish. second.join(); // Wait for the second half to finish. catch(interruptedexception e) /* Ignore */ return Math.sqrt(first.distance+second.distance); Rest of class (main) is unchanged This is Functional Abstraction in action! CS18000 15 2011 Chris Clifton, Aditya P. Mathur 8