Loop - 3 & Class. Cheng, Wei COMP May 22, Title

Similar documents
Darrell Bethea May 20, 2011

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP)

Fundamentals of Programming Data Types & Methods

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

COMP Computer Basics. Yi Hong May 13, 2015

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

COMP Introduction to Programming Midterm Review

Last Class. While loops Infinite loops Loop counters Iterations

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

CS111: PROGRAMMING LANGUAGE II

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

COMP 110 Introduction to Programming. What did we discuss?

Loops. CSE 114, Computer Science 1 Stony Brook University

Object Oriented Design

CS 101 Fall 2006 Midterm 1 Name: ID:

Structured Programming

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Claremont McKenna College Computer Science

Question: Total Points: Score:

CS 1316 Exam 1 Summer 2009

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Handout 4 Conditionals. Boolean Expressions.

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

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

Programming with Java

Introduction to Java (All the Basic Stuff)

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

CS260 Intro to Java & Android 02.Java Technology

Darrell Bethea May 25, 2011

COMP String and Console I/O. Yi Hong May 18, 2015

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

CompSci 125 Lecture 11

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

9 Working with the Java Class Library

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

Top-Down Program Development

University of Massachusetts Amherst, Electrical and Computer Engineering

MIDTERM REVIEW. midterminformation.htm

Learning the Java Language. 2.1 Object-Oriented Programming

Collections. Collections. Collections - Arrays. Collections - Arrays

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

1 Inheritance (8 minutes, 9 points)

CS111: PROGRAMMING LANGUAGE II

I. True/False: (2 points each)

CSC 1051 Data Structures and Algorithms I

JAVA: A Primer. By: Amrita Rajagopal

Using Java Classes Fall 2018 Margaret Reid-Miller

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Object Oriented Design: Identifying Objects

Collections. Collections Collection types Collection wrappers Composite classes revisited Collection classes Hashtables Enumerations

C Sc 227 Project 1: Three Main Methods

Chapter 10. Further Abstraction Techniques

STUDENT LESSON A12 Iterations

Controls Structure for Repetition

CS 231 Data Structures and Algorithms, Fall 2016

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Final Exam. COMP Summer I June 26, points

CS212 Midterm. 1. Read the following code fragments and answer the questions.

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

CS 177 Recitation. Week 8 Methods

Introduction to Computer Programming

Building Java Programs

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

Sri Vidya College of Engineering & Technology

13 th Windsor Regional Secondary School Computer Programming Competition

Problem Grade Total

CSC 1051 Data Structures and Algorithms I

Classes. Classes as Code Libraries. Classes as Data Structures

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

ECE 122. Engineering Problem Solving with Java

CSC 1051 Data Structures and Algorithms I

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Flow of Control: Loops. Chapter 4

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Data Structures Lecture 1

Assignment 1 due Monday at 11:59pm

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

CSIS 10A Assignment 9 Solutions

Introduction to Java Programming

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Example Program. public class ComputeArea {

ITI Introduction to Computing II

ITI Introduction to Computing II

Object Oriented Programming. Java-Lecture 6 - Arrays

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Class List. Java Must Have. Class Goals. Class Goals. Schedule

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Transcription:

Loop - 3 & Class Cheng, Wei COMP110-001 May 22, 2014 Title

How many iterations? for (count = 1; count < 10; count++) Loop 9 times for (count = 1; count <= 10; count++) for (count = 0; count <= 10; count++) for (count = 0; count < 10; count++) for (count = 1; count < 10; count+=2)

More examples with String & Loop String internally, a list of character Loop provides a good way to scan through the list String str = COMP110 ; System.out.println( Index\tChar ); for (int i = 0; i<str.length(); i++) { System.out.println( i + \t + str.charat(i) ); Index Char 0 C 1 O 2 M 3 P 4 1 5 1 6 0

More examples with String & Loop Find a character in String ( both the string & character can come from user input or other external sources at run-time ) String str = COMP110 ; char findme = P ; for (int i = 0; i<str.length(); i++) { if (str.charat(i) == findme) { System.out.println( findme + is found in + str ); break;

More examples with String & Loop String str = COMP110 ; char findme = P ; boolean found = false; for (int i = 0; i<str.length(); i++) { if (str.charat(i) == findme) { if (found) { else { found = true; break; System.out.println( findme + is found in + str ); System.out.println( findme + is NOT found in + str ); Find a character in String. Improved version: some output if not found

More examples with String & Loop Count the occurrence of one character in a String String str = COMP110 ; char findme = 1 ; int occurrence = 0; for (int i = 0; i<str.length(); i++) { if (str.charat(i) == findme) { occurrence ++;

More examples with String & Loop Find whether a string is in another string Optional take-home question ( CS students should work this out ) String str = A Longer String Here ; String findme = ger ; for(int i = 0; i<str.length(); i++) {. Fill out the code here. Inner loop is needed

Classes and Objects Review of concepts Java programs (and programs in other objectoriented programming languages) consist of objects of various class types Objects can represent objects in the real world Automobiles, houses, employee records Or abstract concepts Colors, shapes, words 8

Object Oriented Programming (OOP) Object: Attributes + Methods Class: the blue-print of objects of the same type Person name, contact Superclass Subclass Student student ID, program, year Teacher employee ID, department, rank Class S1 name= Alan, contact= 919-.., program = biostat, year = 1st S2 name= Anna, contact= 919-.., program = CS, year = 1st T1 name= Yi, contact= 919-.., dept = CS, rank = no rank Objects T2 name= Yun, contact= 919-.., program = biostat, rank = ast prof

OOP in Practice Import class if necessary: import java.util.*; Create object: Class_Type variable_name = new Class_Type( ); e.g.: Scanner s = new Scanner(System.in); Polygon treetop = new Polygon(); Access object members (attribute or method): int inputnumber = s.nextint(); treetop.setcolor( Color.green );

Class Today, we will talk about how to create our own classes A class is the definition of a kind of object A blueprint for constructing specific objects Class Name: Automobile Data: amount of fuel speed license plate Methods (actions): accelerate: How: Press on gas pedal. decelerate: How: Press on brake pedal. 11

Objects, Instantiation Object Name: patscar amount of fuel: 10 gallons speed: 55 miles per hour license plate: 135 XJK Object Name: suescar amount of fuel: 14 gallons speed: 0 miles per hour license plate: SUES CAR Object Name: ronscar amount of fuel: 2 gallons speed: 75 miles per hour license plate: 351 WLF Instantiations, or instances, of the class Automobile 12

UML (Universal Modeling Language) - fuel: double - speed: double - license: String Automobile + accelerate(double pedalpressure): void + decelerate(double pedalpressure): void Class name Data Methods (actions) 13

Objects Important: classes do not have data; individual objects have data Classes specify what kind of data objects have 14

Class files Each Java class definition goes in its own, SEPARATE.java file ClassName save the file as ClassName.java Student.java includes the class Student 15

Class files and separate compilation What happens when you compile a.java file?.java file gets compiled into a.class file Contains Java bytecode Same filename except for.class instead of.java You can compile a Java class before you have a program that uses it Don t worry about the compilation in this course as Eclipse does it automatically For CS students, I may hold an extra class to talk about how to compile / run programs in command line window. 16

class Student - Name - Year - GPA - Major - Credits - GPA sum Class Name: Student + getname + getmajor + printdata + increaseyear How: increase year by 1 + calcgpa How: average grades 17

class Student - name: String - year: int - gpa: double - major: String - credits: int - gpasum: double Class Name: Student + getname(): String + getmajor(): String + printdata(): void + increaseyear(): void + calcgpa(double grade): void 18

Defining a class public class Student { public String name; public int classyear; public double GPA; public String major; //... Class name Data (instance variables) public String getmajor() { return major; public void increaseyear() { classyear++; Method s 19

Creating an object Create an object jack of class Student Student jack = new Student(); Assign memory address of object to variable Return memory address of object Create an object Scanner keyboard = new Scanner(System.in); Create an object keyboard of class Scanner 20

Instance variables Data defined in the class are called instance variables public String name; public int classyear; public double GPA; public String major; variables public: no restrictions on how these instance variables are used (more details later public is actually a bad idea here) type: int, double, String 21

Our Student Example in Lecture 4 Public Class Student { public String name; public Student Friend; Using the Student class: Data (instance variables) Student s1 = new Student(); s1.name = Alan ;... Student s2 = new Student(); s2.name = Anna ;... s1.friend = s2;

Next Class Continue on Class Define methods