Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 Methods, Using Objects

Similar presentations


Presentation on theme: "CS 200 Methods, Using Objects"— Presentation transcript:

1 CS 200 Methods, Using Objects
Jim Williams, PhD

2 This Week Notes Lecture: Misc, Methods, Using Objects By Friday
Exam Conflict and Accommodations Install Eclipse Team Lab 2 Chap 2 Programs (P2): Due Thursday Lecture: Misc, Methods, Using Objects

3 Magic Numbers (bad practice)
Numbers with unexplained meaning: public class H { public static void main(String []args) { double s = 71 / ; }

4 Variable Names Useful variable names can help with code readability.
public class H { public static void main(String []args) { double height = 71; height = height / ; }

5 Constants Constants (final variables) can help with readability.
public class H { public static void main(String []args) { final double INCHES_IN_METER = ; double heightInInches = 71; double heightInMeters = heightInInches / INCHES_IN_METER; }

6 Number Systems Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary 0, 1

7 Decimal = 302

8 Binary = 14

9 Binary 8 4 2 1 = 5

10 Hexadecimal (group bits by nibble)
0000 = 0 0001 = 1 0010 = 2 0011 = 3 0100 = 4 0101 = 5 0110 = 6 0111 = 7 1000 = 8 1001 = 9 1010 = A 1011 = B 1100 = C 1101 = D 1110 = E 1111 = F

11 What character is this? 0000 0000 0100 0001 A B Unicode:
@ Unicode: 0x003E 62 > 0x003F 63 ? 0x @ 0x A 0x B 0x C A

12 What character is this? char letter = '\u0043'; A B Unicode:
@ Unicode: 0x003E 62 > 0x003F 63 ? 0x @ 0x A 0x B 0x C

13 Color 183, 1, 1 B70101 Red, Green, Blue (RGB)
183, 1, 1 B70101 Red, Green, Blue (RGB)

14 CS Core Principles: Algorithms: A step-by-step set of operations to be performed. Analogy: Recipe, Instructions Abstraction: a technique for managing complexity. Analogy: Automobile, CS200 Computer View

15 Methods A named section of code that can be "called" from other code.
Lots of existing methods that you can use rather than writing yourself. To use, "call the method".

16 Definition vs Call Definition: What to do. Call: Do it.

17 main method public static void main(String []args) {
System.out.println("Hello from main"); } Is this a Method Definition or Method Call?

18 Calling Class (static) Methods
Call the method with the name of the class or, if within the defining class, just use the name of the method. double numInts = Math.pow( 2, 32); double root = Math.sqrt( 16); int num1 = 16; int num2 = 3; double result = num2 + Math.sqrt( num1);

19 API Application Programmer Interface Search for "Java 8 Math"
Describes how to use the method.

20 Find Scanner Methods How can you find out what Scanner methods are available?

21 Calling Instance/object Methods
Scanner elephant = new Scanner(System.in); int num = elephant.nextInt(); String name = elephant.next(); String line = elephant.nextLine(); elephant.close();

22 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

23 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 Scanner in = new Scanner( System.in); int num = in.nextInt();

24 mPrint - which is Call, Definition?
static void mPrint() { System.out.println("my print"); } public static void main(String []args) { mPrint(); mPrint definition mPrint call B is the correct answer

25 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

26 Is count: Argument or Parameter?
public static void main(String []args) { int num = 10; printCount( 23); printCount( num+3); } static void printCount(int count) { System.out.println("result:" + count); argument parameter count is a parameter (zyBooks) or formal parameter the number 23, for example is an argument (also called an actual parameter).

27 This Week Notes Lecture: Methods, Using Objects By Friday
Exam Conflict and Accommodations Install Eclipse Chap 2 Programs (P2): Due Thursday Lecture: Methods, Using Objects

28 Thought Experiment Are you simply looking for an "A" or are you preparing yourself to teach programming to others?

29 Time Spent This Week

30 Which is called first: calc or println?
error static int calc(int num) { num -= 33; //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.

31 Variables Local Parameters
Variables declared within a method. Must be initialized before reading from them. Parameters Variables declared within a method parameter list. Initialized with arguments. Scope: Both are only visible within the method they are declared.

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

33 Returning a Value from a Method
static int triple(int num) { return num * 3; } public static void main(String []args) { int value = 5; int result = triple( value); System.out.println("triple 6 = " + triple( 6));

34 Returning a Value from a Method
Method Definition: must have return data type must have return statement with value Method Call: In an expression, the value of method call is the returned value.

35 Testing Methods Methods written to run test cases to help validate and debug your code. Easily run multiple tests. If changes are made, easy to retest.

36 Testing Method static int triple(int num) { return num * 3; }
static void testTriple() { System.out.println("expect: 9, actual: " + triple(3)); System.out.println("expect: 3, actual: " + triple(1)); } public static void main(String []args) { testTriple();

37 Using Objects CS 200 focus on writing class methods and Use of Objects. Structured/procedural programming CS 300 creating instantiable classes and introductory data structures Object-oriented programming. Must instantiate a class to call instance methods (use an object).

38 Read In Values Using Scanner
Whitespace: space, tab, newline Token: a sequence of non-whitespace characters. hello This, is a sentence. 2nd line 3rd line 43

39 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();

40 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();

41 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();

42 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();

43 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

44 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

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

46 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

47 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

48 What will show when d3 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 try it.

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

50 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.

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

52 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);

53 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.

54 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();

55 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.


Download ppt "CS 200 Methods, Using Objects"

Similar presentations


Ads by Google