Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS Problem Solving and Object Oriented Programming Spring 2019

Similar presentations


Presentation on theme: "CS Problem Solving and Object Oriented Programming Spring 2019"— Presentation transcript:

1 CS 18000 Problem Solving and Object Oriented Programming Spring 2019
Section LE2 Week 6: Lecture 11, February Slides updated: 5:15pm, February Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA

2 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 Week 6

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

4 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”, ); make =“Chevy”; model=“Impala” miles=100000 methods Object: dadsCar 2/13/2019 CS 180. Spring Week 6

5 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”, ); make =“Chevy”; model=“Impala” miles=100000 methods Object: dadsCar 2/13/2019 CS 180. Spring Week 6

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 Week 6

7 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 Week 6

8 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 Week 6

9 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 Week 6

10 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 Week 6

11 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 Week 6

12 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 Week 6

13 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 Week 6

14 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 Week 6

15 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 Week 6

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


Download ppt "CS Problem Solving and Object Oriented Programming Spring 2019"

Similar presentations


Ads by Google