CS Problem Solving and Object Oriented Programming Spring 2019

Slides:



Advertisements
Similar presentations
Week 8 Arrays Part 2 String & Pointer
Advertisements

CS-I Final Review Hao Jiang Computer Science Department Boston College.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 9: Oct 17-21, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for the Final Lecture Dec 7, 2011 Aditya Mathur Department of Computer Science Purdue.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update:
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 11: Nov 1-5, 2010 Aditya Mathur Department of Computer Science Purdue University.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette August 27, 2004 Last update:
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Examples of Classes & Objects
EPSII 59:006 Spring 2004.
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CS 1430: Programming in C++.
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
CS 302 Week 10 Jim Williams.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CS 177 Week 15 Recitation Slides
Peer Instruction 6 Java Arrays.
Data Structures ADT List
Classes & Objects: Examples
Chapter 8 Slides from GaddisText
Variables and Their scope
Pointers Call-by-Reference CSCI 230
Object Oriented Programming in java
Topics discussed in this section:
int [] scores = new int [10];
slides created by Ethan Apter
References and objects
Data Structures ADT List
Strings and Pointer Arrays
CS148 Introduction to Programming II
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Java Programming Language
CS150 Introduction to Computer Science 1
Arrays.
C++ Programming CLASS This pointer Static Class Friend Class
slides created by Ethan Apter and Marty Stepp
Objects with ArrayLists as Attributes
Methods/Functions.
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
First Semester Review.
C# Language & .NET Platform 11th Lecture
CS Problem Solving and Object Oriented Programming Spring 2019
C# Language & .NET Platform 9th Lecture
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE2 Week 6: Lecture 11, February 13. 2019 Slides updated: 5:15pm, February 13. 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA https://www.cs.purdue.edu/homes/apm/courses/cs180_Java/CS180Spring2019/

Review Arrays Example Class public class Car{ } Method: Declaration public int getMiles(){} Method: Arguments public int selectCar(Car c1, Car c2){} Method: Input and output Car c=selectCar(momsCar, dadsCar); 2/13/2019 CS 180. Spring 2019. Week 6

Today Modifiers: default, public, private, static get() and set() methods Methods: Passing parameters Variable sized arrays: ArrayList 2/13/2019 CS 180. Spring 2019. Week 6

Classes: Instance variables momsCar=new Car(“Ferrari”, “Spider”, 120); public class Car{ String make; String model; int miles; methods } make =“Ferrari”; model=“Spider” miles=120 methods Object: momsCar Instance variables Belong to an object dadsCar=new car(“Chevy”, “Impala”, 100000); make =“Chevy”; model=“Impala” miles=100000 methods Object: dadsCar 2/13/2019 CS 180. Spring 2019. Week 6

Classes: Static variables/methods momsCar=new Car(“Ferrari”, “Spider”, 120); public class Car{ static String make; String model; int miles; methods } Replaced by “Chevy” make =“Ferrari”; model=“Spider” miles=120 methods Object: momsCar Class variable (only one copy) Instance variables Belong to an object dadsCar=new car(“Chevy”, “Impala”, 100000); make =“Chevy”; model=“Impala” miles=100000 methods Object: dadsCar 2/13/2019 CS 180. Spring 2019. Week 6

Classes: private and public public class Car{ private String make; String model; int miles; methods } momsCar=new Car(“Ferrari”, “Spider”, 120); Instance variables Belong to an object make =“Ferrari”; model=“Spider” miles=120 methods Object: momsCar String model=momsCar.model; int miles=momsCar.miles; int make=momsCar.make; 2/13/2019 CS 180. Spring 2019. Week 6

Classes: Summary of modifiers Access public All packages in the program private Within a class (none) default Within a package static Class variable/method; common to objects of a class 2/13/2019 CS 180. Spring 2019. Week 6

Classes: get() methods public class Car{ private String make; String model; int miles; methods } momsCar=new Car(“Ferrari”, “Spider”, 120); make =“Ferrari”; model=“Spider” miles=120 methods Object: momsCar public String getModel(){ return (model); } String model=momsCar.getModel(); int miles=momsCar.getMiles(); int make=momsCar.getMake(); 2/13/2019 CS 180. Spring 2019. Week 6

Classes: set() methods public class Car{ private String make; String model; int miles; methods } momsCar=new Car( ); make =“Ferrari”; model=“Spider” miles=120 methods Object: momsCar public void setModel(String m){ model=m; } momsCar.setModel(“Spider”); momsCar.setMiles(0); momsCar.setMake(“Ferrari”); 2/13/2019 CS 180. Spring 2019. Week 6

Methods: Declaration Arguments Access public static void sort(double [] data){ Body } Return type Find if a given point (x,y) is inside a circle of radius r and centered at the origin (0,0). public boolean inCircle(double r, double x, double y){ } 2/13/2019 CS 180. Spring 2019. Week 6

Methods: Parameter passing public boolean inCircle(double radius, double x, double y){ } double r=3.5, x=2.0, y=1.0; boolean pointInCircle= inCircle(r, x, y); Values of r, x, y are passed to inCircle. 2/13/2019 CS 180. Spring 2019. Week 6

Methods: Parameter passing public boolean inCircle(double radius, double x, double y){ } double [] data=new double [10]; double val=21; public void init(val, data); Value of “val” is passed to init(). Address of “data” is copied into a local variable and passed to init(). Thus, a value, that happens to be an address, is passed. Explain using an example. 2/13/2019 CS 180. Spring 2019. Week 6

Methods: Passing arrays public int [] data=new int [4]; // Array of ints public void test(int [] d){. …. } // Method that takes an array as input test(data);// Calling test dataTemp data 10 20 -2 5 1 2 3 A copy of the pointer to the first element of the array is passed to “test”. 2/13/2019 CS 180. Spring 2019. Week 6

ArrayList ArrayList enables creation of resizable arrays. Create an array of size 0. Add and remove elements Sort the array. 2/13/2019 CS 180. Spring 2019. Week 6

ArrayList ArrayList<String> carMake=new ArrayList<String>(); carMake.add(“Ford”); // Add an element carMake.add(“Mazda”); // and another element printCars(); // Print the array (two elements) carMake.remove(0); // Remove car at index 1 printCars(); // Print again (only one element at index 0 carMake.add(“Toyota”); // and another element carMake.add(“Buick”); // and another element Collections.sort(carMake); // Sort cars in carMake array printCars(); // Print again (three cars) Explain using an example. 2/13/2019 CS 180. Spring 2019. Week 6

Week 6: Lecture 11 Feb 13, 2019 Questions? Contact your TA. 2/13/2019 CS 180. Spring 2019. Week 6