Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 Using Objects Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 200 Using Objects Jim Williams, PhD."— Presentation transcript:

1 CS 200 Using Objects Jim Williams, PhD

2 This Week Notes Lecture: Using Objects By Friday
Exam Conflict and Accommodations Install Eclipse (version 8) Help Queue Team Lab 2 Chap 2 Programs (P2): Due Thursday Hours Spent Week? Lecture: Using Objects

3 Review of Key Concepts in Methods
Call Definition Parameter, Parameter list Argument Header Body Return data type, value Variable Scope

4 Is this mPrint Call or Definition?
static void mPrint() { System.out.println("my print"); } B is the correct answer

5 Defining Methods public class M { //method definition
static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); // method call. B is the correct answer

6 Argument vs. Parameter static void printCount(int count) { //parameter
public static void main(String []args) { int num = 10; printCount( 23); //argument is value passed in printCount( num+3); } static void printCount(int count) { //parameter System.out.println( count); count is a parameter (zyBooks) or formal parameter the number 23, for example is an argument (also called an actual parameter).

7 Local Variables Local variables are those declared within a method.
Variables declared within a method are only available within that method.

8 What prints out? public static void calc(int num) { num = num + 3; }
5 35 error public static void calc(int num) { num = num + 3; } public static void main(String []args) { int num = 5; calc( num); System.out.println( num); try it.

9 Returning a Value from a Method
public static void main(String []args) { int value = 5; int result = triple( value); } public static int triple(int num) { return num * 3;

10 Which is called first: calc or println?
error public static int calc(int num) { num -= 33; return num; } public static void main(String []args) { int n = 55; System.out.println( calc( n)); put a print statement within the calc method to see if it is called before the println method.

11 Using Objects Lots of existing Java code!
How do we build off it rather than having to write it ourselves? API - Application Programming Interface

12 Calling Class (static) Methods
double numInts = Math.pow( 2, 32); double root = Math.sqrt( 16); int num1 = 16; int num2 = 3; double result; result = num2 + Math.sqrt( num1); API

13 Instance vs. Class (static) Methods
method definition has “static” modifier use name of class when calling Math.max( 10, 20); Instance (non-static) Methods method definition does Not have “static” modifier use instance of class when calling Random randGen = new Random(); randGen.nextInt( 5);

14 Primitive vs Reference Data Types
Store value int i = 5; 5 i Reference Store a reference to another location in memory that contains value Integer num; num = new Integer(9); num 9

15 Thought Experiment Imagine: Someone asks you to teach an introductory programming course this summer. Are you preparing yourself? Are you simply looking for an "A" or are you preparing yourself to teach programming to others?

16 The Study Cycle Check Am I using study methods that are effective?
Do I understand the material enough to explain it to others?

17 What kind of methods? max: class nextInt: instance Math.max( 10, 20);
max: instance nextInt: class Math.max( 10, 20); Random randGen = new Random(); randGen.nextInt( 5); A is the best answer

18 Wrapper Classes Wrapper classes contain helpful methods.
Primitive type Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double

19 Data Type Conversions String weekNum = 3; String weekNum = "" + 3;
String numStr = Integer.toString( 4); int num = Integer.parseInt( numStr);

20 Wrapper Classes Boxing: Putting primitive into instance of wrapper class Unboxing: retrieve primitive from instance auto-boxing/unboxing: when compiler inserts code to box/unbox. Primitive type Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double

21 Primitive vs Reference Variables
int num1; num1 = 5; Integer num2; num2 = new Integer(7); Integer num3 = 9; int num4 = num3; //use Java Visualizer, options Show String/Integer objects checkbox //use Java Visualizer, options Show String/Integer objects checkbox

22 What will show when d1 is printed?
Double d1 = new Double(10); double d2 = d1; d1 = 14.1; Double d3 = d1; d1 = d2; System.out.println( d3); 10 14.1 a reference error yes. try it.

23 String class A reference (class), not a primitive data type.
Frequently used final String TITLE_PREFIX = "Welcome to Week "; int week = 2; System.out.println( TITLE_PREFIX + week);

24 String Alex Johnson alex johnson String name2 = "Alex";
error String name2 = "Alex"; name2.toLowerCase(); String name3 = name2 + " Johnson"; System.out.print( name3); instances of String are immutable (cannot change) methods in String class that appear to change strings actually return new strings.

25 Calling String methods
aChar: 'i' aChar: 's' aChar: ' ' String strA = "This is a string"; char aChar = strA.charAt( 3);

26 What is the answer? AP? String s1 = "An important programming tool.";
String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charAt( s1.indexOf('g') -3); String answer = (s2 + c3 + s4).toUpperCase(); AP? api ANP IM API try it.

27 Read In Values Recall: import java.util.Scanner;
Scanner input = new Scanner( System.in); int age = input.nextInt(); String name = input.nextLine();

28 What are values of variables?
String note = "1 2\nAlex two words"; Scanner input = new Scanner( note); int num1 = input.nextInt(); int num2 = input.nextInt(); String name = input.next(); String words = input.nextLine();

29 What are values of variables?
name: Minsub\n age: 22\nCS major: name: Minsub\n22\CS age: name: Minsub age: 22 String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();

30 What are values of variables?
name: Minsub\n22\CS age: major: name: Minsub age: 22 name: Minsub major: CS String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();

31 What are values of variables?
score1: 20 score2: 25 title: scores title: score1: score2: scores String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextInt(); int score2 = input.nextInt(); String title = input.nextLine();

32 java.util.Random Random randGen; //Declare reference variable
randGen = new Random(); //create instance // randGen.setSeed( 123); //set state int valueA = randGen.nextInt( 5); //get a value int valueB = randGen.nextInt( 5); int valueC = randGen.nextInt( 5);

33 What is the answer? x = rand.nextInt(10)-2;
What expression to get a value between 2 and 10, inclusive of both 2 and 10? Assume: Random rand = new Random(); int x; x = rand.nextInt(10)-2; x = rand.nextInt(9)+1; x = rand.nextInt(8)+2; x = rand.nextInt(9)+2; Other try it.

34 Debugging with Print statements
See what is going on. Divide and conquer. String s1 = "An important programming tool."; String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charAt( s1.indexOf('g') -3); String answer = (s2 + c3 + s4).toUpperCase();


Download ppt "CS 200 Using Objects Jim Williams, PhD."

Similar presentations


Ads by Google