CS 200 Using Objects Jim Williams, PhD
This Week Notes 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
Review of Key Concepts in Methods Call Definition Parameter, Parameter list Argument Header Body Return data type, value Variable Scope
Is this mprint Call or Definition? static void mprint() { } System.out.println("my print"); call definition
Defining Methods public class M { //method definition static void mprint() { System.out.println("my print"); } public static void main(string []args) { mprint(); // method call. } }
Argument vs. 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); }
Local Variables Local variables are those declared within a method. Variables declared within a method are only available within that method.
What prints out? public static void calc(int num) { num = num + 3; } public static void main(string []args) { int num = 5; calc( num); System.out.println( num); } 3 5 35 error
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; }
Which is called first: calc or println? public static int calc(int num) { num -= 33; return num; } public static void main(string []args) { int n = 55; System.out.println( calc( n)); } calc println error
Using Objects Lots of existing Java code! How do we build off it rather than having to write it ourselves? API - Application Programming Interface
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
Instance vs. Class (static) Methods 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);
Primitive vs Reference Data Types Primitive Store value int i = 5; i 5 Reference Store a reference to another location in memory that contains value Integer num; num = new Integer(9); num 9
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?
The Study Cycle Check Am I using study methods that are effective? Do I understand the material enough to explain it to others? http://students.lsu.edu/academicsuccess/studying/strategies/tests/studying
What kind of methods? Math.max( 10, 20); Random randgen = new Random(); randgen.nextint( 5); max: class nextint: instance max: instance nextint: class max: class nextint: class max: instance nextint: instance
Wrapper Classes Primitive type Wrapper class boolean Boolean Wrapper classes contain helpful methods. byte char float int long short double Byte Character Float Integer Long Short Double
Data Type Conversions String weeknum = 3; String weeknum = "" + 3; String numstr = Integer.toString( 4); int num = Integer.parseInt( numstr);
Wrapper Classes Primitive type Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long Boxing: Putting primitive into instance of wrapper class Unboxing: retrieve primitive from instance auto-boxing/unboxing: when compiler inserts code to box/unbox. short double Short Double
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
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
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);
String String name2 = "Alex"; name2.tolowercase(); String name3 = name2 + " Johnson"; System.out.print( name3); Alex Johnson alex johnson alex Johnson error instances of String are immutable (cannot change) methods in String class that appear to change strings actually return new strings.
Calling String methods String stra = "This is a string"; char achar = stra.charat( 3); achar: 'i' achar: 's' achar: ' '
What is the answer? 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
Read In Values Recall: import java.util.scanner; Scanner input = new Scanner( System.in); int age = input.nextint(); String name = input.nextline();
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();
What are values of variables? String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextline(); int age = input.nextint(); String major = input.nextline(); name: Minsub\n age: 22\nCS major: name: Minsub\n22\CS age: major: name: Minsub age: 22 major:
What are values of variables? String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextline(); int age = input.nextint(); String major = input.nextline(); name: Minsub\n22\CS age: major: name: Minsub age: 22 major: name: Minsub age: 22 major: CS
What are values of variables? String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextint(); int score2 = input.nextint(); String title = input.nextline(); score1: 20 score2: 25 title: scores score1: 20 score2: 25 title: score1: 20 25 score2: scores title:
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);
What is the answer? 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
Debugging with Print statements See what is going on. Divide and conquer.