Learning objec<ves. Classes, Objects, and Methods. Problem Decomposi<on. Problem Decomposi<on (cont d)
|
|
- Edgar Todd
- 6 years ago
- Views:
Transcription
1 Learning objec<ves Classes, Objects, and Methods CS160 Spring 2012 Decomposi<on of a solu<on into objects and methods Methods Sta<c methods Instance methods Sharing data between methods Local Data Class Data 2 Problem Decomposi<on As discussed in previous slides: Read input Figure out the loop structure Ini<alize variables (loop counters, accumula<ng variables, such as totals) Find an appropriate loop termina<ng condi<on Figure out what happens inside a loop Perhaps nested loops Print output Problem Decomposi<on (cont d) But shouldn t the language help us decompose problems? it does! Decompose a big method into smaller methods We will see two ways of doing this (sta<c and instance methods) Decompose into objects (and classes) We introduce this topic with a simple example, but more details are covered in CS161, CS200, CS314,
2 Methods A method (a.k.a. func<on, procedure, rou<ne) is a piece of code that performs a useful ac<on You defined a rou<ne called main. When you run a Java program, it always begins by running the main method. Methods can also return a value to the program that called them More details in a minute Mysteries Revealed public class Temperature { public static void main(string[] args) { // your code here In the last assignment (P7), you defined a class called Temperature! You also defined a method (think function ) called main that took an array of Strings as its arguments 5 6 Terminology A class is a data type Combines variables with methods A class is like a set (defines the proper<es of the elements) An object is an instance of a class Objects are elements of the set (an object has the proper<es defined in the class) Data inside objects and classes They are of two types They may belong to the class (and will take the same value for all the objects) They may belong to the object (and can take different values for each object) 7 8 2
3 Another mystery: sta<c In order to call a method, you need an instance of the class You must have a String variable called word to call word.length() The length() method can access data in the instance it is called on Such methods are called instance methods Excep<on: sta<c methods Not all methods need to access data specific to objects Sta<c declares that a method will not access instance data Sta<c methods can be called without a class instance Uses the nota<on classname.method() Sta<c methods may access class data, but not instance data public sta<c void main Remember that magic incanta<on at the start of your program? main is the name of your method main is the method called by the OS void says that the main func<on does not return a value What would the OS do with a return value? sta<c promises that main will not access instance variables Because the OS needs to call it without crea<ng a class instance public is des<ned to remain a mystery just a bit longer Sta<c methods main is an example of a sta<c method It can only access class variables (or sta<c variables) Therefore main cannot access instance variables. To use instance variables, we will have main create an instance of its class But first, let s see some sta<c methods First we will see sta<c methods that don t share data Then we will see sta<c methods that can share data 11 Simple example (main method calling snowservice) import java.u<l.scanner; public class SnowRemoval { System.out.println("Enter your address:"); Scanner keyboard = new Scanner(System.in); String address = keyboard.nextline(); int delay = snowservice(address); if(delay==0) System.out.println("My driveway is safe now ); else // assume status is non- nega<ve System.out.println( I have to wait for + delay + hours ); 12 3
4 Simple example (snowservice method) public sta<c int snowservice(string home){ System.out.prink("Clearing driveway of %s...", home); System.out.println( done"); System.out.println("Clearing sidewalk"); return 0; 13 Communica<on between calling and called methods Using arguments and return values Method parameters: Method declares a parameter formal parameter (String home) to state what can be provided by the Caller of the method There can be zero to any number of parameters Method return type and value: Can return void (i.e., nothing) or a type (e.g., int, char, int[], String, etc) If a type is returned, there must be a return statement in the method body Calling method (in this case main): Supplies arguments (actual parameters) that must match the type of the parameters in the method declara<on Uses the return value to do something 14 Communica<on using sta<c variables public class Fun { sta<c int data = 0; public sta<c void main (String[] args) { System.out.println( Storing 5 into store ); store(5); System.out.println( Retrieving + retrieve() + from store ); public sta<c void store(int value) { data = value; public sta<c int retrieve() { return data; 15 Cau<on: Pass by value What do you expect this to print? public class PassByValue { int num = 100; increment(num); System.out.println("Aoer calling increment, num is " + num); public sta<c void increment(int n) { n++; The value of the argument is copied. Any changes to the copy are not reflected in the original argument. 16 4
5 Cau<on: Pass by value Another example public class PassByValueString { String word = new String("Good morning"); changegree<ng(word); System.out.println("Aoer calling changegree<ng, word is " + word); public sta<c void changegree<ng(string w) { w = new String("Good night"); Gree<ng remains unchanged 17 Incorrect Swapping public class Swapper { String s1 = "Mar<n"; String s2 = "Scorcese"; swap(s1, s2); System.out.println( main: Aoer swap, s1=" +s1+ " and s2=" +s2); public sta<c void swap(string x, String y) { System.out.println( swap: Before swap, x= +x+ and y= +y); String temp = x; x = y; y = temp; System.out.println( swap: Aoer swap, x=" +x+ " and y=" +y); Nothing gets swapped! 18 Simplifying Temperature s main method Original code with one long main method (Temperature0.java) Refer to sample program version 1 (called Temperature1.java) Pseudo- code Check the number of command- line args Print error message and exit if there are no arguments Otherwise, loop through each argument and call processcity on each argument 19 Decomposing processcity Refer to sample program version 2 (called Temperature2.java) In version 1, processcity was doing three logical tasks: Ini<alizing the counters Reading the file to set the counters Prin<ng the histogram Version 2 makes two new methods, one for reading the file and the other for prin<ng the histogram 20 5
6 Further simplifica<on For prin<ng the histogram, prin<ng stars can be taken out as a separate method (see Temperature3.java) There can be many different ways of decomposing a solu<on Reuse can be taken into account Ooen, smaller methods can be reused in other programs Top- down thinking Break up a big program into smaller pieces The main method calls the other pieces. The pieces can themselves call other pieces Use methods for subtasks The general rule is: Break subtasks into tasks un<l tasks are trivial Every subtask is a method Some methods (subtasks) may call others Objects An object in Java is A set of methods (think: func<ons) A set of members (think: variables) Fancy CS buzzwords: Objects encapsulate data and func3onality Objects encapsulate behavior and state Objects: Concept The idea is that a program manipulates data via methods (func<ons) In P7, the temperature files were an important collec<on of data So, too, was the names of the text files submised by the user on the command line You wrote a method (called main)
7 Object Example: String You have been using objects all along String is an example of an object in Java The characters are the data in the object Methods include: length() : how long is the string? charat(int): what character is at a given posi<on? Syntax: You call an object s method using. and args () E.g.: word.charat(5); word.length() You access an object s data using just. Another example: Scanner Scanner is a more complex object Its data is a stream of characters May come from a file May come from the terminal (a stream) May come from a string Its ac<ons are to parse and interpret the characters next() returns the next valid string nextint() returns the next valid integer nextdouble() returns the next valid double and there are many more (see on- line Java reference) Yet another example: arrays Arrays are the simplest objects Their data is a set of variables of the type provided Their data includes a variable that stores the length of the array Note that array lengths are stored in variables, not computed by func<ons This is why there are no () s aoer args, if args is an array. Classes as data types Classes are data types (just like primi<ves): int counter; String word; MyClass example; By conven<on, class names are capitalized Variables with object types s<ll need names E.g. counter, word, and example above Variables cannot be used un<l they are assigned values True for both primi<ve and object types
8 Object Instances The value assigned to a variable of an object type is an object instance Just like 9 is an instance of int (except that int is not a class, it s a primi<ve type) For example: String word = new String( the ); word is a variable of type String. String( the ) creates an instance of String All 0bject instances are created using the keyword new. 29 More Mysteries Revealed Scanner terminal = new Scanner(System.in); Declares Scanner a is variable an object called class terminal that parses of character type Scanner streams so that they can be easily read as strings, ints or other data types System Initializes is terminal an object to instance be a that specific holds Scanner information that reads about the from system System.in you are on. The data in within System describes the input device your terminal. 30 Methods inside a class Order of wri<ng methods is arbitrary Generally constructors are wrisen first Shared data problem: what if two methods need to share data? One subtask reads input and creates an array of words Another subtask checks each word in the array and does something with it Solu<on #1 Method1 for subtask 1 returns a value,v Method2 for subtask 2 uses the value,v Example: public sta+c void main(string[] args) { String[] wordlist = readinput(); processwords(wordlist);
9 Solu<on #2 Use instance variables Define String[] wordlist; as an instance variable Any method of a class can access its variables readinput() can create & write the array processwords() can access it Data Variables in Classes How does a method access data in a class? Every method can access the class instance it is called on Think of word.length(); it can access the data in the string word Think of the class instance as a hidden argument to the method Class variables look like any other variables in the code of a method They do not need to be re- declared Simple example public class Course { private String department, number private String[] sec<ons = new String[2]; public Course(String dept, String num) { department = dept; number = num; public String getfullname(){ return new String(department + " " + number); Course c1 = new Course("CS", "160"); System.out.println(c1.getFullName()); 10/8/2012 CS 160, Fall Semester Temperature example using instance methods Refer to the example posted under sample programs. This is version 4 (called Temperature4.java). Shows one way to define a constructor and instance methods that operate on the object s data. 36 9
method method public class Temperature { public static void main(string[] args) { // your code here } new
Methods Defining Classes and Methods (Savitch, Chapter 5) TOPICS Java methods Java objects Static keyword Parameter passing Constructors A method (a.k.a. func2on, procedure, rou2ne) is a piece of code
More informationMethods. Methods. Mysteries Revealed
Methods Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private A method (a.k.a. func2on, procedure, rou2ne) is a piece of code
More informationDeclaring and ini,alizing 2D arrays
Declaring and ini,alizing 2D arrays 4 2D Arrays (Savitch, Chapter 7.5) TOPICS Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe Game // se2ng up a 2D array final int M=3, N=4;
More informationArrays. Loops + arrays: challenge problem. Arrays (II) An array is a set of variables of the same type accessed by their index.
Arrays Arrays (Savitch, Chapter 7) TOPICS Array Basics Array Loops Array Programming An array is a set of variables of the same type accessed by their index int[] day = new int[4]; 31 28 31 30 day[0] day[2]
More informationArray Basics: Outline
Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and
More informationArray Basics: Outline
Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationCh. 6. User-Defined Methods
Ch. 6 User-Defined Methods Func5onal Abstrac5on Func5onal regarding func5ons/methods Abstrac5on solving a problem in a crea5ve way Stepwise refinement breaking down large problems into small problems The
More informationArray Basics: Outline
Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and
More informationLoops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)
Loops! Flow of Control: Loops (Savitch, Chapter 4) TOPICS while Loops do while Loops for Loops break Statement continue Statement CS 160, Fall Semester 2014 2 An Example while Loop Step- by- step int count
More informationClasses. Classes as Code Libraries. Classes as Data Structures
Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They
More informationCondi(onals and Loops
Condi(onals and Loops 1 Review Primi(ve Data Types & Variables int, long float, double boolean char String Mathema(cal operators: + - * / % Comparison: < > = == 2 A Founda(on for Programming any program
More informationCOSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan
COSC 111: Computer Programming I Dr. Bowen Hui University of Bri>sh Columbia Okanagan 1 Administra>on Midterm 2 To return next class Assignment 3 Available on website Due next Thursday Today Sta>c modifier
More informationMethods and Data (Savitch, Chapter 5)
Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private 2 public class Temperature { public static void main(string[] args) {
More informationAP Computer Science Unit 1. Programs
AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationA Founda4on for Programming
3.1 Objects LOGO STYLE GUIDE Schools within the University Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 3/16/14 11:29 PM A Founda4on
More informationCheck out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.
Chapter 4 Lab Loops and Files Lab Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write an do-while loop Be able to write a for loop
More informationIntroduction 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 informationPeer Instruction 1. Elementary Programming
Peer Instruction 1 Elementary Programming 0 Which of the following variable declarations will not compile? Please select the single correct answer. A. int i = 778899; B. double x = 5.43212345; C. char
More informationArrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)
Arrays Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Object Quick Primer A large subset of Java s features are for OOP Object- Oriented Programming We ll get to that
More informationC Programming Primer 8/31/15 1
C Programming Primer 8/31/15 1 Objec5ves Develop a func5onal knowledge of C programming concepts Understand basic variables, constructs, and control flow statements 8/31/15 2 Special Notes Copying and
More information4. Java language basics (4) Func%on July 19, 2015
IEDU Programming AcEvity, Summer, 2015 4. Java language basics (4) Func%on July 19, 2015 Minhaeng Lee Review : loop Program print from 0 to 10 While/for Two guys write down on the board! Make a condieon
More informationStatic Methods. Why use methods?
Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit
More informationComputational 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 informationCS111: 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 informationMethods CSC 121 Fall 2016 Howard Rosenthal
Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods
More informationFundamentals of Programming Data Types & Methods
Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators
More informationCONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops
COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables
More informationControls Structure for Repetition
Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power
More informationJAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods
Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA
More informationProgramming Constructs Overview. Method Call System.out.print( hello ); Method Parameters
Programming Constructs Overview Method calls More selection statements More assignment operators Conditional operator Unary increment and decrement operators Iteration statements Defining methods 27 October
More informationD0010E Object- oriented programming and design. Today. Today An introduc<on to the basic syntax and seman<cs of Java
D0010E Object- oriented programming and design An introduc
More informationClasses. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)
Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They
More informationProgramming with Java
Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules
More informationStrings, Arrays, A1. COMP 401, Spring 2015 Lecture 4 1/20/2015
Strings, Arrays, A1 COMP 401, Spring 2015 Lecture 4 1/20/2015 Java Execu@on Model Your program is always execu@ng within the context of some method. Starts off in the main class method defined in whatever
More informationCS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons
CS 61C: Great Ideas in Computer Architecture Strings and Func.ons Instructor: Krste Asanovic, Randy H. Katz hdp://inst.eecs.berkeley.edu/~cs61c/sp12 Fall 2012 - - Lecture #7 1 New- School Machine Structures
More informationCS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++
CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ 10 Stacks of Coins You have 10 stacks with 10 coins each that look and feel
More informationMenu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:
Menu Driven Systems While loops, menus and the switch statement Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list while loops recap
More informationCOMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette
COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.
More informationJava Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM
Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM Let s get some practice creating programs that repeat commands inside of a loop in order to accomplish a particular task. You may
More informationChapter 4: Conditionals and Recursion
Chapter 4: Conditionals and Recursion Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey Agenda The modulus operator Random Number Generation Conditional Execution Alternative
More informationLinked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }
Linked Lists Since a variable referencing an object just holds the address of the object in memory, we can link multiple objects together to form dynamic lists or other structures. In our case we will
More informationMethods CSC 121 Spring 2017 Howard Rosenthal
Methods CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods
More informationCS 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 informationVariables and Assignments CSC 121 Spring 2017 Howard Rosenthal
Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand variables Understand how to declare and use variables in Java Programs Learn how to formulate assignment statements
More informationLecture 5: Methods CS2301
Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int
More informationMidterm Examination (MTA)
M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and
More informationCOSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan
COSC 111: Computer Programming I Dr. Bowen Hui University of Bri>sh Columbia Okanagan 1 First half of course SoEware examples From English to Java Template for building small programs Exposure to Java
More informationRelated Course Objec6ves
Syntax 9/18/17 1 Related Course Objec6ves Develop grammars and parsers of programming languages 9/18/17 2 Syntax And Seman6cs Programming language syntax: how programs look, their form and structure Syntax
More informationCOMP 110 Programming Exercise: Simulation of the Game of Craps
COMP 110 Programming Exercise: Simulation of the Game of Craps Craps is a game of chance played by rolling two dice for a series of rolls and placing bets on the outcomes. The background on probability,
More informationCOMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements
COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money
More informationEncapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods
Encapsulation You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other objects in the program
More informationCS1150 Principles of Computer Science Methods
CS1150 Principles of Computer Science Methods Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Opening Problem Find the sum of integers from 1 to
More informationLecture 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 informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Methods
Computer Science 210 Data Structures Siena College Fall 2017 Topic Notes: Methods As programmers, we often find ourselves writing the same or similar code over and over. We learned that we can place code
More informationConstants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous
CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we
More informationClasses & Objects CMSC 202
Classes & Objects CMSC 202 Programming & Abstrac9on All programming languages provide some form of abstrac'on Also called informa'on hiding Separa9ng how one uses a program and how the program has been
More informationInstructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer
CS 61C: Great Ideas in Computer Architecture Everything is a Number Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13 9/19/13 Fall 2013 - - Lecture #7 1 New- School Machine Structures
More informationText User Interfaces. Keyboard IO plus
Text User Interfaces Keyboard IO plus User Interface and Model Model: objects that solve problem at hand. User interface: interacts with user getting input from user giving output to user reporting on
More information4. Java language basics: Function. Minhaeng Lee
4. Java language basics: Function Minhaeng Lee Review : loop Program print from 20 to 10 (reverse order) While/for Program print from 1, 3, 5, 7.. 21 (two interval) Make a condition that make true only
More informationLearning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods
Learning objec-ves 2D Arrays (Savitch, Chapter 7.5) TOPICS Using 2D arrays Decomposi-on of a solu-on into objects and methods Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe
More informationInput. Scanner keyboard = new Scanner(System.in); String name;
Reading Resource Input Read chapter 4 (Variables and Constants) in the textbook A Guide to Programming in Java, pages 77 to 82. Key Concepts A stream is a data channel to or from the operating system.
More informationWe have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really
Week 9: Methods 1 We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really long and hard to read Sometimes you
More informationGarbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }
Objec,ves Inheritance Ø Overriding methods Garbage collec,on Parameter passing in Java Sept 21, 2016 Sprenkle - CSCI209 1 Assignment 2 Review private int onevar; public Assign2(int par) { onevar = par;
More informationCS 112 Introduction to Programming
CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin
More informationCS 251 Intermediate Programming Methods and More
CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationSchool of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15
Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL
More informationArray Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)
Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants
More informationSupplementary Test 1
Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2009 Supplementary Test 1 Question
More informationMore on Java. Object-Oriented Programming
More on Java Object-Oriented Programming Outline Instance variables vs. local variables Primitive vs. reference types Object references, object equality Objects' and variables' lifetime Parameters passing
More informationCS 251 Intermediate Programming Methods and Classes
CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationA Quick and Dirty Overview of Java and. Java Programming
Department of Computer Science New Mexico State University. CS 272 A Quick and Dirty Overview of Java and.......... Java Programming . Introduction Objectives In this document we will provide a very brief
More informationAP Computer Science A
AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive
More informationThe for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming
The for Loop, Accumulator Variables, Seninel Values, and The Random Class CS0007: Introduction to Computer Programming Review General Form of a switch statement: switch (SwitchExpression) { case CaseExpression1:
More informationHistory of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995
Java Introduc)on History of Java Java was originally developed by Sun Microsystems star:ng in 1991 James Gosling Patrick Naughton Chris Warth Ed Frank Mike Sheridan This language was ini:ally called Oak
More informationCSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists
CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists 1 A programming problem Consider the following task: Double values representing grades are read until the user enters a negative
More informationAPCS Semester #1 Final Exam Practice Problems
Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester
More informationSt. Edmund Preparatory High School Brooklyn, NY
AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More informationJava Programming Unit 7. Error Handling. Collec7ons
Java Programming Unit 7 Error Handling. Collec7ons Run7me errors An excep7on is an run- 7me error that may stop the execu7on of your program. For example: - someone deleted a file that your program reads
More informationJava Coding 3. Over & over again!
Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java
More informationCS 10: Problem solving via Object Oriented Programming. Lists Part 1
CS 10: Problem solving via Object Oriented Programming Lists Part 1 Agenda 1. Defining a List ADT 2. Generics 3. Singly linked list implementaeon 4. ExcepEons 5. Visibility: public vs. private vs. protected
More informationNAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS
NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS Name Binding and Binding Time Name binding is the associa1on of objects (data and/or code) with names (iden1fiers) Shape S = new Shape(); The binding
More informationRepe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
More informationObject-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018
Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative
More informationLecture 02, Fall 2018 Friday September 7
Anatomy of a class Oliver W. Layton CS231: Data Structures and Algorithms Lecture 02, Fall 2018 Friday September 7 Follow-up Python is also cross-platform. What s the advantage of Java? It s true: Python
More informationSequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;
Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The
More informationArrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;
Arrays What are they? An array is a data structure that holds a number of related variables. Thus, an array has a size which is the number of variables it can store. All of these variables must be of the
More informationLast Class. While loops Infinite loops Loop counters Iterations
Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();
More informationCSCI 102 Fall 2010 Exam #1
Name: USC Username: CSCI 102 Fall 2010 Exam #1 Problems Problem #1 (14 points) Problem #2 (15 points) Problem #3 (20 points) Problem #4 (16 points) Problem #5 (35 points) Total (100 points) Problem 1 Short
More informationPROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write
More informationExample Program. public class ComputeArea {
COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for
More informationCS 180. Recitation 8 / {30, 31} / 2007
CS 180 Recitation 8 / 30, 31 / 2007 Announcements Project 1 is out Due 9/5 @ 10:00pm Start early! Use the newsgroup for your questions LWSN B146 Lab help hours 7-10pm Monday through Wednesday Naming Conventions
More informationAlgorithms Lecture 11. UC Davis, ECS20, Winter Discrete Mathematics for Computer Science
UC Davis, ECS20, Winter 2017 Discrete Mathematics for Computer Science Prof. Raissa D Souza (slides adopted from Michael Frank and Haluk Bingöl) Lecture 11 Algorithms 3.1-3.2 Algorithms Member of the House
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and
More informationLecture 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 informationOpening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i
More informationSection 003 Fall CS 170 Exam 1. Name (print): Instructions:
CS 170 Exam 1 Section 003 Fall 2012 Name (print): Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than
More informationVectors and Pointers CS 16: Solving Problems with Computers I Lecture #13
Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13 Ziad Matni Dept. of Computer Science, UCSB Announcements Midterm grades will be available on Tuesday, 11/21 If you *need* to know
More information